build_manifest.py 4.01 KB
Newer Older
1
2
3
4
5
6
#!/usr/bin/env python3

import os
import requests
import sys

7
from typing import Dict, Tuple, List
8
from urllib.parse import urlparse
9

10
11
GSTREAMER_MODULES: List[str] = [
    # 'orc',
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
    'gst-build',
    'gstreamer',
    'gst-plugins-base',
    'gst-plugins-good',
    'gst-plugins-bad',
    'gst-plugins-ugly',
    'gst-libav',
    'gst-devtools',
    'gst-docs',
    'gst-editing-services',
    'gst-omx',
    'gst-python',
    'gst-rtsp-server'
]

27
MANIFEST_TEMPLATE: str = """<?xml version="1.0" encoding="UTF-8"?>
28
<manifest>
29
  <remote fetch="{}" name="user"/>
30
31
  <remote fetch="https://gitlab.freedesktop.org/gstreamer/" name="gstreamer"/>
  <remote fetch="git://anongit.freedesktop.org/gstreamer/" name="origin"/>
32
{}
33
34
35
</manifest>"""


36
37
38
def request_raw(path: str, token: str, project_url: str) -> Dict[str, str]:
    gitlab_header: Dict[str, str] = {'JOB_TOKEN': token }
    base_url: str = get_hostname(project_url)
39

40
    return requests.get(f"https://{base_url}/api/v4/" + path, headers=gitlab_header).json()
41

42

43
44
45
46
47
48
def request(path: str) -> Dict[str, str]:
    token = os.environ["CI_JOB_TOKEN"]
    project_url = os.environ['CI_PROJECT_URL']
    return request_raw(path, token, project_url)


49
50
51
52
53
54
55
56
57
58
59
60
def get_hostname(url: str) -> str:
    return urlparse(url).hostname


def test_get_hostname():
    gitlab = 'https://gitlab.com/example/a_project'
    assert get_hostname(gitlab) == 'gitlab.com'

    fdo = 'https://gitlab.freedesktop.org/example/a_project'
    assert get_hostname(fdo) == 'gitlab.freedesktop.org'


61
def find_repository_sha(module: str, branchname: str) -> Tuple[str, str]:
62
63
64
65
66
67
68
69
    for project in request('projects?search=' + module):
        if project['name'] != module:
            continue

        if 'namespace' not in project:
            # print("No 'namespace' in: %s - ignoring?" % project, file=sys.stderr)
            continue

70
        id = project['id']
71
72
        if project['namespace']['path'] in useful_namespaces:
            if project['namespace']['path'] == user_namespace:
73
                # If we have a branch with same name, use it.
74
                for branch in request(f"{id}/repository/branches"):
75
                    if branch['name'] == branchname:
76
                        name = project['namespace']['path']
77
                        print(f"{name}/{branchname}")
78
79
80

                        return 'user', branch['commit']['id']
            else:
81
                for branch in request(f"{id}/repository/branches"):
82
                    if branch['name'] == branchname:
83
                        print(f"gstreamer/{branchname}")
84
85
                        return 'gstreamer', branch['commit']['id']

86
                branch, = request(f"{id}/repository/branches?search=master")
87
88
89
90
91
92
93
                print('gstreamer/master')
                return 'gstreamer', branch.attributes['commit']['id']

    print('origin/master')
    return 'origin', 'master'

if __name__ == "__main__":
94
95
96
    user_namespace: str = os.environ['CI_PROJECT_NAMESPACE']
    project_name: str = os.environ['CI_PROJECT_NAME']
    branchname: str = os.environ['CI_COMMIT_REF_NAME']
97

98
    useful_namespaces: List[str] = ['gstreamer']
99
100
101
102
    if branchname != 'master':
        useful_namespaces.append(user_namespace)

    # Shouldn't be needed.
103
104
    remote: str = "git://anongit.freedesktop.org/gstreamer/"
    projects: str = ''
105
    project_template: str = "  <project name=\"{}\" remote=\"{}\" revision=\"{}\" />\n"
106
    user_remote: str = os.path.dirname(os.environ['CI_PROJECT_URL'])
107
    for module in GSTREAMER_MODULES:
108
        print(f"Checking {module}:", end=' ')
109

110
        remote = 'origin'
111
112
113
        revision = None
        if module == project_name:
            revision = os.environ['CI_COMMIT_SHA']
114
115
            remote = 'user'
            print(f"{user_namespace}/{branchname}")
116
117
118
119
120
        else:
            remote, revision = find_repository_sha(module, branchname)

        if not revision:
            revision = 'master'
121
        projects += project_template.format(module, remote, revision)
122
123

    with open('manifest.xml', mode='w') as manifest:
124
        print(MANIFEST_TEMPLATE.format(user_remote, projects), file=manifest)