This repository has been archived by the owner on Jul 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_version.py
executable file
·161 lines (107 loc) · 4.66 KB
/
set_version.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
161
#!/usr/bin/env python
# Run this script with a '-h' or '--help' option to get usage and description.
from optparse import OptionParser
import json
from datetime import datetime
import sys
usage = '''Usage: %prog [options]'''
description = '''
This script will sanitize json input while helping to set values.
'''
products = [
'bronze',
'cloud',
'pitcher',
'reporting',
'webclient',
'webstore',
]
environments = [
'cloud',
'production',
'staging',
'wsr_production',
'wsr_staging',
]
def update_versions(infile, outfile, product, environment, version, comment):
'''Set specific JSON version paylod values'''
versions = _get_json_from_file(infile)
# Set specific values in the dictionary
versions['products'][product]['environments'][environment]['version'] = \
version
versions['products'][product]['environments'][environment]['comment'] = \
comment
# Reoutput the json file
with open(outfile, 'w') as filehandle:
json.dump(versions, filehandle, sort_keys=True, indent=4,
separators=(',', ': '))
def _get_json_from_file(filename):
'''Parse a json file to get a dictionary'''
with open(filename, 'r') as filehandle:
versions = json.load(filehandle)
return versions
def get_version(infile, product, environment):
'''Parse a json file to get a dictionary'''
versions = _get_json_from_file(infile)
return versions['products'][product]['environments'][environment]['version']
def parse_args(argv):
'''Process command-line arguments'''
parser = OptionParser(usage=usage, description=description)
parser.add_option('-i', '--infile', dest='infile',
type='string', nargs=1, action='store',
default='versions.json',
help='(OPTIONAL) JSON input file to process')
parser.add_option('-o', '--outfile', dest='outfile',
type='string', nargs=1, action='store',
default='versions.json',
help='(OPTIONAL) JSON output file to process')
parser.add_option('-p', '--product', dest='product',
type='string', nargs=1, action='store',
help='Product to be updated: {}'.format(products))
parser.add_option('-e', '--environment', dest='environment',
type='string', nargs=1, action='store',
help='Environment to be updated: {}'.format(environments))
parser.add_option('-v', '--version', dest='version',
type='string', nargs=1, action='store',
help='Name of the tag/commit/branch to enter')
parser.add_option('-c', '--comment', dest='comment',
type='string', nargs=1, action='store',
help='(OPTIONAL) Comment to try to describe the version')
parser.add_option('-g', '--get', dest='get',
type='int', nargs=1, action='store', default=0,
help='(OPTIONAL) set to 1 to get a console return of the value desired. Otherwise 0 assumed')
(options, args) = parser.parse_args(argv)
# Sanitize the product string
if options.product not in products:
parser.error('Product must be one of {}'.format(products))
# Sanitize the environment string
if options.environment not in environments:
parser.error('Environment must be one of {}'.format(environments))
# Produce a sensible UTC value to use for naming things
tagtime = datetime.utcnow().strftime('%Y-%m-%d-%H%M')
# Sanitize the version string
if options.version is None or options.version == '':
options.version = '{}-{}'.format(options.product, tagtime)
# Sanitize the comment string
if options.comment is None:
options.comment = '{} {} at {}'.format(options.product,
options.environment, tagtime)
return (options, args)
def do_work(options):
'''Fire off the version and comment string stuff'''
if options.get == 0:
update_versions(infile=options.infile, outfile=options.outfile,
product=options.product,
environment=options.environment,
version=options.version, comment=options.comment)
else:
print get_version(infile=options.infile, product=options.product,
environment=options.environment)
def main(argv=None):
'''main() flow. Parse arguments and execute appropriate actions'''
if argv is None:
argv = sys.argv
(options, args) = parse_args(argv)
do_work(options)
if __name__ == '__main__':
sys.exit(main())