Skip to main content

Git-based version information from Python script

I had this idea of generating version information for a Python script that uses ArgParse. The code is a little more than I was expecting but I think it works well. Here is the code:

Usage

Here is an example of its usage if the script is part of a git repository:

$ ./version-example --version
b92798, master, 2019-01-18 10:35:02,
['origin:https://github.com/pfuntner/gists.git']
$

It contains:
  • The SHA1 of last git commit that changed the script
  • The current branch of the repository
  • The date of the commit - I think the timezone element is present in this but I didn't want to deal with timezones so I'm ignoring it
  • A list of the remote repositories

This is printed on two lines but that's something that ArgParse is doing, not me.

Here is an example of its usage if the script is not part of a git repository - we don't have much information to work from but we can at least get the timestamp of the script:

$ ~/tmp/version-example --version
2019-01-18 11:07:33.873846
$

You'll also get this output if the script is in the repository directory but hasn't been committed to it.

Comments

Popular posts from this blog

Dynamic Python script loading

I have a bunch of toys and tools in a Git repository - I affectionately call this my toys repo . Most are just scripts that I use from a Unix (Cygwin or Git bash on Windoze) command line but there are some Python classes that I sometimes use in another script. Today at work, I was coming up with a new Python script that made use of a couple of my public classes. The script is good enough to share with my colleagues but I'm faced with the problem of my public classes: I imagine that most of my colleagues haven't even heard of my public classes and I don't expect them to download the entire repo just to get a couple of classes If I'm going to distribute the classes as separate files I introduce new problems: It could be confusing to have the files tag along. What is the user supposed to do with them? The answer is nothing - they should leave them alone and make sure they are in the same directory as the main script in case they decide to move th...

New Years Eve 2009

So this is my first blog entry!! It's New Years Eve and I've got a horrible cold so I'm staying in a lot. I had planned to see Avatar at the IMAX theater in Raleigh this afternoon but don't want to risk people exposing people to what I have. Also, I would probably annoy people with lots of nose blowing. I ordered tickets online and, using Craigslist , I arranged to sell them to someone else who can use them since the prime show times have been selling out. So... I have to wait to see Avatar. I still have foster cats Jack and Purrvis from the SPCA ... Jack gets supervised visits but I've had to defuse lots of situations: a couple of fights with Katie in the past week plus Sofi let out this noise I'd never heard before.... I didn't think she had it in her. So usually I just ferry Jack back into the bedroom and let them cool off. I just had to break up a prolonged fight so I may not let Jack out much until I take them back to the SPCA on Saturday. I went i...

Python ArgParse

Maybe it's my use of the getopt() function in C/C++ but in my Python scripts, I've always just made do with the native getopt package and it's served me well. Over time, people have encouraged me to look at using ArgParse but I've always been kind of resistant because it was a little mysterious to me. I finally sat down and figured out how to do basic things I do a lot in scripts using getopt and worked up a simple script using ArgParse to do the same thing. There are definitely some advantages to ArgParse ! Here's the gist with this sample: I've made a few updates to the gist as I discover new features of which I want to take advantage: Mutually-exclusive options Script description etc. I will admit that since I've created this gist, I've been using ArgParse for new scripts. I can't whip out a script using ArgParse from memory yet (which I can do when I use getopt !) but it's becoming easier. I haven't gone back and...