@@ -21,12 +21,12 @@ class Dependency(object):
2121 """
2222
2323 def __init__ (self ):
24- self .name = ''
24+ self .name = ""
2525 self .requirement = []
26- self .autorequire = ''
27- self .source = ''
26+ self .autorequire = ""
27+ self .source = ""
2828 self .parent = []
29- self .group = ''
29+ self .group = ""
3030
3131 def to_dict (self ):
3232 return dict (
@@ -43,41 +43,44 @@ class GemfileParser(object):
4343 """
4444 Create a GemfileParser object to perform operations.
4545 """
46+
4647 gemfile_regexes = collections .OrderedDict ()
47- gemfile_regexes ['source' ] = re .compile (r"source:[ ]?(?P<source>[a-zA-Z:\/\.-]+)" )
48- gemfile_regexes ['git' ] = re .compile (r"git:[ ]?(?P<git>[a-zA-Z:\/\.-]+)" )
49- gemfile_regexes ['platform' ] = re .compile (r"platform:[ ]?(?P<platform>[a-zA-Z:\/\.-]+)" )
50- gemfile_regexes ['path' ] = re .compile (r"path:[ ]?(?P<path>[a-zA-Z:\/\.-]+)" )
51- gemfile_regexes ['branch' ] = re .compile (r"branch:[ ]?(?P<branch>[a-zA-Z:\/\.-]+)" )
52- gemfile_regexes ['autorequire' ] = re .compile (r"require:[ ]?(?P<autorequire>[a-zA-Z:\/\.-]+)" )
53- gemfile_regexes ['group' ] = re .compile (r"group:[ ]?(?P<group>[a-zA-Z:\/\.-]+)" )
54- gemfile_regexes ['name' ] = re .compile (r"(?P<name>[a-zA-Z]+[\.0-9a-zA-Z _-]*)" )
55- gemfile_regexes ['requirement' ] = re .compile (r"(?P<requirement>([>|<|=|~>|\d]+[ ]*[0-9\.\w]+[ ,]*)+)" )
48+ gemfile_regexes ["source" ] = re .compile (r"source:[ ]?(?P<source>[a-zA-Z:\/\.-]+)" )
49+ gemfile_regexes ["git" ] = re .compile (r"git:[ ]?(?P<git>[a-zA-Z:\/\.-]+)" )
50+ gemfile_regexes ["platform" ] = re .compile (r"platform:[ ]?(?P<platform>[a-zA-Z:\/\.-]+)" )
51+ gemfile_regexes ["path" ] = re .compile (r"path:[ ]?(?P<path>[a-zA-Z:\/\.-]+)" )
52+ gemfile_regexes ["branch" ] = re .compile (r"branch:[ ]?(?P<branch>[a-zA-Z:\/\.-]+)" )
53+ gemfile_regexes ["autorequire" ] = re .compile (r"require:[ ]?(?P<autorequire>[a-zA-Z:\/\.-]+)" )
54+ gemfile_regexes ["group" ] = re .compile (r"group:[ ]?(?P<group>[a-zA-Z:\/\.-]+)" )
55+ gemfile_regexes ["name" ] = re .compile (r"(?P<name>[a-zA-Z]+[\.0-9a-zA-Z _-]*)" )
56+ gemfile_regexes ["requirement" ] = re .compile (
57+ r"(?P<requirement>([>|<|=|~>|\d]+[ ]*[0-9\.\w]+[ ,]*)+)"
58+ )
5659
5760 group_block_regex = re .compile (r"group[ ]?:[ ]?(?P<groupblock>.*?) do" )
5861
5962 gemspec_add_dvtdep_regex = re .compile (r".*add_development_dependency(?P<line>.*)" )
6063 gemspec_add_rundep_regex = re .compile (r".*add_runtime_dependency(?P<line>.*)" )
6164 gemspec_add_dep_regex = re .compile (r".*add_dependency(?P<line>.*)" )
6265
63- def __init__ (self , filepath , appname = '' ):
66+ def __init__ (self , filepath , appname = "" ):
6467 self .filepath = filepath
6568
66- self .current_group = ' runtime'
69+ self .current_group = " runtime"
6770
6871 self .appname = appname
6972 self .dependencies = {
70- ' development' : [],
71- ' runtime' : [],
72- ' dependency' : [],
73- ' test' : [],
74- ' production' : [],
75- ' metrics' : [],
73+ " development" : [],
74+ " runtime" : [],
75+ " dependency" : [],
76+ " test" : [],
77+ " production" : [],
78+ " metrics" : [],
7679 }
7780 with open (filepath ) as gf :
7881 self .contents = gf .readlines ()
7982
80- self .gemspec = filepath .endswith ((' .gemspec' , ' .podspec' ))
83+ self .gemspec = filepath .endswith ((" .gemspec" , " .podspec" ))
8184
8285 @staticmethod
8386 def preprocess (line ):
@@ -86,7 +89,7 @@ def preprocess(line):
8689 """
8790
8891 if "#" in line :
89- line = line [:line .index ('#' )]
92+ line = line [: line .index ("#" )]
9093 line = line .strip ()
9194 return line
9295
@@ -97,7 +100,7 @@ def parse_line(self, line):
97100
98101 # csv requires a file-like object
99102 linefile = io .StringIO (line )
100- for line in csv .reader (linefile , delimiter = ',' ):
103+ for line in csv .reader (linefile , delimiter = "," ):
101104 column_list = []
102105 for column in line :
103106 stripped_column = (
@@ -122,7 +125,7 @@ def parse_line(self, line):
122125 for criteria , criteria_regex in GemfileParser .gemfile_regexes .items ():
123126 match = criteria_regex .match (column )
124127 if match :
125- if criteria == ' requirement' :
128+ if criteria == " requirement" :
126129 dep .requirement .append (match .group (criteria ))
127130 else :
128131 setattr (dep , criteria , match .group (criteria ))
@@ -139,18 +142,18 @@ def parse_gemfile(self):
139142
140143 for line in self .contents :
141144 line = self .preprocess (line )
142- if line == '' or line .startswith (' source' ):
145+ if line == "" or line .startswith (" source" ):
143146 continue
144147
145- elif line .startswith (' group' ):
148+ elif line .startswith (" group" ):
146149 match = self .group_block_regex .match (line )
147150 if match :
148- self .current_group = match .group (' groupblock' )
151+ self .current_group = match .group (" groupblock" )
149152
150- elif line .startswith (' end' ):
151- self .current_group = ' runtime'
153+ elif line .startswith (" end" ):
154+ self .current_group = " runtime"
152155
153- elif line .startswith (' gemspec' ):
156+ elif line .startswith (" gemspec" ):
154157 # Gemfile contains a call to gemspec
155158 gemfiledir = os .path .dirname (self .filepath )
156159 gemspec_list = glob .glob (os .path .join (gemfiledir , "*.gemspec" ))
@@ -160,7 +163,7 @@ def parse_gemfile(self):
160163 gemspec_file = gemspec_list [0 ]
161164 self .parse_gemspec (path = os .path .join (gemfiledir , gemspec_file ))
162165
163- elif line .startswith (' gem ' ):
166+ elif line .startswith (" gem " ):
164167 line = line [3 :]
165168 self .parse_line (line )
166169
@@ -176,17 +179,17 @@ def parse_gemspec(self, path=None):
176179 line = self .preprocess (line )
177180 match = self .gemspec_add_dvtdep_regex .match (line )
178181 if match :
179- self .current_group = ' development'
182+ self .current_group = " development"
180183 else :
181184 match = self .gemspec_add_rundep_regex .match (line )
182185 if match :
183- self .current_group = ' runtime'
186+ self .current_group = " runtime"
184187 else :
185188 match = self .gemspec_add_dep_regex .match (line )
186189 if match :
187- self .current_group = ' dependency'
190+ self .current_group = " dependency"
188191 if match :
189- line = match .group (' line' )
192+ line = match .group (" line" )
190193 self .parse_line (line )
191194 return self .dependencies
192195
@@ -205,6 +208,7 @@ def command_line():
205208 A minimal command line entry point.
206209 """
207210 import sys
211+
208212 if len (sys .argv ) < 2 :
209213 print ("Usage : parsegemfile <input file>" )
210214 sys .exit (0 )
0 commit comments