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

Class GemfileLockParser

src/packagedcode/gemfile_lock.py:347–563  ·  view source on GitHub ↗

Parse a Gemfile.lock. Code originally derived from Bundler's /bundler/lib/bundler/lockfile_parser.rb parser The parsing use a simple state machine, switching states based on sections headings. The result is a tree of Gems objects stored in self.dependencies.

Source from the content-addressed store, hash-verified

345
346
347class GemfileLockParser:
348 """
349 Parse a Gemfile.lock. Code originally derived from Bundler's
350 /bundler/lib/bundler/lockfile_parser.rb parser
351
352 The parsing use a simple state machine, switching states based on sections
353 headings. The result is a tree of Gems objects stored in
354 self.dependencies.
355 """
356
357 def __init__(self, lockfile):
358 self.lockfile = lockfile
359 # map of a line start string to the next parsing state function
360 self.STATES = {
361 DEPENDENCIES: self.parse_dependency,
362 PLATFORMS: self.parse_platform,
363 BUNDLED: self.parse_bundler_version,
364 GIT: self.parse_options,
365 PATH: self.parse_options,
366 SVN: self.parse_options,
367 GEM: self.parse_options,
368 SPECS: self.parse_spec
369 }
370
371 # the final tree of dependencies, keyed by name
372 self.dependency_tree = {}
373
374 # the package that the gemfile.lock is for
375 self.primary_gem = None
376
377 # a flat dict of all gems, keyed by name
378 self.all_gems = {}
379
380 self.platforms = []
381
382 self.bundled_with = None
383
384 # init parsing state
385 self.reset_state()
386
387 # parse proper
388 for line in analysis.unicode_text_lines(lockfile):
389 line = line.rstrip()
390
391 # reset state
392 if not line:
393 self.reset_state()
394 continue
395
396 # switch to new state
397 if line in self.STATES:
398 if line in GEM_TYPES:
399 self.current_type = line
400 self.state = self.STATES[line]
401 continue
402
403 # process state
404 if self.state:

Callers 1

parseMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected