Git commit hash arg

This commit is contained in:
rasbt 2014-06-29 01:22:49 -04:00
parent fbe7759fd7
commit 1bbf720e69
2 changed files with 72 additions and 38 deletions

View File

@ -1,7 +1,7 @@
{ {
"metadata": { "metadata": {
"name": "", "name": "",
"signature": "sha256:72529e8db6a991fa9a1ce25da1f89e9aa64dd9fdffdcdbef3ae0f18098f5ab0a" "signature": "sha256:f60eef12fdb981a16a1ac0578a3dcf7377d991866b8528653a69fd33bf2ba8f9"
}, },
"nbformat": 3, "nbformat": 3,
"nbformat_minor": 0, "nbformat_minor": 0,
@ -81,6 +81,26 @@
], ],
"prompt_number": 1 "prompt_number": 1
}, },
{
"cell_type": "code",
"collapsed": false,
"input": [
"%install_ext ./watermark.py"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Installed watermark.py. To use it, type:\n",
" %load_ext watermark\n"
]
}
],
"prompt_number": 1
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@ -153,20 +173,18 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"<pre>%watermark [-a AUTHOR] [-e AUTHOR_EMAIL] [-d] [-n] [-t] [-z] [-u]\n", "<pre> %watermark [-a AUTHOR] [-d] [-n] [-t] [-z] [-u] [-c CUSTOM_TIME] [-v]\n",
" [-c CUSTOM_TIME] [-v] [-p PACKAGES] [-m]\n", " [-p PACKAGES] [-h] [-m] [-g]\n",
"\n", "\n",
" \n", " \n",
"IPython magic function to print date/time stamps \n", "IPython magic function to print date/time stamps \n",
"and various system information.\n", "and various system information.\n",
"\n", "\n",
"watermark version 1.0.2\n", "watermark version 1.1.0\n",
"\n", "\n",
"optional arguments:\n", "optional arguments:\n",
" -a AUTHOR, --author AUTHOR\n", " -a AUTHOR, --author AUTHOR\n",
" prints author name\n", " prints author name\n",
" -e AUTHOR_EMAIL, --author_email AUTHOR_EMAIL\n",
" prints author name and link to email address\n",
" -d, --date prints current date\n", " -d, --date prints current date\n",
" -n, --datename prints date with abbrv. day and month names\n", " -n, --datename prints date with abbrv. day and month names\n",
" -t, --time prints current time\n", " -t, --time prints current time\n",
@ -180,6 +198,7 @@
" packages\n", " packages\n",
" -h, --hostname prints the host name\n", " -h, --hostname prints the host name\n",
" -m, --machine prints system and machine info\n", " -m, --machine prints system and machine info\n",
" -g, --githash prints current Git commit hash\n",
"</pre>" "</pre>"
] ]
}, },
@ -212,7 +231,7 @@
"output_type": "stream", "output_type": "stream",
"stream": "stdout", "stream": "stdout",
"text": [ "text": [
"28/06/2014 16:35:11\n", "29/06/2014 01:19:10\n",
"\n", "\n",
"CPython 3.4.1\n", "CPython 3.4.1\n",
"IPython 2.1.0\n", "IPython 2.1.0\n",
@ -227,7 +246,7 @@
] ]
} }
], ],
"prompt_number": 4 "prompt_number": 3
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -249,11 +268,11 @@
"output_type": "stream", "output_type": "stream",
"stream": "stdout", "stream": "stdout",
"text": [ "text": [
"28/06/2014 16:35:12 \n" "29/06/2014 01:19:11 \n"
] ]
} }
], ],
"prompt_number": 5 "prompt_number": 4
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -275,11 +294,11 @@
"output_type": "stream", "output_type": "stream",
"stream": "stdout", "stream": "stdout",
"text": [ "text": [
"Last updated: Sat Jun 35 2014 16:35:12 EDT\n" "Last updated: Sun Jun 19 2014 01:19:12 EDT\n"
] ]
} }
], ],
"prompt_number": 6 "prompt_number": 5
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -306,7 +325,7 @@
] ]
} }
], ],
"prompt_number": 7 "prompt_number": 6
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -338,7 +357,7 @@
] ]
} }
], ],
"prompt_number": 8 "prompt_number": 7
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -376,7 +395,7 @@
] ]
} }
], ],
"prompt_number": 9 "prompt_number": 8
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -389,7 +408,7 @@
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"%watermark -a \"John Doe\" -d -v -m " "%watermark -a \"John Doe\" -d -v -m -g"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
@ -398,7 +417,7 @@
"output_type": "stream", "output_type": "stream",
"stream": "stdout", "stream": "stdout",
"text": [ "text": [
"John Doe 28/06/2014 \n", "John Doe 29/06/2014 01:20:48 EDT\n",
"\n", "\n",
"CPython 3.4.1\n", "CPython 3.4.1\n",
"IPython 2.1.0\n", "IPython 2.1.0\n",
@ -409,11 +428,12 @@
"machine : x86_64\n", "machine : x86_64\n",
"processor : i386\n", "processor : i386\n",
"CPU cores : 2\n", "CPU cores : 2\n",
"interpreter: 64bit\n" "interpreter: 64bit\n",
"Git hash : fbe7759fd7e0298bf0bd05ea4aac01b87aa8ed25\n"
] ]
} }
], ],
"prompt_number": 10 "prompt_number": 16
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",

