This repository has been archived by the owner on Jan 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcut_release.py
155 lines (110 loc) · 4.16 KB
/
cut_release.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
#!/usr/bin/env python
# * coding: utf8 *
"""
Releaser: Package up the python toolbox and related files for downloading
Usage:
cut_release (major|minor|patch) [--python-version=<version>]
cut_release publish
Options:
--python-version=<version> the desktop (2) or pro (3) tool to release [default: 3]
"""
from json import dump, load
from pathlib import Path
from zipfile import ZipFile
from docopt import docopt
from git import Repo
BUMP_TYPES = ['major', 'minor', 'patch']
BRANCHES = {'3': 'master', '2': 'py-2'}
VERSION_NAMES = {'3': 'PRO_VERSION_NUMBER', '2': 'VERSION_NUMBER'}
CONVENTIONAL_COMMITS = {'3': 'py-3', '2': 'py-2'}
BUILD_ASSETS = [Path('src') / 'agrcgeocoding' / 'geocode.py',
Path('src') / 'AGRC Geocode Tools.pyt'] + [Path('tool-version.json')]
TOOL_ZIP = Path('tool') / 'AGRC Geocode Tools.zip'
def cut_release(args):
"""main method
"""
python_version = args['--python-version']
print(f'release branch: {BRANCHES[python_version]}')
repo = Repo(Path(__file__).resolve().parent)
git_cli = repo.git
release_type = BUMP_TYPES[2]
if args['major']:
release_type = BUMP_TYPES[0]
elif args['minor']:
release_type = BUMP_TYPES[1]
print(f'release type: {release_type}')
current_version = get_version(python_version)
print(f'prior version: {current_version}')
if python_version == '2':
desktop_branch = BRANCHES['2']
git_cli.checkout(desktop_branch)
new_version = bump(current_version, release_type)
set_version(python_version, new_version)
build_zip()
release_commit(git_cli, new_version, python_version, include_zip=True)
#: switch back to master
git_cli.checkout(BRANCHES['3'])
set_version(python_version, new_version)
release_commit(git_cli, new_version, python_version, tag=False)
else:
new_version = bump(current_version, release_type)
set_version(python_version, new_version)
build_zip()
release_commit(git_cli, new_version, python_version, include_zip=True)
print(f'new version: {new_version}')
def release_commit(git_cli, new_version, python_version, include_zip=False, tag=True):
"""make release commit
"""
git_cli.add('tool-version.json')
if include_zip:
git_cli.add(TOOL_ZIP)
scope = CONVENTIONAL_COMMITS[python_version]
git_cli.commit(m=f'release({scope}): v{new_version}')
if tag:
git_cli.tag(f'v{new_version}-{scope}')
def build_zip():
"""bundle files into zip
"""
with ZipFile(TOOL_ZIP, 'w') as zip_file:
for filename in BUILD_ASSETS:
zip_file.write(filename, filename.name)
def bump(current_version, release_type):
"""bump the version number based on the release type
"""
parts = [int(part) for part in current_version.split('.')]
#: increment appropriate version number
bump_type_index = BUMP_TYPES.index(release_type)
parts[bump_type_index] += 1
#: reset any lower parts to zero
for lower_part in BUMP_TYPES[bump_type_index + 1:]:
parts[BUMP_TYPES.index(lower_part)] = 0
return '.'.join([str(part) for part in parts])
def get_version(python_version):
"""get the appropriate version number from tool-version.json
"""
with open('tool-version.json', 'r') as version_file:
version_json = load(version_file)
return version_json[VERSION_NAMES[python_version]]
def set_version(python_version, new_version):
"""update the version number in tool-version.json
"""
with open('tool-version.json', 'r+') as version_file:
version_json = load(version_file)
version_json[VERSION_NAMES[python_version]] = new_version
version_file.seek(0)
version_file.truncate()
dump(version_json, version_file, indent=2)
version_file.write('\n')
def publish():
"""push new commits/tags to GitHub
"""
repo = Repo(Path(__file__).resolve().parent)
git_cli = repo.git
for branch in BRANCHES.values():
git_cli.push('origin', branch, tags=True)
if __name__ == '__main__':
options = docopt(__doc__)
if options['publish']:
publish()
else:
cut_release(options)