Skip to content

Commit 000197e

Browse files
committed
use regex to detect data
Signed-off-by: rpotter12 <rohitpotter12@gmail.com>
1 parent 37bd479 commit 000197e

11 files changed

Lines changed: 105 additions & 759 deletions

setup.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,6 @@ def read(*names, **kwargs):
202202
'toml >= 0.10.0',
203203
'pkginfo >= 1.5.0.1',
204204
'dparse >= 0.4.1',
205-
'gemfileparser >= 0.6.2',
206-
'nose >= 1.3.7',
207205

208206
# used to fix mojibake in Windows PE
209207
'ftfy < 5.0.0; python_version == "2.7"',

src/packagedcode/cocoapods.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from commoncode import filetype
3636
from commoncode import fileutils
3737
from packagedcode import models
38-
from packagedcode import spec
38+
from packagedcode.spec import Spec
3939

4040

4141
"""
@@ -44,8 +44,6 @@
4444
See https://cocoapods.org
4545
"""
4646

47-
# TODO: implementation to get dependency data using gemsfileparser
48-
# Check: https://gitlab.com/balasankarc/gemfileparser
4947
# TODO: override the license detection to detect declared_license correctly.
5048

5149

@@ -94,7 +92,8 @@ def parse(location):
9492
if not is_podspec(location):
9593
return
9694

97-
podspec_data = spec.parse_spec(location)
95+
podspec_object = Spec()
96+
podspec_data = podspec_object.parse_spec(location)
9897
return build_package(podspec_data)
9998

10099

@@ -113,17 +112,15 @@ def build_package(podspec_data):
113112

114113
author_names = []
115114
author_email = []
116-
for split_author in authors:
117-
split_author = split_author.strip()
118-
author, email = parse_person(split_author)
119-
author_names.append(author)
120-
author_email.append(email)
115+
if authors:
116+
for split_author in authors:
117+
split_author = split_author.strip()
118+
author, email = parse_person(split_author)
119+
author_names.append(author)
120+
author_email.append(email)
121121

122122
parties = list(party_mapper(author_names, author_email))
123123

124-
if len(summary) > len(description):
125-
description = summary
126-
127124
package = CocoapodsPackage(
128125
name=name,
129126
version=version,

src/packagedcode/rubygems.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
from extractcode.uncompress import get_gz_compressed_file_content
4343
from packagedcode import models
4444
from packagedcode.gemfile_lock import GemfileLockParser
45-
from packagedcode import spec
45+
from packagedcode.spec import Spec
4646
from packagedcode.utils import combine_expressions
4747

4848

@@ -630,7 +630,8 @@ def build_packages_from_gemspec(location):
630630
"""
631631
Return RubyGem Package from gemspec file.
632632
"""
633-
gemspec_data = spec.parse_spec(location)
633+
gemspec_object = Spec()
634+
gemspec_data = gemspec_object.parse_spec(location)
634635

635636
name = gemspec_data.get('name')
636637
version = gemspec_data.get('version')

src/packagedcode/spec.py

Lines changed: 93 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@
3333
import os
3434
import re
3535

36-
from gemfileparser import GemfileParser
36+
from gemfileparser2 import GemfileParser
3737

3838
"""
3939
Handle Cocoapods(.podspec) and Ruby(.gemspec) files.
4040
"""
4141

42+
4243
TRACE = False
4344

4445
logger = logging.getLogger(__name__)
@@ -49,80 +50,104 @@
4950
logger.setLevel(logging.DEBUG)
5051

5152

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):
116134
"""
117-
Return line after removing unnecessary special character and space.
135+
Return line after comments and space.
118136
"""
119137
if '#' in line:
120138
line = line[:line.index('#')]
121139
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+
"""
122147
for strippable in ("'",'"', '{', '}', '[', ']', '%q',):
123-
stripped_data = stripped_data.replace(strippable, '')
148+
data = data.replace(strippable, '')
124149

125-
return stripped_data.strip()
150+
return data.strip()
126151

127152

128153
def get_description(location):
-16.2 KB
Binary file not shown.
-16.2 KB
Binary file not shown.
-4.07 KB
Binary file not shown.

0 commit comments

Comments
 (0)