Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
277 lines (239 sloc) 9.88 KB
#!/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()
You can’t perform that action at this time.