scanoss 1.27.1__py3-none-any.whl → 1.43.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. protoc_gen_swagger/options/annotations_pb2.py +18 -12
  2. protoc_gen_swagger/options/annotations_pb2.pyi +48 -0
  3. protoc_gen_swagger/options/annotations_pb2_grpc.py +20 -0
  4. protoc_gen_swagger/options/openapiv2_pb2.py +110 -99
  5. protoc_gen_swagger/options/openapiv2_pb2.pyi +1317 -0
  6. protoc_gen_swagger/options/openapiv2_pb2_grpc.py +20 -0
  7. scanoss/__init__.py +1 -1
  8. scanoss/api/common/v2/scanoss_common_pb2.py +49 -22
  9. scanoss/api/common/v2/scanoss_common_pb2_grpc.py +25 -0
  10. scanoss/api/components/v2/scanoss_components_pb2.py +68 -43
  11. scanoss/api/components/v2/scanoss_components_pb2_grpc.py +83 -22
  12. scanoss/api/cryptography/v2/scanoss_cryptography_pb2.py +136 -47
  13. scanoss/api/cryptography/v2/scanoss_cryptography_pb2_grpc.py +650 -33
  14. scanoss/api/dependencies/v2/scanoss_dependencies_pb2.py +56 -37
  15. scanoss/api/dependencies/v2/scanoss_dependencies_pb2_grpc.py +64 -12
  16. scanoss/api/geoprovenance/v2/scanoss_geoprovenance_pb2.py +74 -31
  17. scanoss/api/geoprovenance/v2/scanoss_geoprovenance_pb2_grpc.py +252 -13
  18. scanoss/api/licenses/__init__.py +23 -0
  19. scanoss/api/licenses/v2/__init__.py +23 -0
  20. scanoss/api/licenses/v2/scanoss_licenses_pb2.py +84 -0
  21. scanoss/api/licenses/v2/scanoss_licenses_pb2_grpc.py +302 -0
  22. scanoss/api/scanning/v2/scanoss_scanning_pb2.py +32 -21
  23. scanoss/api/scanning/v2/scanoss_scanning_pb2_grpc.py +49 -8
  24. scanoss/api/semgrep/v2/scanoss_semgrep_pb2.py +50 -23
  25. scanoss/api/semgrep/v2/scanoss_semgrep_pb2_grpc.py +151 -16
  26. scanoss/api/vulnerabilities/v2/scanoss_vulnerabilities_pb2.py +78 -31
  27. scanoss/api/vulnerabilities/v2/scanoss_vulnerabilities_pb2_grpc.py +282 -18
  28. scanoss/cli.py +1000 -186
  29. scanoss/components.py +80 -50
  30. scanoss/constants.py +7 -1
  31. scanoss/cryptography.py +89 -55
  32. scanoss/csvoutput.py +13 -7
  33. scanoss/cyclonedx.py +141 -9
  34. scanoss/data/build_date.txt +1 -1
  35. scanoss/data/osadl-copyleft.json +133 -0
  36. scanoss/delta.py +197 -0
  37. scanoss/export/__init__.py +23 -0
  38. scanoss/export/dependency_track.py +227 -0
  39. scanoss/file_filters.py +2 -163
  40. scanoss/filecount.py +37 -38
  41. scanoss/gitlabqualityreport.py +214 -0
  42. scanoss/header_filter.py +563 -0
  43. scanoss/inspection/policy_check/__init__.py +0 -0
  44. scanoss/inspection/policy_check/dependency_track/__init__.py +0 -0
  45. scanoss/inspection/policy_check/dependency_track/project_violation.py +479 -0
  46. scanoss/inspection/{policy_check.py → policy_check/policy_check.py} +65 -72
  47. scanoss/inspection/policy_check/scanoss/__init__.py +0 -0
  48. scanoss/inspection/{copyleft.py → policy_check/scanoss/copyleft.py} +89 -73
  49. scanoss/inspection/{undeclared_component.py → policy_check/scanoss/undeclared_component.py} +52 -46
  50. scanoss/inspection/summary/__init__.py +0 -0
  51. scanoss/inspection/summary/component_summary.py +170 -0
  52. scanoss/inspection/{license_summary.py → summary/license_summary.py} +62 -12
  53. scanoss/inspection/summary/match_summary.py +341 -0
  54. scanoss/inspection/utils/file_utils.py +44 -0
  55. scanoss/inspection/utils/license_utils.py +57 -71
  56. scanoss/inspection/utils/markdown_utils.py +63 -0
  57. scanoss/inspection/{inspect_base.py → utils/scan_result_processor.py} +53 -67
  58. scanoss/osadl.py +125 -0
  59. scanoss/scanner.py +135 -253
  60. scanoss/scanners/folder_hasher.py +47 -32
  61. scanoss/scanners/scanner_hfh.py +50 -18
  62. scanoss/scanoss_settings.py +33 -3
  63. scanoss/scanossapi.py +23 -25
  64. scanoss/scanossbase.py +1 -1
  65. scanoss/scanossgrpc.py +543 -289
  66. scanoss/services/dependency_track_service.py +132 -0
  67. scanoss/spdxlite.py +11 -4
  68. scanoss/threadeddependencies.py +19 -18
  69. scanoss/threadedscanning.py +10 -0
  70. scanoss/utils/scanoss_scan_results_utils.py +41 -0
  71. scanoss/winnowing.py +71 -19
  72. {scanoss-1.27.1.dist-info → scanoss-1.43.1.dist-info}/METADATA +8 -5
  73. scanoss-1.43.1.dist-info/RECORD +110 -0
  74. scanoss/inspection/component_summary.py +0 -94
  75. scanoss-1.27.1.dist-info/RECORD +0 -87
  76. {scanoss-1.27.1.dist-info → scanoss-1.43.1.dist-info}/WHEEL +0 -0
  77. {scanoss-1.27.1.dist-info → scanoss-1.43.1.dist-info}/entry_points.txt +0 -0
  78. {scanoss-1.27.1.dist-info → scanoss-1.43.1.dist-info}/licenses/LICENSE +0 -0
  79. {scanoss-1.27.1.dist-info → scanoss-1.43.1.dist-info}/top_level.txt +0 -0
