-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-repo
executable file
·157 lines (133 loc) · 4.47 KB
/
build-repo
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python
import argparse
from enum import Enum, auto
import subprocess
import sys
import tempfile
from collections import OrderedDict
from pathlib import Path
from typing import Union
PEARL_CONF_TEMPLATE = """
# This is an auto generated file by repo-builder (https://github.com/pearl-core/repo-builder)
PEARL_REPO_NAME = "pearl"
# The following list of packages are sorted alphabetically
PEARL_PACKAGES = {{
{body}
}}
"""
PACKAGE_TEMPLATE = """
{name}: {{
'url': {url},
'description': {description},
'homepage': {homepage},
'author': {author},
'license': {license},
'os': {os},
'keywords': {keywords},
'depends': {depends},
}},
"""
def run_bash(
script: str,
capture_stdout: bool = False, capture_stderr: bool = False,
check: bool = True, input: str = None
):
return subprocess.run(
['/usr/bin/env', 'bash', '-c', script],
check=check,
stdout=subprocess.PIPE if capture_stdout else None,
stderr=subprocess.PIPE if capture_stderr else None,
universal_newlines=True,
input=input,
)
def parse_args(sys_args: list):
parser = argparse.ArgumentParser(
description="Generate Pearl repository from a list of git URLs"
)
parser.add_argument(
'urls_filepath',
metavar='URLS_FILE',
type=Path,
help="File containing Git urls (one for each line) to fetch"
)
parser.add_argument(
'repo_out_filepath',
metavar='OUT_FILE',
type=Path,
help="File where to write the Pearl repo conf"
)
args = parser.parse_args(sys_args)
return args
class PackageAttribute(Enum):
NAME = auto()
DESCRIPTION = auto()
HOMEPAGE = auto()
AUTHOR = auto()
LICENSE = auto()
OS = auto()
KEYWORDS = auto()
DEPENDS = auto()
@staticmethod
def from_name(name) -> Union[None, 'PackageAttribute']:
return PackageAttribute.__members__.get(name.upper())
def _build_package_info(package_conf_filepath: Path, url: str):
local_dict = {}
exec(package_conf_filepath.open().read(), {}, local_dict)
package_info = {
'url': url
}
for attr, value in local_dict.items():
pkg_attr = PackageAttribute.from_name(attr)
if pkg_attr is None:
continue
package_info[pkg_attr.name.lower()] = value
return package_info
def generate_pearl_repo(urls_filepath: Path) -> dict:
pearl_repo = OrderedDict()
with urls_filepath.open('r') as urls_file:
urls = [url.strip() for url in urls_file.readlines()]
for url in urls:
url = url.strip()
if url.startswith('#'):
print(f'Skipping {url} because is commented')
continue
print(f"Processing {url}...")
with tempfile.TemporaryDirectory() as tmp_dir:
tmp_dirpath = Path(tmp_dir)
run_bash(
f"git clone {url} {tmp_dirpath}"
)
package_conf_filepath = (tmp_dirpath / 'pearl-config/package.conf')
if not package_conf_filepath.exists():
print(f"Skipping {url} because package.conf does not exist...")
continue
package_info = _build_package_info(package_conf_filepath, url)
name = package_info['name']
pearl_repo[name] = package_info
return pearl_repo
def write_pearl_repo(pearl_repo: dict, repo_out_filepath: Path):
package_str = ""
with repo_out_filepath.open('w') as repo_out_file:
for name, pearl_info in pearl_repo.items():
package_str += PACKAGE_TEMPLATE.format(
name=repr(name),
url=repr(pearl_info['url']),
description=repr(pearl_info['description']),
homepage=repr(pearl_info['homepage']),
author=repr(pearl_info['author']),
license=repr(pearl_info.get('license')),
os=repr(pearl_info['os']),
keywords=repr(pearl_info['keywords']),
depends=repr(pearl_info['depends']),
)
pearl_repo_str = PEARL_CONF_TEMPLATE.format(
body=package_str
)
repo_out_file.write(pearl_repo_str)
def main():
args = parse_args(sys.argv[1:])
print(args)
pearl_repo = generate_pearl_repo(args.urls_filepath)
write_pearl_repo(pearl_repo, args.repo_out_filepath)
if __name__ == '__main__':
main()