From 247deaf1404fef4123d4a9c1d86d0048199d56b9 Mon Sep 17 00:00:00 2001 From: rpotter12 Date: Wed, 22 Jul 2020 18:27:30 +0530 Subject: [PATCH 1/2] even commits with original repository --- bin/parsegemfile | 2 +- gemfileparser/__init__.py | 106 ++++++++++++++++---------------------- setup.cfg | 2 +- setup.py | 22 +------- 4 files changed, 48 insertions(+), 84 deletions(-) diff --git a/bin/parsegemfile b/bin/parsegemfile index 6fc82f4..0f11e39 100755 --- a/bin/parsegemfile +++ b/bin/parsegemfile @@ -14,4 +14,4 @@ output = parsed.parse() for key, value in list(output.items()): print(key, ":") for item in value: - print("\t", item) + print("\t", item) \ No newline at end of file diff --git a/gemfileparser/__init__.py b/gemfileparser/__init__.py index 16aa69a..049bf9a 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 @@ -206,4 +190,4 @@ def parse(self): if self.gemspec: return self.parse_gemspec() else: - return self.parse_gemfile() + return self.parse_gemfile() \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index b88034e..224a779 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,2 +1,2 @@ [metadata] -description-file = README.md +description-file = README.md \ No newline at end of file diff --git a/setup.py b/setup.py index db91450..0b6c373 100644 --- a/setup.py +++ b/setup.py @@ -15,52 +15,37 @@ 'long_description': ''' Installation ~~~~~~~~~~~~ - | If using pip, use the command ``sudo pip install gemfileparser`` | Else use the following commands - :: - git clone https://github.com/balasankarc/gemfileparser.git cd gemfileparser python setup.py install - Usage ~~~~~ - :: - from gemfileparser import GemfileParser parser = GemfileParser(, ) dependency_dictionary = parser.parse() - The parse() method returns a dict object of the following format - :: - { 'development': [list of dependency objects inside group 'development'], 'runtime': [list of runtime dependency objects], . . .} - Each dependency object contains the following attributes - :: - name - Name of the gem requirement - Version requirement autorequire - Autorequire value source - Source URL of the gem parent - Dependency of which gem group - Group in which gem is a member of (default : runtime) - Example ^^^^^^^ - :: - from gemfileparser import GemfileParser n = GemfileParser('Gemfile', 'diaspora') deps = n.parse() @@ -69,17 +54,12 @@ print key for dependency in deps[key]: print "\t", dependency - Copyright ~~~~~~~~~ - 2015-2018 Balasankar C balasankarc@autistici.org - License ~~~~~~~ - gemfileparser is released under two licenses: `GNU GPL version 3 (or above) License` and `MIT License`_. - .. _GNU GPL version 3 (or above) License: http://www.gnu.org/licenses/gpl ''', 'install_requires': ['nose'], @@ -96,4 +76,4 @@ 'License :: OSI Approved :: MIT', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3.4', - ], **config) + ], **config) \ No newline at end of file From b693bb2e2dc4238b168041934f6118c5703bc7ea Mon Sep 17 00:00:00 2001 From: rpotter12 Date: Wed, 22 Jul 2020 18:32:39 +0530 Subject: [PATCH 2/2] even changes as original repository --- bin/parsegemfile | 2 +- gemfileparser/__init__.py | 2 +- setup.cfg | 2 +- setup.py | 22 +++++++++++++++++++++- tests/gemfileparser_tests.py | 15 ++++----------- 5 files changed, 28 insertions(+), 15 deletions(-) diff --git a/bin/parsegemfile b/bin/parsegemfile index 0f11e39..6fc82f4 100755 --- a/bin/parsegemfile +++ b/bin/parsegemfile @@ -14,4 +14,4 @@ output = parsed.parse() for key, value in list(output.items()): print(key, ":") for item in value: - print("\t", item) \ No newline at end of file + print("\t", item) diff --git a/gemfileparser/__init__.py b/gemfileparser/__init__.py index 049bf9a..84b82f4 100644 --- a/gemfileparser/__init__.py +++ b/gemfileparser/__init__.py @@ -190,4 +190,4 @@ def parse(self): if self.gemspec: return self.parse_gemspec() else: - return self.parse_gemfile() \ No newline at end of file + return self.parse_gemfile() diff --git a/setup.cfg b/setup.cfg index 224a779..b88034e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,2 +1,2 @@ [metadata] -description-file = README.md \ No newline at end of file +description-file = README.md diff --git a/setup.py b/setup.py index 0b6c373..db91450 100644 --- a/setup.py +++ b/setup.py @@ -15,37 +15,52 @@ 'long_description': ''' Installation ~~~~~~~~~~~~ + | If using pip, use the command ``sudo pip install gemfileparser`` | Else use the following commands + :: + git clone https://github.com/balasankarc/gemfileparser.git cd gemfileparser python setup.py install + Usage ~~~~~ + :: + from gemfileparser import GemfileParser parser = GemfileParser(, ) dependency_dictionary = parser.parse() + The parse() method returns a dict object of the following format + :: + { 'development': [list of dependency objects inside group 'development'], 'runtime': [list of runtime dependency objects], . . .} + Each dependency object contains the following attributes + :: + name - Name of the gem requirement - Version requirement autorequire - Autorequire value source - Source URL of the gem parent - Dependency of which gem group - Group in which gem is a member of (default : runtime) + Example ^^^^^^^ + :: + from gemfileparser import GemfileParser n = GemfileParser('Gemfile', 'diaspora') deps = n.parse() @@ -54,12 +69,17 @@ print key for dependency in deps[key]: print "\t", dependency + Copyright ~~~~~~~~~ + 2015-2018 Balasankar C balasankarc@autistici.org + License ~~~~~~~ + gemfileparser is released under two licenses: `GNU GPL version 3 (or above) License` and `MIT License`_. + .. _GNU GPL version 3 (or above) License: http://www.gnu.org/licenses/gpl ''', 'install_requires': ['nose'], @@ -76,4 +96,4 @@ 'License :: OSI Approved :: MIT', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3.4', - ], **config) \ No newline at end of file + ], **config) 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()