Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding more columns to the display #4 #9

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions dep_license/__init__.py
Original file line number Diff line number Diff line change
@@ -34,7 +34,6 @@
"conda.yml",
]
PYPYI_URL = "https://pypi.python.org/pypi"
COLUMNS = ["Name", "Meta", "Classifier"]


def is_valid_git_remote(project):
@@ -69,6 +68,9 @@ def get_params(argv=None):
parser.add_argument(
"-f", "--format", default="github", help="define how result is formatted"
)
parser.add_argument(
"--columns", nargs='+', default=["Name", "Meta", "Classifier"], help="define which columns to display. The options are ['Author', 'Author Email', 'Bugtrack Url', 'Classifiers', 'Description', 'Description Content Type', 'Docs Url', 'Download Url', 'Downloads', 'Home Page', 'Keywords', 'License', 'Maintainer', 'Maintainer Email', 'Name', 'Package Url', 'Platform', 'Project Url', 'Project Urls', 'Release Url', 'Requires Dist', 'Requires Python', 'Summary', 'Version', 'Yanked', 'Yanked Reason']. Defaults to only the license information"
)
parser.add_argument("-o", "--output", default=None, help="path for output file")
parser.add_argument(
"-d",
@@ -99,42 +101,41 @@ def get_params(argv=None):
project = args.PROJECT
w = args.workers
fmt = args.format
columns = args.columns
output = args.output
dev = args.dev
name = args.name
check = args.check
env = args.env

return project, w, fmt, output, dev, name, check, env
return project, w, fmt, columns, output, dev, name, check, env


def worker(d):
d = d.replace('"', "")
d = d.replace("'", "")
record = [d]
d = d.replace('"', "").replace("'", "")
try:
with urlopen("{}/{}/json".format(PYPYI_URL, d)) as conn:
output = json.loads(conn.read().decode()).get("info")
pypi_response = json.loads(conn.read().decode()).get("info")

except Exception:
logger.warning(f"{d}: error in fetching pypi metadata")
return None

meta = output.get("license", "")
record.append(meta.strip())
record = {}
for column, value in pypi_response.items():
record[column.replace('_', ' ').title()] = value

license_class = set()
classifier = output.get("classifiers", "")
classifier = pypi_response.get("classifiers", "")
for c in classifier:
if c.startswith("License"):
license_class.add("::".join([x.strip() for x in c.split("::")[1:]]))

license_class_str = (
license_class.pop() if len(license_class) == 1 else str(license_class)
)
record.append(license_class_str)
record['Classifier'] = ', '.join(license_class)
record['Meta'] = record['License']
record['Name'] = d

return dict(zip(COLUMNS, record))
return record


def start_concurrent(dependencies, max_workers=5):
@@ -158,7 +159,7 @@ def start_concurrent(dependencies, max_workers=5):
def run(argv=None):
warnings.simplefilter("ignore", UserWarning)

projects, max_workers, fmt, output_file, dev, name, check, env = get_params(argv)
projects, max_workers, fmt, columns, output_file, dev, name, check, env = get_params(argv)
return_val = 0

if name:
@@ -234,19 +235,19 @@ def run(argv=None):
fmt = fmt.lower()
if fmt == "json":
import json

results = [{column: result.get(column) for column in columns} for result in results]
output = json.dumps(results, indent=4)

else:
rows = []
for r in results:
rows.append(list(r.values()))
rows.append([r.get(column, '') for column in columns])
if fmt == "csv":
output += ",".join(COLUMNS) + "\n"
output += ",".join(columns) + "\n"
for row in rows:
output += ",".join(row) + "\n"
else:
output = tabulate(rows, COLUMNS, tablefmt=fmt)
output = tabulate(rows, columns, tablefmt=fmt)

if not check:
print(output, end="\n")
56 changes: 30 additions & 26 deletions tests/test_worker.py
Original file line number Diff line number Diff line change
@@ -4,30 +4,34 @@
from dep_license import worker


@pytest.mark.parametrize(
"dependency,expected",
[
(
"dep_license",
{
"Name": "dep_license",
"Meta": "MIT",
"Classifier": "OSI Approved::MIT License",
},
),
("SomethingThatDoesntExist", None),
],
)
@pytest.fixture
def dependency():
return 'dep_license'


@pytest.fixture
def dependency_does_not_exist():
return 'SomethingThatDoesntExist'


@pytest.fixture
def expected(dependency):
return {
"Name": dependency,
"Meta": "MIT",
"Classifier": "OSI Approved::MIT License",
}.items()


def test_worker(dependency, expected):
assert worker(dependency) == expected


def test_concurrent_workers():
results = start_concurrent(["dep_license", "SomethingElseThatDoesntExist"])
assert results == [
{
"Name": "dep_license",
"Meta": "MIT",
"Classifier": "OSI Approved::MIT License",
}
]
assert worker(dependency).items() >= expected


def test_worker_dependency_does_not_exist(dependency_does_not_exist):
assert worker(dependency_does_not_exist) is None


def test_concurrent_workers(dependency, dependency_does_not_exist, expected):
results = start_concurrent([dependency, dependency_does_not_exist])
assert len(results), 1
assert results[0].items() >= expected