minuteware

minute discoveries

Converting Python datetime object to UTC

| Comments

While adding support for ISO 8601 date/time strings to Flask-RESTful I was struggling with all that hated Python timezone stuff, specifically with the need to convert a datetime object to UTC timezone. With the libraries that Flask-RESTful was already using this solution looked the easiest:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
In [1]: from datetime import datetime

In [2]: from calendar import timegm

In [3]: import pytz

In [5]: dt_offset = datetime(2014, 11, 21, 10, 30, 45, tzinfo=pytz.timezone('Europe/Kiev'))

In [6]: dt_no_offset = datetime(2014, 11, 21, 10, 30, 45)

In [7]: dt_offset
Out[7]: datetime.datetime(2014, 11, 21, 10, 30, 45, tzinfo=<DstTzInfo 'Europe/Kiev' LMT+2:02:00 STD>)

In [8]: dt_no_offset
Out[8]: datetime.datetime(2014, 11, 21, 10, 30, 45)

In [9]: datetime.fromtimestamp(timegm(dt_offset.utctimetuple()), tz=pytz.UTC)
Out[9]: datetime.datetime(2014, 11, 21, 8, 28, 45, tzinfo=<UTC>)

In [10]: datetime.fromtimestamp(timegm(dt_no_offset.utctimetuple()), tz=pytz.UTC)
Out[10]: datetime.datetime(2014, 11, 21, 10, 30, 45, tzinfo=<UTC>)

UPD

Actually there is a much cleaner way to do the same, using exclusively pytz:

1
2
3
4
5
6
7
8
9
In [68]: def dt_to_utc(dt):
   ....:     return pytz.UTC.normalize(dt) if dt.tzinfo else dt.replace(tzinfo=pytz.UTC)
   ....:

In [69]: dt_to_utc(dt_offset)
Out[69]: datetime.datetime(2014, 11, 21, 8, 28, 45, tzinfo=<UTC>)

In [70]: dt_to_utc(dt_no_offset)
Out[70]: datetime.datetime(2014, 11, 21, 10, 30, 45, tzinfo=<UTC>)

Using a local Git repository in Jenkins

| Comments

Even if you want to run a Jenkins server locally for testing purposes to correctly configure a job Jenkins still needs to fetch the source from a repository. With Git this can be easily done without configuring a Git server. Just specify the path to a local Git repo with file:// protocol like this:

1
file:///home/ay/dev/projects/my_new_project

Then input this path as repository URL in Jenkins.

Global .gitignore

| Comments

Very useful if you need, for example, to igonre the .idea directory in all your projects and not add it to each project’s .gitignore explicitly. To do so execute the following:

1
2
git config --global core.excludesfile "~/.gitignore"
echo ".idea" > ~/.gitignore

That’s it. Now the global .gitignore will act just like the project’s .gitignore.

Integrate Piwik into Jekyll

| Comments

First we have to take the Piwik JavaScript Tracking Code from Piwik Administration website and replace Piwik URL and site ID with Jekyll variables, like so (note the {{ site.piwik.base_url }} and {{ site.piwik.site_id }}):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- Piwik -->
<script type="text/javascript">
  var _paq = _paq || [];
  _paq.push(["trackPageView"]);
  _paq.push(["enableLinkTracking"]);

  (function() {
    var u=(("https:" == document.location.protocol) ? "https" : "http") + "://{{ site.piwik.base_url }}/";
    _paq.push(["setTrackerUrl", u+"piwik.php"]);
    _paq.push(["setSiteId", "{{ site.piwik.site_id }}"]);
    var d=document, g=d.createElement("script"), s=d.getElementsByTagName("script")[0]; g.type="text/javascript";
    g.defer=true; g.async=true; g.src=u+"piwik.js"; s.parentNode.insertBefore(g,s);
  })();
</script>
<!-- End Piwik Code -->

Save this code snippet as _includes/piwik

Now we need base_url and site_id variables defined in _config.yml. Add the following to _config.yml:

1
2
3
piwik:
  base_url: piwik.mydomain.net
  site_id: 1

For the default Jekyll template add the following after </head> tag in _layouts/default.html:

1
{% raw %}{% include piwik %}{% endraw %}

That’s it. After our website is rebuilt all the pages will include the above Piwik JS tracking code with variables replaced with their values from _config.yml

Align tab text in Firefox

| Comments

Linux Mint uses it’s customized Firefox theme which has tabs text centered. This is not something you’d want if you are using Tree Style Tab extension. This can be overridden though. In your Firefox profile directory (which should be ~/.mozilla/firefox/<some_random_chars>.default) create a subdirectory called chrome and then create a file userChrome.css in this chrome subdirectory with the following content:

1
2
3
.tab-text {
  text-align: left !important;
}

Restart Firefox and you’re done – tabs text is aligned left.

Scrolling background windows in Xfce

| Comments

I recently found that I was lacking this feature in my Xubuntu 12.04. I remember once having this on by default in KDE, so I wanted to find out how to do this in Xfce.

So this is what you need to do to enable background sccrolling in Xfce (in a terminal):

1
$ xfconf-query -c xfwm4 -p /general/raise_with_any_button -s false

Another way is to open Xfce Setting editor and navigate to xfwm4 -> general -> raise_with_any_button -> false

Kudos to Will Santos for his blog post.

Random hostname for CentOS Kickstart installation

| Comments

A cool option to install CentOS (and some other distros) is to use Kickstart. One thing I found missing in Kickstart is a possibility of adding some randomness to my installation out of the box. For example if deploying many CentOS virtual machines from one kiskstart file I wanted each of them to have a unique hostname. This is not possible to do with kickstart’s network --hostname=<my-hostname> option because it does not accept any code snippets. Instead this can be done with Kickstart’s %pre script.

Create a %pre section like so:

1
2
3
%pre
echo "network --hostname=`echo centos-$RANDOM$RANDOM`" > /tmp/pre-hostname
%end

Then somewhere in the main Kickstart section place this:

1
%include /tmp/pre-hostname

This command will include the network --hostname option into your main Kickstart section and you will end up with a hostname like centos-2656530171

Playing ALAC with GStreamer

| Comments

I googled hard and installed lots of different packages until I found out that gstreamer-ffmpeg will do the job. Note that you might need to restart your player after installing it.

VMware Player and VMware VIX. The specified version was not found

| Comments

Having VMware Player 4.0.2 and VIX 1.11 (the latest to the time of writing this) installed, it appeared that they don’t play well together. Trying to run my VM I was getting The specified version was not found error.

1
2
3
$ vmrun -T player start ~/vmware/gentoo/gentoo_x86.vmx nogui
Unable to connect to host.
Error: The specified version was not found

A solution to this problem is quite simple. Actually, as far as I can tell, this is a bug. There is a file /usr/lib/vmware/vixwrapper-product-config.txt, which is installed along with VMware Player, and maps VMware Player/Workstation versions to their corresponding VIX API versions. The problem is that the version of VMware Player in this file is 4.0.0, whereas I installed 4.0.2.

1
player 14 vmdb 4.0.0 Workstation-8.0.0-and-vSphere-5.0.0

I suspect VMware guys just forgot (and are sometimes forgetting, as this problem has been seen before) to update this file when releasing new version. So what you’ve got to do is to substitute 4.0.0 with the verion of Player you have installed.