scanoss/cyclonedx.py CHANGED
@@ -28,6 +28,9 @@ import os.path
28
28
  import sys
29
29
  import uuid
30
30
 
31
+ from cyclonedx.schema import SchemaVersion
32
+ from cyclonedx.validation.json import JsonValidator
33
+
31
34
  from . import __version__
32
35
  from .scanossbase import ScanossBase
33
36
  from .spdxlite import SpdxLite
@@ -48,15 +51,18 @@ class CycloneDx(ScanossBase):
48
51
  self.debug = debug
49
52
  self._spdx = SpdxLite(debug=debug)
50
53
 
51
- def parse(self, data: json): # noqa: PLR0912, PLR0915
54
+ def parse(self, data: dict): # noqa: PLR0912, PLR0915
52
55
  """
53
56
  Parse the given input (raw/plain) JSON string and return CycloneDX summary
54
- :param data: json - JSON object
57
+ :param data: dict - JSON object
55
58
  :return: CycloneDX dictionary, and vulnerability dictionary
56
59
  """
57
- if not data:
60
+ if data is None:
58
61
  self.print_stderr('ERROR: No JSON data provided to parse.')
59
62
  return None, None
63
+ if len(data) == 0:
64
+ self.print_msg('Warning: Empty scan results provided. Returning empty component dictionary.')
65
+ return {}, {}
60
66
  self.print_debug('Processing raw results into CycloneDX format...')
61
67
  cdx = {}
62
68
  vdx = {}
@@ -146,7 +152,11 @@ class CycloneDx(ScanossBase):
146
152
  fdl = []
147
153
  if licenses:
148
154
  for lic in licenses:
