Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 44 additions & 60 deletions gemfileparser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,61 +25,41 @@


class GemfileParser(object):

'''Creates a GemfileParser object to perform operations. '''

class Dependency(object):

''' A class to hold information about a dependency gem.'''

def __init__(self):
self.name = None
self.name = ''
self.requirement = []
self.autorequire = None
self.source = None
self.autorequire = ''
self.source = ''
self.parent = []
self.group = None
self.platform = None
self.platforms = []
self.groups = []

def __str__(self):
attributes = self.__dict__
output = {}
for key, value in attributes.items():
if value is None or value == []:
next
else:
output[key] = value
return str(output)
self.group = ''

gemfile_regexes = collections.OrderedDict()
gemfile_regexes['name'] = re.compile(r"gem ['\"](?P<name>.*?)['\"]")
gemfile_regexes['source'] = re.compile(
r".*source(:|[ ]?=>)[ ]*['\"](?P<source>[a-zA-Z:\/\.-\\]+)['\"].*")
r"source:[ ]?(?P<source>[a-zA-Z:\/\.-]+)")
gemfile_regexes['git'] = re.compile(
r".*git(:|[ ]?=>)[ ]*(?P<git>[a-zA-Z:\/\.-]+).*")
r"git:[ ]?(?P<git>[a-zA-Z:\/\.-]+)")
gemfile_regexes['platform'] = re.compile(
r".*platform(:|[ ]?=>)[ ]*(?P<platform>[a-zA-Z:\/\.-]+).*")
gemfile_regexes['platforms'] = re.compile(
r".*platforms(:|[ ]?=>)[ ]*(?P<platforms>\[.*\])[,]?.*")
r"platform:[ ]?(?P<platform>[a-zA-Z:\/\.-]+)")
gemfile_regexes['path'] = re.compile(
r".*path(:|[ ]?=>)[ ]*(?P<path>.+['\"\)]).*")
gemfile_regexes['github'] = re.compile(
r".*github(:|[ ]?=>)[ ]*[\'\"](?P<github>[a-zA-Z:\/\.-0-9]+)[\'\"].*")
r"path:[ ]?(?P<path>[a-zA-Z:\/\.-]+)")
gemfile_regexes['branch'] = re.compile(
r".*branch(:|[ ]?=>)[ ]*(?P<branch>[a-zA-Z:\/\.-]+).*")
r"branch:[ ]?(?P<branch>[a-zA-Z:\/\.-]+)")
gemfile_regexes['autorequire'] = re.compile(
r".*require(:|[ ]?=>)[ ]*(?P<autorequire>[a-zA-Z:\/\.-]+).*")
r"require:[ ]?(?P<autorequire>[a-zA-Z:\/\.-]+)")
gemfile_regexes['group'] = re.compile(
r".*group(:|[ ]?=>)[ ]*(?P<group>[a-zA-Z:\/\.-]+).*")
gemfile_regexes['groups'] = re.compile(
r".*groups(:|[ ]?=>)[ ]*(?P<groups>\[.*\]),.*")
r"group:[ ]?(?P<group>[a-zA-Z:\/\.-]+)")
gemfile_regexes['name'] = re.compile(
r"(?P<name>[a-zA-Z]+[\.0-9a-zA-Z _-]*)")
gemfile_regexes['requirement'] = re.compile(
r"gem[ ]['\"].*?['\"](?P<requirement>([>|<|=|~>|\d]+[ ]*[0-9\.\w]+[ ,]*)+).*")
r"(?P<requirement>([>|<|=|~>|\d]+[ ]*[0-9\.\w]+[ ,]*)+)")
global_group = 'runtime'
group_block_regex = re.compile(
r".*group[ ]?(:|[ ]?=>)[ ]*(?P<groupblock>.*?) do")
r"group[ ]?:[ ]?(?P<groupblock>.*?) do")
add_dvtdep_regex = re.compile(
r".*add_development_dependency (?P<line>.*)")
add_rundep_regex = re.compile(
Expand Down Expand Up @@ -124,26 +104,33 @@ def parse_line(self, line):
line = unicode(line)
except NameError:
pass
dep = self.Dependency()
dep.group = GemfileParser.global_group
if not self.appname:
dep.parent = []
else:
linefile = io.StringIO(line) # csv requires a file object
for line in csv.reader(linefile, delimiter=','):
column_list = []
for column in line:
stripped_column = column.replace("'", "")
stripped_column = stripped_column.replace('"', "")
stripped_column = stripped_column.strip()
column_list.append(stripped_column)
dep = self.Dependency()
dep.group = GemfileParser.global_group
dep.parent.append(self.appname)
# Check for a match in each regex and assign to
# corresponding variables
for criteria in GemfileParser.gemfile_regexes:
criteria_regex = GemfileParser.gemfile_regexes[criteria]
match = criteria_regex.match(line)
if match:
if criteria == 'requirement':
dep.requirement.append(match.group(criteria))
else:
setattr(dep, criteria, match.group(criteria))
if dep.group in self.dependencies:
self.dependencies[dep.group].append(dep)
else:
self.dependencies[dep.group] = [dep]
for column in column_list:
# Check for a match in each regex and assign to
# corresponding variables
for criteria in GemfileParser.gemfile_regexes:
criteria_regex = GemfileParser.gemfile_regexes[criteria]
match = criteria_regex.match(column)
if match:
if criteria == 'requirement':
dep.requirement.append(match.group(criteria))
else:
setattr(dep, criteria, match.group(criteria))
break
if dep.group in self.dependencies:
self.dependencies[dep.group].append(dep)
else:
self.dependencies[dep.group] = [dep]

def parse_gemfile(self, path=''):
'''Parses a Gemfile and returns a dict of categorized dependencies.'''
Expand All @@ -169,13 +156,10 @@ def parse_gemfile(self, path=''):
if len(gemspec_list) > 1:
print("Multiple gemspec files found")
continue
elif len(gemspec_list) < 1:
print("No gemspec file found. Ignoring the gemspec call")
else:
gemspec_file = gemspec_list[0]
self.parse_gemspec(
path=os.path.join(gemfiledir, gemspec_file))
gemspec_file = gemspec_list[0]
self.parse_gemspec(path=os.path.join(gemfiledir, gemspec_file))
elif line.startswith('gem '):
line = line[3:]
self.parse_line(line)
return self.dependencies

Expand Down
15 changes: 4 additions & 11 deletions tests/gemfileparser_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ def test_name():
def test_requirement():
gemparser = GemfileParser('tests/Gemfile_2')
dependencies = gemparser.parse()
assert_equal(dependencies['runtime'][0].requirement, [u'4.2.4'])
assert_equal(dependencies['runtime'][0].requirement, '4.2.4')


def test_group():
gemparser = GemfileParser('tests/Gemfile_3')
dependencies = gemparser.parse()
assert_equal(dependencies['development'][0].requirement, [u'4.2.4'])
assert_equal(dependencies['development'][0].requirement, '4.2.4')


def test_group_block():
gemparser = GemfileParser('tests/Gemfile_2')
dependencies = gemparser.parse()
assert_equal(dependencies['development'][0].requirement, [u'3.0.0'])
assert_equal(dependencies['runtime'][0].requirement, [u'4.2.4'])
assert_equal(dependencies['development'][0].requirement, '3.0.0')
assert_equal(dependencies['runtime'][0].requirement, '4.2.4')


def test_source():
Expand All @@ -48,13 +48,6 @@ def test_source():
'http://www.example.com')


def test_platforms():
gemparser = GemfileParser('tests/Gemfile_5')
dependencies = gemparser.parse()
assert_equal(dependencies['development'][0].platforms,
u'[:mri, :mingw, :x64_mingw]')


def test_gemspec():
gemparser = GemfileParser('tests/Gemfile_2')
dependencies = gemparser.parse()
Expand Down