|
@@ -0,0 +1,179 @@
|
|
1
|
+#!/usr/bin/env python
|
|
2
|
+# -*- coding: utf-8 -*-
|
|
3
|
+import argparse
|
|
4
|
+import subprocess
|
|
5
|
+
|
|
6
|
+import requests
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+def execute(*args, **kwargs):
|
|
10
|
+ try:
|
|
11
|
+ return subprocess.check_output(*args, **kwargs)
|
|
12
|
+ except subprocess.CalledProcessError as e:
|
|
13
|
+ print(e.output.decode('utf-8'))
|
|
14
|
+ exit(1)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+def check_setup_py_clean():
|
|
18
|
+ git_status = execute(['git', 'status'])
|
|
19
|
+ for line in git_status.decode('utf-8').split('\n'):
|
|
20
|
+ if 'setup.py' in line:
|
|
21
|
+ print('ERR: setup.py file seems to be already modified')
|
|
22
|
+ exit(1)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+def extract_version_from_setup_py_content():
|
|
26
|
+ with open('setup.py', 'r') as setup_file:
|
|
27
|
+ setup_file_content = setup_file.read()
|
|
28
|
+
|
|
29
|
+ for line in setup_file_content.split('\n'):
|
|
30
|
+ if line.strip().startswith('version='):
|
|
31
|
+ version_str = line.replace('\'', '')\
|
|
32
|
+ .replace('version=', '')\
|
|
33
|
+ .replace(',', '')
|
|
34
|
+
|
|
35
|
+ major_version_str = version_str.split('.')[0].strip()
|
|
36
|
+ minor_version_str = version_str.split('.')[1]
|
|
37
|
+
|
|
38
|
+ if minor_version_str is None:
|
|
39
|
+ print('ERR: No minor version found !')
|
|
40
|
+ exit(1)
|
|
41
|
+
|
|
42
|
+ try:
|
|
43
|
+ int(minor_version_str)
|
|
44
|
+ except ValueError:
|
|
45
|
+ print(
|
|
46
|
+ 'ERR: Error when read minor version, value found: "{}"'
|
|
47
|
+ .format(minor_version_str)
|
|
48
|
+ )
|
|
49
|
+
|
|
50
|
+ return version_str.strip()
|
|
51
|
+
|
|
52
|
+ print('ERR: Version not found in setup.py')
|
|
53
|
+ exit(1)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+def get_last_pypi_version():
|
|
57
|
+ hapic_pypi_version_response = requests.get(
|
|
58
|
+ 'https://pypi.python.org/pypi/hapic/json',
|
|
59
|
+ )
|
|
60
|
+ hapic_pypi_versions_data = hapic_pypi_version_response.json()
|
|
61
|
+ hapic_pypi_versions = hapic_pypi_versions_data['releases'].keys()
|
|
62
|
+ last_version = sorted(hapic_pypi_versions)[-1]
|
|
63
|
+ return last_version
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+def get_new_hapic_version():
|
|
67
|
+ version_str = extract_version_from_setup_py_content()
|
|
68
|
+ major_version_str = version_str.split('.')[0].strip()
|
|
69
|
+ minor_version_str = version_str.split('.')[1]
|
|
70
|
+
|
|
71
|
+ next_version = '{}.{}'.format(major_version_str, int(minor_version_str)+1)
|
|
72
|
+ last_pypi_version = get_last_pypi_version()
|
|
73
|
+
|
|
74
|
+ last_version_major = last_pypi_version.split('.')[0]
|
|
75
|
+ last_version_minor = last_pypi_version.split('.')[1]
|
|
76
|
+ last_version_more_one = '{}.{}'.format(
|
|
77
|
+ last_version_major,
|
|
78
|
+ int(last_version_minor)+1,
|
|
79
|
+ )
|
|
80
|
+
|
|
81
|
+ if next_version != last_version_more_one:
|
|
82
|
+ if input(
|
|
83
|
+ 'WARN: This script was preparing to create version {} but '
|
|
84
|
+ 'last version on pypi is {}. Consider to use version {} '
|
|
85
|
+ '?(y/N)'.format(
|
|
86
|
+ next_version,
|
|
87
|
+ last_pypi_version,
|
|
88
|
+ last_version_more_one,
|
|
89
|
+ )
|
|
90
|
+ ) in ['y', 'Y']:
|
|
91
|
+ return last_version_more_one
|
|
92
|
+ else:
|
|
93
|
+ exit(1)
|
|
94
|
+
|
|
95
|
+ return last_version_more_one
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+def rewrite_setup_py_file(new_version: str):
|
|
99
|
+ with open('setup.py', 'r') as setup_file:
|
|
100
|
+ setup_file_content = setup_file.read()
|
|
101
|
+
|
|
102
|
+ new_content_lines = []
|
|
103
|
+ for line in setup_file_content.split('\n'):
|
|
104
|
+ if line.strip().startswith('version='):
|
|
105
|
+ new_content_lines.append(' version=\'{}\','.format(
|
|
106
|
+ new_version
|
|
107
|
+ ))
|
|
108
|
+ else:
|
|
109
|
+ new_content_lines.append(line)
|
|
110
|
+
|
|
111
|
+ with open('setup.py', 'w') as setup_file:
|
|
112
|
+ setup_file.write('\n'.join(new_content_lines))
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+def commit_setup_py():
|
|
116
|
+ execute(['git', 'reset'])
|
|
117
|
+ execute(['git', 'add', 'setup.py'])
|
|
118
|
+
|
|
119
|
+ version = extract_version_from_setup_py_content()
|
|
120
|
+ execute(
|
|
121
|
+ ['git', 'commit', '-m', 'v{}'.format(version)]
|
|
122
|
+ )
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+def check_branch(must_be_branch):
|
|
126
|
+ current_branch = execute(
|
|
127
|
+ ['git', 'rev-parse', '--abbrev-ref', 'HEAD']
|
|
128
|
+ ).strip().decode('utf-8')
|
|
129
|
+
|
|
130
|
+ if current_branch != must_be_branch:
|
|
131
|
+ print('ERR: Your are actually on branch {} and must be {}'.format(
|
|
132
|
+ current_branch,
|
|
133
|
+ must_be_branch,
|
|
134
|
+ ))
|
|
135
|
+ exit(1)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+def push_on_github(remote_name):
|
|
139
|
+ execute(
|
|
140
|
+ ['git', 'push', remote_name, 'master']
|
|
141
|
+ )
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+def push_on_pypi():
|
|
145
|
+ execute(
|
|
146
|
+ ['python', 'setup.py', 'sdist', 'upload']
|
|
147
|
+ )
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+def main():
|
|
151
|
+ parser = argparse.ArgumentParser()
|
|
152
|
+ parser.add_argument(
|
|
153
|
+ '--disable-version-increment',
|
|
154
|
+ '-d',
|
|
155
|
+ action='store_true',
|
|
156
|
+ dest='disable_version_increment',
|
|
157
|
+ )
|
|
158
|
+ args = parser.parse_args()
|
|
159
|
+
|
|
160
|
+ if not args.disable_version_increment:
|
|
161
|
+ must_be_branch = 'master'
|
|
162
|
+ remote_name = 'origin'
|
|
163
|
+
|
|
164
|
+ check_setup_py_clean()
|
|
165
|
+ check_branch(must_be_branch)
|
|
166
|
+
|
|
167
|
+ new_version_will_be = get_new_hapic_version()
|
|
168
|
+ actual_version = extract_version_from_setup_py_content()
|
|
169
|
+
|
|
170
|
+ if new_version_will_be != actual_version:
|
|
171
|
+ rewrite_setup_py_file(new_version_will_be)
|
|
172
|
+ commit_setup_py()
|
|
173
|
+ push_on_github(remote_name)
|
|
174
|
+
|
|
175
|
+ push_on_pypi()
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+if __name__ == '__main__':
|
|
179
|
+ main()
|