From 6edab47b45202bbd6bbadc05eca367bc134c55fd Mon Sep 17 00:00:00 2001 From: "Aleksandar N. Kostadinov" Date: Fri, 30 Jun 2023 19:38:29 +0300 Subject: [PATCH] support parsing BUNDLED WITH Signed-off-by: Aleksandar N. Kostadinov --- src/packagedcode/gemfile_lock.py | 15 +++++++++++++++ .../data/gemfile_lock/bundled/Gemfile.lock | 2 ++ .../bundled/Gemfile.lock.expected.json | 1 + tests/packagedcode/test_gemfile_lock.py | 8 ++++++++ 4 files changed, 26 insertions(+) create mode 100644 tests/packagedcode/data/gemfile_lock/bundled/Gemfile.lock create mode 100644 tests/packagedcode/data/gemfile_lock/bundled/Gemfile.lock.expected.json diff --git a/src/packagedcode/gemfile_lock.py b/src/packagedcode/gemfile_lock.py index 66dff924569..0639a475bc5 100644 --- a/src/packagedcode/gemfile_lock.py +++ b/src/packagedcode/gemfile_lock.py @@ -109,6 +109,7 @@ def logger_debug(*args): SVN = 'SVN' GEM = 'GEM' PLATFORMS = 'PLATFORMS' +BUNDLED = 'BUNDLED WITH' DEPENDENCIES = 'DEPENDENCIES' SPECS = ' specs:' @@ -340,6 +341,7 @@ def get_option(s): '$' % locals()).match PLATS = re.compile('^ (?P.*)$').match +BUNDLED_WITH = re.compile('^\s+(?P(?:\d+.)+\d+)\s*$').match class GemfileLockParser: @@ -358,6 +360,7 @@ def __init__(self, lockfile): self.STATES = { DEPENDENCIES: self.parse_dependency, PLATFORMS: self.parse_platform, + BUNDLED: self.parse_bundler_version, GIT: self.parse_options, PATH: self.parse_options, SVN: self.parse_options, @@ -376,6 +379,8 @@ def __init__(self, lockfile): self.platforms = [] + self.bundled_with = None + # init parsing state self.reset_state() @@ -536,6 +541,16 @@ def parse_platform(self, line): plat = plat.group('platform') self.platforms.append(plat.strip()) + def parse_bundler_version(self, line): + version = BUNDLED_WITH(line) + if not version: + if TRACE: + logger_debug('ERROR: parse_bundler_version: ' + 'line not matched: %(line)r' % locals()) + return + version = version.group('version') + self.bundled_with = version + def flatten(self): """ Return the Gems dependency_tree as a sorted list of unique diff --git a/tests/packagedcode/data/gemfile_lock/bundled/Gemfile.lock b/tests/packagedcode/data/gemfile_lock/bundled/Gemfile.lock new file mode 100644 index 00000000000..f7e043cca05 --- /dev/null +++ b/tests/packagedcode/data/gemfile_lock/bundled/Gemfile.lock @@ -0,0 +1,2 @@ +BUNDLED WITH + 2.0.1 diff --git a/tests/packagedcode/data/gemfile_lock/bundled/Gemfile.lock.expected.json b/tests/packagedcode/data/gemfile_lock/bundled/Gemfile.lock.expected.json new file mode 100644 index 00000000000..0637a088a01 --- /dev/null +++ b/tests/packagedcode/data/gemfile_lock/bundled/Gemfile.lock.expected.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/tests/packagedcode/test_gemfile_lock.py b/tests/packagedcode/test_gemfile_lock.py index 3e1405c39f6..f3eb9e336e7 100644 --- a/tests/packagedcode/test_gemfile_lock.py +++ b/tests/packagedcode/test_gemfile_lock.py @@ -372,6 +372,14 @@ def test_GemfileLockParser_can_parse_platform(self): expected_loc = 'gemfile_lock/platform/Gemfile.lock.expected.json' self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES) + def test_GemfileLockParser_can_parse_bundled(self): + test_file = 'gemfile_lock/bundled/Gemfile.lock' + expected_loc = 'gemfile_lock/bundled/Gemfile.lock.expected.json' + self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES) + + gfl = gemfile_lock.GemfileLockParser(self.get_test_loc(test_file)) + assert gfl.bundled_with == "2.0.1" + def test_GemfileLockParser_can_parse_spec_single_level(self): test_file = 'gemfile_lock/spec/Gemfile.lock1' expected_loc = 'gemfile_lock/spec/Gemfile.lock1.expected.json'