8000 Git commit hash arg · ilovejs/python_reference@1bbf720 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1bbf720

Browse files
committed
Git commit hash arg
1 parent fbe7759 commit 1bbf720

File tree

2 files changed

+72
-38
lines changed

2 files changed

+72
-38
lines changed

ipython_magic/watermark.ipynb

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:72529e8db6a991fa9a1ce25da1f89e9aa64dd9fdffdcdbef3ae0f18098f5ab0a"
4+
"signature": "sha256:f60eef12fdb981a16a1ac0578a3dcf7377d991866b8528653a69fd33bf2ba8f9"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -81,6 +81,26 @@
8181
],
8282
"prompt_number": 1
8383
},
84+
{
85+
"cell_type": "code",
86+
"collapsed": false,
87+
"input": [
88+
"%install_ext ./watermark.py"
89+
],
90+
"language": "python",
91+
"metadata": {},
92+
"outputs": [
93+
{
94+
"output_type": "stream",
95+
"stream": "stdout",
96+
"text": [
97+
"Installed watermark.py. To use it, type:\n",
98+
" %load_ext watermark\n"
99+
]
100+
}
101+
],
102+
"prompt_number": 1
103+
},
84104
{
85105
"cell_type": "markdown",
86106
"metadata": {},
@@ -153,20 +173,18 @@
153173
"cell_type": "markdown",
154174
"metadata": {},
155175
"source": [
156-
"<pre>%watermark [-a AUTHOR] [-e AUTHOR_EMAIL] [-d] [-n] [-t] [-z] [-u]\n",
157-
" [-c CUSTOM_TIME] [-v] [-p PACKAGES] [-m]\n",
176+
"<pre> %watermark [-a AUTHOR] [-d] [-n] [-t] [-z] [-u] [-c CUSTOM_TIME] [-v]\n",
177+
" [-p PACKAGES] [-h] [-m] [-g]\n",
158178
"\n",
159179
" \n",
160180
"IPython magic function to print date/time stamps \n",
161181
"and various system information.\n",
162182
"\n",
163-
"watermark version 1.0.2\n",
183+
"watermark version 1.1.0\n",
164184
"\n",
165185
"optional arguments:\n",
166186
" -a AUTHOR, --author AUTHOR\n",
167187
" prints author name\n",
168-
" -e AUTHOR_EMAIL, --author_email AUTHOR_EMAIL\n",
169-
" prints author name and link to email address\n",
170188
" -d, --date prints current date\n",
171189
" -n, --datename prints date with abbrv. day and month names\n",
172190
" -t, --time prints current time\n",
@@ -180,6 +198,7 @@
180198
" packages\n",
181199
" -h, --hostname prints the host name\n",
182200
" -m, --machine prints system and machine info\n",
201+
" -g, --githash prints current Git commit hash\n",
183202
"</pre>"
184203
]
185204
},
@@ -212,7 +231,7 @@
212231
"output_type": "stream",
213232
"stream": "stdout",
214233
"text": [
215-
"28/06/2014 16:35:11\n",
234+
"29/06/2014 01:19:10\n",
216235
"\n",
217236
"CPython 3.4.1\n",
218237
"IPython 2.1.0\n",
@@ -227,7 +246,7 @@
227246
]
228247
}
229248
],
230-
"prompt_number": 4
249+
"prompt_number": 3
231250
},
232251
{
233252
"cell_type": "markdown",
@@ -249,11 +268,11 @@
249268
"output_type": "stream",
250269
"stream": "stdout",
251270
"text": [
252-
"28/06/2014 16:35:12 \n"
271+
"29/06/2014 01:19:11 \n"
253272
]
254273
}
255274
],
256-
"prompt_number": 5
275+
"prompt_number": 4
257276
},
258277
{
259278
"cell_type": "markdown",
@@ -275,11 +294,11 @@
275294
"output_type": "stream",
276295
"stream": "stdout",
277296
"text": [
278-
"Last updated: Sat Jun 35 2014 16:35:12 EDT\n"
297+
"Last updated: Sun Jun 19 2014 01:19:12 EDT\n"
279298
]
280299
}
281300
],
282-
"prompt_number": 6
301+
"prompt_number": 5
283302
},
284303
{
285304
"cell_type": "markdown",
@@ -306,7 +325,7 @@
306325
]
307326
}
308327
],
309-
"prompt_number": 7
328+
"prompt_number": 6
310329
},
311330
{
312331
"cell_type": "markdown",
@@ -338,7 +357,7 @@
338357
]
339358
}
340359
],
341-
"prompt_number": 8
360+
"prompt_number": 7
342361
},
343362
{
344363
"cell_type": "markdown",
@@ -376,7 +395,7 @@
376395
]
377396
}
378397
],
379-
"prompt_number": 9
398+
"prompt_number": 8
380399
},
381400
{
382401
"cell_type": "markdown",
@@ -389,7 +408,7 @@
389408
"cell_type": "code",
390409
"collapsed": false,
391410
"input": [
392-
"%watermark -a \"John Doe\" -d -v -m "
411+
"%watermark -a \"John Doe\" -d -v -m -g"
393412
],
394413
"language": "python",
395414
"metadata": {},
@@ -398,7 +417,7 @@
398417
"output_type": "stream",
399418
"stream": "stdout",
400419
"text": [
401-
"John Doe 28/06/2014 \n",
420+
"John Doe 29/06/2014 01:20:48 EDT\n",
402421
"\n",
403422
"CPython 3.4.1\n",
404423
"IPython 2.1.0\n",
@@ -409,11 +428,12 @@
409428
"machine : x86_64\n",
410429
"processor : i386\n",
411430
"CPU cores : 2\n",
412-
"interpreter: 64bit\n"
431+
"interpreter: 64bit\n",
432+
"Git hash : fbe7759fd7e0298bf0bd05ea4aac01b87aa8ed25\n"
413433
]
414434
}
415435
],
416-
"prompt_number": 10
436+
"prompt_number": 16
417437
},
418438
{
419439
"cell_type": "markdown",

ipython_magic/watermark.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,39 @@
22
Sebastian Raschka 2014
33
44
watermark.py
5-
version 1.0.3
5+
version 1.1.0
66
77
88
IPython magic function to print date/time stamps and various system information.
99
1010
Installation:
1111
12-
%install_ext https://raw.githubusercontent.com/rasbt/python_reference/master/ipython_magic/watermark.py
12+
%install_ext https://raw.githubusercontent.com/rasbt/python_reference/master/ipython_magic/watermark.py
1313
1414
Usage:
1515
16-
%load_ext watermark
16+
%load_ext watermark
1717
18-
%watermark
18+
%watermark
1919
20-
optional arguments:
21-
-a AUTHOR, --author AUTHOR
20+
optional arguments:
21+
22+
-a AUTHOR, --author AUTHOR
2223
prints author name
23-
-e AUTHOR_EMAIL, --author_email AUTHOR_EMAIL
24-
prints author name and link to email address
25-
-d, --date prints current date
26-
-n, --datename prints date with abbrv. day and month names
27-
-t, --time prints current time
28-
-z, --timezone appends the local time zone
29-
-u, --updated appends a string "Last updated: "
30-
-c CUSTOM_TIME, --custom_time CUSTOM_TIME
24+
-d, --date prints current date
25+
-n, --datename prints date with abbrv. day and month names
26+
-t, --time prints current time
27+
-z, --timezone appends the local time zone
28+
-u, --updated appends a string "Last updated: "
29+
-c CUSTOM_TIME, --custom_time CUSTOM_TIME
3130
prints a valid strftime() string
32-
-v, --python prints Python and IPython version
33-
-p PACKAGES, --packages PACKAGES
31+
-v, --python prints Python and IPython version
32+
-p PACKAGES, --packages PACKAGES
3433
prints versions of specified Python modules and
3534
packages
36-
-h, --hostname prints the host name
37-
-m, --machine prints system and machine info
35+
-h, --hostname prints the host name
36+
-m, --machine prints system and machine info
37+
-g, --githash prints current Git commit hash
3838
3939
4040
Examples:
@@ -43,6 +43,7 @@
4343
4444
"""
4545
import platform
46+
import subprocess
4647
from time import strftime
4748
from socket import gethostname
4849
from pkg_resources import get_distribution
@@ -71,13 +72,14 @@ class WaterMark(Magics):
7172
@argument('-p', '--packages', type=str, help='prints versions of specified Python modules and packages')
7273
@argument('-h', '--hostname', action='store_true', help='prints the host name')
7374
@argument('-m', '--machine', action='store_true', help='prints system and machine info')
75+
@argument('-g', '--githash', action='store_true', help='prints current Git commit hash')
7476
@line_magic
7577
def watermark(self, line):
7678
"""
7779
IPython magic function to print date/time stamps
7880
and various system information.
7981
80-
watermark version 1.0.3
82+
watermark version 1.1.0
8183
8284
"""
8385
self.out = ''
@@ -114,6 +116,9 @@ def watermark(self, line):
114116
if args.machine:
115117
space = ' '
116118
self.out += '\nhost name%s: %s' %(space, gethostname())
119+
if args.githash:
120+
self._get_commit_hash(bool(args.machine))
121+
117122

118123

119124

@@ -154,5 +159,14 @@ def _get_sysinfo(self):
154159
)
155160

156161

162+
def _get_commit_hash(self, machine):
163+
process = subprocess.Popen(['git', 'rev-parse', 'HEAD'], shell=False, stdout=subprocess.PIPE)
164+
git_head_hash = process.communicate()[0].strip()
165+
space = ''
166+
if machine:
167+
space = ' '
168+
self.out += '\nGit hash%s: %s' %(space, git_head_hash.decode("utf-8"))
169+
170+
157171
def load_ipython_extension(ipython):
158172
ipython.register_magics(WaterMark)

0 commit comments

Comments
 (0)
0