Commit 47d731eb authored by Mickaël Desfrênes's avatar Mickaël Desfrênes
Browse files

start command trying ports when requested ones are not available

parent a83c565b
Loading
Loading
Loading
Loading
+1 −1
Changes for pyproject.toml: 1 added line, 1 removed line.
Original line number Diff line number Diff line
[project]
name = "climax-CERTIC"
version = "0.1.7"
version = "0.1.8"
description = "CLI tool for MaX, Le Moteur d'Affichage XML."
readme = "README.md"
license = {text = "CECILL-C"}
+21 −3
Changes for src/climax/__main__.py: 21 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -60,7 +60,7 @@ JAVA_DISTROS = {
    "windows/amd64": "https://download.java.net/java/GA/jdk22.0.2/c9ecb94cd31b495da20a27d4581645e8/9/GPL/openjdk-22.0.2_windows-x64_bin.zip",
}
WEB_PORT = 8080
WEB_HOST = "localhost"
WEB_HOST = "127.0.0.1"
BASEX_PORT = 1984
STOP_PORT = 8081
CONFIG_INIT_TEMPLATE = """<?xml version="1.0" encoding="UTF-8"?>
@@ -79,6 +79,19 @@ CONFIG_INIT_TEMPLATE = """<?xml version="1.0" encoding="UTF-8"?>
"""


def _port_is_in_use(port: int) -> bool:
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        return s.connect_ex(("localhost", port)) == 0


def _closer_free_port_to(start_port: int) -> int:
    limit_iter = 0
    while _port_is_in_use(start_port) and limit_iter < 50:
        start_port = start_port + 1
        limit_iter = limit_iter + 1
    return start_port


def _free_port() -> int:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(("localhost", 0))
@@ -666,6 +679,11 @@ def start(
        ),
    ] = False,
):
    basex_port = _closer_free_port_to(basex_port)
    http_port = _closer_free_port_to(http_port)
    if http_stop_port < http_port:
        http_stop_port = http_port + 1
    http_stop_port = _closer_free_port_to(http_stop_port)
    check_cwd_is_max()
    java_bin_path = ensure_java()
    process_args = [
@@ -677,10 +695,10 @@ def start(
        f"-p{basex_port}",
        f"-h{http_port}",
        f"-n{http_host}",
        f"-s{http_stop_port}",
    ]
    if service:
        process_args.append("-S")
        process_args.append(f"-s{http_stop_port}")
    print(
        "[green]"
        + _("Démarrage de MaX sur http://{}:{}").format(http_host, http_port)
@@ -717,6 +735,7 @@ def freeze(
    # start server on specific port
    port_number = _free_port()
    stop_port = port_number + 1
    stop_port = _closer_free_port_to(stop_port)
    start(WEB_HOST, port_number, http_stop_port=stop_port, service=True)
    # copy website
    try:
@@ -874,7 +893,6 @@ def feed(xml_file: Path):
def basex():
    check_cwd_is_max()
    java_bin_path = ensure_java()
    # "if(not(db:exists('max'))) then db:create('max') else ()"
    process_args = [
        str(java_bin_path),
        "-cp",