2020import urllib .parse as urlparse
2121import urllib .request as urlreq
2222
23- import pkg_resources
23+ import packaging . requirement
2424
2525try :
2626 PYPI_LOCATION = os .environ ['PYPI_LOCATION' ]
2727except KeyError :
2828 PYPI_LOCATION = 'http://pypi.org/project'
2929
3030
31- KEEP_KEYS = frozenset ([
32- 'author' ,
33- 'author_email' ,
34- 'maintainer' ,
35- 'maintainer_email' ,
36- 'license' ,
37- 'summary' ,
38- 'home_page' ,
39- ])
31+ KEEP_KEYS = frozenset (
32+ [
33+ 'author' ,
34+ 'author_email' ,
35+ 'maintainer' ,
36+ 'maintainer_email' ,
37+ 'license' ,
38+ 'summary' ,
39+ 'home_page' ,
40+ ]
41+ )
4042
4143
4244def iter_names (req ):
43- for k in ( req .key , req . project_name ):
44- yield k
45- yield k .title ()
46- yield k .replace ("-" , "_" )
47- yield k .replace ("-" , "_" ).title ()
45+ yield req .name
46+ yield req . name . lower ()
47+ yield req . name .title ()
48+ yield req . name .replace ("-" , "_" )
49+ yield req . name .replace ("-" , "_" ).title ()
4850
4951
5052def release_data (req ):
5153 # Try to find it with various names...
5254 attempted = []
5355 for name in iter_names (req ):
54- url = PYPI_LOCATION + "/%s/json" % ( urlparse .quote (name ))
56+ url = PYPI_LOCATION + f"/ { urlparse .quote (name )} /json"
5557 if url in attempted :
5658 continue
5759 with contextlib .closing (urlreq .urlopen (url )) as uh :
5860 if uh .getcode () != 200 :
5961 attempted .append (url )
6062 continue
6163 return json .loads (uh .read ())
62- attempted = [" * %s" % u for u in attempted ]
63- raise IOError ("Could not find '%s' on pypi\n Attempted urls:\n %s"
64- % (req .key , "\n " .join (attempted )))
64+ attempted = [f" * { u } " for u in attempted ]
65+ raise OSError (
66+ "Could not find '{}' on pypi\n Attempted urls:\n {}" .format (
67+ req .key , "\n " .join (attempted )
68+ )
69+ )
6570
6671
6772def main ():
6873 if len (sys .argv ) == 1 :
69- print ("%s requirement-file ..." % ( sys . argv [ 0 ]) , file = sys .stderr )
74+ print (f" { sys . argv [ 0 ] } requirement-file ..." , file = sys .stderr )
7075 sys .exit (1 )
7176 for filename in sys .argv [1 :]:
72- print ("Analyzing file: %s" % ( filename ) )
77+ print (f "Analyzing file: { filename } " )
7378 details = {}
7479 with open (filename , "rb" ) as fh :
7580 for line in fh .read ().splitlines ():
7681 line = line .strip ()
7782 if line .startswith ("#" ) or not line :
7883 continue
79- req = pkg_resources . Requirement . parse (line )
80- print (" - processing: %s" % ( req ) )
84+ req = packaging . requirement . Requirement (line )
85+ print (f " - processing: { req } " )
8186 try :
8287 raw_req_data = release_data (req )
83- except IOError :
88+ except OSError :
8489 traceback .print_exc ()
8590 details [req .key ] = None
8691 else :
8792 req_info = {}
88- for ( k , v ) in raw_req_data .get ('info' , {}).items ():
93+ for k , v in raw_req_data .get ('info' , {}).items ():
8994 if k not in KEEP_KEYS :
9095 continue
9196 req_info [k ] = v
@@ -94,9 +99,12 @@ def main():
9499 'info' : req_info ,
95100 }
96101 filename , _ext = os .path .splitext (filename )
97- with open ("%s.json" % (filename ), "wb" ) as fh :
98- fh .write (json .dumps (details , sort_keys = True , indent = 4 ,
99- separators = ("," , ": " )))
102+ with open (f"{ filename } .json" , "wb" ) as fh :
103+ fh .write (
104+ json .dumps (
105+ details , sort_keys = True , indent = 4 , separators = ("," , ": " )
106+ )
107+ )
100108
101109
102110if __name__ == '__main__' :
0 commit comments