aboutsummaryrefslogtreecommitdiff
path: root/build-scripts/rewrite-manifest.py
blob: 352e2070e44e0cb673f7f607551d2f9c6f8cec6f (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
#!/usr/bin/env python
from __future__ import print_function
import argparse
import os
import sys

from xml.etree.cElementTree import (
    ElementTree,
    tostring,
)


rewrite_urls = {
#    'git://git.linaro.org': 'http://git.linaro.org/git-ro',
#    'git://git.linaro.org/': 'http://git.linaro.org/git-ro/',
#    'git://android.git.linaro.org': 'http://android.git.linaro.org/git-ro',
#    'git://android.git.linaro.org/': 'http://android.git.linaro.org/git-ro/',
    'git://git.linaro.org': 'http://git.linaro.org/git',
    'git://git.linaro.org/': 'http://git.linaro.org/git/',
    'git://android.git.linaro.org': 'http://android.git.linaro.org/git',
    'git://android.git.linaro.org/': 'http://android.git.linaro.org/git/',
}

def fatal(msg):
    sys.stderr.write("Error: " + msg + "\n")
    sys.exit(1)


def rewrite_git_urls(root, options):
    """Rewrites all android.git.linaro.org and git.linaro.org URLs.

    Makes them use /git-ro and appends '.git' to repo names if they
    do not already have it.
    """
    remotes_to_handle = set([])
    default_remote = root.find('default').get('remote', None)

    for remote in root.findall("remote"):
        if default_remote is None:
            default_remote = remote.get('name')

        remote_url = remote.get('fetch')
        if "://" not in remote_url:
            if not options.manifest_repo:
                fatal("Remote %s uses relative fetch path '%s', "
                  "but repo url (-u) is not known" % (remote.get('name'),
                        remote.get('fetch')))
            base_url = os.path.dirname(options.manifest_repo)
            remote_url = os.path.join(base_url, remote_url)
            proto, rest = remote_url.split("://")
            rest = os.path.normpath(rest)
            remote_url = proto + "://" + rest
            print("Remote %s uses relative fetch path '%s', "
                  "normalized to '%s'" % (remote.get('name'),
                        remote.get('fetch'), remote_url))

        if remote_url in rewrite_urls:
            remote.set('fetch', rewrite_urls[remote_url])
            remotes_to_handle.add(remote.get('name'))

    if default_remote in remotes_to_handle:
        remotes_to_handle.remove(default_remote)

    for project in root.findall("project"):
        if (project.get('remote') in remotes_to_handle or
            project.get('remote') is None and
            default_remote in remotes_to_handle):
            if not project.get('name').endswith('.git'):
                project.set('name', project.get('name') + '.git')

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description=(
            'Switch Linaro git URLs to scalable git URLs in repo manifest '
            'files and print the result manifest on stdout.'))
    parser.add_argument('manifest', type=str,
                        help='manifest file to process')
    parser.add_argument('-o', type=str, metavar='FILE', dest='outfile',
                        required=True,
                        help='output manifest file')
    parser.add_argument('-u', type=str, metavar='URL', dest='manifest_repo',
                        help='Manifest repository URL (required if manifest uses relative paths)')
    args = parser.parse_args()

    # Parse the manifest file.
    et = ElementTree()
    doc = open(args.manifest)
    root = et.parse(doc)
    rewrite_git_urls(root, args)
    f = open(args.outfile, "w")
    f.write(tostring(root, 'UTF-8'))
    f.close()