1717# SPDX-License-Identifier: Apache-2.0
1818# Copyright (c) OWASP Foundation. All Rights Reserved.
1919import json
20+ from typing import Any , Dict
2021
2122from . import BaseParser
2223from ..model import ExternalReference , ExternalReferenceType , HashType
@@ -27,26 +28,25 @@ class PipEnvParser(BaseParser):
2728
2829 def __init__ (self , pipenv_contents : str ):
2930 super ().__init__ ()
31+
3032 pipfile_lock_contents = json .loads (pipenv_contents )
33+ pipfile_default : Dict [str , Dict [str , Any ]] = pipfile_lock_contents .get ('default' ) or {}
3134
32- for package_name in pipfile_lock_contents ['default' ].keys ():
33- package_data = pipfile_lock_contents ['default' ][package_name ]
35+ for (package_name , package_data ) in pipfile_default .items ():
3436 c = Component (
35- name = package_name , version = str (package_data ['version' ]).strip ('=' ),
37+ name = package_name ,
38+ version = str (package_data .get ('version' ) or 'unknown' ).lstrip ('=' ),
3639 )
37-
38- if 'index' in package_data .keys () and package_data ['index' ] == 'pypi' :
40+ if package_data .get ('index' ) == 'pypi' and isinstance (package_data .get ('hashes' ), list ):
3941 # Add download location with hashes stored in Pipfile.lock
40- if 'hashes' in package_data .keys ():
41- for pip_hash in package_data ['hashes' ]:
42-
43- ext_ref = ExternalReference (
44- reference_type = ExternalReferenceType .DISTRIBUTION ,
45- url = c .get_pypi_url (),
46- comment = 'Distribution available from pypi.org'
47- )
48- ext_ref .add_hash (HashType .from_composite_str (pip_hash ))
49- c .add_external_reference (ext_ref )
42+ for pip_hash in package_data ['hashes' ]:
43+ ext_ref = ExternalReference (
44+ reference_type = ExternalReferenceType .DISTRIBUTION ,
45+ url = c .get_pypi_url (),
46+ comment = 'Distribution available from pypi.org'
47+ )
48+ ext_ref .add_hash (HashType .from_composite_str (pip_hash ))
49+ c .add_external_reference (ext_ref )
5050
5151 self ._components .append (c )
5252
@@ -56,4 +56,3 @@ class PipEnvFileParser(PipEnvParser):
5656 def __init__ (self , pipenv_lock_filename : str ):
5757 with open (pipenv_lock_filename ) as r :
5858 super (PipEnvFileParser , self ).__init__ (pipenv_contents = r .read ())
59- r .close ()
0 commit comments