149
- fdl.append({'id': lic.get('name')})
155
+ name = lic.get('name')
156
+ source = lic.get('source')
157
+ if source not in ('component_declared', 'license_file', 'file_header'):
158
+ continue
159
+ fdl.append({'id': name})
150
160
  fd['licenses'] = fdl
151
161
  cdx[purl] = fd
152
162
  # self.print_stderr(f'VD: {vdx}')
@@ -170,12 +180,12 @@ class CycloneDx(ScanossBase):
170
180
  success = self.produce_from_str(f.read(), output_file)
171
181
  return success
172
182
 
173
- def produce_from_json(self, data: json, output_file: str = None) -> tuple[bool, json]: # noqa: PLR0912
183
+ def produce_from_json(self, data: dict, output_file: str = None) -> tuple[bool, dict]: # noqa: PLR0912
174
184
  """
175
185
  Produce the CycloneDX output from the raw scan results input data
176
186
 
177
187
  Args:
178
- data (json): JSON object
188
+ data (dict): JSON object
179
189
  output_file (str, optional): Output file (optional). Defaults to None.
180
190
 
181
191
  Returns:
@@ -183,9 +193,11 @@ class CycloneDx(ScanossBase):
183
193
  json: The CycloneDX output
184
194
  """
185
195
  cdx, vdx = self.parse(data)
186
- if not cdx:
196
+ if cdx is None:
187
197
  self.print_stderr('ERROR: No CycloneDX data returned for the JSON string provided.')
188
- return False, None
198
+ return False, {}
199
+ if len(cdx) == 0:
200
+ self.print_msg('Warning: Empty scan results - generating minimal CycloneDX SBOM with no components.')
189
201
  self._spdx.load_license_data() # Load SPDX license name data for later reference
190
202
  #
191
203
  # Using CDX version 1.4: https://cyclonedx.org/docs/1.4/json/
@@ -219,6 +231,8 @@ class CycloneDx(ScanossBase):
219
231
  lic_set = set()
220
232
  for lic in licenses: # Get a unique set of licenses
221
233
  lc_id = lic.get('id')
234
+ if not lc_id:
235
+ continue
222
236
  spdx_id = self._spdx.get_spdx_license_id(lc_id)
223
237
  lic_set.add(spdx_id if spdx_id else lc_id)
224
238
  for lc_id in lic_set: # Store licenses for later inclusion
@@ -285,7 +299,84 @@ class CycloneDx(ScanossBase):
285
299
  except Exception as e:
286
300
  self.print_stderr(f'ERROR: Problem parsing input JSON: {e}')
287
301
  return False
