blob: 4dda133f3dc16c42f643fb66607056caa47776a1 [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
Monty Taylorc0df8b02012-07-29 12:54:27 -050019# - project: PROJECT_NAME
20# options:
21# - remote: https://gerrit.googlesource.com/gerrit
Monty Taylorda9ef082012-05-10 13:12:31 -040022
Monty Taylorc0df8b02012-07-29 12:54:27 -050023
Monty Taylorda9ef082012-05-10 13:12:31 -040024import logging
25import os
Monty Taylorda9ef082012-05-10 13:12:31 -040026import subprocess
Andrew Hutchings16a6c462012-05-25 14:26:41 +010027import shlex
Monty Taylorc0df8b02012-07-29 12:54:27 -050028import yaml
Monty Taylorda9ef082012-05-10 13:12:31 -040029
30def run_command(cmd, status=False, env={}):
Monty Taylorda9ef082012-05-10 13:12:31 -040031 cmd_list = shlex.split(str(cmd))
32 newenv = os.environ
33 newenv.update(env)
34 p = subprocess.Popen(cmd_list, stdout=subprocess.PIPE,
35 stderr=subprocess.STDOUT, env=newenv)
36 (out, nothing) = p.communicate()
37 if status:
38 return (p.returncode, out.strip())
39 return out.strip()
40
41
42def run_command_status(cmd, env={}):
43 return run_command(cmd, True, env)
44
45
46logging.basicConfig(level=logging.ERROR)
47
48REPO_ROOT = os.environ.get('REPO_ROOT',
Monty Taylor7d5eedf2012-05-26 10:15:27 -040049 '/home/gerrit2/review_site/git')
Monty Taylorc0df8b02012-07-29 12:54:27 -050050PROJECTS_YAML = os.environ.get('PROJECTS_YAML',
51 '/home/gerrit2/projects.yaml')
Monty Taylorda9ef082012-05-10 13:12:31 -040052
Monty Taylorc0df8b02012-07-29 12:54:27 -050053config = yaml.load(open(PROJECTS_YAML))
Monty Taylorda9ef082012-05-10 13:12:31 -040054
Monty Taylorc0df8b02012-07-29 12:54:27 -050055for section in config:
56 project = section['project']
Monty Taylorda9ef082012-05-10 13:12:31 -040057
Monty Taylorc0df8b02012-07-29 12:54:27 -050058 if 'remote' not in section:
Monty Taylorda9ef082012-05-10 13:12:31 -040059 continue
Monty Taylorc0df8b02012-07-29 12:54:27 -050060
Monty Taylorda9ef082012-05-10 13:12:31 -040061 project_git = "%s.git" % project
62 os.chdir(os.path.join(REPO_ROOT, project_git))
63
Monty Taylorda9ef082012-05-10 13:12:31 -040064 # Make sure that the specified remote exists
Monty Taylorc0df8b02012-07-29 12:54:27 -050065 remote_url = section['remote']
Monty Taylorda9ef082012-05-10 13:12:31 -040066 # We could check if it exists first, but we're ignoring output anyway
67 # So just try to make it, and it'll either make a new one or do nothing
68 run_command("git remote add -f upstream %s" % remote_url)
69 # Fetch new revs from it
70 run_command("git remote update upstream")