|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from __future__ import print_function |
| 4 | + |
| 5 | +from datetime import datetime |
| 6 | +import os |
| 7 | +from os import path |
| 8 | +import re |
| 9 | +import shutil |
| 10 | +import subprocess |
| 11 | +from subprocess import Popen |
| 12 | +import sys |
| 13 | + |
| 14 | +SHARE_DIR = path.join(path.dirname(__file__), "../share/") |
| 15 | + |
| 16 | + |
| 17 | +def run(args): |
| 18 | + return Popen(args, stdout=sys.stdout, stderr=sys.stderr).wait() |
| 19 | + |
| 20 | + |
| 21 | +status = subprocess.check_output(["git", "status", "--porcelain"]) |
| 22 | +if len(status) > 0: |
| 23 | + print("Unclean working tree. Commit or stash changes first.", file=sys.stderr) |
| 24 | + sys.exit(1) |
| 25 | + |
| 26 | +timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S +0000") |
| 27 | + |
| 28 | +cht_curr = path.join(SHARE_DIR, "cht.sh.txt") |
| 29 | +cht_new = path.join(SHARE_DIR, "cht.sh.txt.new") |
| 30 | + |
| 31 | +re_version = re.compile(r"^__CHTSH_VERSION=(.*)$") |
| 32 | +re_timestamp = re.compile(r"^__CHTSH_DATETIME=.*$") |
| 33 | + |
| 34 | +with open(cht_curr, "rt") as fin: |
| 35 | + with open(cht_new, "wt") as fout: |
| 36 | + for line in fin: |
| 37 | + match = re_version.match(line) |
| 38 | + if match: |
| 39 | + version = int(match.group(1)) + 1 |
| 40 | + fout.write("__CHTSH_VERSION=%s\n" % version) |
| 41 | + continue |
| 42 | + |
| 43 | + match = re_timestamp.match(line) |
| 44 | + if match: |
| 45 | + fout.write('__CHTSH_DATETIME="%s"\n' % timestamp) |
| 46 | + continue |
| 47 | + |
| 48 | + fout.write(line) |
| 49 | + |
| 50 | +shutil.copymode(cht_curr, cht_new) |
| 51 | +os.remove(cht_curr) |
| 52 | +os.rename(cht_new, cht_curr) |
| 53 | + |
| 54 | +message = "cht: v%s" % version |
| 55 | +run(["git", "add", cht_curr]) |
| 56 | +run(["git", "commit", "-m", message]) |
| 57 | +run(["git", "tag", "cht@%s" % version, "-m", message]) |
0 commit comments