1 | #!/usr/bin/python |
---|
2 | """ |
---|
3 | Encapsulate logical Subversion operations so the various MergeBot actors can |
---|
4 | operate at a higher level of abstraction. |
---|
5 | """ |
---|
6 | |
---|
7 | import os |
---|
8 | import time |
---|
9 | import re |
---|
10 | |
---|
11 | def shell_quote(string): |
---|
12 | """Given a string, escape the characters interpretted by the shell.""" |
---|
13 | for char in ["\\", "\"", "$"]: |
---|
14 | string = string.replace(char, "\\%s" % (char, )) |
---|
15 | return '"%s"' % (string, ) |
---|
16 | |
---|
17 | def logcmd(cmd, logfile): |
---|
18 | """Log the cmd string, then execute it, appending its stdout and stderr to |
---|
19 | logfile.""" |
---|
20 | open(logfile, "a").write("%s: %s\n" % (time.asctime(), cmd)) |
---|
21 | return os.system("(%s) >>%s 2>&1" % (cmd, logfile)) |
---|
22 | |
---|
23 | def get_rev_from_log(logentry): |
---|
24 | """Given a log entry split out of svn log, return its revision number""" |
---|
25 | return int(logentry.split()[0][1:]) |
---|
26 | |
---|
27 | def get_branch_info(url, logfile): |
---|
28 | """Given a subversion url and a logfile, return (start_revision, |
---|
29 | end_revision) or None if it does not exist.""" |
---|
30 | svncmd = os.popen("svn log --stop-on-copy --non-interactive %s 2>>%s" % \ |
---|
31 | (url, logfile), "r") |
---|
32 | branchlog = svncmd.read() |
---|
33 | returnval = svncmd.close() |
---|
34 | if returnval: |
---|
35 | # This branch apparently doesn't exist |
---|
36 | return None |
---|
37 | logs = branchlog.split("-"*72 + "\n") |
---|
38 | # If there have been no commits on the branch since it was created, there |
---|
39 | # will only be one revision listed.... but the log will split into 3 parts. |
---|
40 | endrev = get_rev_from_log(logs[1]) |
---|
41 | startrev = get_rev_from_log(logs[-2]) |
---|
42 | return (startrev, endrev) |
---|
43 | |
---|
44 | def create_branch(from_url, to_url, commit_message, logfile): |
---|
45 | """Create a branch copying from_url to to_url. Commit as mergebot, and use |
---|
46 | the provided commit message.""" |
---|
47 | svncmd = \ |
---|
48 | "svn copy --username=mergebot --password=mergebot -m %s %s %s" \ |
---|
49 | % (shell_quote(commit_message), from_url, to_url) |
---|
50 | return logcmd(svncmd, logfile) |
---|
51 | |
---|
52 | def delete_branch(url, commit_message, logfile): |
---|
53 | """This will generate a new revision. Return the revision number, or -1 on |
---|
54 | failure. |
---|
55 | Assumes that the url exists. You should call get_branch_info() to |
---|
56 | determine that first""" |
---|
57 | svncmd = "svn rm --no-auth-cache --username=mergebot --password=mergebot " \ |
---|
58 | "-m %s %s 2>>%s" % (shell_quote(commit_message), url, logfile) |
---|
59 | return _svn_new_rev_command(svncmd) |
---|
60 | |
---|
61 | def checkout(from_url, workingdir, logfile): |
---|
62 | """Checkout from the given url into workingdir""" |
---|
63 | return os.system("svn checkout %s %s >>%s 2>&1" % (from_url, workingdir, |
---|
64 | logfile)) |
---|
65 | |
---|
66 | def merge(from_url, workingdir, revision_range, logfile): |
---|
67 | """Returns a list (status, filename) tuples""" |
---|
68 | # There are a couple of different 'Skipped' messages. |
---|
69 | skipped_regex = re.compile("Skipped.* '(.*)'", re.M) |
---|
70 | start_rev, end_rev = revision_range |
---|
71 | pipe = os.popen("cd %s && svn merge --non-interactive --revision %s:%s %s . 2>>%s" % \ |
---|
72 | (workingdir, start_rev, end_rev, from_url, logfile)) |
---|
73 | output = pipe.readlines() |
---|
74 | # FIXME: check pipe.close for errors |
---|
75 | results = [] |
---|
76 | for line in output: |
---|
77 | if line.startswith("Skipped"): |
---|
78 | # This kind of conflict requires special handling. |
---|
79 | filename = skipped_regex.findall(line)[0] |
---|
80 | status = "C" |
---|
81 | elif line.startswith('--- Merging '): |
---|
82 | continue # ignore this line for now |
---|
83 | elif line.startswith('Summary of conflicts:'): |
---|
84 | continue # ignore this line for now |
---|
85 | elif line.startswith(' Text conflicts:'): |
---|
86 | continue # ignore this line for now |
---|
87 | else: |
---|
88 | assert line[4] == ' ', "Unexpected output from svn merge " \ |
---|
89 | "operation; the 5th character should always be a space." \ |
---|
90 | " Output was %r." % line |
---|
91 | filename = line[5:-1] # (strip trailing newline) |
---|
92 | status = line[:4].rstrip() |
---|
93 | results.append((status, filename)) |
---|
94 | return results |
---|
95 | |
---|
96 | def conflicts_from_merge_results(results): |
---|
97 | "Given the output from merge, return a list of files that had conflicts." |
---|
98 | conflicts = [filename for status, filename in results if 'C' in status] |
---|
99 | return conflicts |
---|
100 | |
---|
101 | def commit(workingdir, commit_message, logfile): |
---|
102 | """Returns newly committed revision number, or None if there was nothing to |
---|
103 | commit. -1 on error.""" |
---|
104 | svncmd = "cd %s && svn commit --no-auth-cache --username=mergebot " \ |
---|
105 | "--password=mergebot -m %s 2>>%s" % (workingdir, |
---|
106 | shell_quote(commit_message), logfile) |
---|
107 | return _svn_new_rev_command(svncmd) |
---|
108 | |
---|
109 | def _svn_new_rev_command(svncmd): |
---|
110 | """Given an svn command that results in a new revision, return the revision |
---|
111 | number, or -1 on error.""" |
---|
112 | pipe = os.popen(svncmd) |
---|
113 | output = pipe.read() |
---|
114 | retval = pipe.close() |
---|
115 | if retval: |
---|
116 | new_revision = -1 |
---|
117 | else: |
---|
118 | new_revisions = re.compile("Committed revision ([0-9]+)\\.", |
---|
119 | re.M).findall(output) |
---|
120 | if new_revisions: |
---|
121 | new_revision = new_revisions[0] |
---|
122 | else: |
---|
123 | new_revision = None |
---|
124 | return new_revision |
---|
125 | |
---|
126 | # vim:foldcolumn=4 foldmethod=indent |
---|
127 | # vim:tabstop=4 shiftwidth=4 softtabstop=4 expandtab |
---|