MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / parse

Method parse

src/packagedcode/npm.py:994–1158  ·  view source on GitHub ↗

Parse a classic yarn.lock format which looks like this: "@babel/core@^7.1.0", "@babel/core@^7.3.4": version "7.3.4" resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.3.4.tgz#921a5a13746c21e32445bf0798680e9d11a6530b" integrity s

(cls, location, package_only=False)

Source from the content-addressed store, hash-verified

992
993 @classmethod
994 def parse(cls, location, package_only=False):
995 """
996 Parse a classic yarn.lock format which looks like this:
997 "@babel/core@^7.1.0", "@babel/core@^7.3.4":
998 version "7.3.4"
999 resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.3.4.tgz#921a5a13746c21e32445bf0798680e9d11a6530b"
1000 integrity sha512-jRsuseXBo9pN197KnDwhhaaBzyZr2oIcLHHTt2oDdQrej5Qp57dCCJafWx5ivU8/alEYDpssYqv1MUqcxwQlrA==
1001 dependencies:
1002 "@babel/code-frame" "^7.0.0"
1003 "@babel/generator" "^7.3.4"
1004
1005 Yield a single PackageData
1006 """
1007 with io.open(location, encoding='utf-8') as yl:
1008 yl_dependencies = yl.read().split('\n\n')
1009
1010 dependencies_by_purl = {}
1011 for yl_dependency in yl_dependencies:
1012 lines = yl_dependency.splitlines(False)
1013 if all(l.startswith('#') or not l.strip() for l in lines):
1014 # header or empty blocks are all comments or empties
1015 continue
1016
1017 top_requirements = []
1018 dependency_data = {}
1019 sub_dependencies = []
1020
1021 for line in lines:
1022 stripped = line.strip()
1023 comment = line.startswith('#')
1024 if not stripped or comment:
1025 continue
1026
1027 if line.startswith(' ' * 4):
1028 # "@babel/code-frame" "^7.0.0"
1029 # hosted-git-info "^2.1.4"
1030 # semver "2 || 3 || 4 || 5"
1031 ns_name, _, constraint = stripped.partition(' ')
1032 if '"' in ns_name:
1033 ns_name = ns_name.replace('"', '')
1034 ns, _ , name = ns_name.rpartition('/')
1035
1036 if ':' in constraint and '@' in constraint:
1037 # dependencies with requirements like this are aliases and should be reported
1038 aliased_package, _, constraint = constraint.rpartition('@')
1039 _, _, aliased_package_name = aliased_package.rpartition(':')
1040 ns, _ , name = aliased_package_name.rpartition('/')
1041
1042 sub_dependencies.append((ns, name, constraint,))
1043
1044 elif line.startswith(' ' * 2):
1045 # version "7.3.4"
1046 # resolved "https://registry.yarnpkg.com/@babel...."
1047 # integrity sha512-jRsuseXBo9
1048 key, _, value = stripped.partition(' ')
1049 value = value.strip().strip("\"'")
1050 key = key.strip()
1051 if key != 'dependencies':

Callers

nothing calls this directly

Calls 15

get_checksum_and_urlFunction · 0.85
get_algo_hexsumFunction · 0.85
from_dataMethod · 0.80
logger_debugFunction · 0.70
get_urlsFunction · 0.70
getMethod · 0.65
openMethod · 0.45
splitMethod · 0.45
readMethod · 0.45
replaceMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected