blob: d6978de2c2dc8e08b7f7ab3e7ed085219bfdd556 [file] [log] [blame]
Monty Taylor911b6202012-11-22 08:50:18 -08001#! /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
21import logging
22import os
23import subprocess
24import shlex
25import yaml
26
Monty Taylorda3bada2012-11-22 09:38:22 -080027
Monty Taylor911b6202012-11-22 08:50:18 -080028def run_command(cmd, status=False, env={}):
29 cmd_list = shlex.split(str(cmd))
30 newenv = os.environ
31 newenv.update(env)
32 p = subprocess.Popen(cmd_list, stdout=subprocess.PIPE,
33 stderr=subprocess.STDOUT, env=newenv)
34 (out, nothing) = p.communicate()
35 if status:
36 return (p.returncode, out.strip())
37 return out.strip()
38
39
40def run_command_status(cmd, env={}):
41 return run_command(cmd, True, env)
42
43
Monty Taylorda3bada2012-11-22 09:38:22 -080044def main():
Monty Taylor911b6202012-11-22 08:50:18 -080045
Monty Taylorda3bada2012-11-22 09:38:22 -080046 logging.basicConfig(level=logging.ERROR)
Monty Taylor911b6202012-11-22 08:50:18 -080047
Monty Taylorda3bada2012-11-22 09:38:22 -080048 PROJECTS_YAML = os.environ.get('PROJECTS_YAML',
49 '/etc/openstackci/projects.yaml')
50 PIP_TEMP_DOWNLOAD = os.environ.get('PIP_TEMP_DOWNLOAD',
51 '/var/lib/pip-download')
52 GIT_SOURCE = os.environ.get('GIT_SOURCE', 'https://github.com')
53 pip_command = '/usr/local/bin/pip install -M -U -I --exists-action=w ' \
54 '--no-install %s'
Monty Taylor911b6202012-11-22 08:50:18 -080055
Monty Taylorda3bada2012-11-22 09:38:22 -080056 run_command(pip_command % "pip")
Monty Taylor911b6202012-11-22 08:50:18 -080057
Monty Taylorda3bada2012-11-22 09:38:22 -080058 (defaults, config) = [config for config in
59 yaml.load_all(open(PROJECTS_YAML))]
Monty Taylor911b6202012-11-22 08:50:18 -080060
Monty Taylorda3bada2012-11-22 09:38:22 -080061 for section in config:
62 project = section['project']
Monty Taylor911b6202012-11-22 08:50:18 -080063
Monty Taylorda3bada2012-11-22 09:38:22 -080064 os.chdir(PIP_TEMP_DOWNLOAD)
65 short_project = project.split('/')[1]
66 if not os.path.isdir(short_project):
67 run_command("git clone %s/%s.git %s" % (GIT_SOURCE, project,
68 short_project))
69 os.chdir(short_project)
70 run_command("git fetch origin")
Monty Taylor911b6202012-11-22 08:50:18 -080071
Monty Taylorda3bada2012-11-22 09:38:22 -080072 for branch in run_command("git branch -a").split("\n"):
73 branch = branch.strip()
74 if (not branch.startswith("remotes/origin")
75 or "origin/HEAD" in branch):
76 continue
77 run_command("git reset --hard %s" % branch)
78 run_command("git clean -x -f -d -q")
79 print("*********************")
80 print("Fetching pip requires for %s:%s" % (project, branch))
81 for requires_file in ("tools/pip-requires",
82 "tools/test-requires"):
83 if os.path.exists(requires_file):
84 stanza = "-r %s" % requires_file
85 run_command(pip_command % stanza)