3939 from cyclonedx .model .license import License
4040
4141
42+ def _try_load (dist : 'Distribution' , metadir : str , filename : str ) -> Union [str , None ]:
43+ # Might raise NotImplementedError in theory
44+ # but nothing we can do in that case.
45+ try :
46+ candidate = dist .locate_file (join (metadir , filename ))
47+ except NotImplementedError :
48+ return None
49+
50+ if not candidate :
51+ return None
52+
53+ try :
54+ with open (str (candidate ), 'rb' ) as fin :
55+ return io2str (fin )
56+ except FileNotFoundError :
57+ pass
58+ return None
59+
60+
4261def handle_bad_license_file_encoding (
4362 dist : 'Distribution' ,
4463 lfile : str ,
4564 logger : 'Logger'
4665) -> Union [str , None ]:
47-
48- def try_load (dist : 'Distribution' , metadir : str , filename : str ) -> Union [str , None ]:
49- # Might raise NotImplementedError in theory
50- # but nothing we can do in that case.
51- try :
52- candidate = dist .locate_file (join (metadir , filename ))
53- except NotImplementedError :
54- return None
55-
56- if not candidate :
57- return None
58-
59- try :
60- with open (str (candidate ), 'rb' ) as fin :
61- return io2str (fin )
62- except FileNotFoundError :
63- pass
64- return None
65-
6666 # Distribution has no method to find the actual metadata dir,
6767 # e.g. dist-info or egg-info.
6868 # So we mimic the logic in PathDistribution and check both subdirs
6969 content : Union [str , None ] = None
7070 for metadir in ('.dist-info' , '.egg-info' ):
71- content = try_load (dist , metadir , lfile )
71+ content = _try_load (dist , metadir , lfile )
7272 if content :
7373 break
7474
@@ -91,12 +91,11 @@ def gather_license_texts(
9191 # per spec > license files are stored in the `.dist-info/licenses/` subdirectory of the produced wheel.
9292 # but in practice, other locations are used, too.
9393 # loop over the candidate location and pick the first one found.
94- locations = ('licenses' , 'license_files' , '.' )
9594 malformed = None
9695 content = None
97- for loc in locations :
96+ for loc in ('licenses' , 'license_files' , '.' ):
97+ path = join (loc , mlfile )
9898 try :
99- path = join (loc , mlfile )
10099 content = dist .read_text (path )
101100 except UnicodeDecodeError :
102101 # Malformed, stop looking
@@ -106,11 +105,11 @@ def gather_license_texts(
106105 if content is not None :
107106 break
108107
109- if content is None and malformed : # pragma: no cover
108+ if content is None and malformed :
110109 # Try a little harder
111110 content = handle_bad_license_file_encoding (dist , malformed , logger )
112111
113- if content is None : # pragme: no cover
112+ if content is None :
114113 logger .debug ('Error: failed to read license file %r for dist %r' ,
115114 mlfile , dist .metadata ['Name' ])
116115 continue
@@ -143,6 +142,5 @@ def dist2licenses(
143142 # see spec: https://peps.python.org/pep-0639/#add-license-expression-field
144143 yield lfac .make_from_string (lexp ,
145144 license_acknowledgement = lack )
146- if gather_text and (lfiles := set (str (fn ) for fn in metadata .get_all ('License-File' , ()))):
147- for lic in gather_license_texts (dist , lfiles , logger ):
148- yield lic
145+ if gather_text and (lfiles := set (fn for fn in metadata .get_all ('License-File' , ()))):
146+ yield from gather_license_texts (dist , lfiles , logger )
0 commit comments