-
Notifications
You must be signed in to change notification settings - Fork 3
/
upload-revision.py
executable file
·109 lines (88 loc) · 2.89 KB
/
upload-revision.py
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
#!/usr/bin/env python
import sys
import subprocess
import argparse
import re
import os
import shutil
import datetime
import urllib2
DRAFT_PATTERN="(draft-[a-zA-Z0-9-]+-[0-9][0-9])(\.txt)?"
FILENAME_PATTERN="(draft-[a-zA-Z0-9-]+)\.txt$"
DOWNLOAD_TEMPLATE="https://www.ietf.org/id/%s.txt"
def debug(msg):
global args
if args.verbose:
print msg
def die(msg):
print msg
sys.exit(1)
def checkout_branch(bname):
debug("Checking out branch %s"%bname)
c = subprocess.call(["git", "checkout", bname])
if c != 0:
subprocess.check_call(["git", "checkout", "--track", "-b", bname, "master"])
return True
return False
def strip_file(infile, outfile):
stripped = subprocess.check_output(["awk", "-f", sys.path[0] + "/strip.awk", infile])
o = open(outfile, "w")
o.write(stripped)
o.close()
def add_file(fname):
global args
# Reduce the file to the basic name
draft_name = os.path.split(fname)[1]
m = re.match(FILENAME_PATTERN, draft_name)
if m == None:
die("Bogus filename: %s"%draft_name)
f = m.group(1)
base = f
m = re.match("(.*)-[0-9][0-9]$", f)
if m != None:
base = m.group(1)
branch = "branch-%s"%base
# Delete branch if asked
if args.new:
subprocess.call(["git", "branch", "-D", branch])
# Check out a branch named after this basename
created = checkout_branch(branch)
base += ".txt"
debug("Draft basename: %s"%base)
# Copy it here, stripping headers and footers
strip_file(fname, base)
# Now git add it
subprocess.check_call(["git", "add", base])
message = "%s: %s"%(draft_name, str(datetime.datetime.now()))
debug(message)
subprocess.check_call(["git", "commit", "-m", message])
# Finally, upload it.
args = ["arc","diff","--verbatim","--allow-untracked","master"]
if created:
args.append("--reviewers")
args.append("ekr")
subprocess.check_call(args)
def download_draft(draft):
debug("Downloading draft %s"%draft)
to_fetch = DOWNLOAD_TEMPLATE%draft
to_save = "%s.txt"%draft
u = urllib2.urlopen(to_fetch)
f = open(to_save, "w")
f.write(u.read())
f.close()
return to_save
parser = argparse.ArgumentParser(description='Git for review')
parser.add_argument("--file", dest="file", help="filename for draft", default=None)
parser.add_argument("--draft", dest="draft", help="draft name (to be downloaded)", default=None)
parser.add_argument('--verbose', dest='verbose', action='store_true')
parser.add_argument('--new', dest="new", action='store_true')
args = parser.parse_args()
if args.draft != None:
m = re.match(DRAFT_PATTERN, args.draft)
if m is None:
die("Bogus draft name: %s"%args.draft)
file = download_draft(m.group(1))
debug("Saved draft in %s"%file)
add_file(file)
if args.file != None:
add_file(args.file)