forked from becheran/ntest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.py
136 lines (112 loc) · 4.65 KB
/
publish.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
import toml
from pathlib import Path
import os
from enum import Enum
import argparse
import subprocess
import time
import sys
FILE_DIR = os.path.dirname(os.path.realpath(__file__))
class Version:
def __init__(self, version: str):
splited = version.split('.')
if len(splited) != 3:
raise Exception(
'Unknown version string {} Must be of format <major>.<minor>.<patch>.'.format(version))
self._major = int(splited[0])
self._minor = int(splited[1])
self._patch = int(splited[2])
def __repr__(self):
return '{}.{}.{}'.format(
self._major,
self._minor,
self._patch,
)
def update(self, version_type: str):
if version_type == 'major':
self._major += 1
self._minor = 0
self._patch = 0
elif version_type == 'minor':
self._minor += 1
self._patch = 0
elif version_type == 'patch':
self._patch += 1
else:
raise Exception("Unknown version type {}.".format(version_type))
def version_type(string: str):
types = ['major', 'minor', 'patch']
if string not in types:
msg = 'Not valid. Must be one of the following values: {}'.format(
types)
raise argparse.ArgumentTypeError(msg)
return string
def cli_parse_args():
parser = argparse.ArgumentParser(description='Publish new rust version')
parser.add_argument('type', type=version_type)
opts = parser.parse_args()
return opts.type
def main():
update_type = cli_parse_args()
print('Update "{}"'.format(update_type))
version = read_current_version()
print('Current version is {}'.format(version))
version.update(update_type)
print('Update to version {}? Press y to continue'.format(version))
char = sys.stdin.read(1)
if char == 'y':
update_version_in_files(str(version))
deploy_crate()
print('Add tag and push via git.')
git_push_with_tag(version)
def deploy_crate():
# TODO wait till new package version was published
timeoutSec = 10
subprocess.run(["cargo", "publish", "--verbose", "--manifest-path",
"ntest_test_cases/Cargo.toml", "--allow-dirty"])
subprocess.run(["cargo", "publish", "--verbose", "--manifest-path",
"ntest_timeout/Cargo.toml", "--allow-dirty"])
print('Wait {} seconds before the main lib will be published'.format(timeoutSec))
time.sleep(timeoutSec)
subprocess.run(["cargo", "publish", "--verbose",
"--manifest-path", "ntest/Cargo.toml", "--allow-dirty"])
def git_push_with_tag(version: str):
subprocess.run(["git", "add", "ntest/Cargo.toml"])
subprocess.run(["git", "add", "ntest_test_cases/Cargo.toml"])
subprocess.run(["git", "add", "ntest_timeout/Cargo.toml"])
subprocess.run(["git", "tag",
"-a", "v{}".format(version),
"-m Version {}".format(version)])
subprocess.run(["git", "commit", '-m "Release {}"'.format(version)])
subprocess.run(["git", "push", "origin", "v{}".format(version)])
def read_current_version():
ntest_toml_path = os.path.join(FILE_DIR, 'ntest', 'Cargo.toml')
with open(ntest_toml_path, 'r') as toml_file:
toml_content = toml.loads(toml_file.read())
version = Version(toml_content['package']['version'])
return version
def update_version_in_files(version: str):
ntest_toml_path = os.path.join(FILE_DIR, 'ntest', 'Cargo.toml')
with open(ntest_toml_path, 'r') as toml_file:
toml_content = toml.loads(toml_file.read())
toml_content['package']['version'] = version
toml_content['dependencies']['ntest_test_cases']['version'] = version
toml_content['dependencies']['ntest_timeout']['version'] = version
with open(ntest_toml_path, 'w') as toml_file:
toml_file.write(toml.dumps(toml_content))
ntest_test_cases_toml_path = os.path.join(
FILE_DIR, 'ntest_test_cases', 'Cargo.toml')
with open(ntest_test_cases_toml_path, 'r') as toml_file:
toml_content = toml.loads(toml_file.read())
toml_content['package']['version'] = version
with open(ntest_test_cases_toml_path, 'w') as toml_file:
toml_file.write(toml.dumps(toml_content))
ntest_timeout_toml_path = os.path.join(
FILE_DIR, 'ntest_timeout', 'Cargo.toml')
with open(ntest_timeout_toml_path, 'r') as toml_file:
toml_content = toml.loads(toml_file.read())
toml_content['package']['version'] = version
with open(ntest_timeout_toml_path, 'w') as toml_file:
toml_file.write(toml.dumps(toml_content))
if __name__ == "__main__":
main()