add delete option for precompilation, closes #4

This commit is contained in:
Florian Förster 2025-04-03 14:41:27 +02:00
parent 7c632047e4
commit e04e42c70a
2 changed files with 34 additions and 3 deletions

View File

@ -1,6 +1,6 @@
[project]
name = "pycage"
version = "0.2.2"
version = "0.2.3"
description = "tool to handle standalone Python installations from the CLI"
authors = [
{name = "Florian Förster", email = "f.foerster@d-opt.com"},
@ -78,7 +78,7 @@ directory = "reports/coverage"
[tool.bumpversion]
current_version = "0.1.1"
current_version = "0.2.3"
parse = """(?x)
(?P<major>0|[1-9]\\d*)\\.
(?P<minor>0|[1-9]\\d*)\\.
@ -111,7 +111,7 @@ pre_commit_hooks = []
post_commit_hooks = []
[tool.bumpversion.parts.pre_l]
values = ["dev", "a", "b", "rc", "final"]
values = ["dev", "rc", "final"]
optional_value = "final"
[[tool.bumpversion.files]]

View File

@ -4,10 +4,27 @@ import subprocess
import click
from pycage import clean
from pycage.helpers import get_interpreter, print_error
@click.command(help="precompile all Python files in the standalone distribution")
@click.option(
"-q",
"--quiet",
is_flag=True,
default=False,
show_default=True,
help=("do not list compiling files during procedure"),
)
@click.option(
"-d",
"--delete-existing",
is_flag=True,
default=False,
show_default=True,
help=("delete other pre-compiled files before compilation"),
)
@click.option(
"-f",
"--force",
@ -33,6 +50,8 @@ from pycage.helpers import get_interpreter, print_error
def compile(
optimise: int,
force: bool,
delete_existing: bool,
quiet: bool,
) -> None:
try:
pth_intp = get_interpreter()
@ -40,12 +59,23 @@ def compile(
click.echo("Base interpreter path could not be found", err=True)
return
if delete_existing:
try:
clean.delete_files(r"**/*.pyc")
except Exception as err:
print_error(err)
return
opt_level: str = ""
if optimise == 1:
opt_level = "-O"
elif optimise == 2:
opt_level = "-OO"
quiet_opt: str = ""
if quiet:
quiet_opt = "-q"
exclude_rx: str = "[\\\\|/]+tcl[\\\\|/]+"
run_cmd: list[str] = [
str(pth_intp),
@ -57,6 +87,7 @@ def compile(
exclude_rx,
"-o",
str(optimise),
quiet_opt,
]
if force:
run_cmd.extend(("-f",))