Skip to content

Commit

Permalink
Merge pull request sagemath#130 from marcelotrevisani/fb-125-disable-…
Browse files Browse the repository at this point in the history
…output

Add possibility to disable output
  • Loading branch information
marcelotrevisani authored May 25, 2020
2 parents 46784de + 3366d9e commit 1c10a47
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 27 deletions.
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pr:
variables:
- group: Codecov
- name: grayskull_deps
value: pytest pytest-azurepipelines pytest-xdist pytest-cov pytest-forked requests ruamel.yaml codecov ruamel.yaml.jinja2 "coverage<5.0" stdlib-list pip setuptools mock rapidfuzz git colorama progressbar2
value: pytest pytest-azurepipelines pytest-xdist pytest-cov pytest-forked requests ruamel.yaml "codecov>=2.1.3" ruamel.yaml.jinja2 coverage stdlib-list pip setuptools mock rapidfuzz git colorama progressbar2

jobs:
- job:
Expand Down
21 changes: 17 additions & 4 deletions grayskull/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

import grayskull
from grayskull.base.factory import GrayskullFactory
from grayskull.cli import CLIConfig
from grayskull.cli.parser import parse_pkg_name_version
from grayskull.cli.stdout import print_msg

colorama.init(autoreset=True)
logging.basicConfig(format="%(levelname)s:%(message)s")
Expand Down Expand Up @@ -64,6 +66,13 @@ def main(args=None):
default=".",
help="Path to where the recipe will be created",
)
pypi_cmds.add_argument(
"--stdout",
dest="stdout",
default=True,
help="Disable or enable stdout, if it is False, Grayskull"
" will disable the prints. Default is True",
)

args = parser.parse_args(args)

Expand All @@ -72,18 +81,22 @@ def main(args=None):
return

logging.debug(f"All arguments received: args: {args}")
print(Style.RESET_ALL)
print(clear_screen())

if args.grayskull_power:
print(
f"{Fore.BLUE}By the power of Grayskull...\n"
f"{Style.BRIGHT}I have the power!"
)
return

CLIConfig().stdout = args.stdout

print_msg(Style.RESET_ALL)
print_msg(clear_screen())