View File

@ -2,7 +2,7 @@
Sebastian Raschka 2014 Sebastian Raschka 2014
watermark.py watermark.py
version 1.0.3 version 1.1.0
IPython magic function to print date/time stamps and various system information. IPython magic function to print date/time stamps and various system information.
@ -17,11 +17,10 @@ Usage:
%watermark %watermark
optional arguments: optional arguments:
-a AUTHOR, --author AUTHOR -a AUTHOR, --author AUTHOR
prints author name prints author name
-e AUTHOR_EMAIL, --author_email AUTHOR_EMAIL
prints author name and link to email address
-d, --date prints current date -d, --date prints current date
-n, --datename prints date with abbrv. day and month names -n, --datename prints date with abbrv. day and month names
-t, --time prints current time -t, --time prints current time
@ -35,6 +34,7 @@ Usage:
packages packages
-h, --hostname prints the host name -h, --hostname prints the host name
-m, --machine prints system and machine info -m, --machine prints system and machine info
-g, --githash prints current Git commit hash
Examples: Examples:
@ -43,6 +43,7 @@ Examples:
""" """
import platform import platform
import subprocess
from time import strftime from time import strftime
from socket import gethostname from socket import gethostname
from pkg_resources import get_distribution from pkg_resources import get_distribution
@ -71,13 +72,14 @@ class WaterMark(Magics):
@argument('-p', '--packages', type=str, help='prints versions of specified Python modules and packages') @argument('-p', '--packages', type=str, help='prints versions of specified Python modules and packages')
@argument('-h', '--hostname', action='store_true', help='prints the host name') @argument('-h', '--hostname', action='store_true', help='prints the host name')
@argument('-m', '--machine', action='store_true', help='prints system and machine info') @argument('-m', '--machine', action='store_true', help='prints system and machine info')
@argument('-g', '--githash', action='store_true', help='prints current Git commit hash')
@line_magic @line_magic
def watermark(self, line): def watermark(self, line):
""" """
IPython magic function to print date/time stamps IPython magic function to print date/time stamps
and various system information. and various system information.
watermark version 1.0.3 watermark version 1.1.0
""" """
self.out = '' self.out = ''
@ -114,6 +116,9 @@ class WaterMark(Magics):
if args.machine: if args.machine:
space = ' ' space = ' '
self.out += '\nhost name%s: %s' %(space, gethostname()) self.out += '\nhost name%s: %s' %(space, gethostname())
if args.githash:
self._get_commit_hash(bool(args.machine))
@ -154,5 +159,14 @@ class WaterMark(Magics):
) )
def _get_commit_hash(self, machine):
process = subprocess.Popen(['git', 'rev-parse', 'HEAD'], shell=False, stdout=subprocess.PIPE)
git_head_hash = process.communicate()[0].strip()
space = ''
if machine:
space = ' '
self.out += '\nGit hash%s: %s' %(space, git_head_hash.decode("utf-8"))
def load_ipython_extension(ipython): def load_ipython_extension(ipython):
ipython.register_magics(WaterMark) ipython.register_magics(WaterMark)