11#
2- # Copyright (c) 2015 nexB Inc. and others. All rights reserved.
2+ # Copyright (c) 2016 nexB Inc. and others. All rights reserved.
33# http://nexb.com and https://github.com/nexB/scancode-toolkit/
44# The ScanCode software is licensed under the Apache License version 2.0.
55# Data generated with ScanCode require an acknowledgment.
2525from __future__ import absolute_import
2626from __future__ import print_function
2727
28+ import json
2829import logging
30+ import os
2931import re
3032
33+ from commoncode import fileutils
3134from packagedcode .models import AssertedLicense
3235from packagedcode .models import PythonPackage
3336
37+
3438"""
3539Handle Python PyPi packages
3640"""
@@ -54,9 +58,8 @@ def get_attribute(setup_location, attribute):
5458 'requests' is returned for the attribute 'name'
5559 """
5660 setup_text = open (setup_location , 'rb' ).read ()
57- setup_text = setup_text .replace ('\n ' , '' )
5861 # FIXME Use a valid parser for parsing 'setup.py'
59- values = re .findall ('setup\(.*?' + attribute + '=[\" \' ]{1}.*?\' ,' , setup_text )
62+ values = re .findall ('setup\(.*?' + attribute + '=[\" \' ]{1}.*?\' ,' , setup_text . replace ( ' \n ' , '' ) )
6063 if len (values ) > 1 :
6164 return
6265 else :
@@ -68,18 +71,47 @@ def get_attribute(setup_location, attribute):
6871 return output
6972
7073
74+ def parse_metadata (location ):
75+ parent_dir = fileutils .parent_directory (location )
76+ if os .path .exists (os .path .join (parent_dir , 'METADATA' )) and os .path .exists (os .path .join (parent_dir , 'DESCRIPTION.rst' )):
77+ infos = json .loads (open (location , 'rb' ).read ())
78+ homepage_url = None
79+ authors = []
80+ if infos ['extensions' ]:
81+ try :
82+ homepage_url = infos ['extensions' ]['python.details' ]['project_urls' ]['Home' ]
83+ except :
84+ pass
85+ try :
86+ for contact in infos ['extensions' ]['python.details' ]['contacts' ]:
87+ authors .append (contact ['name' ])
88+ except :
89+ pass
90+ package = PythonPackage (
91+ name = infos .get ('name' ),
92+ version = infos .get ('version' ),
93+ summary = infos .get ('summary' ),
94+ asserted_licenses = [AssertedLicense (license = infos .get ('license' ))],
95+ homepage_url = homepage_url ,
96+ authors = authors ,
97+ )
98+ return package
99+
100+
71101def parse (location ):
72102 """
73103 Parse a 'setup.py' and return a PythonPackage object.
74104 """
75- if not location .endswith ('setup.py' ):
76- return
77- package = PythonPackage (
78- name = get_attribute (location , 'name' ),
79- homepage_url = get_attribute (location , 'url' ),
80- description = get_attribute (location , 'description' ),
81- version = get_attribute (location , 'version' ),
82- authors = [get_attribute (location , 'author' )],
83- asserted_licenses = [AssertedLicense (license = get_attribute (location , 'license' ))],
84- )
85- return package
105+ file_name = fileutils .file_name (location )
106+ if file_name == 'setup.py' :
107+ package = PythonPackage (
108+ name = get_attribute (location , 'name' ),
109+ homepage_url = get_attribute (location , 'url' ),
110+ description = get_attribute (location , 'description' ),
111+ version = get_attribute (location , 'version' ),
112+ authors = [get_attribute (location , 'author' )],
113+ asserted_licenses = [AssertedLicense (license = get_attribute (location , 'license' ))],
114+ )
115+ return package
116+ if file_name == 'metadata.json' :
117+ parse_metadata (location )
0 commit comments