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

Can now pass urls to commandline fairtally with progressbar #24

Merged
merged 19 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
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
28 changes: 28 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/sh

echo "Script $0 triggered ..."
echo "Starting prospector analysis ..."

# quietly run prospector
prospector 1>/dev/null

# use return code to abort commit if necessary
if [ $? != "0" ]; then
echo "Commit aborted. Run 'prospector' to see the errors."
exit 1
fi


echo "Starting isort analysis ..."

# recursively run isort on fairtally/ directory, don't try to automatically fix anything
isort --recursive --check-only fairtally

if [ $? != "0" ]; then
echo "Commit aborted."
exit 1
fi

echo "Pre-commit checks completed successfully."
exit 0

9 changes: 6 additions & 3 deletions fairtally/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# -*- coding: utf-8 -*-

import logging

from .__version__ import __version__


logging.getLogger(__name__).addHandler(logging.NullHandler())

__author__ = "FAIR Software"
__email__ = '[email protected]'


__all__ = [
"__version__"
]
10 changes: 2 additions & 8 deletions fairtally/__version__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import os
import sys

# To update the package version number, edit CITATION.cff
citationfile = os.path.join(sys.exec_prefix, 'citation/fairtally', 'CITATION.cff')
with open(citationfile, 'r') as cff:
for line in cff:
if 'version:' in line:
__version__ = line.replace('version:', '').strip().strip('"')

__version__ = "0.1.0"
76 changes: 76 additions & 0 deletions fairtally/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import io
import json
from pathlib import Path
import click
from howfairis import Checker
from howfairis import Compliance
from howfairis import Repo
from jinja2 import Template
from tqdm import tqdm
from fairtally.get_badge_color import get_badge_color
from fairtally.redirect_stdout_stderr import RedirectStdStreams


@click.command()
@click.argument("urls", nargs=-1)
@click.option("--html", "output_file_html",
help="Filename of where to write the results as HTML.",
default=None, type=click.File("wt"))
@click.option("--json", "output_file_json",
help="Filename of where to write the results as JSON.",
default=None, type=click.File("wt"))
def cli(urls=None, output_file_html=None, output_file_json=None):

def write_as_json():
with output_file_json:
json.dump(results, output_file_json, sort_keys=True)

def write_as_html():
parent = Path(__file__).parent
template_file = parent / "data" / "index.html.template"
with open(template_file) as f:
template = Template(f.read())
s = template.render(results=results)
with output_file_html:
output_file_html.write(s)

if len(urls) == 0:
print("No URLs provided, aborting.")
return

results = list()

url_progressbar = tqdm(urls, bar_format="fairtally progress: |{bar}| {n_fmt}/{total_fmt}", ncols=70, position=0)
current_value = tqdm(total=0, bar_format="{desc}", position=1)
for url in url_progressbar:
stderr_buffer = io.StringIO()
stdout_buffer = io.StringIO()
with RedirectStdStreams(stdout=stdout_buffer, stderr=stderr_buffer):
try:
current_value.set_description_str("currently checking " + url)
repo = Repo(url)
compliance = Checker(repo, ignore_repo_config=True, is_quiet=True).check_five_recommendations()
except Exception:
compliance = Compliance(False, False, False, False, False)
finally:
badge = "https://img.shields.io/badge/fair--software.eu-{0}-{1}"\
.format(compliance.urlencode(), get_badge_color(compliance))
d = dict(url=url, badge=badge, repository=compliance.repository, license=compliance.license,
registry=compliance.registry, citation=compliance.citation, checklist=compliance.checklist,
count=compliance.count(), stdout=stdout_buffer.getvalue(), stderr=stderr_buffer.getvalue())

current_value.set_description_str()
results.append(d)

if output_file_json is None and output_file_html is None:
print(json.dumps(results))

if output_file_json is not None:
write_as_json()

if output_file_html is not None:
write_as_html()


if __name__ == "__main__":
cli()
36 changes: 36 additions & 0 deletions fairtally/data/index.html.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<html>

<head>
</head>

<body>

<table>
<tr>
<th>URL</th>
<th>repository</th>
<th>license</th>
<th>registry</th>
<th>citation</th>
<th>checklist</th>
<th>count</th>
<th>badge</th>

</tr>

{% for item in results %}
<tr>
<td><a href="{{item.url}}">{{item.url}}</a></td>
<td>{{item.repository}}</td>
<td>{{item.license}}</td>
<td>{{item.registry}}</td>
<td>{{item.citation}}</td>
<td>{{item.checklist}}</td>
<td>{{item.count}}</td>
<td><a href="https://fair-software.eu"><img src="{{item.badge}}"></a></td>
</tr>
{% endfor %}

