Repository navigation
Expand file tree
/
Copy pathpre-commit.py
More file actions
executable file
·94 lines (67 loc) · 2.37 KB
/
Copy pathpre-commit.py
File metadata and controls
executable file
·94 lines (67 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
# coding:utf-8
"""
a git pre-commit hook to update a date in __version__
more info in README:
https://github.com/jedie/python-code-snippets/tree/master/CodeSnippets/git/#readme
:copyleft: 2012 by Jens Diemer
:license: GNU GPL v3 or above
:homepage: https://github.com/jedie/python-code-snippets/tree/master/CodeSnippets/git/
"""
import os
import re
import datetime
#DEBUG = True
DEBUG = False
FILENAMES = ("__init__.py",)
VERSION_PREFIX = "__version__"
VERSION_REGEX = re.compile(r'^__version__ = \((.*,\s*)(\d{6}\s*)\)$', re.MULTILINE)
NEW_VERSION = '__version__ = (%(ver)s%(date)s)'
NEW_DATE = datetime.datetime.utcnow().strftime("%y%m%d")
class DateChanger(object):
def __init__(self):
self.changed = False
self.matched = False
def sub(self, matchobj):
self.matched = True
old_date = matchobj.group(2)
if old_date == NEW_DATE:
if DEBUG:
print "date %r is up to date" % old_date
return matchobj.group(0)
if DEBUG:
print "change date from %r to %r" % (old_date, NEW_DATE)
self.changed = True
version = matchobj.group(1)
new_date = NEW_VERSION % {"ver": version, "date": NEW_DATE}
return new_date
def update_file(filepath):
with file(filepath, "r") as f:
old_content = f.read()
if not VERSION_PREFIX in old_content:
return
if DEBUG:
print "VERSION_PREFIX found in %r" % filepath
date_changer = DateChanger()
new_content = VERSION_REGEX.sub(date_changer.sub, old_content)
if not date_changer.matched:
print "Error: date not found in %s" % filepath
return
if not date_changer.changed:
print "No updated needed in %r" % filepath
return
print "update __version__ with %r in file %r" % (NEW_DATE, filepath)
with file(filepath, "w") as f:
f.write(new_content)
def update_version_number(path):
count = 0
for root, dirlist, filelist in os.walk(path, followlinks=True):
for filename in FILENAMES:
if filename in filelist:
count += 1
update_file(os.path.join(root, filename))
print "(%i files checked.)" % count
if __name__ == '__main__':
print "*** START %r ***" % __file__
update_version_number(".")
print "*** END %r ***" % __file__