blob: 6f8ab8b4ed6932c2a79dc1afbbe71d56a4deea09 [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
36def run_command(cmd, status=False, env={}):
Monty Taylorda9ef082012-05-10 13:12:31 -040037 cmd_list = shlex.split(str(cmd))
38 newenv = os.environ
39 newenv.update(env)
40 p = subprocess.Popen(cmd_list, stdout=subprocess.PIPE,
41 stderr=subprocess.STDOUT, env=newenv)
42 (out, nothing) = p.communicate()
43 if status:
44 return (p.returncode, out.strip())
45 return out.strip()
46
47
48def run_command_status(cmd, env={}):
49 return run_command(cmd, True, env)
50
51
52logging.basicConfig(level=logging.ERROR)
53
54REPO_ROOT = os.environ.get('REPO_ROOT',
Monty Taylor7d5eedf2012-05-26 10:15:27 -040055 '/home/gerrit2/review_site/git')
Monty Taylorc0df8b02012-07-29 12:54:27 -050056PROJECTS_YAML = os.environ.get('PROJECTS_YAML',
57 '/home/gerrit2/projects.yaml')
Monty Taylorda9ef082012-05-10 13:12:31 -040058
Clark Boylanbfef7002012-11-14 09:11:09 -080059(defaults, config) = [config for config in yaml.load_all(open(PROJECTS_YAML))]
Monty Taylorda9ef082012-05-10 13:12:31 -040060
Monty Taylorc0df8b02012-07-29 12:54:27 -050061for section in config:
62 project = section['project']
Monty Taylorda9ef082012-05-10 13:12:31 -040063
Monty Taylorc0df8b02012-07-29 12:54:27 -050064 if 'remote' not in section:
Monty Taylorda9ef082012-05-10 13:12:31 -040065 continue
Monty Taylorc0df8b02012-07-29 12:54:27 -050066
Monty Taylorda9ef082012-05-10 13:12:31 -040067 project_git = "%s.git" % project
68 os.chdir(os.path.join(REPO_ROOT, project_git))
69
Monty Taylorda9ef082012-05-10 13:12:31 -040070 # Make sure that the specified remote exists
Monty Taylorc0df8b02012-07-29 12:54:27 -050071 remote_url = section['remote']
Monty Taylorda9ef082012-05-10 13:12:31 -040072 # 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")