| 1 |
|
| 2 |
|
| 3 |
import argparse |
| 4 |
import json |
| 5 |
import os |
| 6 |
import subprocess |
| 7 |
import sys |
| 8 |
|
| 9 |
def createRelease(projectName): |
| 10 |
if not inProjectRoot(projectName): |
| 11 |
print("Run this command from inside the project root folder.") |
| 12 |
sys.exit(1) |
| 13 |
|
| 14 |
treeDirty = subprocess.check_output("git diff --stat", shell=True).decode(encoding="utf-8") != "" |
| 15 |
if treeDirty: |
| 16 |
print("Working tree is dirty. Finish committing files or stash, then try again.") |
| 17 |
sys.exit(1) |
| 18 |
|
| 19 |
with open("semver.txt", 'r') as fh: |
| 20 |
currentVersion = fh.read().strip() |
| 21 |
semver = parseSemver(currentVersion) |
| 22 |
|
| 23 |
try: |
| 24 |
with open("secrets.json", 'r') as fh: |
| 25 |
secrets = json.load(fh) |
| 26 |
except IOError: |
| 27 |
print("Error trying to load the secrets.json file.") |
| 28 |
raise |
| 29 |
|
| 30 |
args = argparse.ArgumentParser(description=f"Releases a new {projectName} version to pypi.org.") |
| 31 |
args.add_argument("bump", |
| 32 |
choices=["break", "feature", "bugfix"], |
| 33 |
help=f"Which type of release is this?") |
| 34 |
args.add_argument("--test", dest="test", action="store_true", help="Upload to test.pypi.org instead.") |
| 35 |
options, extras = args.parse_known_args() |
| 36 |
|
| 37 |
|
| 38 |
if options.bump == "break": |
| 39 |
semver[0] += 1 |
| 40 |
semver[1] = 0 |
| 41 |
semver[2] = 0 |
| 42 |
elif options.bump == "feature": |
| 43 |
semver[1] += 1 |
| 44 |
semver[2] = 0 |
| 45 |
elif options.bump == "bugfix": |
| 46 |
semver[2] += 1 |
| 47 |
newVersion = '.'.join(str(x) for x in semver) |
| 48 |
print(f"Bumping to {newVersion}...") |
| 49 |
|
| 50 |
|
| 51 |
with open("semver.txt", 'w') as fh: |
| 52 |
fh.write(newVersion) |
| 53 |
|
| 54 |
try: |
| 55 |
|
| 56 |
subprocess.call("rm -r build dist", shell=True) |
| 57 |
subprocess.check_call("python setup.py sdist bdist_wheel", shell=True) |
| 58 |
if options.test: |
| 59 |
subprocess.check_call(' '.join([ |
| 60 |
"twine upload", |
| 61 |
"--repository-url https://test.pypi.org/legacy/", |
| 62 |
"--username __token__", |
| 63 |
"--password", secrets["test.pypi.org release key"], |
| 64 |
"dist/*", |
| 65 |
]), shell=True) |
| 66 |
else: |
| 67 |
subprocess.check_call(' '.join([ |
| 68 |
"twine upload", |
| 69 |
"--username __token__", |
| 70 |
"--password", secrets["pypi.org release key"], |
| 71 |
"dist/*", |
| 72 |
]), shell=True) |
| 73 |
subprocess.call("rm -r build dist", shell=True) |
| 74 |
except: |
| 75 |
|
| 76 |
with open("semver.txt", 'w') as fh: |
| 77 |
fh.write(currentVersion) |
| 78 |
raise |
| 79 |
|
| 80 |
|
| 81 |
subprocess.check_call("git add semver.txt", shell=True) |
| 82 |
subprocess.check_call(f"git commit -m 'Bump semver to {newVersion}'", shell=True) |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
def inProjectRoot(projectName): |
| 88 |
|
| 89 |
try: |
| 90 |
remotes = subprocess.check_output( |
| 91 |
"git remote -v", |
| 92 |
stderr=subprocess.DEVNULL, |
| 93 |
shell=True).decode("utf-8") |
| 94 |
if projectName in remotes: |
| 95 |
return os.path.isdir(".git") |
| 96 |
else: |
| 97 |
return False |
| 98 |
except: |
| 99 |
return False |
| 100 |
|
| 101 |
|
| 102 |
def parseSemver(s): |
| 103 |
|
| 104 |
return [int(x) for x in s.strip().split(".")] |
| 105 |
|
| 106 |
|
| 107 |
if __name__ == "__main__": |
| 108 |
createRelease("railroad-diagrams") |