| 1 |
import os |
| 2 |
import sys |
| 3 |
import re |
| 4 |
from subprocess import check_output, call, CalledProcessError, DEVNULL |
| 5 |
from difflib import Differ |
| 6 |
import requests |
| 7 |
import json |
| 8 |
|
| 9 |
red_begin = "\033[91m" |
| 10 |
red_end = "\033[0m" |
| 11 |
bold_begin = "\033[1m" |
| 12 |
bold_end = red_end |
| 13 |
|
| 14 |
gitroot = check_output(["git", "rev-parse", |
| 15 |
"--show-toplevel"]).decode().strip() |
| 16 |
|
| 17 |
|
| 18 |
def die(msg): |
| 19 |
print(msg, file=sys.stderr) |
| 20 |
sys.exit(1) |
| 21 |
|
| 22 |
|
| 23 |
def check_git_status(): |
| 24 |
ret = call(["git", "diff-index", "--quiet", "HEAD", "--"]) |
| 25 |
if ret > 0: |
| 26 |
print("{}There are unstaged changes in the working tree.{}\n" |
| 27 |
"Are you sure you want to continue?".format( |
| 28 |
red_begin, red_end |
| 29 |
)) |
| 30 |
wait_for_ret() |
| 31 |
|
| 32 |
|
| 33 |
def chdir(): |
| 34 |
scriptpath = sys.argv[0] |
| 35 |
scriptdir = os.path.dirname(scriptpath) or "." |
| 36 |
os.chdir(scriptdir) |
| 37 |
|
| 38 |
|
| 39 |
def parse_version_string(ver): |
| 40 |
try: |
| 41 |
ver = tuple(map(int, ver.split("."))) |
| 42 |
if len(ver) != 3: |
| 43 |
raise ValueError |
| 44 |
return ver |
| 45 |
except ValueError: |
| 46 |
die("Invalid version string. Please provide a version of the form " |
| 47 |
"X.Y.Z (all integers).") |
| 48 |
|
| 49 |
|
| 50 |
def wait_for_ret(): |
| 51 |
try: |
| 52 |
input("Hit return to continue or ^C to cancel ...") |
| 53 |
except KeyboardInterrupt: |
| 54 |
die("\nCancelled") |
| 55 |
|
| 56 |
|
| 57 |
def diff_lines(old, new): |
| 58 |
d = Differ() |
| 59 |
diff = list(d.compare(old, new)) |
| 60 |
for line in diff[:]: |
| 61 |
if line.startswith(" "): |
| 62 |
diff.remove(line) |
| 63 |
|
| 64 |
return diff |
| 65 |
|
| 66 |
|
| 67 |
def update_readme(): |
| 68 |
branch = check_output(["git", "rev-parse", |
| 69 |
"--abbrev-ref", "HEAD"]).decode().strip() |
| 70 |
print("Current branch is '{}'".format(branch)) |
| 71 |
wait_for_ret() |
| 72 |
|
| 73 |
readmefn = os.path.join(gitroot, "README.rst") |
| 74 |
|
| 75 |
with open(readmefn) as oldreadme: |
| 76 |
oldrmtext = oldreadme.readlines() |
| 77 |
|
| 78 |
newrmtext = [] |
| 79 |
for line in oldrmtext: |
| 80 |
if any(("travis-ci.org" in line, "coveralls.io" in line, |
| 81 |
"ci.appveyor.com" in line)): |
| 82 |
line = line.replace("master", branch) |
| 83 |
newrmtext.append(line) |
| 84 |
|
| 85 |
diff = diff_lines(oldrmtext, newrmtext) |
| 86 |
|
| 87 |
if not diff: |
| 88 |
print("No changes required in README.") |
| 89 |
wait_for_ret() |
| 90 |
return False |
| 91 |
|
| 92 |
print("".join(diff)) |
| 93 |
print("{}The above changes will be written to README.rst{}".format( |
| 94 |
red_begin, red_end |
| 95 |
)) |
| 96 |
wait_for_ret() |
| 97 |
with open(readmefn, "w") as readme: |
| 98 |
readme.writelines(newrmtext) |
| 99 |
return True |
| 100 |
|
| 101 |
|
| 102 |
def update_info(newver): |
| 103 |
infofn = os.path.join(gitroot, "nixio", "info.json") |
| 104 |
with open(infofn) as infofile: |
| 105 |
infodict = json.load(infofile) |
| 106 |
|
| 107 |
verstring = infodict["VERSION"] |
| •108 |
newverstring = re.sub(r"[0-9\.a-z]+", newver, verstring) |
| 109 |
|
| 110 |
if newverstring == verstring: |
| 111 |
print("No changes required in info.json") |
| 112 |
wait_for_ret() |
| 113 |
return False |
| 114 |
|
| 115 |
oldrelstring = infodict["RELEASE"] |
| 116 |
newrelstring = "{} Release".format(newverstring) |
| 117 |
|
| 118 |
print("VERSION: '{}' → '{}'".format(verstring, newverstring)) |
| 119 |
print("RELEASE: '{}' → '{}'".format(oldrelstring, newrelstring)) |
| 120 |
print("{}The above changes will be written to info.json{}".format( |
| 121 |
red_begin, red_end |
| 122 |
)) |
| 123 |
wait_for_ret() |
| 124 |
with open(infofn, "w") as infofile: |
| 125 |
infodict["VERSION"] = newverstring |
| 126 |
infodict["RELEASE"] = newrelstring |
| 127 |
json.dump(infodict, infofile, indent=4) |
| 128 |
|
| 129 |
return True |
| 130 |
|
| 131 |
|
| 132 |
def update_ci_confs(newver): |
| 133 |
change = False |
| 134 |
if os.path.exists(os.path.join(gitroot, ".travis.yml")): |
| 135 |
nixbranch = ".".join(newver.split(".")[:-1]) |
| 136 |
travisfn = os.path.join(gitroot, ".travis.yml") |
| 137 |
with open(travisfn) as travisconf: |
| 138 |
oldconf = travisconf.readlines() |
| 139 |
|
| 140 |
newconf = [] |
| 141 |
for line in oldconf: |
| 142 |
if "NIX_BRANCH" in line: |
| •143 |
line = re.sub(r"1\.[0-9\.]+[0-9\.a-z]+(dev){0,1}", nixbranch, line) |
| 144 |
line = line.replace("master", nixbranch) |
| 145 |
newconf.append(line) |
| 146 |
|
| 147 |
diff = diff_lines(oldconf, newconf) |
| 148 |
|
| 149 |
if len(diff) == 0: |
| 150 |
print("No changes required in .travis.yml") |
| 151 |
wait_for_ret() |
| 152 |
else: |
| 153 |
print("".join(diff)) |
| 154 |
print("{}The above changes will be written to .travis.yml{}".format( |
| 155 |
red_begin, red_end |
| 156 |
)) |
| 157 |
wait_for_ret() |
| 158 |
with open(travisfn, "w") as travisconf: |
| 159 |
travisconf.writelines(newconf) |
| 160 |
change = True |
| 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 |
return change |
| 190 |
|
| 191 |
|
| 192 |
def tag_head(newverstr): |
| 193 |
try: |
| 194 |
tagcommitstr = newverstr + "^{commit}" |
| 195 |
tagrev = check_output(["git", "rev-parse", tagcommitstr], |
| 196 |
stderr=DEVNULL) |
| 197 |
headrev = check_output(["git", "rev-parse", "HEAD"]) |
| 198 |
if tagrev != headrev: |
| 199 |
die("Tag or object named {} already exists " |
| 200 |
"and is not current HEAD.".format(newverstr)) |
| 201 |
tagexists = True |
| 202 |
except CalledProcessError: |
| 203 |
tagexists = False |
| 204 |
|
| 205 |
if tagexists: |
| 206 |
print("HEAD already tagged with {}".format(newverstr)) |
| 207 |
return |
| 208 |
|
| 209 |
print("{}Tagging current commit with '{}'{}".format( |
| 210 |
red_begin, newverstr, red_end)) |
| 211 |
print("[{}$ git tag {}{}]".format(red_begin, newverstr, red_end)) |
| 212 |
wait_for_ret() |
| 213 |
ret = call(["git", "tag", newverstr]) |
| 214 |
if ret > 0: |
| 215 |
die("Error during 'git tag' operation.") |
| 216 |
|
| 217 |
|
| 218 |
def print_appveyor_urls(): |
| 219 |
print("Hit return to print the URLs for the latest wheels on Appveyor.") |
| 220 |
print("If the build is still running, you can wait until it's finished.") |
| 221 |
wait_for_ret() |
| 222 |
|
| 223 |
apiurl = "https://ci.appveyor.com/api/" |
| 224 |
account = "achilleas-k" |
| 225 |
project_name = "nixpy-um2sy" |
| 226 |
|
| 227 |
url = os.path.join(apiurl, "projects", account, project_name) |
| 228 |
r = requests.get(url) |
| 229 |
|
| 230 |
projects = json.loads(r.text) |
| 231 |
build = projects["build"] |
| 232 |
for job in build["jobs"]: |
| 233 |
if job["status"] == "success": |
| 234 |
artifacts_url = os.path.join(apiurl, "buildjobs", job["jobId"], |
| 235 |
"artifacts") |
| 236 |
r = requests.get(artifacts_url) |
| 237 |
artifacts = json.loads(r.text) |
| 238 |
print(job["name"]) |
| 239 |
for a in artifacts: |
| 240 |
print(os.path.join(apiurl, "buildjobs", job["jobId"], |
| 241 |
"artifacts", a["fileName"])) |
| 242 |
|
| 243 |
|
| 244 |
def main(): |
| 245 |
check_git_status() |
| 246 |
|
| 247 |
chdir() |
| 248 |
|
| 249 |
if len(sys.argv) < 2: |
| 250 |
die("Specify version") |
| 251 |
|
| 252 |
args = sys.argv[1:] |
| 253 |
|
| 254 |
skipchg = False |
| 255 |
if "--skip-changes" in args: |
| 256 |
print("Skipping changes for README, info, and CI configs") |
| 257 |
skipchg = True |
| 258 |
args.remove("--skip-changes") |
| 259 |
|
| 260 |
newverstr = args[0] |
| 261 |
prepline = "Preparing new release: {}".format(newverstr) |
| 262 |
banner = "=" * len(prepline) + "\n" + prepline + "\n" + "=" * len(prepline) |
| 263 |
banner = bold_begin + banner + bold_end |
| 264 |
print(banner) |
| 265 |
|
| 266 |
if not skipchg: |
| 267 |
chg = (update_readme() | |
| 268 |
update_info(newverstr) | |
| 269 |
update_ci_confs(newverstr)) |
| 270 |
if chg: |
| 271 |
print("Files have changed. " |
| 272 |
"Commit these changes and rerun the script.") |
| 273 |
sys.exit(0) |
| 274 |
|
| 275 |
check_git_status() |
| 276 |
|
| 277 |
tag_head(newverstr) |
| 278 |
|
| 279 |
print("Creating archives...") |
| 280 |
os.chdir(gitroot) |
| 281 |
ret = call(["python3", "setup.py", "sdist", "bdist_wheel"]) |
| 282 |
|
| 283 |
if ret > 0: |
| 284 |
die("Error creating package") |
| 285 |
|
| 286 |
print("{}Package ready. See 'dist' directory under project root.{}".format( |
| 287 |
bold_begin, bold_end |
| 288 |
)) |
| 289 |
|
| 290 |
|
| 291 |
if __name__ == "__main__": |
| 292 |
main() |