-
-
Notifications
You must be signed in to change notification settings - Fork 561
/
debian.py
676 lines (562 loc) · 22.1 KB
/
debian.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
233
234
235
236
237
238
239
240
241
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/scancode-toolkit for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
import os
import logging
from pathlib import Path
from commoncode import fileutils
from debian_inspector.debcon import get_paragraph_data_from_file
from debian_inspector.debcon import get_paragraphs_data_from_file
from debian_inspector.package import DebArchive
from packageurl import PackageURL
from packagedcode import models
from packagedcode.utils import get_ancestor
"""
Handle Debian package archives, control files and installed databases.
"""
SCANCODE_DEBUG_PACKAGE_API = os.environ.get('SCANCODE_DEBUG_PACKAGE_API', False)
TRACE = SCANCODE_DEBUG_PACKAGE_API
def logger_debug(*args):
pass
logger = logging.getLogger(__name__)
if TRACE:
import sys
logging.basicConfig(stream=sys.stdout)
logger.setLevel(logging.DEBUG)
def logger_debug(*args):
return logger.debug(
' '.join(isinstance(a, str) and a or repr(a) for a in args)
)
# TODO: add dependencies
# TODO: introspect archive
class DebianDebPackageHandler(models.DatafileHandler):
datasource_id = 'debian_deb'
default_package_type = 'deb'
path_patterns = ('*.deb',)
filetypes = ('debian binary package',)
description = 'Debian binary package archive'
documentation_url = 'https://manpages.debian.org/unstable/dpkg-dev/deb.5.en.html'
@classmethod
def parse(cls, location):
yield build_package_data_from_package_filename(
filename=fileutils.file_name(location),
datasource_id=cls.datasource_id,
package_type=cls.default_package_type,
)
@classmethod
def assign_package_to_resources(cls, package, resource, codebase):
# only assign this resource
return models.DatafileHandler.assign_package_to_resources(package, resource, codebase)
# TODO: introspect archive
class DebianSourcePackageMetadataTarballHandler(models.DatafileHandler):
datasource_id = 'debian_source_metadata_tarball'
default_package_type = 'deb'
path_patterns = ('*.debian.tar.xz', '*.debian.tar.gz',)
filetypes = ('posix tar archive',)
description = 'Debian source package metadata archive'
documentation_url = 'https://manpages.debian.org/unstable/dpkg-dev/deb.5.en.html'
@classmethod
def parse(cls, location):
# strip extension
filename, _, _ = location.rpartition('.tar')
yield build_package_data_from_package_filename(
filename=filename,
datasource_id=cls.datasource_id,
package_type=cls.default_package_type,
)
@classmethod
def assign_package_to_resources(cls, package, resource, codebase):
# only assign this resource
return models.DatafileHandler.assign_package_to_resources(package, resource, codebase)
# TODO: introspect archive
class DebianSourcePackageTarballHandler(models.DatafileHandler):
datasource_id = 'debian_original_source_tarball'
default_package_type = 'deb'
path_patterns = ('*.orig.tar.xz', '*.orig.tar.gz',)
filetypes = ('posix tar archive',)
description = 'Debian package original source archive'
documentation_url = 'https://manpages.debian.org/unstable/dpkg-dev/deb.5.en.html'
@classmethod
def parse(cls, location):
# strip extension
filename, _, _ = location.rpartition('.tar')
yield build_package_data_from_package_filename(
filename=filename,
datasource_id=cls.datasource_id,
package_type=cls.default_package_type,
)
@classmethod
def assign_package_to_resources(cls, package, resource, codebase):
# only assign this resource
return models.DatafileHandler.assign_package_to_resources(package, resource, codebase)
# TODO: also look into neighboring md5sum and data.tarball copyright files!!!
class DebianControlFileInExtractedDebHandler(models.DatafileHandler):
datasource_id = 'debian_control_extracted_deb'
default_package_type = 'deb'
path_patterns = ('*/control.tar.gz-extract/control',)
description = 'Debian control file - extracted layout'
documentation_url = 'https://www.debian.org/doc/debian-policy/ch-controlfields.html'
@classmethod
def parse(cls, location):
# TODO: we cannot know the distro from the name only
yield build_package_data(
debian_data=get_paragraph_data_from_file(location=location),
datasource_id=cls.datasource_id,
package_type=cls.default_package_type,
)
@classmethod
def assign_package_to_resources(cls, package, resource, codebase):
# two levels up
root = resource.parent(codebase).parent(codebase)
if root:
return models.DatafileHandler.assign_package_to_resources(package, root, codebase)
# TODO: also look into neighboring copyright files!!!
class DebianControlFileInSourceHandler(models.DatafileHandler):
datasource_id = 'debian_control_in_source'
default_package_type = 'deb'
path_patterns = ('*/debian/control',)
description = 'Debian control file - source layout'
documentation_url = 'https://www.debian.org/doc/debian-policy/ch-controlfields.html'
@classmethod
def parse(cls, location):
# TODO: we cannot know the distro from the name only
# NOTE: a control file in a source repo or debina.tar tarball can contain more than one package
for debian_data in get_paragraphs_data_from_file(location=location):
yield build_package_data(
debian_data,
datasource_id=cls.datasource_id,
package_type=cls.default_package_type,
)
@classmethod
def assign_package_to_resources(cls, package, resource, codebase):
# two levels up
root = resource.parent(codebase).parent(codebase)
if root:
return models.DatafileHandler.assign_package_to_resources(package, root, codebase)
class DebianDscFileHandler(models.DatafileHandler):
# See http://deb.debian.org/debian/pool/main/p/python-docutils/python-docutils_0.16+dfsg-4.dsc
# or http://ftp.debian.org/debian/pool/main/7/7kaa/7kaa_2.15.4p1+dfsg-1.dsc
# these are source control files
datasource_id = 'debian_source_control_dsc'
default_package_type = 'deb'
path_patterns = ('*.dsc',)
description = 'Debian source control file'
documentation_url = 'https://wiki.debian.org/dsc'
@classmethod
def parse(cls, location):
# this is typically signed
debian_data = get_paragraph_data_from_file(
location=location,
remove_pgp_signature=True,
)
yield build_package_data(
debian_data=debian_data,
datasource_id=cls.datasource_id,
package_type=cls.default_package_type,
)
@classmethod
def assign_package_to_resources(cls, package, resource, codebase):
# only assign this resource
return models.DatafileHandler.assign_package_to_resources(package, resource, codebase)
class DebianInstalledStatusDatabaseHandler(models.DatafileHandler):
datasource_id = 'debian_installed_status_db'
default_package_type = 'deb'
path_patterns = ('*var/lib/dpkg/status',)
description = 'Debian installed packages database'
documentation_url = 'https://www.debian.org/doc/debian-policy/ch-controlfields.html'
@classmethod
def parse(cls, location):
# note that we do not know yet the distro at this stage
# we could get it... but we get that later during assemble()
for debian_data in get_paragraphs_data_from_file(location):
yield build_package_data(
debian_data,
datasource_id=cls.datasource_id,
package_type=cls.default_package_type,
)
@classmethod
def assemble(cls, package_data, resource, codebase):
# get the root resource of the rootfs
levels_up = len('var/lib/dpkg/status'.split('/'))
root_resource = get_ancestor(
levels_up=levels_up,
resource=resource,
codebase=codebase,
)
package_name = package_data.name
package = models.Package.from_package_data(
package_data=package_data,
datafile_path=resource.path,
)
package_file_references = []
package_file_references.extend(package_data.file_references)
package_uid = package.package_uid
dependent_packages = package_data.dependencies
if dependent_packages:
yield from models.Dependency.from_dependent_packages(
dependent_packages=dependent_packages,
datafile_path=resource.path,
datasource_id=package_data.datasource_id,
package_uid=package_uid,
)
# Multi-Arch can be: "foreign", "same", "allowed", "all", "optional" or
# empty/non-present. See https://wiki.debian.org/Multiarch/HOWTO
# We only need to adjust the md5sum/list path in the case of `same`
qualifiers = package_data.qualifiers or {}
architecture = qualifiers.get('architecture')
multi_arch = package_data.extra_data.get('multi_arch')
if TRACE:
logger_debug(f' debian: assemble: multi_arch: {multi_arch}')
logger_debug(f' debian: assemble: architecture: {architecture}')
if multi_arch == 'same':
arch_path = f':{architecture}'
else:
arch_path = ''
# collect .list, .md5sum, and copyright file for this package
# and assemble package data
assemblable_paths = tuple(set([
# we try both with and without arch in path for multi-arch support
f'var/lib/dpkg/info/{package_name}.md5sums',
f'var/lib/dpkg/info/{package_name}{arch_path}.md5sums',
f'var/lib/dpkg/info/{package_name}.list',
f'var/lib/dpkg/info/{package_name}{arch_path}.list',
f'usr/share/doc/{package_name}/copyright',
]))
# TODO: keep track of missing files
for res in root_resource.walk(codebase):
if not res.path.endswith(assemblable_paths):
continue
for pkgdt in res.package_data:
package_data = models.PackageData.from_dict(pkgdt)
package.update(
package_data=package_data,
datafile_path=res.path,
)
package_file_references.extend(package_data.file_references)
res.for_packages.append(package_uid)
res.save(codebase)
# yield possible dependencies
dependent_packages = package_data.dependencies
if dependent_packages:
yield from models.Dependency.from_dependent_packages(
dependent_packages=dependent_packages,
datafile_path=res.path,
datasource_id=package_data.datasource_id,
package_uid=package_uid,
)
# we yield this as we do not want this further processed
yield res
root_path = Path(root_resource.path)
# FIXME: should we consider ONLY the md5sums?
# merge references for the same path (e.g. .list amd .md5sum)
file_references_by_path = {}
for ref in package_file_references:
# a file ref extends from the root of the filesystem
ref_path = str(root_path / ref.path)
existing = file_references_by_path.get(ref_path)
if existing:
existing.update(ref)
else:
file_references_by_path[ref_path] = ref
for res in root_resource.walk(codebase):
ref = file_references_by_path.get(res.path)
if not ref:
continue
# path is found and processed: remove it, so we can check if we found all of them
del file_references_by_path[res.path]
res.for_packages.append(package_uid)
res.save(codebase)
yield res
# if we have left over file references, add these to extra data
if file_references_by_path:
missing = sorted(file_references_by_path.values(), key=lambda r:r.path)
package.extra_data['missing_file_references'] = missing
yield package
class DebianDistrolessInstalledDatabaseHandler(models.DatafileHandler):
datasource_id = 'debian_distroless_installed_db'
default_package_type = 'deb'
path_patterns = ('*var/lib/dpkg/status.d/*',)
description = 'Debian distroless installed database'
documentation_url = 'https://www.debian.org/doc/debian-policy/ch-controlfields.html'
@classmethod
def parse(cls, location):
"""
Yield installed PackageData objects given a ``location``
var/lib/dpkg/status.d/<status> file as found in a distroless container
rootfs installation. distroless is derived from Debian but each package
has its own status file.
"""
for debian_data in get_paragraphs_data_from_file(location):
yield build_package_data(
debian_data,
datasource_id=cls.datasource_id,
package_type=cls.default_package_type,
distro='distroless',
)
@classmethod
def assemble(cls, package_data, resource, codebase):
# get the root resource of the rootfs
levels_up = len('var/lib/dpkg/status.d/name'.split('/'))
root_resource = get_ancestor(
levels_up=levels_up,
resource=resource,
codebase=codebase,
)
package_name = package_data.name
package = models.Package.from_package_data(
package_data=package_data,
datafile_path=resource.path,
)
package_uid = package.package_uid
dependent_packages = package_data.dependencies
if dependent_packages:
yield from models.Dependency.from_dependent_packages(
dependent_packages=dependent_packages,
datafile_path=resource.path,
datasource_id=package_data.datasource_id,
package_uid=package_uid,
)
# collect copyright file for this package
# and merge in package data
assemblable_paths = (
f'usr/share/doc/{package_name}/copyright',
)
for res in root_resource.walk(codebase):
if not res.path.endswith(assemblable_paths):
continue
for pkgdt in res.package_data:
package.update(
package_data=pkgdt,
datafile_path=res.path,
)
res.for_packages.append(package_uid)
res.save(codebase)
yield res
yield package
class DebianInstalledFilelistHandler(models.DatafileHandler):
# seen in installed rootfs in:
# - /var/lib/dpkg/info/<package name>.list
datasource_id = 'debian_installed_files_list'
default_package_type = 'deb'
path_patterns = (
'*var/lib/dpkg/info/*.list',
)
description = 'Debian installed file paths list'
@classmethod
def parse(cls, location):
return parse_debian_files_list(
location=location,
datasource_id=cls.datasource_id,
package_type=cls.default_package_type,
)
@classmethod
def assemble(cls, package_data, resource, codebase):
# this is assembled only from a database entry
return
class DebianInstalledMd5sumFilelistHandler(models.DatafileHandler):
# seen in installed rootfs in:
# - /var/lib/dpkg/info/<package name>.md5sums
# - /var/lib/dpkg/info/<package name:arch>.md5sums
datasource_id = 'debian_installed_md5sums'
default_package_type = 'deb'
path_patterns = (
'*var/lib/dpkg/info/*.md5sums',
)
description = 'Debian installed file MD5 and paths list'
documentation_url = 'https://www.debian.org/doc/manuals/debian-handbook/sect.package-meta-information.en.html#sect.configuration-scripts'
@classmethod
def parse(cls, location):
return parse_debian_files_list(
location=location,
datasource_id=cls.datasource_id,
package_type=cls.default_package_type,
)
@classmethod
def assemble(cls, package_data, resource, codebase):
# this is assembled only from a database entry
return []
class DebianMd5sumFilelistInPackageHandler(models.DatafileHandler):
datasource_id = 'debian_md5sums_in_extracted_deb'
default_package_type = 'deb'
path_patterns = (
# in .deb control tarball
'*/control.tar.gz-extract/md5sums',
'*/control.tar.xz-extract/md5sums',
)
description = 'Debian file MD5 and paths list in .deb archive'
documentation_url = 'https://www.debian.org/doc/manuals/debian-handbook/sect.package-meta-information.en.html#sect.configuration-scripts'
@classmethod
def parse(cls, location):
return parse_debian_files_list(
location=location,
datasource_id=cls.datasource_id,
package_type=cls.default_package_type,
)
@classmethod
def assign_package_to_resources(cls, package, resource, codebase):
# two levels up
root = resource.parent(codebase).parent(codebase)
if root:
return models.DatafileHandler.assign_package_to_resources(package, root, codebase)
def build_package_data_from_package_filename(filename, datasource_id, package_type,):
"""
Return a PackageData built from the filename of a Debian package archive.
"""
# TODO: we cannot know the distro from the name only
deb = DebArchive.from_filename(filename=filename)
if deb.architecture:
qualifiers = dict(architecture=deb.architecture)
else:
qualifiers = {}
return models.PackageData(
datasource_id=datasource_id,
type=package_type,
name=deb.name,
version=deb.version,
qualifiers=qualifiers,
)
def parse_debian_files_list(location, datasource_id, package_type):
"""
Yield PackageData from a list of file paths at locations such as an from a
Debian installed .list or .md5sums file.
"""
qualifiers = {}
filename = fileutils.file_base_name(location)
if ':' in filename:
name, _, arch = filename.partition(':')
qualifiers['arch'] = arch
else:
name = filename
file_references = []
with open(location) as info_file:
for line in info_file:
line = line.strip()
if not line or line.startswith('#'):
continue
# for a plain file lits, the md5sum will be empty
md5sum, _, path = line.partition(' ')
path = path.strip()
md5sum = md5sum and md5sum.strip() or None
# we ignore dirs in general, and we ignore these that would
# be created a plain dir when we can
if path in ignored_root_dirs:
continue
ref = models.FileReference(path=path, md5=md5sum)
file_references.append(ref)
if not file_references:
return
yield models.PackageData(
datasource_id=datasource_id,
type=package_type,
name=name,
qualifiers=qualifiers,
file_references=file_references,
)
def build_package_data(debian_data, datasource_id, package_type='deb', distro=None):
"""
Return a PackageData object from a package_data mapping (from a dpkg status
or similar file) or None.
"""
name = debian_data.get('package')
version = debian_data.get('version')
qualifiers = {}
architecture = debian_data.get('architecture')
if architecture:
qualifiers['architecture'] = architecture
extra_data = {}
# Multi-Arch can be: "foreign", "same", "allowed", "all", "optional" or
# empty/non-present. See https://wiki.debian.org/Multiarch/HOWTO
multi_arch = debian_data.get('multi-arch')
if multi_arch:
extra_data['multi_arch'] = multi_arch
description = debian_data.get('description')
homepage_url = debian_data.get('homepage')
size = debian_data.get('installed')
parties = []
maintainer = debian_data.get('maintainer')
if maintainer:
party = models.Party(role='maintainer', name=maintainer)
parties.append(party)
orig_maintainer = debian_data.get('original_maintainer')
if orig_maintainer:
party = models.Party(role='original_maintainer', name=orig_maintainer)
parties.append(party)
keywords = []
keyword = debian_data.get('section')
if keyword:
keywords.append(keyword)
source_packages = []
source = debian_data.get('source')
if source:
source_pkg_purl = PackageURL(
type=package_type,
name=source,
namespace=distro
).to_string()
source_packages.append(source_pkg_purl)
return models.PackageData(
datasource_id=datasource_id,
type=package_type,
namespace=distro,
name=name,
version=version,
qualifiers=qualifiers,
description=description,
homepage_url=homepage_url,
size=size,
source_packages=source_packages,
keywords=keywords,
parties=parties,
extra_data=extra_data,
)
ignored_root_dirs = {
'/.',
'/bin',
'/boot',
'/cdrom',
'/dev',
'/etc',
'/etc/skel',
'/home',
'/lib',
'/lib32',
'/lib64',
'/lost+found',
'/mnt',
'/media',
'/opt',
'/proc',
'/root',
'/run',
'/usr',
'/sbin',
'/snap',
'/sys',
'/tmp',
'/usr',
'/usr/games',
'/usr/include',
'/usr/sbin',
'/usr/share/info',
'/usr/share/man',
'/usr/share/misc',
'/usr/src',
'/var',
'/var/backups',
'/var/cache',
'/var/lib/dpkg',
'/var/lib/misc',
'/var/local',
'/var/lock',
'/var/log',
'/var/opt',
'/var/run',
'/var/spool',
'/var/tmp',
'/var/lib',
}