forked from modflowpy/flopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit.py
160 lines (132 loc) · 5.29 KB
/
pre-commit.py
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/python
from __future__ import print_function
import subprocess
import os
import sys
import datetime
files = ['version.py', 'README.md']
pak = 'flopy'
def get_version_str(v0, v1, v2, v3):
version_type = ('{}'.format(v0),
'{}'.format(v1),
'{}'.format(v2),
'{}'.format(v3))
version = '.'.join(version_type)
return version
def get_tag(v0, v1, v2):
tag_type = ('{}'.format(v0),
'{}'.format(v1),
'{}'.format(v2))
tag = '.'.join(tag_type)
return tag
def update_version():
try:
pth = os.path.join(pak, files[0])
vmajor = 0
vminor = 0
vmicro = 0
vbuild = 0
lines = [line.rstrip('\n') for line in open(pth, 'r')]
for line in lines:
t = line.split()
if 'major =' in line:
vmajor = int(t[2])
elif 'minor =' in line:
vminor = int(t[2])
elif 'micro =' in line:
vmicro = int(t[2])
elif 'build =' in line:
vbuild = int(t[2])
v0 = get_version_str(vmajor, vminor, vmicro, vbuild)
# get latest build number
tag = get_tag(vmajor, vminor, vmicro)
print('determining version build from {}'.format(tag))
try:
b = subprocess.Popen(("git", "describe", "--match", tag),
stdout=subprocess.PIPE).communicate()[0]
vbuild = int(b.decode().strip().split('-')[1]) + 1
# assume if tag does not exist that it has not been added
except:
vbuild = 0
v1 = get_version_str(vmajor, vminor, vmicro, vbuild)
# get current build number
b = subprocess.Popen(("git", "describe", "--match", "build"),
stdout=subprocess.PIPE).communicate()[0]
vcommit = int(b.decode().strip().split('-')[1]) + 2
print('Updating version:')
print(' ', v0, '->', v1)
# write new version file
f = open(pth, 'w')
f.write('# {} version file automatically '.format(pak) +
'created using...{0}\n'.format(os.path.basename(__file__)))
f.write('# created on...' +
'{0}\n'.format(
datetime.datetime.now().strftime("%B %d, %Y %H:%M:%S")))
f.write('\n')
f.write('major = {}\n'.format(vmajor))
f.write('minor = {}\n'.format(vminor))
f.write('micro = {}\n'.format(vmicro))
f.write('build = {}\n'.format(vbuild))
f.write('commit = {}\n\n'.format(vcommit))
f.write("__version__ = '{:d}.{:d}.{:d}'.format(major, minor, micro)\n")
f.write(
"__build__ = '{:d}.{:d}.{:d}.{:d}'.format(major, minor, micro, build)\n")
f.write("__git_commit__ = '{:d}'.format(commit)\n")
f.close()
print('Succesfully updated version.py')
except:
print('There was a problem updating the version file')
sys.exit(1)
# update README.md with new version information
update_readme_markdown(vmajor, vminor, vmicro, vbuild)
def add_updated_files():
try:
# add modified version file
print('Adding updated files to repo')
b = subprocess.Popen(("git", "add", "-u"),
stdout=subprocess.PIPE).communicate()[0]
except:
print('Could not add updated version file')
sys.exit(1)
def update_readme_markdown(vmajor, vminor, vmicro, vbuild):
try:
# determine current buildstat branch
b = subprocess.Popen(("git", "status"),
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT).communicate()[0]
if isinstance(b, bytes):
b = b.decode('utf-8')
# determine current buildstat branch
for line in b.splitlines():
if 'On branch' in line:
branch = line.replace('On branch ', '').rstrip()
except:
print('Cannot update README.md - could not determine current branch')
return
# create version
version = get_tag(vmajor, vminor, vmicro)
# read README.md into memory
with open(files[1], 'r') as file:
lines = [line.strip() for line in file]
# rewrite README.md
f = open(files[1], 'w')
for line in lines:
if '### Version ' in line:
line = '### Version {}'.format(version)
if vbuild > 0:
line += ' {} — build {}'.format(branch, vbuild)
elif '[Build Status]' in line:
line = '[![Build Status](https://travis-ci.org/modflowpy/' + \
'flopy.svg?branch={})]'.format(branch) + \
'(https://travis-ci.org/modflowpy/flopy)'
elif '[Coverage Status]' in line:
line = '[![Coverage Status](https://coveralls.io/repos/github/' + \
'modflowpy/flopy/badge.svg?branch={})]'.format(branch) + \
'(https://coveralls.io/github/modflowpy/' + \
'flopy?branch={})'.format(branch)
f.write('{}\n'.format(line))
f.close()
return
if __name__ == "__main__":
update_version()
add_updated_files()