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 | |
Clark Boylan | bfef700 | 2012-11-14 09:11:09 -0800 | [diff] [blame] | 19 | # - homepage: http://openstack.org |
| 20 | # team-id: 153703 |
| 21 | # has-wiki: False |
| 22 | # has-issues: False |
| 23 | # has-downloads: False |
| 24 | # --- |
Monty Taylor | c0df8b0 | 2012-07-29 12:54:27 -0500 | [diff] [blame] | 25 | # - project: PROJECT_NAME |
| 26 | # options: |
| 27 | # - remote: https://gerrit.googlesource.com/gerrit |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 28 | |
Monty Taylor | c0df8b0 | 2012-07-29 12:54:27 -0500 | [diff] [blame] | 29 | |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 30 | import logging |
| 31 | import os |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 32 | import subprocess |
Andrew Hutchings | 16a6c46 | 2012-05-25 14:26:41 +0100 | [diff] [blame] | 33 | import shlex |
Monty Taylor | c0df8b0 | 2012-07-29 12:54:27 -0500 | [diff] [blame] | 34 | import yaml |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 35 | |
| 36 | def run_command(cmd, status=False, env={}): |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 37 | 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 | |
| 48 | def run_command_status(cmd, env={}): |
| 49 | return run_command(cmd, True, env) |
| 50 | |
| 51 | |
| 52 | logging.basicConfig(level=logging.ERROR) |
| 53 | |
| 54 | REPO_ROOT = os.environ.get('REPO_ROOT', |
Monty Taylor | 7d5eedf | 2012-05-26 10:15:27 -0400 | [diff] [blame] | 55 | '/home/gerrit2/review_site/git') |
Monty Taylor | c0df8b0 | 2012-07-29 12:54:27 -0500 | [diff] [blame] | 56 | PROJECTS_YAML = os.environ.get('PROJECTS_YAML', |
| 57 | '/home/gerrit2/projects.yaml') |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 58 | |
Clark Boylan | bfef700 | 2012-11-14 09:11:09 -0800 | [diff] [blame] | 59 | (defaults, config) = [config for config in yaml.load_all(open(PROJECTS_YAML))] |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 60 | |
Monty Taylor | c0df8b0 | 2012-07-29 12:54:27 -0500 | [diff] [blame] | 61 | for section in config: |
| 62 | project = section['project'] |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 63 | |
Monty Taylor | c0df8b0 | 2012-07-29 12:54:27 -0500 | [diff] [blame] | 64 | if 'remote' not in section: |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 65 | continue |
Monty Taylor | c0df8b0 | 2012-07-29 12:54:27 -0500 | [diff] [blame] | 66 | |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 67 | project_git = "%s.git" % project |
| 68 | os.chdir(os.path.join(REPO_ROOT, project_git)) |
| 69 | |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 70 | # Make sure that the specified remote exists |
Monty Taylor | c0df8b0 | 2012-07-29 12:54:27 -0500 | [diff] [blame] | 71 | remote_url = section['remote'] |
Monty Taylor | da9ef08 | 2012-05-10 13:12:31 -0400 | [diff] [blame] | 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") |