1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
#!/usr/bin/env python
from pkg_resources import get_distribution
import sys
import json
def get_package_info(name):
read_desc = False
result = {}
# PKG-INFO keys that are considered lists
requires_list = [
"classifier",
"project_url",
"requires_dist",
"provides_extra",
]
# Get package dist information
try:
pkg = get_distribution(name)
except Exception as e:
return result
# Parse PKG-INFO file
for i, record in enumerate(pkg._get_metadata(pkg.PKG_INFO)):
# Are we reading key pairs or the long package descripton (at the end of the file)?
if not read_desc:
# Is a key pair present? If not, we're at the long desciption
if ':' not in record or not record:
result["description"] = record + "\n"
read_desc = True
continue
# Parse the record into a key pair
key, value = record.split(':', 1)
# Remove dashes from key and convert to lower-case
key = key.replace("-", "_").lower()
# Remove leading and trailing whitespace from value
value = value.lstrip().strip()
# Is the key we've encounted supposed to be a list, or string?
if key in requires_list:
# Create a new key in the dictionary as a list
if result.get(key) is None:
result[key] = []
# Write value to result
result[key].append(value)
else:
# Write string value to result
result[key] = value
else:
# Write description block to result
result["description"] += record + '\n'
return result
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Not enough arguments: need package name", file=sys.stderr)
exit(1)
name = sys.argv[1]
result = get_package_info(name)
if len(sys.argv) < 3:
# Dump all data as JSON
print(json.dumps(result, indent=4))
else:
# Dump the value of a key as JSON
pattern = sys.argv[2]
try:
print(json.dumps(result[pattern], indent=4))
except KeyError:
print("{}")
exit(1)
|