288
- return self.produce_from_json(data, output_file)
302
+ success, _ = self.produce_from_json(data, output_file)
303
+ return success
304
+
305
+ def _normalize_vulnerability_id(self, vuln: dict) -> tuple[str, str]:
306
+ """
307
+ Normalize vulnerability ID and CVE from different possible field names.
308
+ Returns tuple of (vuln_id, vuln_cve).
309
+ """
310
+ vuln_id = vuln.get('ID', '') or vuln.get('id', '')
311
+ vuln_cve = vuln.get('CVE', '') or vuln.get('cve', '')
312
+
313
+ # Skip CPE entries, use CVE if available
314
+ if vuln_id.upper().startswith('CPE:') and vuln_cve:
315
+ vuln_id = vuln_cve
316
+
317
+ return vuln_id, vuln_cve
318
+
319
+ def _create_vulnerability_entry(self, vuln_id: str, vuln: dict, vuln_cve: str, purl: str) -> dict:
320
+ """
321
+ Create a new vulnerability entry for CycloneDX format.
322
+ """
323
+ vuln_source = vuln.get('source', '').lower()
324
+ return {
325
+ 'id': vuln_id,
326
+ 'source': {
327
+ 'name': 'NVD' if vuln_source == 'nvd' else 'GitHub Advisories',
328
+ 'url': f'https://nvd.nist.gov/vuln/detail/{vuln_cve}'
329
+ if vuln_source == 'nvd'
330
+ else f'https://github.com/advisories/{vuln_id}',
331
+ },
332
+ 'ratings': [{'severity': self._sev_lookup(vuln.get('severity', 'unknown').lower())}],
333
+ 'affects': [{'ref': purl}],
334
+ }
335
+
336
+ def append_vulnerabilities(self, cdx_dict: dict, vulnerabilities_data: dict, purl: str) -> dict:
337
+ """
338
+ Append vulnerabilities to an existing CycloneDX dictionary
339
+
340
+ Args:
341
+ cdx_dict (dict): The existing CycloneDX dictionary
342
+ vulnerabilities_data (dict): The vulnerabilities data from get_vulnerabilities_json
343
+ purl (str): The PURL of the component these vulnerabilities affect
344
+
345
+ Returns:
346
+ dict: The updated CycloneDX dictionary with vulnerabilities appended
347
+ """
348
+ if not cdx_dict or not vulnerabilities_data:
349
+ return cdx_dict
350
+
351
+ if 'vulnerabilities' not in cdx_dict:
352
+ cdx_dict['vulnerabilities'] = []
353
+
354
+ # Extract vulnerabilities from the response
355
+ vulns_list = vulnerabilities_data.get('purls', [])
356
+ if not vulns_list:
357
+ return cdx_dict
358
+
359
+ vuln_items = vulns_list[0].get('vulnerabilities', [])
360
+
361
+ for vuln in vuln_items:
362
+ vuln_id, vuln_cve = self._normalize_vulnerability_id(vuln)
363
+
364
+ # Skip empty IDs or CPE-only entries
365
+ if not vuln_id or vuln_id.upper().startswith('CPE:'):
366
+ continue
367
+
368
+ # Check if vulnerability already exists
369
+ existing_vuln = next((v for v in cdx_dict['vulnerabilities'] if v.get('id') == vuln_id), None)
370
+
371
+ if existing_vuln:
372
+ # Add this PURL to the affects list if not already present
373
+ if not any(ref.get('ref') == purl for ref in existing_vuln.get('affects', [])):
374
+ existing_vuln['affects'].append({'ref': purl})
375
+ else:
376
+ # Create new vulnerability entry
377
+ cdx_dict['vulnerabilities'].append(self._create_vulnerability_entry(vuln_id, vuln, vuln_cve, purl))
378
+
379
+ return cdx_dict
289
380
 
290
381
  @staticmethod
291
382
  def _sev_lookup(value: str):
@@ -305,6 +396,47 @@ class CycloneDx(ScanossBase):
305
396
  'unknown': 'unknown',
306
397
  }.get(value, 'unknown')
307
398
 
399
+ def is_cyclonedx_json(self, json_string: str) -> bool:
400
+ """
401
+ Validate if the given JSON string is a valid CycloneDX JSON string
402
+
403
+ Args:
404
+ json_string (str): JSON string to validate
405
+ Returns:
406
+ bool: True if the JSON string is valid, False otherwise
407
+ """
408
+ try:
409
+ cdx_json_validator = JsonValidator(SchemaVersion.V1_6)
410
+ json_validation_errors = cdx_json_validator.validate_str(json_string)
411
+ if json_validation_errors:
412
+ self.print_stderr(f'ERROR: Problem parsing input JSON: {json_validation_errors}')
413
+ return False
414
+ return True
415
+ except Exception as e:
416
+ self.print_stderr(f'ERROR: Problem parsing input JSON: {e}')
417
+ return False
418
+
419
+ def get_purls_request_from_cdx(self, cdx_dict: dict, field: str = 'purls') -> dict:
420
+ """
421
+ Get the list of PURL requests (purl + requirement) from the given CDX dictionary
422
+
423
+ Args:
424
+ cdx_dict (dict): CDX dictionary to parse
425
+ field (str): Field to extract from the CDX dictionary
426
+ Returns:
427
+ list[dict]: List of PURL requests (purl + requirement)
428
+ """
429
+ components = cdx_dict.get('components', [])
430
+ parsed_purls = []
431
+ for component in components:
432
+ version = component.get('version')
433
+ if version:
434
+ parsed_purls.append({'purl': component.get('purl'), 'requirement': version})
435
+ else:
436
+ parsed_purls.append({'purl': component.get('purl')})
437
+ purl_request = {field: parsed_purls}
438
+ return purl_request
439
+
308
440
 
