Пример #1
0
def indent(text, prefix):
    """ String indentation """
    try:
        from textwrap import indent as _indent
    except ImportError:  # undefined function (wasn't added until Python 3.3)

        def _indent(text, prefix):  # Currently not supporting predicate arg
            return ''.join(prefix + line for line in text.splitlines(True))

    return _indent(text, prefix)
Пример #2
0
def indent(text, prefix):
    """ String indentation """
    try:
        from textwrap import indent as _indent
    except ImportError:  # undefined function (wasn't added until Python 3.3)
        def _indent(text, prefix, predicate=None):
            if predicate:
                warnings.warn("'predicate' argument not used", RuntimeWarning)
            return ''.join(prefix+line for line in text.splitlines(True))

    return _indent(text, prefix)
Пример #3
0
def linux_install_udev_rules():
    """verify and install the udev rules"""
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--overwrite-existing",
        help="overwrite rules if already present",
        action="store_true",
    )
    parser.add_argument(
        "rules",
        help="rules file (default: download from github)",
        default="",
        nargs="?",
    )
    args = parser.parse_args()

    if args.rules:
        if not os.path.exists(args.rules):
            raise IOError("rules file '{}' doesn't exist".format(args.rules))
        udev_tmp_file = None
        udev_fn = args.rules

    else:
        udev_tmp_file = tempfile.NamedTemporaryFile()
        udev_fn = udev_tmp_file.name

    try:
        # download rules from github if no file is provided
        if udev_tmp_file is not None:
            url = "{}/{}".format(_GITHUB_REPO_URL,
                                 os.path.basename(_UDEV_RULES_PATH))
            try:
                _log.info("downloading rules from github")
                udev_data = urlopen(url).read()
            except HTTPError:
                _log.error("can't download '{}'".format(url))
                sys.exit(1)
            udev_tmp_file.write(udev_data)
            udev_tmp_file.flush()

        # check if rules need to be overwritten
        if os.path.exists(_UDEV_RULES_PATH) and not args.overwrite_existing:

            rules_differ = _diff_files(_UDEV_RULES_PATH, udev_fn)
            if not rules_differ:
                _log.info("udev rules already newest version")
                sys.exit(0)
            else:
                _log.info(_indent(rules_differ, u"  ").rstrip())
                _log.info(
                    "udev rules differ. To overwrite run with '--overwrite-existing'"
                )
                sys.exit(1)

        if not _request_confirmation("Install udev rules?"):
            sys.exit(0)

        # cp rules and execute
        _log.info("Copying udev rules to {}".format(_UDEV_RULES_PATH))
        subprocess.call(["sudo", "cp", udev_fn, _UDEV_RULES_PATH])
        _log.info("Calling udevadm control --reload-rules")
        subprocess.call(["sudo", "udevadm", "control", "--reload-rules"])
        _log.info("Success")
        sys.exit(0)

    finally:
        if udev_tmp_file is not None:
            udev_tmp_file.close()  # removes tempfile
Пример #4
0
def indent(val):
    res = _indent(val, "    ")
    return res
Пример #5
0
def main_parser():  # type: () -> argparse.ArgumentParser
    """
    Construct the main parser.
    """
    # mypy does not recognize module.__path__
    # https://github.com/python/mypy/issues/1422
    paths = build.__path__  # type: Iterable[Optional[str]]  # type: ignore
    parser = argparse.ArgumentParser(
        description=_indent(  # textwrap.indent
            textwrap.dedent(
                '''
                A simple, correct PEP 517 package builder.

                By default, a source distribution (sdist) is built from {srcdir}
                and a binary distribution (wheel) is built from the sdist.
                This is recommended as it will ensure the sdist can be used
                to build wheels.

                Pass -s/--sdist and/or -w/--wheel to build a specific distribution.
                If you do this, the default behavior will be disabled, and all
                artifacts will be built from {srcdir} (even if you combine
                -w/--wheel with -s/--sdist, the wheel will be built from {srcdir}).
                '''
            ).strip(),
            '    ',
        ),
        formatter_class=argparse.RawTextHelpFormatter,
    )
    parser.add_argument(
        'srcdir',
        type=str,
        nargs='?',
        default=os.getcwd(),
        help='source directory (defaults to current directory)',
    )
    parser.add_argument(
        '--version',
        '-V',
        action='version',
        version='build {} ({})'.format(build.__version__, ', '.join(path for path in paths if path)),
    )
    parser.add_argument(
        '--sdist',
        '-s',
        action='store_true',
        help='build a source distribution (disables the default behavior)',
    )
    parser.add_argument(
        '--wheel',
        '-w',
        action='store_true',
        help='build a wheel (disables the default behavior)',
    )
    parser.add_argument(
        '--outdir',
        '-o',
        type=str,
        help='output directory (defaults to {{srcdir}}{sep}dist)'.format(sep=os.sep),
    )
    parser.add_argument(
        '--skip-dependency-check',
        '-x',
        action='store_true',
        help='do not check that build dependencies are installed',
    )
    parser.add_argument(
        '--no-isolation',
        '-n',
        action='store_true',
        help='do not isolate the build in a virtual environment',
    )
    parser.add_argument(
        '--config-setting',
        '-C',
        action='append',
        help='pass options to the backend.  options which begin with a hyphen must be in the form of '
        '"--config-setting=--opt(=value)" or "-C--opt(=value)"',
    )
    return parser