Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame^] | 1 | #! /usr/bin/env python |
| 2 | # Copyright (C) 2011 OpenStack, LLC. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
| 16 | # Ensure that the specified remote exists in the repo and pull revisions |
| 17 | # from upstream |
| 18 | |
| 19 | # [project "UPSTREAM_PROJECT"] |
| 20 | # remote = https://gerrit.googlesource.com/gerrit |
| 21 | |
| 22 | import ConfigParser |
| 23 | import StringIO |
| 24 | import logging |
| 25 | import os |
| 26 | import re |
| 27 | import subprocess |
| 28 | |
| 29 | |
| 30 | def run_command(cmd, status=False, env={}): |
| 31 | if VERBOSE: |
| 32 | print datetime.datetime.now(), "Running:", cmd |
| 33 | cmd_list = shlex.split(str(cmd)) |
| 34 | newenv = os.environ |
| 35 | newenv.update(env) |
| 36 | p = subprocess.Popen(cmd_list, stdout=subprocess.PIPE, |
| 37 | stderr=subprocess.STDOUT, env=newenv) |
| 38 | (out, nothing) = p.communicate() |
| 39 | if status: |
| 40 | return (p.returncode, out.strip()) |
| 41 | return out.strip() |
| 42 | |
| 43 | |
| 44 | def run_command_status(cmd, env={}): |
| 45 | return run_command(cmd, True, env) |
| 46 | |
| 47 | |
| 48 | logging.basicConfig(level=logging.ERROR) |
| 49 | |
| 50 | REPO_ROOT = os.environ.get('REPO_ROOT', |
| 51 | '/home/gerrit2/git') |
| 52 | REMOTES_CONFIG = os.environ.get('REMOTES_CONFIG', |
| 53 | '/home/gerrit2/remote.config') |
| 54 | |
| 55 | PROJECT_RE = re.compile(r'^project\s+"(.*)"$') |
| 56 | |
| 57 | config = ConfigParser.ConfigParser() |
| 58 | config.read(REMOTES_CONFIG) |
| 59 | |
| 60 | |
| 61 | for section in config.sections(): |
| 62 | # Each section looks like [project "openstack/project"] |
| 63 | m = PROJECT_RE.match(section) |
| 64 | if not m: |
| 65 | continue |
| 66 | project = m.group(1) |
| 67 | project_git = "%s.git" % project |
| 68 | os.chdir(os.path.join(REPO_ROOT, project_git)) |
| 69 | |
| 70 | if not (config.has_option(section, "remote")): |
| 71 | continue |
| 72 | |
| 73 | # Make sure that the specified remote exists |
| 74 | remote_url = config.get(section, "remote") |
| 75 | # We could check if it exists first, but we're ignoring output anyway |
| 76 | # So just try to make it, and it'll either make a new one or do nothing |
| 77 | run_command("git remote add -f upstream %s" % remote_url) |
| 78 | # Fetch new revs from it |
| 79 | run_command("git remote update upstream") |