| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/python | |
| 2 | |
| 3 # Update Dartium DEPS automatically. | |
| 4 | |
| 5 from datetime import datetime, timedelta | |
| 6 import optparse | |
| 7 import os | |
| 8 import re | |
| 9 from subprocess import Popen, PIPE | |
| 10 import sys | |
| 11 from time import strptime | |
| 12 | |
| 13 # Instructions: | |
| 14 # | |
| 15 # To run locally: | |
| 16 # (a) Create and change to a directory to run the updater in: | |
| 17 # > mkdir /usr/local/google/home/$USER/dartium_deps_updater | |
| 18 # > cd /usr/local/google/home/$USER/dartium_deps_updater | |
| 19 # | |
| 20 # (b) Make a 'deps' directory to store temporary files: | |
| 21 # > mkdir deps | |
| 22 # | |
| 23 # (c) Checkout dart/tools/dartium (with this script): | |
| 24 # > svn co https://dart.googlecode.com/svn/branches/bleeding_edge/dart/tool
s/dartium dartium_tools | |
| 25 # | |
| 26 # (d) If your home directory is remote, consider redefining it for this shell/s
cript: | |
| 27 # > cp -R $HOME/.subversion /usr/local/google/home/$USER | |
| 28 # > export HOME=/usr/local/google/home/$USER | |
| 29 # | |
| 30 # (e) Test by running (Ctrl-C to quit): | |
| 31 # > ./dartium_tools/update_deps.py | |
| 32 # > ./dartium_tools/update_deps.py --target=clank | |
| 33 # > ./dartium_tools/update_deps.py --target=integration | |
| 34 # | |
| 35 # (f) Run periodical update: | |
| 36 # > while true; do ./dartium_tools/update_deps.py --force ; sleep 300 ; don
e | |
| 37 | |
| 38 ######################################################################## | |
| 39 # Repositories to auto-update | |
| 40 ######################################################################## | |
| 41 | |
| 42 BRANCH_CURRENT="dart/dartium" | |
| 43 BRANCH_NEXT="dart/2454_1" | |
| 44 | |
| 45 # (repo_name, deps_dir, repo_branch, prefix, repos, branch) | |
| 46 | |
| 47 TARGETS = { | |
| 48 'dartium': ( | |
| 49 'git@github.com:dart-lang/sdk.git', | |
| 50 'tools/deps/dartium.deps', | |
| 51 'origin/master', | |
| 52 'dartium', | |
| 53 # TODO(vsm): Reenable 'chromium' | |
| 54 ['webkit'], | |
| 55 BRANCH_CURRENT, | |
| 56 ), | |
| 57 'integration': ( | |
| 58 # TODO(jacobr): what is the git repo for integration if any? | |
| 59 'git@github.com:dart-lang/sdk.git', | |
| 60 'tools/deps/dartium.deps', | |
| 61 'origin/integration', | |
| 62 'dartium', | |
| 63 # TODO(vsm): Reenable 'chromium' | |
| 64 ['webkit'], | |
| 65 BRANCH_NEXT, | |
| 66 ), | |
| 67 } | |
| 68 | |
| 69 # Each element in this map represents a repository to update. Entries | |
| 70 # take the form: | |
| 71 # (repo_tag: (svn_url, view_url)) | |
| 72 # | |
| 73 # The repo_tag must match the DEPS revision entry. I.e, there must be | |
| 74 # an entry of the form: | |
| 75 # 'dartium_%s_revision' % repo_tag | |
| 76 # to roll forward. | |
| 77 # | |
| 78 # The view_url should be parameterized by revision number. This is | |
| 79 # used to generated the commit message. | |
| 80 REPOSITORY_INFO = { | |
| 81 'webkit': ( | |
| 82 'https://src.chromium.org/blink/branches/%s', | |
| 83 'https://src.chromium.org/viewvc/blink?view=rev&revision=%s'), | |
| 84 'blink': ( | |
| 85 'https://src.chromium.org/blink/branches/%s', | |
| 86 'https://src.chromium.org/viewvc/blink?view=rev&revision=%s'), | |
| 87 'chromium': ( | |
| 88 'https://src.chromium.org/chrome/branches/%s', | |
| 89 'https://src.chromium.org/viewvc/chrome?view=rev&revision=%s'), | |
| 90 } | |
| 91 | |
| 92 REPOSITORIES = REPOSITORY_INFO.keys() | |
| 93 | |
| 94 ######################################################################## | |
| 95 # Actions | |
| 96 ######################################################################## | |
| 97 | |
| 98 def write_file(filename, content): | |
| 99 f = open(filename, "w") | |
| 100 f.write(content) | |
| 101 f.close() | |
| 102 | |
| 103 def run_cmd(cmd): | |
| 104 print "\n[%s]\n$ %s" % (os.getcwd(), " ".join(cmd)) | |
| 105 pipe = Popen(cmd, stdout=PIPE, stderr=PIPE) | |
| 106 output = pipe.communicate() | |
| 107 if pipe.returncode == 0: | |
| 108 return output[0] | |
| 109 else: | |
| 110 print output[1] | |
| 111 print "FAILED. RET_CODE=%d" % pipe.returncode | |
| 112 sys.exit(pipe.returncode) | |
| 113 | |
| 114 def parse_iso_time(s): | |
| 115 pair = s.rsplit(' ', 1) | |
| 116 d = datetime.strptime(pair[0], '%Y-%m-%d %H:%M:%S') | |
| 117 offset = timedelta(hours=int(pair[1][0:3])) | |
| 118 return d - offset | |
| 119 | |
| 120 def parse_git_log(output, repo): | |
| 121 if len(output) < 4: | |
| 122 return [] | |
| 123 lst = output.split(os.linesep) | |
| 124 lst = [s.strip('\'') for s in lst] | |
| 125 lst = [s.split(',', 3) for s in lst] | |
| 126 lst = [{'repo': repo, | |
| 127 'rev': s[0], | |
| 128 'isotime':s[1], | |
| 129 'author': s[2], | |
| 130 'utctime': parse_iso_time(s[1]), | |
| 131 'info': s[3]} for s in lst] | |
| 132 return lst | |
| 133 | |
| 134 def parse_svn_log(output, repo): | |
| 135 lst = output.split(os.linesep) | |
| 136 lst = [s.strip('\'') for s in lst] | |
| 137 output = '_LINESEP_'.join(lst) | |
| 138 lst = output.split('----------------------------------------------------------
--------------') | |
| 139 lst = [s.replace('_LINESEP_', '\n') for s in lst] | |
| 140 lst = [s.strip('\n') for s in lst] | |
| 141 lst = [s.strip(' ') for s in lst] | |
| 142 lst = [s for s in lst if len(s) > 0] | |
| 143 pattern = re.compile(' \| (\d+) line(s|)') | |
| 144 lst = [pattern.sub(' | ', s) for s in lst] | |
| 145 lst = [s.split(' | ', 3) for s in lst] | |
| 146 lst = [{'repo': repo, | |
| 147 'rev': s[0].replace('r', ''), | |
| 148 'author': s[1], | |
| 149 'isotime':s[2][0:25], | |
| 150 'utctime': parse_iso_time(s[2][0:25]), | |
| 151 'info': s[3].split('\n')[2]} for s in lst] | |
| 152 return lst | |
| 153 | |
| 154 def commit_url(repo, rev): | |
| 155 numrev = rev.replace('r', '') | |
| 156 if repo in REPOSITORIES: | |
| 157 (_, view_url) = REPOSITORY_INFO[repo] | |
| 158 return view_url % numrev | |
| 159 else: | |
| 160 raise Exception('Unknown repo'); | |
| 161 | |
| 162 def find_max(revs): | |
| 163 max_time = None | |
| 164 max_position = None | |
| 165 for i, rev in enumerate(revs): | |
| 166 if rev == []: | |
| 167 continue | |
| 168 if max_time is None or rev[0]['utctime'] > max_time: | |
| 169 max_time = rev[0]['utctime'] | |
| 170 max_position = i | |
| 171 return max_position | |
| 172 | |
| 173 def merge_revs(revs): | |
| 174 position = find_max(revs) | |
| 175 if position is None: | |
| 176 return [] | |
| 177 item = revs[position][0] | |
| 178 revs[position] = revs[position][1:] | |
| 179 return [item] + merge_revs(revs) | |
| 180 | |
| 181 def main(): | |
| 182 option_parser = optparse.OptionParser() | |
| 183 option_parser.add_option('', '--target', help="Update one of [dartium|integrat
ion]", action="store", dest="target", default="dartium") | |
| 184 option_parser.add_option('', '--force', help="Push DEPS update to server witho
ut prompting", action="store_true", dest="force") | |
| 185 options, args = option_parser.parse_args() | |
| 186 | |
| 187 target = options.target | |
| 188 if not target in TARGETS.keys(): | |
| 189 print "Error: invalid target" | |
| 190 print "Choose one of " + str(TARGETS) | |
| 191 (repo_name, deps_dir, repo_branch, prefix, repos, branch) = TARGETS[target] | |
| 192 deps_file = deps_dir + '/DEPS' | |
| 193 repo_branch_parts = repo_branch.split('/') | |
| 194 | |
| 195 src_dir = "/usr/local/google/home/%s/dartium_deps_updater/deps/%s" % (os.envir
on["USER"], target) | |
| 196 os.putenv("GIT_PAGER", "") | |
| 197 | |
| 198 if not os.path.exists(src_dir): | |
| 199 print run_cmd(['git', 'clone', repo_name, src_dir]) | |
| 200 | |
| 201 os.chdir(src_dir) | |
| 202 deps = run_cmd(['git', 'fetch']) | |
| 203 deps = run_cmd(['git', 'stash']) | |
| 204 deps = run_cmd(['git', 'checkout', '-B', repo_branch_parts[1], repo_branch]) | |
| 205 | |
| 206 # parse DEPS | |
| 207 deps = run_cmd(['cat', deps_file]) | |
| 208 rev_num = {} | |
| 209 for repo in repos: | |
| 210 revision = '%s_%s_revision":\s*"(.+)"' % (prefix, repo) | |
| 211 rev_num[repo] = re.search(revision, deps).group(1) | |
| 212 | |
| 213 # update repos | |
| 214 all_revs = [] | |
| 215 for repo in repos: | |
| 216 (svn_url, _) = REPOSITORY_INFO[repo] | |
| 217 output = run_cmd(["svn", "log", "-r", "HEAD:%s" % rev_num[repo], svn_url %
branch]) | |
| 218 revs = parse_svn_log(output, repo) | |
| 219 if revs and revs[-1]['rev'] == rev_num[repo]: | |
| 220 revs.pop() | |
| 221 all_revs.append(revs) | |
| 222 | |
| 223 pending_updates = merge_revs(all_revs) | |
| 224 pending_updates.reverse() | |
| 225 | |
| 226 print | |
| 227 print "Current DEPS revisions:" | |
| 228 for repo in repos: | |
| 229 print ' %s_%s_revision=%s' % (prefix, repo, rev_num[repo]) | |
| 230 | |
| 231 if len(pending_updates) == 0: | |
| 232 print "DEPS is up-to-date." | |
| 233 sys.exit(0) | |
| 234 else: | |
| 235 print "Pending DEPS updates:" | |
| 236 for s in pending_updates: | |
| 237 print " %s to %s (%s) %s" % (s['repo'], s['rev'], s['isotime'], s['info']
) | |
| 238 | |
| 239 # make the next DEPS update | |
| 240 os.chdir(src_dir) | |
| 241 run_cmd(['rm', deps_file]) | |
| 242 s = pending_updates[0] | |
| 243 | |
| 244 pattern = re.compile(prefix + '_' + s['repo'] + '_revision":\s*"(.+)"') | |
| 245 new_deps = pattern.sub(prefix + '_' + s['repo'] + '_revision": "' + s['rev'] +
'"', deps) | |
| 246 write_file(deps_file, new_deps) | |
| 247 | |
| 248 commit_log = 'DEPS AutoUpdate: %s to %s (%s) %s\n' % (s['repo'], s['rev'], s['
isotime'], s['author']) | |
| 249 commit_log += s['info'] + '\n' + commit_url(s['repo'], s['rev']) | |
| 250 | |
| 251 write_file('commit_log.txt', commit_log) | |
| 252 run_cmd(['git', 'add', deps_file]) | |
| 253 | |
| 254 print run_cmd(['git', 'diff', 'HEAD']) | |
| 255 print | |
| 256 print "Commit log:" | |
| 257 print "---------------------------------------------" | |
| 258 print commit_log | |
| 259 print "---------------------------------------------" | |
| 260 | |
| 261 if not options.force: | |
| 262 print "Ready to push; press Enter to continue or Control-C to abort..." | |
| 263 sys.stdin.readline() | |
| 264 print run_cmd(['git', 'commit', '-F', 'commit_log.txt']) | |
| 265 print run_cmd(['git', 'push', repo_branch_parts[0], repo_branch_parts[1]]) | |
| 266 print "Done." | |
| 267 | |
| 268 | |
| 269 if '__main__' == __name__: | |
| 270 main() | |
| OLD | NEW |