Skip to content

Commit 0d4905b

Browse files
rakesh balusapombredanne
authored andcommitted
#253 Recognize Python .egg files as packages
Signed-off-by: Philippe Ombredanne <[email protected]>
1 parent bd70867 commit 0d4905b

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

src/packagedcode/pypi.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,45 @@
4646
# logger.setLevel(logging.DEBUG)
4747

4848

49+
PKG_INFO_ATTRIBUTES = [
50+
'Name',
51+
'Version',
52+
'Author',
53+
'Author-email',
54+
'License',
55+
'Description',
56+
'Platform',
57+
'Home-page',
58+
'Summary'
59+
]
60+
61+
62+
def parse_pkg_info(location):
63+
"""
64+
parse a 'PKG-INFO' file at 'location' and return a PythonPackage object.
65+
"""
66+
infos = {}
67+
with open(location, 'rb') as txt_file:
68+
lines = []
69+
for line in txt_file:
70+
lines.append(line)
71+
lines = ''.join(lines)
72+
for attribute in PKG_INFO_ATTRIBUTES:
73+
infos[attribute] = re.findall('^'+attribute+'[\s:]*.*', lines, flags=re.MULTILINE)[0]
74+
infos[attribute] = re.sub('^'+attribute+'[\s:]*', '', infos[attribute], flags=re.MULTILINE)
75+
if infos[attribute] == 'UNKNOWN':
76+
infos[attribute] = None
77+
package = PythonPackage(
78+
name=infos.get('Name'),
79+
version=infos.get('Version'),
80+
summary=infos.get('Summary'),
81+
homepage_url=infos.get('Home-page'),
82+
asserted_licenses=[AssertedLicense(license=infos.get('License'))],
83+
authors=[infos.get('Author')],
84+
)
85+
return package
86+
87+
4988
def get_attribute(setup_location, attribute):
5089
"""
5190
Return the value specified for a given 'attribute' mentioned in a 'setup.py'
@@ -115,3 +154,5 @@ def parse(location):
115154
return package
116155
if file_name == 'metadata.json':
117156
parse_metadata(location)
157+
if file_name == 'PKG-INFO':
158+
parse_pkg_info(location)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Metadata-Version: 1.0
2+
Name: TicketImport
3+
Version: 0.7a
4+
Summary: Import CSV and Excel files
5+
Home-page: http://nexb.com
6+
Author: Francois Granade
7+
Author-email: [email protected]
8+
License: BSD
9+
Description: UNKNOWN
10+
Platform: UNKNOWN

tests/packagedcode/test_pypi.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,13 @@ def test_parse_metadata(self):
5959
assert 'MIT' == package.asserted_licenses[0].license
6060
assert ['Benjamin Peterson'] == package.authors
6161
assert 'http://pypi.python.org/pypi/six/' == package.homepage_url
62+
63+
def test_parse_pkg_info(self):
64+
test_file = self.get_test_loc('pypi/PKG-INFO')
65+
package = pypi.parse_pkg_info(test_file)
66+
assert 'TicketImport' == package.name
67+
assert '0.7a' == package.version
68+
assert 'Import CSV and Excel files' == package.summary
69+
assert 'BSD' == package.asserted_licenses[0].license
70+
assert 'http://nexb.com' == package.homepage_url
71+
assert ['Francois Granade'] == package.authors

0 commit comments

Comments
 (0)