-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommit.py
More file actions
84 lines (74 loc) · 2.64 KB
/
commit.py
File metadata and controls
84 lines (74 loc) · 2.64 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
#!/usr/bin/python3
import sys, os, subprocess
import configparser
def get_version():
"""
Get current version number from git-tag
Returns:
string: v0.0.0
"""
result = subprocess.run(['git','tag'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False)
versionList= result.stdout.decode('utf-8').strip()
versionList= [i[1:] for i in versionList.split('\n')]
if versionList == ['']: #default
return 'v0.0.1'
versionList.sort(key=lambda s: list(map(int, s.split('.'))))
return 'v'+versionList[-1]
def newVersion(level=2, message=''):
"""
Create a new version
Args:
level (int): which number of the version to increase 0=mayor,1=minor,2=sub
message (str): what is the name/message
"""
#get old version number
version = [int(i) for i in get_version()[1:].split('.')]
#create new version number
version[level] += 1
for i in range(level+1,3):
version[i] = 0
version = '.'.join([str(i) for i in version])
print('======== Version '+version+' =======')
#update python files
filesToUpdate = {'micromechanics_indentationGUI/__init__.py':'__version__ = ', 'docs/source/conf.py':'version = '}
for path in filesToUpdate:
with open(path, encoding='utf-8') as fIn:
fileOld = fIn.readlines()
fileNew = []
for line in fileOld:
line = line[:-1] #only remove last char, keeping front part
if line.startswith(filesToUpdate[path]):
line = filesToUpdate[path]+'"'+version+'"'
fileNew.append(line)
with open(path,'w', encoding='utf-8') as fOut:
fOut.write('\n'.join(fileNew)+'\n')
#execute git commands: move tests away and back
os.system('git commit -a -m "'+message+'"')
os.system('git tag -a v'+version+' -m "Version '+version+'"')
os.system('git push')
os.system('git push origin v'+version)
return
def createRequirementsFile():
"""
Create a requirements.txt file from the setup.cfg information
"""
config = configparser.ConfigParser()
config.read('setup.cfg')
requirements = config['options']['install_requires'].split('\n')
requirementsLinux = [i for i in requirements if i!='' and 'Windows' not in i]
with open('requirements.txt','w', encoding='utf-8') as req:
req.write('#This file is autogenerated by commit.py from setup.cfg. Change content there\n')
req.write('\n'.join(requirementsLinux))
return
if __name__=='__main__':
createRequirementsFile()
if len(sys.argv)==1:
print("**Require more arguments for creating new version 'message' 'level (optionally)' ")
level = None
elif len(sys.argv)==2:
level=2
else:
level = int(sys.argv[2])
if level is not None:
message = sys.argv[1]
newVersion(level, message)