Monty Taylor | 911b620 | 2012-11-22 08:50:18 -0800 | [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 | |
| 16 | # run_mirrors reads a project config file called projects.yaml |
| 17 | # It should look like: |
| 18 | |
| 19 | # - project: PROJECT_NAME |
| 20 | |
| 21 | import logging |
| 22 | import os |
| 23 | import subprocess |
| 24 | import shlex |
| 25 | import yaml |
| 26 | |
| 27 | def run_command(cmd, status=False, env={}): |
| 28 | cmd_list = shlex.split(str(cmd)) |
| 29 | newenv = os.environ |
| 30 | newenv.update(env) |
| 31 | p = subprocess.Popen(cmd_list, stdout=subprocess.PIPE, |
| 32 | stderr=subprocess.STDOUT, env=newenv) |
| 33 | (out, nothing) = p.communicate() |
| 34 | if status: |
| 35 | return (p.returncode, out.strip()) |
| 36 | return out.strip() |
| 37 | |
| 38 | |
| 39 | def run_command_status(cmd, env={}): |
| 40 | return run_command(cmd, True, env) |
| 41 | |
| 42 | |
| 43 | logging.basicConfig(level=logging.ERROR) |
| 44 | |
| 45 | PROJECTS_YAML = os.environ.get('PROJECTS_YAML', |
| 46 | '/etc/openstackci/projects.yaml') |
| 47 | PIP_TEMP_DOWNLOAD = os.environ.get('PIP_TEMP_DOWNLOAD', |
| 48 | '/var/lib/pip-download') |
| 49 | GIT_SOURCE = os.environ.get('GIT_SOURCE', 'https://github.com') |
| 50 | pip_command = '/usr/local/bin/pip install -M -U -I --exists-action=w ' \ |
| 51 | '--no-install %s' |
| 52 | |
| 53 | run_command(pip_command % "pip") |
| 54 | |
| 55 | (defaults, config) = [config for config in yaml.load_all(open(PROJECTS_YAML))] |
| 56 | |
| 57 | for section in config: |
| 58 | project = section['project'] |
| 59 | |
| 60 | os.chdir(PIP_TEMP_DOWNLOAD) |
| 61 | short_project = project.split('/')[1] |
| 62 | if not os.path.isdir(short_project): |
| 63 | run_command("git clone %s/%s.git %s" % (GIT_SOURCE, project, |
| 64 | short_project)) |
| 65 | os.chdir(short_project) |
| 66 | run_command("git fetch origin") |
| 67 | |
| 68 | for branch in run_command("git branch -a").split("\n"): |
| 69 | branch = branch.strip() |
| 70 | if (not branch.startswith("remotes/origin") |
| 71 | or "origin/HEAD" in branch): |
| 72 | continue |
| 73 | run_command("git reset --hard %s" % branch) |
| 74 | run_command("git clean -x -f -d -q") |
| 75 | print("*********************") |
| 76 | print("Fetching pip requires for %s:%s" % (project, branch)) |
| 77 | for requires_file in ("tools/pip-requires", "tools/test-requires"): |
| 78 | if os.path.exists(requires_file): |
| 79 | stanza = "-r %s" % requires_file |
| 80 | run_command(pip_command % stanza) |
| 81 | |