diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2020-09-27 18:49:06 -0400 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2020-09-27 18:49:06 -0400 |
commit | 8f5123afebff5cbb622d12edff318564c3771f1f (patch) | |
tree | e4b207c32496eb9b79d337219059b55fa0b87ac0 | |
download | package_info-8f5123afebff5cbb622d12edff318564c3771f1f.tar.gz |
Initial commit
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | package_info/__init__.py | 1 | ||||
-rwxr-xr-x | package_info/package_info.py | 79 | ||||
-rw-r--r-- | setup.py | 14 |
5 files changed, 99 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..42e1d25 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +__pycache__ +dist +build +.eggs diff --git a/README.md b/README.md new file mode 100644 index 0000000..9bd3248 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# package_info diff --git a/package_info/__init__.py b/package_info/__init__.py new file mode 100644 index 0000000..60633e3 --- /dev/null +++ b/package_info/__init__.py @@ -0,0 +1 @@ +from . import package_data diff --git a/package_info/package_info.py b/package_info/package_info.py new file mode 100755 index 0000000..229af80 --- /dev/null +++ b/package_info/package_info.py @@ -0,0 +1,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) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..e85760b --- /dev/null +++ b/setup.py @@ -0,0 +1,14 @@ +from setuptools import setup +from setuptools import find_packages + +setup( + name="package_info", + use_scm_version=True, + setup_requires=[ + "setuptools_scm", + ], + description="Get PKG-INFO metadata as JSON", + author="Joseph Hunkeler", + author_email="jhunk@stsci.edu", + packages=find_packages(), +) |