コード例 #1
0
def gitlab_logs(request):
    # put logs in tests/logs
    log_path = Path("tests") / "logs"

    # tidy logs in subdirectories based on test module and class names
    module = request.module
    class_ = request.cls
    name = request.node.name + ".log"

    if module:
        log_path /= module.__name__.replace("tests.", "")
    if class_:
        log_path /= class_.__name__

    log_path.mkdir(parents=True, exist_ok=True)

    # append last part of the name and enable logger
    log_path /= name
    if log_path.exists():
        log_path.unlink()
    enable_logger(sink=log_path,
                  level=os.environ.get("PYTEST_LOG_LEVEL", "TRACE"))
コード例 #2
0
ファイル: main.py プロジェクト: back8/github_pawamoy_aria2p
def main(args: Optional[List[str]] = None) -> int:
    """
    Run the main program.

    This function is executed when you type `aria2p` or `python -m aria2p`.

    Arguments:
        args: Arguments passed from the command line.

    Returns:
        An exit code.
    """
    parser = get_parser()
    opts = parser.parse_args(args=args)
    kwargs = opts.__dict__  # noqa: WPS609 (special attribute)

    log_level = kwargs.pop("log_level")
    log_path = kwargs.pop("log_path")

    if log_path:
        log_path = Path(log_path)
        if log_path.is_dir():
            log_path = log_path / "aria2p-{time}.log"
        enable_logger(sink=log_path, level=log_level or "WARNING")
    elif log_level:
        enable_logger(sink=sys.stderr, level=log_level)

    logger.debug("Checking arguments")
    check_args(parser, opts)

    logger.debug("Instantiating API")
    api = API(
        Client(
            host=kwargs.pop("host"),
            port=kwargs.pop("port"),
            secret=kwargs.pop("secret"),
            timeout=kwargs.pop("client_timeout"),
        ),
    )

    logger.info(f"API instantiated: {api!r}")

    # Warn if no aria2 daemon process seems to be running
    logger.debug("Testing connection")
    try:
        api.client.get_version()
    except requests.ConnectionError as error:
        print(f"[ERROR] {error}", file=sys.stderr)
        print(file=sys.stderr)
        print("Please make sure that an instance of aria2c is running with RPC mode enabled,", file=sys.stderr)
        print("and that you have provided the right host, port and secret token.", file=sys.stderr)
        print("More information at https://pawamoy.github.io/aria2p.", file=sys.stderr)
        return 2

    subcommand = kwargs.pop("subcommand")

    if subcommand:
        logger.debug("Running subcommand " + subcommand)
    try:
        return commands[subcommand](api, **kwargs)  # type: ignore
    except ClientException as error:  # noqa: WPS440 (variable overlap)
        print(str(error), file=sys.stderr)
        return error.code
コード例 #3
0
ファイル: cli.py プロジェクト: cargo12/aria2p
def main(args: Optional[List[str]] = None) -> int:
    """The main function, which is executed when you type ``aria2p`` or ``python -m aria2p``."""

    parser = get_parser()
    args = parser.parse_args(args=args)
    kwargs = args.__dict__

    log_level = kwargs.pop("log_level")
    log_path = kwargs.pop("log_path")

    if log_path:
        log_path = Path(log_path)
        if log_path.is_dir():
            log_path = log_path / "aria2p-{time}.log"
        enable_logger(sink=log_path, level=log_level or "WARNING")
    elif log_level:
        enable_logger(sink=sys.stderr, level=log_level)

    logger.debug("Checking arguments")
    check_args(parser, args)

    logger.debug("Instantiating API")
    api = API(
        Client(host=kwargs.pop("host"),
               port=kwargs.pop("port"),
               secret=kwargs.pop("secret")))

    logger.info(f"API instantiated: {api!r}")

    # Warn if no aria2 daemon process seems to be running
    logger.debug("Testing connection")
    try:
        api.client.get_version()
    except requests.ConnectionError as error:
        print(f"[ERROR] {error}", file=sys.stderr)
        print(file=sys.stderr)
        print(
            "Please make sure that an instance of aria2c is running with RPC mode enabled,",
            file=sys.stderr)
        print(
            "and that you have provided the right host, port and secret token.",
            file=sys.stderr)
        print("More information at https://aria2p.readthedocs.io/en/latest.",
              file=sys.stderr)
        return 2

    subcommands = {
        None: subcommand_top,
        "show": subcommand_show,
        "top": subcommand_top,
        "call": subcommand_call,
        "add": subcommand_add,
        "add-magnet": subcommand_add_magnets,
        "add-magnets": subcommand_add_magnets,
        "add-torrent": subcommand_add_torrents,
        "add-torrents": subcommand_add_torrents,
        "add-metalink": subcommand_add_metalinks,
        "add-metalinks": subcommand_add_metalinks,
        "pause": subcommand_pause,
        "stop": subcommand_pause,  # alias for pause
        "resume": subcommand_resume,
        "start": subcommand_resume,  # alias for resume
        "remove": subcommand_remove,
        "rm": subcommand_remove,  # alias for remove
        "del": subcommand_remove,  # alias for remove
        "delete": subcommand_remove,  # alias for remove
        "autopurge": subcommand_autopurge,
        "autoclear": subcommand_autopurge,  # alias for autopurge
        "listen": subcommand_listen,
    }

    subcommand = kwargs.pop("subcommand")

    if subcommand:
        logger.debug("Running subcommand " + subcommand)
    try:
        return subcommands.get(subcommand)(api, **kwargs)
    except ClientException as error:
        print(error.message, file=sys.stderr)
        return error.code