This repository has been archived by the owner on Apr 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
create_release_file.py
executable file
·102 lines (74 loc) · 3.46 KB
/
create_release_file.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
#!/usr/bin/python2
from ruamel.yaml import YAML
from ruamel.yaml.comments import CommentedMap
import os
import argparse
import magic
def get_opts():
parser = argparse.ArgumentParser(description='Create a release yaml file for consumption '
'by git_release script.')
parser.add_argument('body', metavar='BODY', type=str, default=None,
help='a release notes .md file.')
parser.add_argument('version', metavar='VER', type=str, default=None,
help='oshinko version (eg. 0.5.2).')
parser.add_argument('destination', metavar='DEST', type=str, default=None,
help='destination path of where the release file generated '
'(e.g. /etc/file.yaml.')
# Optionals
parser.add_argument('-t', dest='tag_name', type=str, default=None,
help='tag name for this release, auto generated `v{$VERSION}` by default.')
parser.add_argument('-n', dest='name', type=str, default=None,
help='name for this release, auto generated '
'`version {$VERSION}` by default.')
parser.add_argument('-tc', dest='target_commit', type=str, default='master',
help='target commit for this release, master by default.')
parser.add_argument('-d', dest='draft', action='store_true', help='sets draft to true')
parser.add_argument('-pr', dest='prerelease', action='store_true',
help='sets this release as a prerelease if specified.')
parser.add_argument('-a', dest='assets', type=str, default=None,
help='path to dir containing asset files for this release, note all files '
'in this directory will be used as asset files so ensure only assets '
'exist in this directory.')
args = parser.parse_args()
version = args.version
body = args.body
# optionals
tag_name = args.tag_name
name = args.name
target_commit = args.target_commit
draft = args.draft
prerelease = args.prerelease
assets = args.assets
destination = args.destination
return version, body, tag_name, name, target_commit, draft, prerelease, assets, destination
def main():
version, body, tag_name, name, target_commit, draft, prerelease, asset_path, dest = get_opts()
tag_name = 'v{}'.format(version) if not tag_name else tag_name
name = 'version {}'.format(version) if not name else name
yaml = YAML()
yaml.indent(mapping=1)
data = CommentedMap()
with open(body, 'r') as notes:
notes_read = notes.read()
data['tag_name'] = tag_name
data['target_commitish'] = target_commit
data['name'] = name
data['body'] = notes_read
data['draft'] = draft
data['prerelease'] = prerelease
if asset_path:
mime = magic.Magic(mime=True)
data['assets'] = []
for asset in os.listdir(asset_path):
abs_path = os.path.join(asset_path, asset)
asset_map = CommentedMap()
content_type = mime.from_file(abs_path)
label = os.path.basename(asset)
asset_map['Content-Type'] = content_type
asset_map['name'] = abs_path
asset_map['label'] = label
data['assets'].append(asset_map)
with open(dest, 'w') as dest:
yaml.dump(data, dest)
if __name__ == "__main__":
main()