forked from gemfileparser/gemfileparser
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
222 lines (203 loc) · 7.96 KB
/
Copy path__init__.py
File metadata and controls
222 lines (203 loc) · 7.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python
#
# Copyright 2015 Balasankar C <[email protected]>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# .
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# .
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Python library to parse Ruby's Gemfiles and gemspec files.
"""
import csv
import io
import re
import os
import glob
import collections
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 = ''
self.requirement = []
self.autorequire = ''
self.source = ''
self.parent = []
self.group = ''
gemfile_regexes = collections.OrderedDict()
gemfile_regexes['source'] = re.compile(
r'source:[ ]?(?P<source>[a-zA-Z:\/\.-]+)')
gemfile_regexes['git'] = re.compile(
r'git:[ ]?(?P<git>[a-zA-Z:\/\.-]+)')
gemfile_regexes['platform'] = re.compile(
r'platform:[ ]?(?P<platform>[a-zA-Z:\/\.-]+)')
gemfile_regexes['path'] = re.compile(
r'path:[ ]?(?P<path>[a-zA-Z:\/\.-]+)')
gemfile_regexes['branch'] = re.compile(
r'branch:[ ]?(?P<branch>[a-zA-Z:\/\.-]+)')
gemfile_regexes['autorequire'] = re.compile(
r'require:[ ]?(?P<autorequire>[a-zA-Z:\/\.-]+)')
gemfile_regexes['group'] = re.compile(
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'(?P<requirement>([>|<|=|~>|\d]+[ ]*[0-9\.\w]+[ ,]*)+)')
global_group = 'runtime'
group_block_regex = re.compile(
r'group[ ]?:[ ]?(?P<groupblock>.*?) do')
add_dvtdep_regex = re.compile(
r'.*add_development_dependency(?P<line>.*)')
add_rundep_regex = re.compile(
r'.*add_runtime_dependency(?P<line>.*)')
add_dep_regex = re.compile(
r'.*dependency(?P<line>.*)')
def __init__(self, filepath, appname=''):
self.filepath = filepath # Required when calls to gemspec occurs
self.gemfile = open(filepath)
self.appname = appname
self.dependencies = {
'development': [],
'runtime': [],
'dependency': [],
'test': [],
'production': [],
'metrics': []
}
self.contents = self.gemfile.readlines()
path = ('gemspec', 'podspec')
if filepath.endswith(path):
self.gemspec = True
else:
self.gemspec = False
@staticmethod
def preprocess(line):
"""
Return line after removing comment portion and excess spaces.
"""
if '#' in line:
line = line[:line.index('#')]
line = line.strip()
return line
def parse_line(self, line):
"""
Parses each line and creates dependency objects accordingly.
"""
try:
# StringIO requires a unicode object.
# But unicode() doesn't work with Python3
# as it is already in unicode format.
# So, first try converting and if that fails, use original.
line = unicode(line)
except NameError:
pass
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.replace('%q<', '')
stripped_column = stripped_column.replace('(', '')
stripped_column = stripped_column.replace(')', '')
stripped_column = stripped_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)
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=''):
"""
Return dependencies after parsing gemfile.
"""
if path == '':
contents = self.contents
else:
contents = open(path).readlines()
for line in contents:
line = self.preprocess(line)
if line == '' or line.startswith('source'):
continue
elif line.startswith('group'):
match = self.group_block_regex.match(line)
if match:
GemfileParser.global_group = match.group('groupblock')
elif line.startswith('end'):
GemfileParser.global_group = 'runtime'
elif line.startswith('gemspec'):
# Gemfile contains a call to gemspec
gemfiledir = os.path.dirname(self.filepath)
gemspec_list = glob.glob(os.path.join(gemfiledir, '*.gemspec'))
if len(gemspec_list) > 1:
print('Multiple gemspec files found')
continue
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
def parse_gemspec(self, path=''):
"""
Return dependencies after parsing gemspec/podspec files.
"""
if path == '':
contents = self.contents
else:
contents = open(path).readlines()
for line in contents:
line = self.preprocess(line)
match = GemfileParser.add_dvtdep_regex.match(line)
if match:
GemfileParser.global_group = 'development'
else:
match = GemfileParser.add_rundep_regex.match(line)
if match:
GemfileParser.global_group = 'runtime'
else:
match = GemfileParser.add_dep_regex.match(line)
if match:
GemfileParser.global_group = 'dependency'
if match:
line = match.group('line')
self.parse_line(line)
return self.dependencies
def parse(self):
"""
Calls necessary function based on whether file is a gemspec/podspec file
or not and forwards the dicts returned by them.
"""
if self.gemspec:
return self.parse_gemspec()
else:
return self.parse_gemfile()