Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 1 | #! /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 Taylor | c0df8b0 | 2012-07-29 12:54:27 -0500 | [diff] [blame] | 16 | # Fetch remotes reads a project config file called projects.yaml |
| 17 | # It should look like: |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 18 | |
Monty Taylor | c0df8b0 | 2012-07-29 12:54:27 -0500 | [diff] [blame] | 19 | # - project: PROJECT_NAME |
| 20 | # options: |
| 21 | # - remote: https://gerrit.googlesource.com/gerrit |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 22 | |
Monty Taylor | c0df8b0 | 2012-07-29 12:54:27 -0500 | [diff] [blame] | 23 | |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 24 | import logging |
| 25 | import os |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 26 | import subprocess |
Andrew Hutchings | 16a6c46 | 2012-05-25 14:26:41 +0100 | [diff] [blame] | 27 | import shlex |
Monty Taylor | c0df8b0 | 2012-07-29 12:54:27 -0500 | [diff] [blame] | 28 | import yaml |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 29 | |
| 30 | def run_command(cmd, status=False, env={}): |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 31 | 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 | |
| 42 | def run_command_status(cmd, env={}): |
| 43 | return run_command(cmd, True, env) |
| 44 | |
| 45 | |
| 46 | logging.basicConfig(level=logging.ERROR) |
| 47 | |
| 48 | REPO_ROOT = os.environ.get('REPO_ROOT', |
Monty Taylor | 7d5eedf | 2012-05-26 10:15:27 -0400 | [diff] [blame] | 49 | '/home/gerrit2/review_site/git') |
Monty Taylor | c0df8b0 | 2012-07-29 12:54:27 -0500 | [diff] [blame] | 50 | PROJECTS_YAML = os.environ.get('PROJECTS_YAML', |
| 51 | '/home/gerrit2/projects.yaml') |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 52 | |
Monty Taylor | c0df8b0 | 2012-07-29 12:54:27 -0500 | [diff] [blame] | 53 | config = yaml.load(open(PROJECTS_YAML)) |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 54 | |
Monty Taylor | c0df8b0 | 2012-07-29 12:54:27 -0500 | [diff] [blame] | 55 | for section in config: |
| 56 | project = section['project'] |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 57 | |
Monty Taylor | c0df8b0 | 2012-07-29 12:54:27 -0500 | [diff] [blame] | 58 | if 'remote' not in section: |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 59 | continue |
Monty Taylor | c0df8b0 | 2012-07-29 12:54:27 -0500 | [diff] [blame] | 60 | |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 61 | project_git = "%s.git" % project |
| 62 | os.chdir(os.path.join(REPO_ROOT, project_git)) |
| 63 | |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 64 | # Make sure that the specified remote exists |
Monty Taylor | c0df8b0 | 2012-07-29 12:54:27 -0500 | [diff] [blame] | 65 | remote_url = section['remote'] |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 66 | # 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") |