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
#!/usr/bin/python3
# flatdeb — build Flatpak runtimes from Debian packages
#
# Copyright © 2016-2017 Simon McVittie
# Copyright © 2017-2018 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.
"""
Fetch source code for packages installed in the given sysroot.
"""
import argparse
import logging
import os
import re
import subprocess
import sys
logger = logging.getLogger('flatdeb.collect-source-code')
class InstalledPackage:
def __init__(self, fields):
self.binary = fields[0]
self.binary_version = fields[1]
self.source = fields[2]
if self.source.endswith(')'):
self.source, self.source_version = self.source.rstrip(')').split(' (')
else:
self.source_version = self.binary_version
if not self.source:
self.source = self.binary
self.installed_size = fields[3]
def __str__(self):
return '{}_{}'.format(self.binary, self.binary_version)
def __hash__(self):
return hash(self.binary) ^ hash(self.binary_version)
def __eq__(self, other):
if isinstance(other, InstalledPackage):
return (
self.binary,
self.binary_version,
) == (
other.binary,
other.binary_version,
)
else:
return NotImplemented
class SourceRequired:
def __init__(self, source, source_version):
self.source = source
self.source_version = source_version
def __str__(self):
return 'src:{}_{}'.format(self.source, self.source_version)
def __hash__(self):
return hash(self.source) ^ hash(self.source_version)
def __eq__(self, other):
if isinstance(other, SourceRequired):
return (
self.source,
self.source_version,
) == (
other.source,
other.source_version,
)
else:
return NotImplemented
def read_manifest(path):
ret = []
with open(path, encoding='utf-8') as reader:
for line in reader:
line = line.rstrip('\n')
if not line:
continue
if line.startswith('#'):
continue
assert '\t' in line, repr(line)
ret.append(InstalledPackage(line.rstrip('\n').split('\t')))
return ret
def read_built_using(path):
ret = set()
with open(path, encoding='utf-8') as reader:
for line in reader:
line = line.rstrip('\n')
if line.startswith('#'):
continue
package, source, version = line.split('\t')
s = SourceRequired(source, version)
logger.info(
'%s was Built-Using %s',
package, s)
ret.add(s)
return ret
def main():
parser = argparse.ArgumentParser(
description='Collect source code',
)
parser.add_argument('--strip-source-version-suffix', default='')
parser.add_argument('sysroot')
args = parser.parse_args()
strip_source_version_suffix = None
if args.strip_source_version_suffix:
strip_source_version_suffix = re.compile(
'(?:' + args.strip_source_version_suffix + ')$')
in_chroot = [
'systemd-nspawn',
'--directory={}'.format(args.sysroot),
'--as-pid2',
'env',
]
for var in ('ftp_proxy', 'http_proxy', 'https_proxy', 'no_proxy'):
if var in os.environ:
in_chroot.append('{}={}'.format(var, os.environ[var]))
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
manifest = os.path.join(args.sysroot, 'usr', 'manifest.dpkg')
platform_manifest = os.path.join(
args.sysroot, 'usr', 'manifest.dpkg.platform')
built_using = os.path.join(
args.sysroot, 'usr', 'manifest.dpkg.built-using')
platform_built_using = os.path.join(
args.sysroot, 'usr', 'manifest.dpkg.built-using.platform')
sdk_packages = read_manifest(manifest)
packages = sdk_packages[:]
sources_required = set()
if os.path.exists(platform_manifest):
platform_packages = read_manifest(manifest)
else:
platform_packages = []
for p in platform_packages:
logger.info('Package in Platform: %s', p)
if p not in sdk_packages:
logger.warning('Package in Platform but not SDK: %s', p)
packages.append(p)
for p in sdk_packages:
if p not in platform_packages:
logger.info('Additional package in SDK: %s', p)
for p in packages:
sources_required.add(SourceRequired(p.source, p.source_version))
sources_required |= read_built_using(built_using)
if os.path.exists(platform_built_using):
sources_required |= read_built_using(platform_built_using)
sources = []
missing_sources = set()
for s in sources_required:
source = s.source
source_version = s.source_version
# TODO: Is this necessary any more?
source = source.split(':', 1)[0]
if strip_source_version_suffix is not None:
source_version = strip_source_version_suffix.sub(
'', source_version)
sources.append('{}={}'.format(source, source_version))
try:
subprocess.check_call(in_chroot + [
'sh', '-euc',
'dir="$1"; shift; mkdir -p "$dir"; cd "$dir"; "$@"',
'sh', # argv[0]
'/ostree/source/files', # working directory
'apt-get', '-y', '--download-only', '-q', '-q',
'-oAPT::Get::Only-Source=true', 'source',
] + sources)
except subprocess.CalledProcessError:
logger.warning(
'Unable to download some sources as a batch, trying '
'to download sources individually')
for source in sources:
try:
subprocess.check_call(in_chroot + [
'sh', '-euc',
'dir="$1"; shift; mkdir -p "$dir"; cd "$dir"; "$@"',
'sh', # argv[0]
'/ostree/source/files', # working directory
'apt-get', '-y', '--download-only', '-q', '-q',
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
'-oAPT::Get::Only-Source=true', 'source',
source,
])
except subprocess.CalledProcessError:
# Non-fatal for now
logger.warning(
'Unable to get source code for %s', source)
missing_sources.add(source)
source_package = source.split('=', 1)[0]
subprocess.call(in_chroot + [
'apt-cache', 'showsrc', source_package,
])
if missing_sources:
logger.warning('Missing source packages:')
for p in sorted(missing_sources):
logger.warning('- %s', p)
logger.warning('Check that this runtime is GPL-compliant!')
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)
except subprocess.CalledProcessError as e:
logger.error('%s', e)
raise SystemExit(1)