Newer
Older
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
#!/usr/bin/python3
# flatdeb — build Flatpak runtimes from Debian packages
#
# Copyright © 2016-2017 Simon McVittie
# Copyright © 2017-2019 Collabora Ltd.
#
# SPDX-License-Identifier: MIT
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import argparse
import logging
import os
import subprocess
import sys
try:
import typing
except ImportError:
pass
else:
typing # silence "unused" warnings
logger = logging.getLogger('flatdeb.set-build-id')
def quote(s):
# We can't use shlex.quote() because it relies on concatenated
# strings, which are valid for sh but not for /etc/os-release
quoted = s.replace(
'\\', '\\\\',
).replace(
'$', '\\$',
).replace(
'`', '\\`',
).replace(
'"', '\\"',
)
for c in quoted:
if c >= '\x7f' or not c.isalnum():
quoted = '"{}"'.format(quoted)
break
return quoted
def main():
# type: (...) -> None
parser = argparse.ArgumentParser(
description='Set build ID in chroot'
)
parser.add_argument('--build-id', default='')
parser.add_argument('--variant', default='')
parser.add_argument('--variant-id', default='')
parser.add_argument('--test-quoting', action='store_true')
parser.add_argument('sysroot')
args = parser.parse_args()
lines = []
if args.test_quoting:
for x in (
'debian',
'10',
):
assert quote(x) == x, x
for x in (
'Debian GNU/Linux 10 (buster)',
'3.141592654',
):
assert quote(x) == '"' + x + '"', x
for orig, quoted in (
("My 'great' distro", '"My \'great\' distro"'),
('My "great" distro', '"My \\"great\\" distro"'),
('$PATH', '"\\$PATH"'),
('C:\\Windows', '"C:\\\\Windows"'),
('Shell `injection`', '"Shell \\`injection\\`"'),
):
assert quote(orig) == quoted
unquoted = subprocess.check_output(
'printf "%s" ' + quoted,
universal_newlines=True,
shell=True,
)
assert unquoted == orig, (unquoted, orig)
return
with open(
os.path.join(args.sysroot, 'usr', 'lib', 'os-release'),
'r',
) as reader:
for line in reader:
if line.startswith(('VARIANT=', 'VARIANT_ID=', 'BUILD_ID=')):
logger.info('# Ignoring: %s', line.strip())
else:
logger.info('%s', line.strip())
lines.append(line)
if args.build_id:
logger.info('Adding BUILD_ID=%s', quote(args.build_id))
lines.append('BUILD_ID={}\n'.format(quote(args.build_id)))
if args.variant:
logger.info('Adding VARIANT=%s', quote(args.variant))
lines.append('VARIANT={}\n'.format(quote(args.variant)))
if args.variant_id:
logger.info('Adding VARIANT_ID=%s', quote(args.variant_id))
lines.append('VARIANT_ID={}\n'.format(quote(args.variant_id)))
with open(
os.path.join(args.sysroot, 'usr', 'lib', 'os-release.new'),
'w',
) as writer:
writer.writelines(lines)
os.rename(
os.path.join(args.sysroot, 'usr', 'lib', 'os-release.new'),
os.path.join(args.sysroot, 'usr', 'lib', 'os-release'),
)
if __name__ == '__main__':
if sys.stderr.isatty():
try:
import colorlog
except ImportError:
pass
else:
formatter = colorlog.ColoredFormatter(
'%(log_color)s%(levelname)s:%(name)s:%(reset)s %(message)s')
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logging.getLogger().addHandler(handler)
else:
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
try:
main()
except KeyboardInterrupt:
raise SystemExit(130)