</body>

</html>
9 changes: 0 additions & 9 deletions fairtally/fairtally.py

This file was deleted.

17 changes: 17 additions & 0 deletions fairtally/get_badge_color.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from howfairis import Compliance


def get_badge_color(compliance: Compliance) -> str:

score = compliance.count(True)

if score in [0, 1]:
return "red"
if score in [2, 3]:
return "orange"
if score in [4]:
return "yellow"
if score == 5:
return "green"

raise Exception("should not happen")
21 changes: 21 additions & 0 deletions fairtally/redirect_stdout_stderr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import sys


# from https://stackoverflow.com/a/6796752
class RedirectStdStreams():

def __init__(self, stdout=None, stderr=None):
self._stdout = stdout or sys.stdout
self._stderr = stderr or sys.stderr

def __enter__(self):
self.old_stdout, self.old_stderr = sys.stdout, sys.stderr
self.old_stdout.flush()
self.old_stderr.flush()
sys.stdout, sys.stderr = self._stdout, self._stderr

def __exit__(self, exc_type, exc_value, traceback):
self._stdout.flush()
self._stderr.flush()
sys.stdout = self.old_stdout
sys.stderr = self.old_stderr
26 changes: 17 additions & 9 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
[metadata]
description-file = README.rst

[aliases]
# Define `python setup.py test`
test=pytest

# Define `python setup.py build_sphinx`
[build_sphinx]
source-dir = docs
build-dir = docs/_build
all_files = 1
builder = html

[coverage:run]
branch = True
source = fairtally

[metadata]
description-file = README.rst

[tool:pytest]
testpaths = tests
addopts = --cov --cov-report xml --cov-report term --cov-report html

# Define `python setup.py build_sphinx`
[build_sphinx]
source-dir = docs
build-dir = docs/_build
all_files = 1
builder = html
[tool:isort]
lines_after_imports = 2
force_single_line = 1
no_lines_before = FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
known_first_party = fairtally
src_paths = fairtally,tests,livetests
line_length = 120
82 changes: 46 additions & 36 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,72 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os

from fairtally.__version__ import __version__
from setuptools import setup

here = os.path.abspath(os.path.dirname(__file__))

# To update the package version number, edit CITATION.cff
with open('CITATION.cff', 'r') as cff:
for line in cff:
if 'version:' in line:
version = line.replace('version:', '').strip().strip('"')

with open('README.rst') as readme_file:
with open("README.rst") as readme_file:
readme = readme_file.read()

setup(
name='fairtally',
version=version,
name="fairtally",
entry_points={
"console_scripts": ["fairtally=fairtally.cli:cli"],
},
version=__version__,
description="Make a report based on howfairis results",
long_description=readme + '\n\n',
long_description=readme + "\n\n",
author="FAIR Software",
author_email='[email protected]',
url='https://github.com/fair-software/fairtally',
author_email="[email protected]",
url="https://github.com/fair-software/fairtally",
packages=[
'fairtally',
"fairtally",
],
include_package_data=True,
license="Apache Software License 2.0",
zip_safe=False,
keywords='fairtally',
keywords="fairtally",
classifiers=[
'Development Status :: 2 - Pre-Alpha',
'Intended Audience :: Developers',
'License :: OSI Approved :: Apache Software License',
'Natural Language :: English',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
"Development Status :: 2 - Pre-Alpha",
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Natural Language :: English",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9"
],
test_suite="tests",
install_requires=[
"click == 7.*",
"howfairis == 0.14.*",
"jinja2 == 2.*",
"tqdm == 4.*"
],
test_suite='tests',
install_requires=[], # FIXME: add your package's dependencies to this list
setup_requires=[
# dependency for `python setup.py test`
'pytest-runner',
# dependencies for `python setup.py build_sphinx`
'sphinx',
'sphinx_rtd_theme',
'recommonmark'
],
tests_require=[
'pytest',
'pytest-cov',
'pycodestyle',
],
extras_require={
'dev': ['prospector[with_pyroma]', 'yapf', 'isort'],
"dev": [
"bumpversion",
"prospector[with_pyroma]",
"pycodestyle",
"pytest-cov",
"pytest-runner",
"pytest",
"recommonmark",
"sphinx_rtd_theme",
"sphinx-click",
"sphinx",
"yapf"
],
"publishing": [
"twine",
"wheel"
]
},
data_files=[('citation/fairtally', ['CITATION.cff'])]
data_files=[("citation/fairtally", ["CITATION.cff"])]
)
2 changes: 0 additions & 2 deletions tests/test_fairtally.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
"""
import pytest

from fairtally import fairtally


def test_something():
assert True
Expand Down