Exemplo n.º 1
0
def replace_in_file(
    filename: PathLike, replacements: List[Substitution], config: Config
):
    print(f"Updating {filename!s}")
    with open(filename, "r", newline="") as file:
        contents = file.read()
    for replacement in replacements:
        pattern = common.resolve(replacement.search, config)
        repl = common.resolve(replacement.replace, config)
        contents = re.sub(pattern, repl, contents)
    with open(filename, "w", newline="") as file:
        file.write(contents)
Exemplo n.º 2
0
def package(config: Config, file_list: ZipFiles, verbose: bool) -> None:
    package = config.package

    compression = package.compression_value
    name = common.resolve(package.filename, config)
    outdir = common.resolve_path(package.output_dir, config)
    archive = outdir / name

    if verbose:
        print(f"Packaging {archive!s}")

    archive.parent.mkdir(parents=True, exist_ok=True)

    if compression is None:
        archive = archive.with_suffix("")
        archive.mkdir(exist_ok=True)
        for src, _dst in file_list.items():
            dst = archive / _dst
            dst.parent.mkdir(parents=True, exist_ok=True)
            if dst.is_dir():
                continue
            if verbose:
                print(f"Writing {src!s} -> {dst!s}")
            shutil.copyfile(src, dst)
    else:
        with zipfile.ZipFile(archive, "w", compression=compression) as zip:
            for src, dst in file_list.items():
                if verbose:
                    print(f"Writing {src!s} -> {dst!s}")
                zip.write(src, dst)

    print(archive)
Exemplo n.º 3
0
def replace_in_file_all(src: PathLike, dst: PathLike, config: Config):
    print(f"Updating {src!s} -> {dst!s}")
    with open(src, "r", newline="") as file:
        contents = file.read()

    contents = common.resolve(contents, config)

    with open(dst, "w", newline="") as file:
        file.write(contents)
Exemplo n.º 4
0
def replace(config: Config) -> None:
    for patterns in config.replace.regex:
        for filename in config.glob(common.resolve(patterns.pattern, config)):
            replace_in_file(filename, patterns.substitutions, config)

    for files in config.replace.template_files:
        src = common.resolve_path(files.source, config)
        dst = common.resolve_path(files.destination, config)

        replace_in_file_all(src, dst, config)
Exemplo n.º 5
0
def install(mapping: Iterable[FileCopy], config: Config):
    for item in mapping:
        src = common.resolve(item.source, config)
        dst = common.resolve_path(item.destination, config)

        for path in config.glob(src):
            if path.is_dir():
                print(f"Copying tree {path!s} -> {dst!s}")
                shutil.copytree(path, dst)
            else:
                print(f"Copying file {path!s} -> {dst!s}")
                shutil.copy(path, dst)
Exemplo n.º 6
0
def add_value_option(
    args: List[str],
    target: BurstTarget,
    name: str,
    config: Config,
    is_path: bool = False,
) -> None:
    option = getattr(target, name, None)
    if option is None:
        return

    if is_path:
        option = common.resolve_path(option, config)
    else:
        option = common.resolve(option, config)

    args.append(f"--{name.replace('_', '-')}={option}")
Exemplo n.º 7
0
def add_option(
    args: List[str],
    target: BurstTarget,
    name: str,
    config: Config,
    if_false: Optional[str] = None,
) -> None:
    option = getattr(target, name, None)
    if option is None:
        return

    option = common.resolve(option, config)

    if option:
        args.append(f"--{name.replace('_', '-')}")
    elif if_false is not None:
        args.append(if_false)