|
33 | 33 | import os |
34 | 34 | import re |
35 | 35 |
|
36 | | -from gemfileparser import GemfileParser |
| 36 | +from gemfileparser2 import GemfileParser |
37 | 37 |
|
38 | 38 | """ |
39 | 39 | Handle Cocoapods(.podspec) and Ruby(.gemspec) files. |
40 | 40 | """ |
41 | 41 |
|
| 42 | + |
42 | 43 | TRACE = False |
43 | 44 |
|
44 | 45 | logger = logging.getLogger(__name__) |
|
49 | 50 | logger.setLevel(logging.DEBUG) |
50 | 51 |
|
51 | 52 |
|
52 | | -def parse_spec(location): |
53 | | - """ |
54 | | - Return dictionary contains podspec or gemspec file data. |
55 | | - """ |
56 | | - with io.open(location, encoding='utf-8', closefd=True) as data: |
57 | | - lines = data.readlines() |
58 | | - |
59 | | - spec_data = {} |
60 | | - |
61 | | - for line in lines: |
62 | | - if '.name' in line: |
63 | | - name = line.rpartition('=') |
64 | | - spec_data['name'] = get_stripped_data(name[2]) |
65 | | - elif '.version' in line and '.version.' not in line: |
66 | | - version = line.rpartition('=') |
67 | | - spec_data['version'] = get_stripped_data(version[2]) |
68 | | - elif '.license' in line: |
69 | | - license_type = line.rpartition('=') |
70 | | - spec_data['license'] = get_stripped_data(license_type[2]) |
71 | | - elif '.email' in line: |
72 | | - emails = line.rpartition('=') |
73 | | - stripped_emails = get_stripped_data(emails[2]) |
74 | | - stripped_emails = stripped_emails.strip() |
75 | | - stripped_emails = stripped_emails.split(',') |
76 | | - spec_data['email'] = stripped_emails |
77 | | - elif '.author' in line: |
78 | | - authors = re.sub(r'/*.*author.*?=', '', line) |
79 | | - stripped_authors = get_stripped_data(authors) |
80 | | - stripped_authors = stripped_authors.replace(' => ', "=>") |
81 | | - stripped_authors = stripped_authors.strip() |
82 | | - stripped_authors = stripped_authors.split(',') |
83 | | - spec_data['author'] = stripped_authors |
84 | | - elif '.summary' in line: |
85 | | - summary = line.rpartition('=') |
86 | | - spec_data['summary'] = get_stripped_data(summary[2]) |
87 | | - elif '.description' in line: |
88 | | - if location.endswith('.gemspec'): |
89 | | - # FIXME: description can be in single or multi-lines |
90 | | - # There are many different ways to write description. |
91 | | - desc = line.rpartition('=') |
92 | | - spec_data['description'] = get_stripped_data(desc[2]) |
93 | | - else: |
94 | | - spec_data['description'] = get_description(line) |
95 | | - elif '.homepage' in line: |
96 | | - homepage_url = line.rpartition('=') |
97 | | - spec_data['homepage_url'] = get_stripped_data(homepage_url[2]) |
98 | | - elif '.source' in line and '.source_files' not in line: |
99 | | - source = re.sub(r'/*.*source.*?>', '', line) |
100 | | - stripped_source = re.sub(r',.*', '', source) |
101 | | - spec_data['source'] = get_stripped_data(stripped_source) |
102 | | - |
103 | | - parser = GemfileParser(location) |
104 | | - deps = parser.parse() |
105 | | - dependencies = OrderedDict() |
106 | | - for key in deps: |
107 | | - depends = deps.get(key, []) or [] |
108 | | - for dep in depends: |
109 | | - dependencies[dep.name] = dep.requirement.split(',') |
110 | | - spec_data['dependencies'] = dependencies |
111 | | - |
112 | | - return spec_data |
113 | | - |
114 | | - |
115 | | -def get_stripped_data(line): |
| 53 | +class Spec(): |
| 54 | + parse_name = re.compile(r'.*\.name(\s*)=(?P<name>.*)') |
| 55 | + parse_version = re.compile(r'.*\.version(\s*)=(?P<version>.*)') |
| 56 | + parse_license = re.compile(r'.*\.license(\s*)=(?P<license>.*)') |
| 57 | + parse_summary = re.compile(r'.*\.summary(\s*)=(?P<summary>.*)') |
| 58 | + parse_description = re.compile(r'.*\.description(\s*)=(?P<description>.*)') |
| 59 | + parse_homepage = re.compile(r'.*\.homepage(\s*)=(?P<homepage>.*)') |
| 60 | + parse_source = re.compile(r'.*\.source(\s*)=(?P<source>.*)') |
| 61 | + |
| 62 | + def parse_spec(self, location): |
| 63 | + """ |
| 64 | + Return dictionary contains podspec or gemspec file data. |
| 65 | + """ |
| 66 | + with io.open(location, encoding='utf-8', closefd=True) as data: |
| 67 | + lines = data.readlines() |
| 68 | + |
| 69 | + spec_data = {} |
| 70 | + |
| 71 | + for line in lines: |
| 72 | + line = pre_process(line) |
| 73 | + match = self.parse_name.match(line) |
| 74 | + if match: |
| 75 | + name = match.group('name') |
| 76 | + spec_data['name'] = get_stripped_data(name) |
| 77 | + match = self.parse_version.match(line) |
| 78 | + if match: |
| 79 | + version = match.group('version') |
| 80 | + spec_data['version'] = get_stripped_data(version) |
| 81 | + match = self.parse_license.match(line) |
| 82 | + if match: |
| 83 | + license_value = match.group('license') |
| 84 | + spec_data['license'] = get_stripped_data(license_value) |
| 85 | + match = self.parse_summary.match(line) |
| 86 | + if match: |
| 87 | + summary = match.group('summary') |
| 88 | + spec_data['summary'] = get_stripped_data(summary) |
| 89 | + match = self.parse_homepage.match(line) |
| 90 | + if match: |
| 91 | + homepage = match.group('homepage') |
| 92 | + spec_data['homepage_url'] = get_stripped_data(homepage) |
| 93 | + match = self.parse_source.match(line) |
| 94 | + if match: |
| 95 | + source = re.sub(r'/*.*source.*?>', '', line) |
| 96 | + stripped_source = re.sub(r',.*', '', source) |
| 97 | + spec_data['source'] = get_stripped_data(stripped_source) |
| 98 | + match = self.parse_description.match(line) |
| 99 | + if match: |
| 100 | + if location.endswith('.gemspec'): |
| 101 | + # FIXME: description can be in single or multi-lines |
| 102 | + # There are many different ways to write description. |
| 103 | + description = match.group('description') |
| 104 | + spec_data['description'] = get_stripped_data(description) |
| 105 | + else: |
| 106 | + spec_data['description'] = get_description(location) |
| 107 | + if '.email' in line: |
| 108 | + _key, _sep, value = line.rpartition('=') |
| 109 | + stripped_emails = get_stripped_data(value) |
| 110 | + stripped_emails = stripped_emails.strip() |
| 111 | + stripped_emails = stripped_emails.split(',') |
| 112 | + spec_data['email'] = stripped_emails |
| 113 | + elif '.author' in line: |
| 114 | + authors = re.sub(r'/*.*author.*?=', '', line) |
| 115 | + stripped_authors = get_stripped_data(authors) |
| 116 | + stripped_authors = re.sub(r'(\s*=>\s*)', '=>', stripped_authors) |
| 117 | + stripped_authors = stripped_authors.strip() |
| 118 | + stripped_authors = stripped_authors.split(',') |
| 119 | + spec_data['author'] = stripped_authors |
| 120 | + |
| 121 | + parser = GemfileParser(location) |
| 122 | + deps = parser.parse() |
| 123 | + dependencies = OrderedDict() |
| 124 | + for key in deps: |
| 125 | + depends = deps.get(key, []) or [] |
| 126 | + for dep in depends: |
| 127 | + dependencies[dep.name] = dep.requirement |
| 128 | + spec_data['dependencies'] = dependencies |
| 129 | + |
| 130 | + return spec_data |
| 131 | + |
| 132 | + |
| 133 | +def pre_process(line): |
116 | 134 | """ |
117 | | - Return line after removing unnecessary special character and space. |
| 135 | + Return line after comments and space. |
118 | 136 | """ |
119 | 137 | if '#' in line: |
120 | 138 | line = line[:line.index('#')] |
121 | 139 | stripped_data = line.strip() |
| 140 | + |
| 141 | + return stripped_data |
| 142 | + |
| 143 | +def get_stripped_data(data): |
| 144 | + """ |
| 145 | + Return data after removing unnecessary special character |
| 146 | + """ |
122 | 147 | for strippable in ("'",'"', '{', '}', '[', ']', '%q',): |
123 | | - stripped_data = stripped_data.replace(strippable, '') |
| 148 | + data = data.replace(strippable, '') |
124 | 149 |
|
125 | | - return stripped_data.strip() |
| 150 | + return data.strip() |
126 | 151 |
|
127 | 152 |
|
128 | 153 | def get_description(location): |
|
0 commit comments