blob: 776188e765599467e6b8151372496df2a9b1029b [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
Monty Taylorda9ef082012-05-10 13:12:31 -040023import logging
24import os
25import re
26import subprocess
Andrew Hutchings16a6c462012-05-25 14:26:41 +010027import shlex
Monty Taylorda9ef082012-05-10 13:12:31 -040028
29def run_command(cmd, status=False, env={}):
Monty Taylorda9ef082012-05-10 13:12:31 -040030 cmd_list = shlex.split(str(cmd))
31 newenv = os.environ
32 newenv.update(env)
33 p = subprocess.Popen(cmd_list, stdout=subprocess.PIPE,
34 stderr=subprocess.STDOUT, env=newenv)
35 (out, nothing) = p.communicate()
36 if status:
37 return (p.returncode, out.strip())
38 return out.strip()
39
40
41def run_command_status(cmd, env={}):
42 return run_command(cmd, True, env)
43
44
45logging.basicConfig(level=logging.ERROR)
46
47REPO_ROOT = os.environ.get('REPO_ROOT',
Monty Taylor7d5eedf2012-05-26 10:15:27 -040048 '/home/gerrit2/review_site/git')
Monty Taylorda9ef082012-05-10 13:12:31 -040049REMOTES_CONFIG = os.environ.get('REMOTES_CONFIG',
Monty Taylor7d5eedf2012-05-26 10:15:27 -040050 '/home/gerrit2/remotes.config')
Monty Taylorda9ef082012-05-10 13:12:31 -040051
52PROJECT_RE = re.compile(r'^project\s+"(.*)"$')
53
54config = ConfigParser.ConfigParser()
55config.read(REMOTES_CONFIG)
56
57
58for section in config.sections():
59 # Each section looks like [project "openstack/project"]
60 m = PROJECT_RE.match(section)
61 if not m:
62 continue
63 project = m.group(1)
64 project_git = "%s.git" % project
65 os.chdir(os.path.join(REPO_ROOT, project_git))
66
67 if not (config.has_option(section, "remote")):
68 continue
69
70 # Make sure that the specified remote exists
71 remote_url = config.get(section, "remote")
72 # We could check if it exists first, but we're ignoring output anyway
73 # So just try to make it, and it'll either make a new one or do nothing
74 run_command("git remote add -f upstream %s" % remote_url)
75 # Fetch new revs from it
76 run_command("git remote update upstream")