Repository navigation
Expand file tree
/
Copy pathrelease.py
More file actions
150 lines (132 loc) · 5.21 KB
/
Copy pathrelease.py
File metadata and controls
150 lines (132 loc) · 5.21 KB
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
import argparse
import json
import os
import re
import sys
import requests
import subprocess
import yaml
from pace_neutrons_cli.utils import release_exists, download_github
def main():
parser = get_parser()
args = parser.parse_args()
test = not args.notest
if args.github:
release_github(test)
if args.pypi:
release_pypi(test)
def release_github(test=True):
from pace_neutrons import __version__
with open('CHANGELOG.md') as f:
changelog = f.read()
with open('CITATION.cff') as f:
citation = yaml.safe_load(f)
pace_ver = 'v' + __version__
version_dict = {}
version_dict['CHANGELOG.md'] = re.findall('# \[(.*)\]\(http', changelog)[0]
version_dict['CITATION.cff'] = 'v' + citation['version']
for ver_name, ver in version_dict.items():
if pace_ver != ver:
raise Exception(f'version mismatch! __version__: {pace_ver}; {ver_name}: {ver}')
VERSION_PATTERN = r'v[0-9]+(?:\.[0-9]+){2}(?:(?:a|b|rc)[0-9]+)?'
desc = re.search(rf'# \[{VERSION_PATTERN}\]\(http.*?\)\n(.*?)# \[{VERSION_PATTERN}\]',
changelog, re.DOTALL | re.MULTILINE).group(1).strip()
payload = {
"tag_name": pace_ver,
"target_commitish": "main",
"name": pace_ver,
"body": desc,
"draft": True,
"prerelease": True
}
if test:
print(payload)
else:
upload_url = release_exists(pace_ver, retval='upload_url')
if not upload_url:
upload_url = _create_gh_release(payload)
else:
upload_url = re.search('^(.*)\{\?', upload_url).groups()[0]
_upload_assets(upload_url)
def release_pypi(test=True):
# Downloads wheels from github and upload to PyPI
response = requests.get(
'https://api.github.com/repos/pace-neutrons/pace-python/releases')
# Get the latest release
releases = response.json()
ids = [r['id'] for r in releases]
latest = [r for r in releases if r['id'] == max(ids)][0]
# Creates a custom wheelhouse folder
try:
os.mkdir('pace_wheelhouse')
except FileExistsError:
pass
# Loops through assets and downloads all the wheels
headers = {"Accept":"application/octet-stream"}
for asset in latest['assets']:
if asset['name'].endswith('whl'):
print('Downloading %s' % (asset['name']))
localfile = os.path.join('pace_wheelhouse', asset['name'])
download_github(asset['url'], localfile, use_auth=False)
if not test:
subprocess.run(['twine', 'upload', 'pace_wheelhouse/*'])
def get_parser():
parser = argparse.ArgumentParser()
parser.add_argument(
'--github',
action='store_true',
help='Release on Github')
parser.add_argument(
'--pypi',
action='store_true',
help='Release on PyPI')
parser.add_argument(
'--notest',
action='store_true',
help='Actually send/upload')
return parser
def _create_gh_release(payload):
response = requests.post(
'https://api.github.com/repos/pace-neutrons/pace-python/releases',
data=json.dumps(payload),
headers={"Authorization": "token " + os.environ["GITHUB_TOKEN"]})
print(response.text)
if response.status_code != 201:
raise RuntimeError('Could not create release')
upload_url = re.search('^(.*)\{\?', json.loads(response.text)['upload_url']).groups()[0]
return upload_url
def _upload_assets(upload_url):
wheelpaths = None
if os.path.exists('dist'):
wheelpaths = [os.path.join('dist', ff) for ff in os.listdir('dist')]
elif os.path.exists('wheelhouse'):
wheelpaths = [os.path.join('wheelhouse', ff) for ff in os.listdir('wheelhouse') if 'manylinux' in ff]
if wheelpaths is not None:
for wheelpath in wheelpaths:
wheelfile = os.path.basename(wheelpath)
print(f'Uploading wheel {wheelpath}')
with open(wheelpath, 'rb') as f:
upload_response = requests.post(
f"{upload_url}?name={wheelfile}",
headers={"Authorization": "token " + os.environ["GITHUB_TOKEN"],
"Content-type": "application/octet-stream"},
data=f.read())
print(upload_response.text)
_, installer_path, _ = next(os.walk('installer'))
if installer_path:
installer_path = [p for p in installer_path if 'pace_python_installer' in p]
if installer_path:
installer_path = os.path.join('installer', installer_path[0])
installer_file0 = [ff for ff in os.listdir(installer_path) if 'MyAppInstaller' in ff][0]
installer_file = installer_file0.replace('MyAppInstaller', f'pace_neutrons_installer_{sys.platform}')
print(f'Uploading installer {installer_file}')
with open(os.path.join(installer_path, installer_file0), 'rb') as f:
upload_response = requests.post(
f"{upload_url}?name={installer_file}",
headers={"Authorization": "token " + os.environ["GITHUB_TOKEN"],
"Content-type": "application/octet-stream"},
data=f.read())
print(upload_response.text)
return None
if __name__ == '__main__':
main()