PYTHON AND GIT ON WINDOWS

I do most of my development under Linux so I have python out of the box and git is only an apt install away. But recently a colleague needed to generate configs based on templates built by yours truly (Jinja2 syntax) so I pointed him at my gencfg script on GitHub. What I realized only later was that he only had a Windows machine and no idea how to create an environment to fetch repositories, install dependencies and run python scripts. Let's fix that.

If you're on Windows 10, then you should take a look at the Windows Subsystem for Linux, a developer feature which provides an Ubuntu environment running natively in Windows, complete with bash and the apt package manager.

The environment

So gencfg is a python script, with one dependency (library) and it's located on GitHub. In order to run it, we need a functional python environment (I made it so that gencfg runs in both python 2.7 and 3) with pip (the python package manager) so we can install the Jinja2 library.

First, download the Windows python installer from www.python.org and run it.

  • while we should be really running 3.x nowadays, the reality is that certain libraries and tools (you may have heard of ansible) still only support python 2.7.x - so let's install that.
  • select add python.exe to PATH
  • all current versions come with pip included (IMPORTANT!) - make sure it's selected

Once it's done, you could open a command prompt now and run python scripts from it, but we're not there yet (and we can do better than good ol' cmd).

To get the scripts from GitHub, you have two options: you download a zip manually from the website or you use git to fetch the repository. You don't need to learn git right now, but it's a very useful tool, well worth investing a bit of time in (for the basics).

Download the installer for Windows from git-scm.com

Use the Git Bash shortcut and customize MinTTY (the Consolas font is pretty good on Windows) - you get a resizable window with a bash environment, this is great. But if you want to use cmd then git should work there as well (installer adds it to the PATH).

One caveat if using Git Bash with MinTTY: python doesn't go into interactive mode (more on stackoverflow) so a solution is to force it start that way: echo alias python=\"python -i\" >> ~/.bash_profile

Running gencfg

If you want to know what gencfg does, I wrote about it in the post Automation, one step at a time (1) after presenting it at iNOG::6 - in short, it generates multiple configuration files using a template fed with data from a csv file.

The first step is to clone the repository (also known as downloading all the files and their history). Open the Git Bash window and navigate to the directory where you wish the scripts to live in.

$ git clone https://github.com/cmsirbu/gencfg
Cloning into 'gencfg'...
remote: Counting objects: 37, done.
remote: Total 37 (delta 0), reused 0 (delta 0), pack-reused 37
Unpacking objects: 100% (37/37), done.

The repository is now found in the gencfg directory, so the next step is to install its dependencies using pip. They are provided conveniently in a text file named requirements.txt.

$ cd gencfg/
$ pip install -r requirements.txt
Collecting jinja2 (from -r requirements.txt (line 1))
  Downloading Jinja2-2.9.5-py2.py3-none-any.whl (340kB)
Collecting MarkupSafe>=0.23 (from jinja2->-r requirements.txt (line 1))
  Downloading MarkupSafe-1.0.tar.gz
Installing collected packages: MarkupSafe, jinja2
  Running setup.py install for MarkupSafe: started
    Running setup.py install for MarkupSafe: finished with status 'done'
Successfully installed MarkupSafe-1.0 jinja2-2.9.5

Finally, rejoice, for you can now run the script! I've included example template and csv files which are used below to generate 10 configs.

$ ./t.py gencfg -t example.j2 -d example.csv
Generated 10 files in configs/

This is how it looks in the actual window from start to finish.

Diagram

You can get MinTTY to be multi-tabbed and look better by following the steps in A usable and good looking shell on Windows. There's no need to install Cygwin, but if you already have it (or you'd rather use it as an environment) then it has its own python and git packages you can install (but they won't be available Windows-wide).

And, as always, thanks for reading.


comments powered by Disqus