blob: f34e518f866d3b909f44b0719e3057625b54f228 [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
Monty Taylorc0df8b02012-07-29 12:54:27 -050016# Fetch remotes reads a project config file called projects.yaml
17# It should look like:
Monty Taylorda9ef082012-05-10 13:12:31 -040018
Clark Boylanbfef7002012-11-14 09:11:09 -080019# - homepage: http://openstack.org
20# team-id: 153703
21# has-wiki: False
22# has-issues: False
23# has-downloads: False
24# ---
Monty Taylorc0df8b02012-07-29 12:54:27 -050025# - project: PROJECT_NAME
26# options:
27# - remote: https://gerrit.googlesource.com/gerrit
Monty Taylorda9ef082012-05-10 13:12:31 -040028
Monty Taylorc0df8b02012-07-29 12:54:27 -050029
Monty Taylorda9ef082012-05-10 13:12:31 -040030import logging
31import os
Monty Taylorda9ef082012-05-10 13:12:31 -040032import subprocess
Andrew Hutchings16a6c462012-05-25 14:26:41 +010033import shlex
Monty Taylorc0df8b02012-07-29 12:54:27 -050034import yaml
Monty Taylorda9ef082012-05-10 13:12:31 -040035
Monty Taylorda3bada2012-11-22 09:38:22 -080036
Monty Taylorda9ef082012-05-10 13:12:31 -040037def run_command(cmd, status=False, env={}):
Monty Taylorda9ef082012-05-10 13:12:31 -040038 cmd_list = shlex.split(str(cmd))
39 newenv = os.environ
40 newenv.update(env)
41 p = subprocess.Popen(cmd_list, stdout=subprocess.PIPE,
42 stderr=subprocess.STDOUT, env=newenv)
43 (out, nothing) = p.communicate()
44 if status:
45 return (p.returncode, out.strip())
46 return out.strip()
47
48
49def run_command_status(cmd, env={}):
50 return run_command(cmd, True, env)
51
52
Monty Taylorda3bada2012-11-22 09:38:22 -080053def main():
54 logging.basicConfig(level=logging.ERROR)
Monty Taylorda9ef082012-05-10 13:12:31 -040055
Monty Taylorda3bada2012-11-22 09:38:22 -080056 REPO_ROOT = os.environ.get('REPO_ROOT',
57 '/home/gerrit2/review_site/git')
58 PROJECTS_YAML = os.environ.get('PROJECTS_YAML',
59 '/home/gerrit2/projects.yaml')
Monty Taylorda9ef082012-05-10 13:12:31 -040060
Monty Taylorda3bada2012-11-22 09:38:22 -080061 (defaults, config) = [config for config in
62 yaml.load_all(open(PROJECTS_YAML))]
Monty Taylorda9ef082012-05-10 13:12:31 -040063
Monty Taylorda3bada2012-11-22 09:38:22 -080064 for section in config:
65 project = section['project']
Monty Taylorda9ef082012-05-10 13:12:31 -040066
Monty Taylorda3bada2012-11-22 09:38:22 -080067 if 'remote' not in section:
68 continue
Monty Taylorc0df8b02012-07-29 12:54:27 -050069
Monty Taylorda3bada2012-11-22 09:38:22 -080070 project_git = "%s.git" % project
71 os.chdir(os.path.join(REPO_ROOT, project_git))
Monty Taylorda9ef082012-05-10 13:12:31 -040072
Monty Taylorda3bada2012-11-22 09:38:22 -080073 # Make sure that the specified remote exists
74 remote_url = 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")