blob: 19df462fdf8b9fc8e8493a2d347d01f4d068b696 [file] [log] [blame]
Monty Taylorda9ef082012-05-10 13:12:31 -04001#! /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
22import ConfigParser
23import StringIO
24import logging
25import os
26import re
27import subprocess
28
29
30def 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
44def run_command_status(cmd, env={}):
45 return run_command(cmd, True, env)
46
47
48logging.basicConfig(level=logging.ERROR)
49
50REPO_ROOT = os.environ.get('REPO_ROOT',
51 '/home/gerrit2/git')
52REMOTES_CONFIG = os.environ.get('REMOTES_CONFIG',
53 '/home/gerrit2/remote.config')
54
55PROJECT_RE = re.compile(r'^project\s+"(.*)"$')
56
57config = ConfigParser.ConfigParser()
58config.read(REMOTES_CONFIG)
59
60
61for 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")