|
2 | 2 | Sebastian Raschka 2014 |
3 | 3 |
|
4 | 4 | watermark.py |
5 | | -version 1.0.3 |
| 5 | +version 1.1.0 |
6 | 6 |
|
7 | 7 |
|
8 | 8 | IPython magic function to print date/time stamps and various system information. |
9 | 9 |
|
10 | 10 | Installation: |
11 | 11 |
|
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 |
13 | 13 | |
14 | 14 | Usage: |
15 | 15 |
|
16 | | - %load_ext watermark |
| 16 | + %load_ext watermark |
17 | 17 | |
18 | | - %watermark |
| 18 | + %watermark |
19 | 19 | |
20 | | - optional arguments: |
21 | | - -a AUTHOR, --author AUTHOR |
| 20 | +optional arguments: |
| 21 | +
|
| 22 | + -a AUTHOR, --author AUTHOR |
22 | 23 | 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 |
31 | 30 | 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 |
34 | 33 | prints versions of specified Python modules and |
35 | 34 | 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 |
38 | 38 |
|
39 | 39 |
|
40 | 40 | Examples: |
|
43 | 43 | |
44 | 44 | """ |
45 | 45 | import platform |
| 46 | +import subprocess |
46 | 47 | from time import strftime |
47 | 48 | from socket import gethostname |
48 | 49 | from pkg_resources import get_distribution |
@@ -71,13 +72,14 @@ class WaterMark(Magics): |
71 | 72 | @argument('-p', '--packages', type=str, help='prints versions of specified Python modules and packages') |
72 | 73 | @argument('-h', '--hostname', action='store_true', help='prints the host name') |
73 | 74 | @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') |
74 | 76 | @line_magic |
75 | 77 | def watermark(self, line): |
76 | 78 | """ |
77 | 79 | IPython magic function to print date/time stamps |
78 | 80 | and various system information. |
79 | 81 | |
80 | | - watermark version 1.0.3 |
| 82 | + watermark version 1.1.0 |
81 | 83 | |
82 | 84 | """ |
83 | 85 | self.out = '' |
@@ -114,6 +116,9 @@ def watermark(self, line): |
114 | 116 | if args.machine: |
115 | 117 | space = ' ' |
116 | 118 | self.out += '\nhost name%s: %s' %(space, gethostname()) |
| 119 | + if args.githash: |
| 120 | + self._get_commit_hash(bool(args.machine)) |
| 121 | + |
117 | 122 |
|
118 | 123 |
|
119 | 124 |
|
@@ -154,5 +159,14 @@ def _get_sysinfo(self): |
154 | 159 | ) |
155 | 160 |
|
156 | 161 |
|
| 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 | + |
157 | 171 | def load_ipython_extension(ipython): |
158 | 172 | ipython.register_magics(WaterMark) |
0 commit comments