diff --git a/gemfileparser/__init__.py b/gemfileparser/__init__.py index 16aa69a..84b82f4 100644 --- a/gemfileparser/__init__.py +++ b/gemfileparser/__init__.py @@ -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.*?)['\"]") gemfile_regexes['source'] = re.compile( - r".*source(:|[ ]?=>)[ ]*['\"](?P[a-zA-Z:\/\.-\\]+)['\"].*") + r"source:[ ]?(?P[a-zA-Z:\/\.-]+)") gemfile_regexes['git'] = re.compile( - r".*git(:|[ ]?=>)[ ]*(?P[a-zA-Z:\/\.-]+).*") + r"git:[ ]?(?P[a-zA-Z:\/\.-]+)") gemfile_regexes['platform'] = re.compile( - r".*platform(:|[ ]?=>)[ ]*(?P[a-zA-Z:\/\.-]+).*") - gemfile_regexes['platforms'] = re.compile( - r".*platforms(:|[ ]?=>)[ ]*(?P\[.*\])[,]?.*") + r"platform:[ ]?(?P[a-zA-Z:\/\.-]+)") gemfile_regexes['path'] = re.compile( - r".*path(:|[ ]?=>)[ ]*(?P.+['\"\)]).*") - gemfile_regexes['github'] = re.compile( - r".*github(:|[ ]?=>)[ ]*[\'\"](?P[a-zA-Z:\/\.-0-9]+)[\'\"].*") + r"path:[ ]?(?P[a-zA-Z:\/\.-]+)") gemfile_regexes['branch'] = re.compile( - r".*branch(:|[ ]?=>)[ ]*(?P[a-zA-Z:\/\.-]+).*") + r"branch:[ ]?(?P[a-zA-Z:\/\.-]+)") gemfile_regexes['autorequire'] = re.compile( - r".*require(:|[ ]?=>)[ ]*(?P[a-zA-Z:\/\.-]+).*") + r"require:[ ]?(?P[a-zA-Z:\/\.-]+)") gemfile_regexes['group'] = re.compile( - r".*group(:|[ ]?=>)[ ]*(?P[a-zA-Z:\/\.-]+).*") - gemfile_regexes['groups'] = re.compile( - r".*groups(:|[ ]?=>)[ ]*(?P\[.*\]),.*") + r"group:[ ]?(?P[a-zA-Z:\/\.-]+)") + gemfile_regexes['name'] = re.compile( + r"(?P[a-zA-Z]+[\.0-9a-zA-Z _-]*)") gemfile_regexes['requirement'] = re.compile( - r"gem[ ]['\"].*?['\"](?P([>|<|=|~>|\d]+[ ]*[0-9\.\w]+[ ,]*)+).*") + r"(?P([>|<|=|~>|\d]+[ ]*[0-9\.\w]+[ ,]*)+)") global_group = 'runtime' group_block_regex = re.compile( - r".*group[ ]?(:|[ ]?=>)[ ]*(?P.*?) do") + r"group[ ]?:[ ]?(?P.*?) do") add_dvtdep_regex = re.compile( r".*add_development_dependency (?P.*)") add_rundep_regex = re.compile( @@ -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.''' @@ -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 diff --git a/tests/gemfileparser_tests.py b/tests/gemfileparser_tests.py index c4f47b3..73c5d7d 100644 --- a/tests/gemfileparser_tests.py +++ b/tests/gemfileparser_tests.py @@ -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(): @@ -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()