for pkg_name in args.pypi_packages:
logging.debug(f"Starting grayskull for pkg: {pkg_name}")
print(
print_msg(
f"{Fore.GREEN}\n\n"
f"#### Initializing recipe for "
f"{Fore.BLUE}{pkg_name} (pypi) {Fore.GREEN}####\n"
Expand All @@ -93,7 +106,7 @@ def main(args=None):
"pypi", pkg_name, pkg_version, download=args.download
)
recipe.generate_recipe(args.output, mantainers=args.maintainers)
print(
print_msg(
f"\n{Fore.GREEN}#### Recipe generated on "
f"{os.path.realpath(args.output)} for {pkg_name} ####\n"
)
Expand Down
3 changes: 2 additions & 1 deletion grayskull/base/base_recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from grayskull.base.extra import get_git_current_user
from grayskull.base.recipe_item import RecipeItem
from grayskull.base.section import Section
from grayskull.cli.stdout import print_msg

yaml = YAML(typ="jinja2")
yaml.indent(mapping=2, sequence=4, offset=2)
Expand Down Expand Up @@ -199,7 +200,7 @@ def _add_extra_section(self, maintainers: Optional[List] = None):
maintainers = maintainers if maintainers else [get_git_current_user()]
self["extra"]["recipe-maintainers"].add_items(maintainers)
prefix = f"{Fore.LIGHTBLACK_EX}\n - {Fore.LIGHTMAGENTA_EX}"
print(f"{Fore.LIGHTBLACK_EX}Maintainers:{prefix}{prefix.join(maintainers)}")
print_msg(f"{Fore.LIGHTBLACK_EX}Maintainers:{prefix}{prefix.join(maintainers)}")

def get_clean_yaml(self, recipe_yaml: CommentedMap) -> CommentedMap:
result = self._clean_yaml(recipe_yaml)
Expand Down
4 changes: 3 additions & 1 deletion grayskull/base/extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import requests
from colorama import Fore

from grayskull.cli.stdout import print_msg

log = logging.getLogger(__name__)


Expand All @@ -27,7 +29,7 @@ def get_git_current_user() -> str:
f"Exception occurred when trying to recover user information from github."
f" Exception: {err}"
)
print(
print_msg(
f"{Fore.LIGHTBLACK_EX}Using default recipe maintainer:"
f" {Fore.LIGHTMAGENTA_EX}AddYourGitHubIdHere"
)
Expand Down
12 changes: 12 additions & 0 deletions grayskull/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

import progressbar

WIDGET_BAR_DOWNLOAD = [
Expand All @@ -8,3 +10,13 @@
progressbar.AdaptiveTransferSpeed(),
progressbar.Bar(),
]


class CLIConfig:
__instance: Optional["CLIConfig"] = None

def __new__(cls, stdout: bool = False):
if CLIConfig.__instance is None:
CLIConfig.__instance = object.__new__(cls)
CLIConfig.__instance.stdout = stdout
return CLIConfig.__instance
27 changes: 27 additions & 0 deletions grayskull/cli/stdout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from contextlib import contextmanager
from copy import deepcopy

from progressbar import ProgressBar

from grayskull.cli import WIDGET_BAR_DOWNLOAD, CLIConfig


def print_msg(msg: str):
if CLIConfig().stdout:
print(msg)


@contextmanager
def manage_progressbar(*, max_value: int, prefix: str):
if CLIConfig().stdout:
with ProgressBar(
widgets=deepcopy(WIDGET_BAR_DOWNLOAD), max_value=max_value, prefix=prefix,
) as bar:
yield bar
else:

class DisabledBar:
def update(self, *args, **kargs):
pass

yield DisabledBar()
11 changes: 7 additions & 4 deletions grayskull/license/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from rapidfuzz.fuzz import token_set_ratio, token_sort_ratio
from requests import HTTPError

from grayskull.cli.stdout import print_msg
from grayskull.license.data import get_all_licenses # noqa

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -43,7 +44,7 @@ def get_all_licenses_from_spdx() -> List:
raise HTTPError(
f"It was not possible to communicate with spdx api.\n{response.text}"
)
print(f"{Fore.LIGHTBLACK_EX}Recovering license info from spdx.org ...")
print_msg(f"{Fore.LIGHTBLACK_EX}Recovering license info from spdx.org ...")
return [
lic
for lic in response.json()["licenses"]
Expand Down Expand Up @@ -213,7 +214,7 @@ def search_license_api_github(
"""
github_url = _get_api_github_url(github_url, version)
log.info(f"Github url: {github_url} - recovering license info")
print(f"{Fore.LIGHTBLACK_EX}Recovering license information from github...")
print_msg(f"{Fore.LIGHTBLACK_EX}Recovering license information from github...")

response = requests.get(url=github_url, timeout=10)
if response.status_code != 200:
Expand Down Expand Up @@ -276,7 +277,7 @@ def search_license_repo(
"""
git_url = re.sub(r"/$", ".git", git_url)
git_url = git_url if git_url.endswith(".git") else f"{git_url}.git"
print(f"{Fore.LIGHTBLACK_EX}Recovering license info from repository...")
print_msg(f"{Fore.LIGHTBLACK_EX}Recovering license info from repository...")
tmp_dir = mkdtemp(prefix="gs-clone-repo-")
try:
check_output(_get_git_cmd(git_url, version, tmp_dir))
Expand Down Expand Up @@ -321,7 +322,9 @@ def get_license_type(path_license: str, default: Optional[str] = None) -> Option
if find_apache:
lic_type = find_apache[0]
return f"Apache-{lic_type[0]}.{lic_type[1]}"
print(f"{Fore.LIGHTBLACK_EX}Matching license file with database from Grayskull...")
print_msg(
f"{Fore.LIGHTBLACK_EX}Matching license file with database from Grayskull..."
)
all_licenses = get_all_licenses()
licenses_text = list(map(itemgetter(1), all_licenses))
best_match = process.extract(
Expand Down
33 changes: 17 additions & 16 deletions grayskull/pypi/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@

import requests
from colorama import Fore, Style
from progressbar import ProgressBar
from requests import HTTPError

from grayskull.base.base_recipe import AbstractRecipeModel
from grayskull.base.track_packages import solve_list_pkg_name
from grayskull.cli import WIDGET_BAR_DOWNLOAD
from grayskull.cli.stdout import manage_progressbar, print_msg
from grayskull.license.discovery import ShortLicense, search_license_file
from grayskull.utils import get_vendored_dependencies

Expand Down Expand Up @@ -58,19 +57,17 @@ def _download_sdist_pkg(sdist_url: str, dest: str):
:param dest: Folder were the method will download the sdist
"""
name = sdist_url.split("/")[-1]
print(
print_msg(
f"{Fore.GREEN}Starting the download of the sdist package"
f" {Fore.BLUE}{Style.BRIGHT}{name}"
)
log.debug(f"Downloading {name} sdist - {sdist_url}")
response = requests.get(sdist_url, allow_redirects=True, stream=True, timeout=5)
total_size = int(response.headers["Content-length"])

with ProgressBar(
widgets=deepcopy(WIDGET_BAR_DOWNLOAD),
max_value=total_size,
prefix=f"{name} ",
) as bar, open(dest, "wb") as pkg_file:
with manage_progressbar(max_value=total_size, prefix=f"{name} ") as bar, open(
dest, "wb"
) as pkg_file:
progress_val = 0
chunk_size = 512
for chunk_data in response.iter_content(chunk_size=chunk_size):
Expand All @@ -97,7 +94,7 @@ def _get_sdist_metadata(self, sdist_url: str, name: str) -> dict:
self.files_to_copy.append(path_pkg)
log.debug(f"Unpacking {path_pkg} to {temp_folder}")
shutil.unpack_archive(path_pkg, temp_folder)
print(f"{Fore.LIGHTBLACK_EX}Recovering information from setup.py")
print_msg(f"{Fore.LIGHTBLACK_EX}Recovering information from setup.py")
with PyPi._injection_distutils(temp_folder) as metadata:
metadata["sdist_path"] = temp_folder
return metadata
Expand Down Expand Up @@ -163,7 +160,7 @@ def _get_setup_cfg(source_path: str) -> dict:
from setuptools.config import read_configuration

log.debug(f"Started setup.cfg from {source_path}")
print(f"{Fore.LIGHTBLACK_EX}Recovering metadata from setup.cfg")
print_msg(f"{Fore.LIGHTBLACK_EX}Recovering metadata from setup.cfg")
path_setup_cfg = list(Path(source_path).rglob("setup.cfg"))
if not path_setup_cfg:
return {}
Expand Down Expand Up @@ -242,10 +239,10 @@ def __fake_distutils_setup(*args, **kwargs):
try:
core.setup = __fake_distutils_setup
path_setup = str(path_setup)
print(f"{Fore.LIGHTBLACK_EX}Executing injected distutils...")
print_msg(f"{Fore.LIGHTBLACK_EX}Executing injected distutils...")
PyPi.__run_setup_py(path_setup, data_dist)
if not data_dist or not data_dist.get("install_requires", None):
print(
print_msg(
f"{Fore.LIGHTBLACK_EX}No data was recovered from setup.py."
f" Forcing to execute the setup.py as script"
)
Expand Down Expand Up @@ -517,8 +514,12 @@ def _get_metadata(self) -> dict:
license_file = os.path.basename(license_metadata.path)
self.files_to_copy.append(license_metadata.path)

print(f"{Fore.LIGHTBLACK_EX}License type: {Fore.LIGHTMAGENTA_EX}{license_name}")
print(f"{Fore.LIGHTBLACK_EX}License file: {Fore.LIGHTMAGENTA_EX}{license_file}")
print_msg(
f"{Fore.LIGHTBLACK_EX}License type: {Fore.LIGHTMAGENTA_EX}{license_name}"
)
print_msg(
f"{Fore.LIGHTBLACK_EX}License file: {Fore.LIGHTMAGENTA_EX}{license_file}"
)

all_requirements = self._extract_requirements(metadata)
all_requirements["host"] = solve_list_pkg_name(
Expand All @@ -530,7 +531,7 @@ def _get_metadata(self) -> dict:

def print_req(name, list_req: List):
prefix_req = f"{Fore.LIGHTBLACK_EX}\n - {Fore.LIGHTCYAN_EX}"
print(
print_msg(
f"{Fore.LIGHTBLACK_EX}{name} requirements:"
f"{prefix_req}{prefix_req.join(list_req)}"
)
Expand Down Expand Up @@ -617,7 +618,7 @@ def _get_pypi_metadata(self, name, version: Optional[str] = None) -> dict:
:param version: Package version
:return: Pypi metadata
"""
print(f"{Fore.LIGHTBLACK_EX}Recovering metadata from pypi...")
print_msg(f"{Fore.LIGHTBLACK_EX}Recovering metadata from pypi...")
if version:
url_pypi = PyPi.URL_PYPI_METADATA.format(pkg_name=f"{name}/{version}")
else:
Expand Down
30 changes: 30 additions & 0 deletions tests/cli/test_stdout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from progressbar import ProgressBar

from grayskull.cli import CLIConfig
from grayskull.cli.stdout import manage_progressbar, print_msg


def test_print_stdout(capsys):
CLIConfig().stdout = True
print_msg("TEST-OUTPUT")
captured_out = capsys.readouterr()
assert captured_out.out == "TEST-OUTPUT\n"


def test_disabled_print(capsys):
CLIConfig().stdout = False
print_msg("TEST-OUTPUT")
captured_out = capsys.readouterr()
assert captured_out.out == ""


def test_progressbar_enable():
CLIConfig().stdout = True
with manage_progressbar(max_value=100, prefix="prefix-") as bar:
assert isinstance(bar, ProgressBar)


def test_progressbar_disable():
CLIConfig().stdout = False
with manage_progressbar(max_value=100, prefix="prefix-") as bar:
assert not isinstance(bar, ProgressBar)

0 comments on commit 1c10a47

Please sign in to comment.