Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / tools / site_sweep / module.lucb

#==============================================================================================
#
#   site_sweep - loads real websites headlessly, one process each, and reports how each went
#
#   DESCRIPTION:
#       A survey tool, not a test: it shows which unported functions real pages reach. One
#       program with two modes, as web_test has:
#
#         site_sweep [URL ...] [--sites FILE] [-j N] [--timeout S] [--resources DIR] [--watch]
#             The driver: runs a worker per site (the URLs given, else the lines of FILE,
#             tools/site_sweep/sites.txt by default; `#` starts a comment), at most N at a time
#             (4 by default, to stay polite), stops one after S seconds (90 by default), and
#             prints a line per site (its outcome and detail), then the traps grouped by message
#             with their counts.
#
#         site_sweep --one URL [--resources DIR] [--width W] [--height H] [--scale DPR] [--watch]
#                    [--png FILE]
#             The worker: loads URL the way luced-browser does (webview_api: the system's fonts,
#             the address-field sanitizing, scripting disabled; with --watch, the engine hands
#             its sockets to the worker's loop, which sleeps until one is ready or the engine is
#             due, as luced-browser's window loop does), waits for its load to finish
#             and for a frame of the loaded page, lets it settle for two seconds (timers, late
#             images), and prints "site_sweep: result ok|blank|timeout DETAIL". A page whose
#             frame is a single color is blank. --png writes the frame it judged. A trap ends the worker; the driver reads its
#             message ("trap: FILE:LINE:COLUMN: MESSAGE") from the worker's output.
#
#       Run from the repository's root (the resources go to build/site_sweep_res by default):
#
#           luce-base build tools/site_sweep -o build/site_sweep && build/site_sweep
#
#       Copyright (c) 2026 the luce-browser authors.
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

import c
import io
import memory
import os
import strings
import thread
import time
import platform
from luce_std import files, net, process
from luce_browser_foundation import ak
from luce_png import png
from luce_raster.binary import Buffer
import webview
import webview_api

# mark: The command line ========================================================================

## What the command line asked for.
struct Options:
    ## Worker mode: the site to load (--one).
    var one: str?
    ## Driver mode: the sites given on the command line.
    var urls: ak.Vector[str]
    ## Driver mode: the file listing the sites when none is given (--sites).
    var sites: str = "tools/site_sweep/sites.txt"
    ## Driver mode: workers run at once (-j).
    var jobs: usize = 4
    ## Driver mode: seconds before a worker is stopped (--timeout).
    var timeout_seconds: u64 = 90
    ## Where the engine's resources are written (--resources).
    var resources: str = "build/site_sweep_res"
    ## Worker mode: the view's size in points and its device pixel ratio.
    var width: f64 = 1280.0
    var height: f64 = 800.0
    var scale: f64 = 1.0
    ## Whether the loop watches the engine's sockets itself, as luced-browser's does (--watch).
    var watch: bool = false
    ## Worker mode: where to write the judged frame as a PNG (--png).
    var png_path: str?

## The usage text.
func usage():
    io.stderr().write("usage: site_sweep [URL ...] [--sites FILE] [-j N] [--timeout SECONDS] [--resources DIR] [--watch]\n") catch failure: return
    io.stderr().write("       site_sweep --one URL [--resources DIR] [--width W] [--height H] [--scale DPR] [--watch] [--png FILE]\n") catch failure: return

## The options of `arguments` (the program name first), or none when they are not understood.
func parse_options(arguments: const str[]) -> Options?:
    var options = Options()
    var index: usize = 1
    while index < arguments.length:
        let argument = arguments[index]
        index += 1
        let has_value = index < arguments.length
        if argument == "--one" and has_value:
            options.one = arguments[index]
        elif argument == "--sites" and has_value:
            options.sites = arguments[index]
        elif argument == "-j" and has_value:
            let jobs = strings.parse_u64(arguments[index]) else return none
            options.jobs = (usize)jobs if jobs > 0 else 1
        elif argument == "--timeout" and has_value:
            options.timeout_seconds = strings.parse_u64(arguments[index]) else return none
        elif argument == "--resources" and has_value:
            options.resources = arguments[index]
        elif argument == "--width" and has_value:
            options.width = strings.parse_f64(arguments[index]) catch failure: return none
        elif argument == "--height" and has_value:
            options.height = strings.parse_f64(arguments[index]) catch failure: return none
        elif argument == "--scale" and has_value:
            options.scale = strings.parse_f64(arguments[index]) catch failure: return none
        elif argument == "--png" and has_value:
            options.png_path = arguments[index]
        elif argument == "--watch":
            options.watch = true
            continue
        elif strings.starts_with(argument, "-"):
            return none
        else:
            ak.vector_append_t(&options.urls, argument)
            continue
        index += 1
    return options

# mark: Text ====================================================================================

## The concatenation of `pieces`.
func cat(pieces: const str[]) -> str:
    return strings.join(pieces, "") catch failure: trap("out of memory")

## `path` as a C string (a NUL-terminated copy).
func c_path(path: str) -> c.str:
    return (c.str)(strings.copy(path) catch failure: trap("out of memory"))

## `value` in decimal.
func decimal(value: u64) -> str:
    var buffer: u8[24] = ---
    let text = strings.format_u64(value, buffer) catch failure: return "?"
    return strings.copy(text) catch failure: trap("out of memory")

# mark: main ====================================================================================

pub func main(arguments: str[]) -> i32:
    if let options = parse_options(arguments):
        if let url = options.one:
            return worker_main(url, &options)
        return driver_main(&options)
    usage()
    return 2