forked from Yelp/detect-secrets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbumpity.py
executable file
·132 lines (105 loc) · 3.08 KB
/
bumpity.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
#!/usr/bin/env python3
"""Bumps the detect-secrets version,
in both `detect_secrets/__init__.py` and `README.md`.
Then commits.
"""
import argparse
import pathlib
import subprocess
import sys
PROJECT_ROOT = pathlib.Path(__file__).absolute().parent.parent
INIT_FILE_PATH = PROJECT_ROOT.joinpath('detect_secrets/__init__.py')
README_FILE_PATH = PROJECT_ROOT.joinpath('README.md')
def _argparse_bump_type(value):
VALID_BUMP_TYPES = ('major', 'minor', 'patch')
if value in VALID_BUMP_TYPES:
return value
raise argparse.ArgumentTypeError(
f"Argument {value} must be one 'major', 'minor', 'patch'.",
)
def parse_args(argv):
parser = argparse.ArgumentParser(
description=__doc__,
prog='bumpity',
)
parser.add_argument(
'--bump',
help='the bump type, specified as one of {major, minor, patch}',
metavar='{major,minor,patch}',
type=_argparse_bump_type,
)
return parser.parse_args(argv)
def get_current_version():
with open(INIT_FILE_PATH) as init_file:
first_line = init_file.read().splitlines()[0]
# e.g. VERSION = '0.13.0'
_, semver = first_line.replace(' ', '').split('=')
return map(
int,
# e.g. '0.13.0'
semver.strip("'").split('.'),
)
def update_init_file(new_version):
with open(INIT_FILE_PATH, 'w') as init_file:
init_file.write(f"VERSION = '{new_version}'\n")
def update_readme(old_version, new_version):
with open(README_FILE_PATH, 'r') as readme:
original_text = readme.read()
with open(README_FILE_PATH, 'w') as readme:
readme.write(
original_text.replace(old_version, new_version),
)
def stage_and_commit(new_version):
# Stage files
subprocess.check_output(
(
'git',
'add',
INIT_FILE_PATH,
README_FILE_PATH,
),
)
# Check they are the only ones staged
staged_files = subprocess.check_output(
(
'git',
'diff',
'--staged',
'--name-only',
),
).splitlines()
if len(staged_files) != 2:
raise RuntimeWarning('More files staged than __init__.py and README.md')
# Make the commit
subprocess.check_output(
(
'git',
'commit',
'--message',
f':fist: Bumping version to {new_version}',
INIT_FILE_PATH,
README_FILE_PATH,
),
)
def main(argv=sys.argv[1:]):
if not argv:
argv.append('--help')
args = parse_args(argv)
major, minor, patch = get_current_version()
old_version = f'{major}.{minor}.{patch}'
if args.bump == 'major':
major += 1
minor = 0
patch = 0
elif args.bump == 'minor':
minor += 1
patch = 0
else:
patch += 1
new_version = f'{major}.{minor}.{patch}'
update_init_file(new_version)
update_readme(old_version, new_version)
stage_and_commit(new_version)
print("Don't forget to update CHANGELOG.md too!")
if __name__ == '__main__':
sys.exit(main())