17
17
# SPDX-License-Identifier: Apache-2.0
18
18
# Copyright (c) OWASP Foundation. All Rights Reserved.
19
19
import json
20
+ from typing import Any , Dict
20
21
21
22
from . import BaseParser
22
23
from ..model import ExternalReference , ExternalReferenceType , HashType
@@ -27,26 +28,25 @@ class PipEnvParser(BaseParser):
27
28
28
29
def __init__ (self , pipenv_contents : str ):
29
30
super ().__init__ ()
31
+
30
32
pipfile_lock_contents = json .loads (pipenv_contents )
33
+ pipfile_default : Dict [str , Dict [str , Any ]] = pipfile_lock_contents .get ('default' ) or {}
31
34
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 ():
34
36
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 ('=' ),
36
39
)
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 ):
39
41
# 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 )
50
50
51
51
self ._components .append (c )
52
52
@@ -56,4 +56,3 @@ class PipEnvFileParser(PipEnvParser):
56
56
def __init__ (self , pipenv_lock_filename : str ):
57
57
with open (pipenv_lock_filename ) as r :
58
58
super (PipEnvFileParser , self ).__init__ (pipenv_contents = r .read ())
59
- r .close ()
0 commit comments