309
441
  #
310
442
  # End of CycloneDX Class
@@ -1 +1 @@
1
- date: 20250709092546, utime: 1752053146
1
+ date: 20260105120224, utime: 1767614544
@@ -0,0 +1,133 @@
1
+ {
2
+ "title": "OSADL Open Source License Obligations Checklist (https:\/\/www.osadl.org\/Checklists)",
3
+ "license": "Creative Commons Attribution 4.0 International license (CC-BY-4.0)",
4
+ "attribution": "A project by the Open Source Automation Development Lab (OSADL) eG. For further information about the project see the description at www.osadl.org\/checklists.",
5
+ "copyright": "(C) 2017 - 2024 Open Source Automation Development Lab (OSADL) eG and contributors, info@osadl.org",
6
+ "disclaimer": "The checklists and particularly the copyleft data have been assembled with maximum diligence and care; however, the authors do not warrant nor can be held liable in any way for its correctness, usefulness, merchantibility or fitness for a particular purpose as far as permissible by applicable law. Anyone who uses the information does this on his or her sole responsibility. For any individual legal advice, it is recommended to contact a lawyer.",
7
+ "timeformat": "%Y-%m-%dT%H:%M:%S%z",
8
+ "timestamp": "2025-10-30T11:23:00+0000",
9
+ "copyleft":
10
+ {
11
+ "0BSD": "No",
12
+ "AFL-2.0": "No",
13
+ "AFL-2.1": "No",
14
+ "AFL-3.0": "No",
15
+ "AGPL-3.0-only": "Yes",
16
+ "AGPL-3.0-or-later": "Yes",
17
+ "Apache-1.0": "No",
18
+ "Apache-1.1": "No",
19
+ "Apache-2.0": "No",
20
+ "APSL-2.0": "Yes (restricted)",
21
+ "Artistic-1.0": "No",
22
+ "Artistic-1.0-Perl": "No",
23
+ "Artistic-2.0": "No",
24
+ "Bitstream-Vera": "No",
25
+ "blessing": "No",
26
+ "BlueOak-1.0.0": "No",
27
+ "BSD-1-Clause": "No",
28
+ "BSD-2-Clause": "No",
29
+ "BSD-2-Clause-Patent": "No",
30
+ "BSD-3-Clause": "No",
31
+ "BSD-3-Clause-Open-MPI": "No",
32
+ "BSD-4-Clause": "No",
33
+ "BSD-4-Clause-UC": "No",
34
+ "BSD-4.3TAHOE": "No",
35
+ "BSD-Source-Code": "No",
36
+ "BSL-1.0": "No",
37
+ "bzip2-1.0.5": "No",
38
+ "bzip2-1.0.6": "No",
39
+ "CC-BY-2.5": "No",
40
+ "CC-BY-3.0": "No",
41
+ "CDDL-1.0": "Yes (restricted)",
42
+ "CDDL-1.1": "Yes (restricted)",
43
+ "CPL-1.0": "Yes",
44
+ "curl": "No",
45
+ "ECL-1.0": "No",
46
+ "ECL-2.0": "No",
47
+ "EFL-2.0": "No",
48
+ "EPL-1.0": "Yes",
49
+ "EPL-2.0": "Yes (restricted)",
50
+ "EUPL-1.1": "Yes",
51
+ "EUPL-1.2": "Yes",
52
+ "FSFAP": "No",
53
+ "FSFUL": "No",
54
+ "FSFULLR": "No",
55
+ "FSFULLRWD": "No",
56
+ "FTL": "No",
57
+ "GPL-1.0-only": "Yes",
58
+ "GPL-1.0-or-later": "Yes",
59
+ "GPL-2.0-only": "Yes",
60
+ "GPL-2.0-only WITH Classpath-exception-2.0": "Yes (restricted)",
61
+ "GPL-2.0-or-later": "Yes",
62
+ "GPL-3.0-only": "Yes",
63
+ "GPL-3.0-or-later": "Yes",
64
+ "HPND": "No",
65
+ "IBM-pibs": "No",
66
+ "ICU": "No",
67
+ "IJG": "No",
68
+ "ImageMagick": "No",
69
+ "Info-ZIP": "No",
70
+ "IPL-1.0": "Yes",
71
+ "ISC": "No",
72
+ "JasPer-2.0": "No",
73
+ "LGPL-2.0-only": "Yes (restricted)",
74
+ "LGPL-2.0-or-later": "Yes (restricted)",
75
+ "LGPL-2.1-only": "Yes (restricted)",
76
+ "LGPL-2.1-or-later": "Yes (restricted)",
77
+ "LGPL-3.0-only": "Yes (restricted)",
78
+ "LGPL-3.0-or-later": "Yes (restricted)",
79
+ "Libpng": "No",
80
+ "libpng-2.0": "No",
81
+ "libtiff": "No",
82
+ "LicenseRef-scancode-bsla-no-advert": "No",
83
+ "LicenseRef-scancode-info-zip-2003-05": "No",
84
+ "LicenseRef-scancode-ppp": "No",
85
+ "Minpack": "No",
86
+ "MirOS": "No",
87
+ "MIT": "No",
88
+ "MIT-0": "No",
89
+ "MIT-CMU": "No",
90
+ "MPL-1.1": "Yes (restricted)",
91
+ "MPL-2.0": "Yes (restricted)",
92
+ "MPL-2.0-no-copyleft-exception": "Yes (restricted)",
93
+ "MS-PL": "Questionable",
94
+ "MS-RL": "Yes (restricted)",
95
+ "NBPL-1.0": "No",
96
+ "NCSA": "No",
97
+ "NTP": "No",
98
+ "OFL-1.1": "Yes (restricted)",
99
+ "OGC-1.0": "No",
100
+ "OLDAP-2.8": "No",
101
+ "OpenSSL": "Questionable",
102
+ "OSL-3.0": "Yes",
103
+ "PHP-3.01": "No",
104
+ "PostgreSQL": "No",
105
+ "PSF-2.0": "No",
106
+ "Python-2.0": "No",
107
+ "Qhull": "No",
108
+ "RSA-MD": "No",
109
+ "Saxpath": "No",
110
+ "SGI-B-2.0": "No",
111
+ "Sleepycat": "Yes",
112
+ "SMLNJ": "No",
113
+ "Spencer-86": "No",
114
+ "SSH-OpenSSH": "No",
115
+ "SSH-short": "No",
116
+ "SunPro": "No",
117
+ "Ubuntu-font-1.0": "Yes (restricted)",
118
+ "Unicode-3.0": "No",
119
+ "Unicode-DFS-2015": "No",
120
+ "Unicode-DFS-2016": "No",
121
+ "Unlicense": "No",
122
+ "UPL-1.0": "No",
123
+ "W3C": "No",
124
+ "W3C-19980720": "No",
125
+ "W3C-20150513": "No",
126
+ "WTFPL": "No",
127
+ "X11": "No",
128
+ "XFree86-1.1": "No",
129
+ "Zlib": "No",
130
+ "zlib-acknowledgement": "No",
131
+ "ZPL-2.0": "No"
132
+ }
133
+ }
scanoss/delta.py ADDED
@@ -0,0 +1,197 @@
1
+ """
2
+ SPDX-License-Identifier: MIT
3
+
4
+ Copyright (c) 2025, SCANOSS
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
23
+ """
24
+ import os
25
+ import shutil
26
+ import tempfile
27
+ from typing import Optional
28
+
29
+ from .scanossbase import ScanossBase
30
+
31
+
32
+ class Delta(ScanossBase):
33
+ """
34
+ Handle delta scan operations by copying files into a dedicated delta directory.
35
+
36
+ This class manages the creation of delta directories and copying of specified files
37
+ while preserving the directory structure. Files are read from an input file where each
38
+ line contains a file path to copy.
39
+ """
40
+
41
+ def __init__( # noqa: PLR0913
42
+ self,
43
+ debug: bool = False,
44
+ trace: bool = False,
45
+ quiet: bool = False,
46
+ filepath: str = None,
47
+ folder: str = None,
48
+ output: str = None,
49
+ root_dir: str = None,
50
+ ):
51
+ """
52
+ Initialise the Delta instance.
53
+
54
+ :param debug: Enable debug logging.
55
+ :param trace: Enable trace logging.
56
+ :param quiet: Enable quiet mode (suppress non-essential output).
57
+ :param filepath: Path to an input file containing a list of files to copy.
58
+ :param folder: A target delta directory path (auto-generated if not provided).
59
+ :param output: Output file path for the delta directory location (stdout if not provided).
60
+ """
61
+ super().__init__(debug, trace, quiet)
62
+ self.filepath = filepath
63
+ self.folder = folder
64
+ self.output = output
65
+ self.root_dir = root_dir if root_dir else '.'
66
+
67
+ def copy(self, input_file: str = None):
68
+ """
69
+ Copy files listed in the input file to the delta directory.
70
+
71
+ Reads the input file line by line, where each line contains a file path.
72
+ Creates the delta directory if it doesn't exist, then copies each file
73
+ while preserving its directory structure.
74
+
75
+ :return: Tuple of (status_code, folder_path) where status_code is 0 for success,
76
+ 1 for error, and folder_path is the delta directory path
77
+ """
78
+ input_file = input_file if input_file else self.filepath
79
+ if not input_file:
80
+ self.print_stderr('ERROR: No input file specified')
81
+ return 1, ''
82
+ # Validate that an input file exists
83
+ if not os.path.isfile(input_file):
84
+ self.print_stderr(f'ERROR: Input file {input_file} does not exist or is not a file')
85
+ return 1, ''
86
+ # Load the input file and validate it contains valid file paths
87
+ files = self.load_input_file(input_file)
88
+ if files is None:
89
+ return 1, ''
90
+ # Create delta dir (folder)
91
+ delta_folder = self.create_delta_dir(self.folder, self.root_dir)
92
+ if not delta_folder:
93
+ return 1, ''
94
+ # Print delta folder location to output
95
+ self.print_to_file_or_stdout(delta_folder, self.output)
96
+ # Process each file and copy it to the delta dir
97
+ for source_file in files:
98
+ # Normalise the source path to handle ".." and redundant separators
99
+ normalised_source = os.path.normpath(source_file)
100
+ if '..' in normalised_source:
101
+ self.print_stderr(f'WARNING: Source path escapes root directory for {source_file}. Skipping.')
102
+ continue
103
+ # Resolve to the absolute path for source validation
104
+ abs_source = os.path.abspath(os.path.join(self.root_dir, normalised_source))
105
+ # Check if the source file exists and is a file
106
+ if not os.path.exists(abs_source) or not os.path.isfile(abs_source):
107
+ self.print_stderr(f'WARNING: File {source_file} does not exist or is not a file, skipping')
108
+ continue
109
+ # Use a normalised source for destination to prevent traversal
110
+ dest_path = os.path.normpath(os.path.join(self.root_dir, delta_folder, normalised_source.lstrip(os.sep)))
111
+ # Final safety check: ensure destination is within the delta folder
112
+ abs_dest = os.path.abspath(dest_path)
113
+ abs_folder = os.path.abspath(os.path.join(self.root_dir, delta_folder))
114
+ if not abs_dest.startswith(abs_folder + os.sep):
115
+ self.print_stderr(
116
+ f'WARNING: Destination path ({abs_dest}) escapes delta directory for {source_file}. Skipping.')
117
+ continue
118
+ # Create the destination directory if it doesn't exist and copy the file
119
+ try:
120
+ dest_dir = os.path.dirname(dest_path)
121
+ if dest_dir:
122
+ self.print_trace(f'Creating directory {dest_dir}...')
123
+ os.makedirs(dest_dir, exist_ok=True)
124
+ self.print_debug(f'Copying {source_file} to {dest_path} ...')
125
+ shutil.copy(abs_source, dest_path)
126
+ except (OSError, shutil.Error) as e:
127
+ self.print_stderr(f'ERROR: Failed to copy {source_file} to {dest_path}: {e}')
128
+ return 1, ''
129
+ return 0, delta_folder
130
+
131
+ def create_delta_dir(self, folder: str, root_dir: str = '.') -> str or None:
132
+ """
133
+ Create the delta directory.
134
+
135
+ If no folder is specified, creates a unique temporary directory with
136
+ a 'delta-' prefix in the current directory. If a folder is specified,
137
+ validates that it doesn't already exist before creating it.
138
+
139
+ :param root_dir: Root directory to create the delta directory in (default: current directory)
140
+ :param folder: Optional target directory
141
+ :return: Path to the delta directory, or None if it already exists or creation fails
142
+ """
143
+ if folder:
144
+ # Resolve a relative folder under root_dir so checks/creation apply to the right place
145
+ resolved = folder if os.path.isabs(folder) else os.path.join(root_dir, folder)
146
+ resolved = os.path.normpath(resolved)
147
+ # Validate the target directory doesn't already exist and create it
148
+ if os.path.exists(resolved):
149
+ self.print_stderr(f'ERROR: Folder {resolved} already exists.')
150
+ return None
151
+ else:
152
+ try:
153
+ self.print_debug(f'Creating delta directory {resolved}...')
154
+ os.makedirs(resolved)
155
+ except (OSError, IOError) as e:
156
+ self.print_stderr(f'ERROR: Failed to create directory {resolved}: {e}')
157
+ return None
158
+ else:
159
+ # Create a unique temporary directory in the given root directory
160
+ try:
161
+ self.print_debug(f'Creating temporary delta directory in {root_dir} ...')
162
+ folder = tempfile.mkdtemp(prefix="delta-", dir=root_dir)
163
+ if folder:
164
+ folder = os.path.relpath(folder, start=root_dir) # Get the relative path from root_dir
165
+ self.print_debug(f'Created temporary delta directory: {folder}')
166
+ except (OSError, IOError) as e:
167
+ self.print_stderr(f'ERROR: Failed to create temporary directory in {root_dir}: {e}')
168
+ return None
169
+ return folder
170
+
171
+ def load_input_file(self, input_file: str) -> Optional[list[str]]:
172
+ """
173
+ Loads and parses the input file line by line. Each line in the input
174
+ file represents a source file path, which will be stripped of trailing
175
+ whitespace and appended to the resulting list if it is not empty.
176
+
177
+ :param input_file: The path to the input file to be read.
178
+ :type input_file: String
179
+ :return: A list of source file paths extracted from the input file,
180
+ or None if an error occurs or the file path is invalid.
181
+ :rtype: An array list[str] or None
182
+ """
183
+ files = []
184
+ if input_file:
185
+ try:
186
+ with open(input_file, 'r', encoding='utf-8') as f:
187
+ for line in f:
188
+ source_file = line.rstrip()
189
+ if source_file:
190
+ # Save the file path without any leading separators
191
+ files.append(source_file.lstrip(os.sep))
192
+ # End of for loop
193
+ except (OSError, IOError) as e:
194
+ self.print_stderr(f'ERROR: Failed to read input file; {input_file}: {e}')
195
+ return None
196
+ self.print_debug(f'Loaded {len(files)} files from input file.')
197
+ return files
@@ -0,0 +1,23 @@
1
+ """
2
+ SPDX-License-Identifier: MIT
3
+
4
+ Copyright (c) 2025, SCANOSS
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
23
+ """