-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdeploy.py
More file actions
276 lines (239 loc) · 9.88 KB
/
Copy pathdeploy.py
File metadata and controls
276 lines (239 loc) · 9.88 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python3
#
# Deploy script for HyVR
# ----------------------
#
# Use this to upload HyVR to github, github-pages and pypi.
#
# The script creates a new temporary directory and pulls the mercurial repository from bitbucket, so
# make sure you pushed your changes.
# By default, the script only does a test run.
#
# The script needs the following things installed:
#
# * mercurial (for the bitbucket repo)
# * git (for github/github-pages)
# * twine (for uploading to pypi, install with pip)
# * a c/c++ compiler (for building the extension/wheel)
# * an up-to-date version of the tempfile module
import os
import glob
import sys
import tempfile
import argparse
import shutil
import subprocess
GITHUB_REPO_ADDRESS = "git@github.com:driftingtides/hyvr.git"
BITBUCKET_REPO_ADDRESS = "ssh://hg@bitbucket.org/jpbennett/hyvr"
GHPAGES_SOURCES = ["hyvr", "docs", "README.rst", "testcases", "versionnumber"]
REPO_NAME = "hyvr"
parser = argparse.ArgumentParser(description="Deploys HyVR to different locations")
parser.add_argument('-v', '--versionnumber', action='store', nargs=1,
help='Versionnumber of the release, e.g. "0.2.3". This is a required argument.')
parser.add_argument('--github', action='store_true',
help='push to github')
parser.add_argument("-m", "--message", action='store', nargs=1,
help='commit message for github')
parser.add_argument('--gh-pages', action='store_true',
help='push to github pages')
parser.add_argument('--pypi', action='store', nargs='*', metavar="FORMAT",
help='push to pypi, give package format as argument (sdist or bdist_wheel)')
parser.add_argument('--pypi-test', action='store', nargs='*', metavar="FORMAT",
help='push to test.pypi.org, give package format as argument (sdist or bdist_wheel)')
parser.add_argument('--no-test', action='store_true',
help="This option disables the test flag. Set this if you really want to push your changes.")
parser.add_argument('--test-all', action='store_true',
help="don't push, test everything")
parser.add_argument('--dont-ask', action='store_true',
help="don't ask before pushing")
# read in arguments
args = parser.parse_args()
github = args.github
gh_pages = args.gh_pages
test = not args.no_test
test_all = args.test_all
if args.pypi is not None:
pypi = True
if args.pypi: # if it is not an emtpy list
package_formats_pypi = args.pypi
else:
print('--pypi requires a package format (e.g. sdist)!')
print()
parser.print_help()
sys.exit(1)
else:
pypi = False
package_formats_pypi = []
if args.pypi_test is not None:
pypi_test = True
if args.pypi_test:
package_formats_pypi_test = args.pypi_test
else:
print('--pypi-test requires a package format (e.g. sdist)!')
print()
parser.print_help()
sys.exit(1)
else:
pypi_test = False
package_formats_pypi_test = []
# make sure versionnumber is set
if args.versionnumber:
versionnumber = args.versionnumber[0]
elif not test_all:
print("-v/--versionnumber is a required argument!")
print()
parser.print_help()
sys.exit(1)
if args.message:
message = args.message[0]
else:
message = "version " + versionnumber
if test_all:
pypi = True
pypi_test = True
package_formats_pypi_test = ["sdist", "bdist_wheel"]
package_formats_pypi = ["sdist", "bdist_wheel"]
github = True
gh_pages = True
test = True
message = 'test generated by deploy.py'
versionnumber = 'test'
def check_continue():
if args.dont_ask:
return
if input('Continue? [y/n] ') != 'y':
raise
def rm_r(pattern):
files = glob.glob(pattern)
for f in files:
if os.path.isdir(f):
shutil.rmtree(f)
elif os.path.exists(f):
os.remove(f)
def cp_to_dir(src_pattern, dest_dir, overwrite=False):
files = glob.glob(src_pattern)
for f in files:
newname = os.path.join(dest_dir, os.path.basename(f))
if os.path.exists(newname):
rm_r(newname)
if os.path.isdir(f):
shutil.copytree(f, newname)
else:
shutil.copy(f, newname)
def main():
if test:
print("------------------------------------------------------------------------------")
print()
print(" >>> TEST <<< ")
print()
print("This is only a test. Use the --no-test option if you're sure you want to push.")
print("Also, make sure to push all your changes to bitbucket, before running this.")
print("Please also run 'pytest' before deploying anything.")
print()
print("------------------------------------------------------------------------------")
print()
print("Deploying Version " + versionnumber)
print()
current_dir = os.getcwd()
with tempfile.TemporaryDirectory() as tmpdir:
# this is all in a large try... finally block so we can change out of the temporary if
# there's an error.
try:
os.chdir(tmpdir)
hgrepodir = os.path.join(tmpdir, 'hgrepo')
os.makedirs(hgrepodir)
gitrepodir = os.path.join(tmpdir, 'gitrepo')
os.makedirs(gitrepodir)
# check out current mercurial repo
os.chdir(hgrepodir)
subprocess.check_call(["hg", "clone", BITBUCKET_REPO_ADDRESS])
os.chdir(REPO_NAME)
# create current versionnumber file
# this will later be read by setup.py
with open('versionnumber', 'w') as f:
f.write(versionnumber)
# build the extension
subprocess.check_call(["python", "setup.py", "build_ext", "--inplace"])
os.chdir(tmpdir)
# Github and Github pages
# =======================
if gh_pages or github:
print("Creating temporary directory for git repo")
os.chdir(gitrepodir)
subprocess.check_call(["git", "clone", GITHUB_REPO_ADDRESS])
os.chdir(REPO_NAME)
print("Remove all files in github repo")
rm_r("*")
os.remove(".gitignore")
print("Adding all files from mercurial repo")
cp_to_dir(os.path.join(hgrepodir, REPO_NAME, "*"), os.getcwd())
cp_to_dir(os.path.join(hgrepodir, REPO_NAME, ".hgignore"), os.getcwd())
shutil.move('.hgignore', '.gitignore')
shutil.rmtree('conda_recipe')
try:
os.remove('.hgtags')
except:
pass
print("Committing changes")
subprocess.check_call(["git", "add", "-A"])
if message:
subprocess.check_call(["git", "commit", "-m", message])
else:
subprocess.check_call(["git", "commit"])
tag = 'v' + versionnumber
subprocess.check_call(["git", "tag", tag])
if github and not test:
# push
print("Pushing to github")
check_continue()
subprocess.check_call(["git", "push", "origin"])
subprocess.check_call(["git", "push", "origin", tag])
if gh_pages:
print("Change branch and update")
subprocess.check_call(["git", "checkout", "gh-pages"])
try:
shutil.rmtree('_sources')
shutil.rmtree('_static')
except:
pass
subprocess.check_call(["git", "checkout", "master", "--"] + GHPAGES_SOURCES)
subprocess.check_call(["git", "reset", "HEAD"])
print("Creating and adding new files")
os.chdir('docs')
subprocess.check_call(['sphinx-apidoc', '-fME', '-H', 'Module Reference', '-o', 'modules', '../hyvr'])
if os.name == 'posix':
subprocess.check_call(["make", "html"])
else:
subprocess.check_call(["make.bat", "html"])
os.chdir(os.path.join(os.getcwd(), os.pardir))
cp_to_dir('docs/_build/html/*', os.getcwd(), overwrite=True)
for d in GHPAGES_SOURCES:
rm_r(d)
subprocess.check_call(["git", "add", "-A"])
print("Committing")
previous_commit = subprocess.check_output(["git", "log", "master", "-1", "--pretty=short", "--abbrev-commit"]).decode('utf-8')
previous_commit = previous_commit.replace('\n\n ', ',').strip('\n').replace('\n', ', ')
commit_message = "Generated gh-pages for " + previous_commit
subprocess.check_call(["git", "commit", "-m", commit_message])
if not test:
print("Pushing to github pages")
check_continue()
subprocess.check_call(["git", "push", "origin", "gh-pages"])
# PyPI
# ====
if pypi or pypi_test:
os.chdir(os.path.join(hgrepodir, REPO_NAME))
package_formats = list(set(package_formats_pypi + package_formats_pypi_test))
subprocess.check_call(["python", "setup.py"] + package_formats)
if pypi_test and not test:
print("Pushing to test.pypi.org")
check_continue()
subprocess.check_call(["twine", "upload", "--repository-url", "https://test.pypi.org/legacy/", "dist/*"])
if pypi and not test:
print("Pushing to PyPI")
check_continue()
subprocess.check_call(["twine", "upload", "dist/*"])
finally:
os.chdir(current_dir)
if __name__ == '__main__':
main()