aboutsummaryrefslogtreecommitdiff
path: root/scripts/mirror-repos
blob: c918d9ff785291dc5276188fb6b441f2c4f3f339 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python
# Copyright (C) 2013 Linaro Ltd.

import argparse
import os
import sys
import subprocess
import urlparse

BASE_PATH = "http://git.linaro.org/git-ro/"


def args_parser():
    """Sets up the argument parser."""
    parser = argparse.ArgumentParser()
    parser.add_argument("--repos-list",
                        required=True,
                        help="File with the repository names to mirror.")
    parser.add_argument("--checkout-dir",
                        required=True,
                        help="Where git repositories will be cloned.")
    parser.add_argument("--user",
                        help="User to run the commands as.")
    return parser


def check_args(args, parser):
    """Checks command line arguments passed.

    :param args: All the command lines as returned by argparse.
    :param parser: The argparse instance.
    """
    if not os.path.exists(args.repos_list) or \
        not os.path.isfile(args.repos_list):
        print ("Error: file '%s' does not exists or is not a regular file." %
                args.repos_list)
        parser.print_usage()
        sys.exit(1)

    if not os.path.exists(args.checkout_dir) or \
        not os.path.isdir(args.checkout_dir):
        print ("Error: directory '%s' does not exists or cannot be "
               "accessed." % args.checkout_dir)
        parser.print_usage()
        sys.exit(1)


def mirror_repos(file, dest, user=None):
    """Clone a mirror copy of a remote repository from git.linaro.org.

    :param file: The file where to read the repositories to mirror.
    :param dest: The directory where to clone the repositories into.
    """
    for line in open(file).readlines():
        line = line.strip()
        base_dir = os.path.basename(line)
        # Git repos need to have a valid name.
        if base_dir.split(".git")[0]:
            # Maintain the same directory layout of original git.linaro.org.
            full_path = os.path.join(dest, line.split(base_dir)[0])

            # We need to do so, to create the directory as the RhodeCode user
            # for our installation.
            cmd_args = ["mkdir", "-p", full_path]
            execute_command(cmd_args, user=user)

            # We mirror the original repository, then through a cron job we can
            # easily update it using the command 'git fetch -q'.
            full_repo = urlparse.urljoin(BASE_PATH, line)
            cmd_args = ["git", "clone", "--mirror", full_repo]

            print "Cloning repository %s..." % full_repo
            execute_command(cmd_args, work_dir=full_path, user=user)


def execute_command(cmd_args, as_sudo=True, user=None, work_dir=os.getcwd()):
    """Executes the command using Popen.

    :param cmd_args: The list of command and parameters to run.
    :param as_sudo: If the command has to be run with 'sudo'.
    :param user: Runs the comand as the specified user.
    :param work_dir: Where the command should be run from.
    """
    exec_args = []
    if not isinstance(cmd_args, list):
        cmd_args = [cmd_args]

    if as_sudo:
        exec_args = ["sudo"]

    if user and as_sudo:
        exec_args += ["-u", user, "-H"]

    exec_args += cmd_args
    process = subprocess.Popen(exec_args,
                               cwd=work_dir,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)
    p_out, p_err = process.communicate()

    if process.returncode != 0:
        print "Error executing the following command: %s" % " ".join(cmd_args)


if __name__ == '__main__':
    parser = args_parser()
    args = parser.parse_args()
    check_args(args, parser)
    mirror_repos(args.repos_list, args.checkout_dir, user=args.user)