-
Notifications
You must be signed in to change notification settings - Fork 2
/
pktk.py
executable file
·232 lines (206 loc) · 8.69 KB
/
pktk.py
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python3
import argparse
import collections
import json
import os
import re
import shlex
import subprocess
def split_list(list, delimiter):
part = []
for line in list:
if line == delimiter:
yield part
part = []
else:
part.append(line)
yield part
def parse_bash_array(str):
if str is None:
return None
elif str.startswith("(") and str.endswith(")"):
# hiding escaped backslashes to find unescaped quotes
str = str.replace(r"\\", ":escaped_backslash:")
pattern = re.compile(r'\[\d+\]="(.*?[^\\])"')
return list(map(lambda s: s.replace(":escaped_backslash:", r"\\"), pattern.findall(str)))
else:
return [str]
# Parse output of bash `set` and return map "key -> value" or "function () -> body"
def parse_set_output(set_output):
result = {}
empty_pattern = re.compile(r"^\s*$")
var_pattern = re.compile(r"^(\w+)=(.*)$")
fun_title_pattern = re.compile(r"^(\w+ \(\))\s*$")
fun_begin_pattern = re.compile(r"^{\s*$")
fun_end_pattern = re.compile(r"^}$")
fun_name = None
fun_body = []
for line in set_output:
if m := var_pattern.match(line):
result[m.group(1)] = m.group(2)
elif m := fun_title_pattern.match(line):
fun_name = m.group(1)
fun_body = []
elif m := fun_end_pattern.match(line):
result[fun_name] = "\n".join(fun_body)
fun_name = None
fun_body = []
elif m := fun_begin_pattern.match(line):
continue
elif fun_name is not None:
fun_body.append(line)
elif m := empty_pattern.match(line):
continue
else:
exit("Can't parse line {}, output={}".format(line, '\n'.join(set_output)))
return result
def read_pkgbuild(file):
with open(file, "r") as f:
pkgbuild_contents = f.read()
separator_before = "PKGBUILD begins here"
separator_after = "PKGBUILD ends here"
command = shlex.split("env -i bash")
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
pkgbuild = f"""
set
echo {separator_before}
{pkgbuild_contents}
echo {separator_after}
set
"""
out, err = process.communicate(input=pkgbuild.encode())
if err:
exit(f"Bash error: {err.decode()}")
before, rest = split_list(out.decode().split("\n"), separator_before)
_, after = split_list(rest, separator_after)
vars_before = parse_set_output(before)
vars_after = parse_set_output(after)
for key, value in vars_before.items():
if vars_after[key] == value:
vars_after.pop(key)
result = {}
result["name"] = vars_after.get("pkgname")
result["version"] = vars_after.get("pkgver")
result["release"] = vars_after.get("pkgrel")
result["description"] = vars_after.get("pkgdesc")
result["source"] = parse_bash_array(vars_after.get("source"))
result["sha256sums"] = parse_bash_array(vars_after.get("sha256sums"))
result["md5sums"] = parse_bash_array(vars_after.get("md5sums"))
result["makedepends"] = parse_bash_array(vars_after.get("makedepends"))
result["depends"] = parse_bash_array(vars_after.get("depends"))
result["provides"] = parse_bash_array(vars_after.get("provides"))
result["replaces"] = parse_bash_array(vars_after.get("replaces"))
result["conflicts"] = parse_bash_array(vars_after.get("conflicts"))
result["license"] = parse_bash_array(vars_after.get("license"))
result["arch"] = parse_bash_array(vars_after.get("arch"))
result["build()"] = vars_after.get("build ()")
result["package()"] = vars_after.get("package ()")
# platform-specific fields:
result["pkgbuild_pkgname"] = vars_after.get("_pkgname")
result["pkgbuild_pkgfolder"] = vars_after.get("_pkgfolder")
return result
def write_json(file, result):
with open(file, "w") as f:
print(json.dumps(result, indent=2), file=f)
def write_rpm_section(f, name, value):
if value:
print("\n%{}\n{}".format(name, value), file=f)
def write_rpm_array(f, name, array):
if array:
for value in array:
write_rpm_field(f, name, value)
def write_rpm_field(f, name, value):
if value:
print("{}: {}".format(name, value), file=f)
def write_rpm_variable(f, name, value):
if value:
print("%define {} {}".format(name, value), file=f)
def map_packages(mapping, list):
MappedPackage=collections.namedtuple("MappedPackage", ["packages", "aliases", "extra_deps"])
if not list:
return MappedPackage([], [], [])
packages = []
aliases = []
extra_deps = []
version_pattern = re.compile(r"^([\w_-]+)>?<?=(.*)$")
for item in list:
if m := version_pattern.match(item):
package = m.group(1)
else:
package = item
# TODO: also use version
mapped = mapping.get(package) or {}
if (mapped_packages := mapped.get("packages")) is None:
packages.append(package)
else:
packages.extend(mapped_packages)
aliases.extend(mapped.get("aliases") or [])
extra_deps.extend(mapped.get("extra_deps") or [])
return MappedPackage(packages, aliases, extra_deps)
def wrap_code_section(code, aliases_list):
if code:
vars = """ export pkgdir="%{buildroot}"
export srcdir="%{srcdir}"
export pkgname="%{pkgname}"
export _pkgname="%{_pkgname}"
export _pkgfolder="%{_pkgfolder}"
"""
aliases = ""
for alias in aliases_list:
aliases += " alias {}\n".format(alias)
return vars + aliases + code
else:
return None
# append rpm-specific stuff to json
def make_rpm_json(value):
with open("pkgbuild_mapping.json", "r") as f:
mapping = json.load(f)
result = value
result["rpm_requires"] = map_packages(mapping, result.get("depends")).packages
result["rpm_provides"] = map_packages(mapping, result.get("provides")).packages
result["rpm_conflicts"] = map_packages(mapping, result.get("conflicts")).packages
result["rpm_obsoletes"] = map_packages(mapping, result.get("obsoletes")).packages
result["rpm_buildrequires"] = map_packages(mapping, result.get("makedepends")).packages
result["rpm_aliases"] = map_packages(mapping, result.get("makedepends")).aliases
result["rpm_extra_deps"] = map_packages(mapping, [result.get("name")]).extra_deps
result["rpm_devel"] = [p + "-devel" for p in result["rpm_requires"] + result["rpm_buildrequires"] + result["rpm_extra_deps"]]
return result
def write_rpm(file, result):
with open(file, "w") as f:
write_rpm_field(f, "Name", result.get("name"))
write_rpm_field(f, "Version", result.get("version"))
write_rpm_field(f, "Release", result.get("release"))
write_rpm_field(f, "Summary", result.get("description"))
write_rpm_array(f, "License", result.get("license"))
write_rpm_array(f, "Requires", result.get("rpm_requires"))
write_rpm_array(f, "Provides", result.get("rpm_provides"))
write_rpm_array(f, "Conflicts", result.get("rpm_conflicts"))
write_rpm_array(f, "Obsoletes", result.get("rpm_obsoletes"))
write_rpm_array(f, "BuildRequires", result.get("rpm_buildrequires"))
write_rpm_field(f, "Source", "{}-{}.tar.gz".format(result.get("name"), result.get("version")))
write_rpm_variable(f, "__brp_mangle_shebangs", "%{nil}")
write_rpm_variable(f, "debug_package", "%{nil}")
write_rpm_variable(f, "srcdir", "%{_builddir}/" + "{}-{}".format(result.get("name"), result.get("version")))
# TODO: just write all vars
write_rpm_variable(f, "pkgname", result.get("name"))
write_rpm_variable(f, "_pkgname", result.get("pkgbuild_pkgname"))
write_rpm_variable(f, "_pkgfolder", result.get("pkgbuild_pkgfolder"))
write_rpm_section(f, "prep", "%setup")
write_rpm_section(f, "description", result.get("description"))
write_rpm_section(f, "build", wrap_code_section(result.get("build()"), result.get("rpm_aliases")))
write_rpm_section(f, "install", wrap_code_section(result.get("package()"), result.get("rpm_aliases")))
write_rpm_section(f, "files",
"/*\n" +
"%exclude %dir /usr/bin\n" +
"%exclude %dir /usr/lib\n")
def main():
parser = argparse.ArgumentParser(description="Convert Linux packages")
parser.add_argument("pkgbuild", help="Path to PKGBUILD file")
parser.add_argument("output", help="Path to output")
args = parser.parse_args()
value = read_pkgbuild(args.pkgbuild)
value = make_rpm_json(value)
write_json(args.output + ".json", value)
write_rpm(args.output + ".spec", value)
if __name__ == "__main__":
main()