mirror of
https://github.com/daijro/camoufox.git
synced 2026-11-19 00:07:28 +00:00
Remove dead and duplicated code across the launchers, scripts and CI
Values both launchers hard-coded (marker fonts, the Windows 11 fonts, the Apple Silicon and Intel Mac core and panel tables, plausible DPR and core counts, the macOS MacinTalk/Eloquence voices, the cache prefs) now live in python/src/camoufox/launcher-constants.json, which the TypeScript package ships as a data file. gen-fonts-json.py and verify-fonts.py read the same file, so the drifted PingFang marker copy is gone (fonts.json regenerates byte-identical) and verify-fonts no longer ast-parses fingerprints.py. - Drop the duplicate fpgen model pin under browser/scripts/data; the TS test checks the fpgen package's MODEL_PIN against the Python copy. - Move pin-fpgen-model.py and clean-fingerprint-data.py to python/scripts. - Delete the font licence report (font-metrics/licences.py and its saved output, open-fonts.txt): the bundle ships as built, so the report checks nothing. - Delete unreferenced files: cleanfonts.sh, python/examples, typescript/renovate.json. - Delete dead code: classify_installs, remove_mmdb, write_upstream_sh, bump_release, unused test constants and fixture helpers, the _virtual_display attribute writes, package.py's pre-groups font copy (a missing groups.json now fails). - Share require_bundle, zip_entries, gh_headers and parse_shard instead of keeping copies; fold GitHubDownloader into CamoufoxFetcher in both launchers. - TS raises ValueError where Python does, and `camoufox version` prints the fpgen package version as Python does. - Python tests get src/ from pytest's pythonpath instead of sys.path inserts; skips that hid untested paths now assert or test the behaviour. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
5c1fd578ad
commit
6c3850f60a
@@ -0,0 +1,119 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Drop identities the shipped data files cannot honestly offer.
|
||||
|
||||
`camoufox.coherence` checks an identity when one is drawn. This does the same
|
||||
to the DATA the identities are drawn FROM, so an impossible row never reaches a
|
||||
build in the first place. Two filters for the same rules: the runtime one has to
|
||||
produce an identity and so repairs what it can, while this one can simply drop a
|
||||
row -- there are plenty of others, and dropping loses nothing but a machine that
|
||||
never existed.
|
||||
|
||||
What it covers:
|
||||
|
||||
fingerprint-presets.json, fingerprint-presets-v150.json
|
||||
Each preset is converted the way a launch converts it and checked, and
|
||||
its GPU must be one fpgen has seen Firefox report on that OS: WebGL
|
||||
parameters come from fpgen, and a GPU it has never seen has none. A row
|
||||
that fails is dropped rather than repaired: repairing would write an
|
||||
invented value ("what core count does a 2-core Apple M1 really have?")
|
||||
into a file whose entire purpose is being real.
|
||||
|
||||
Usage:
|
||||
python3 python/scripts/clean-fingerprint-data.py # report only
|
||||
python3 python/scripts/clean-fingerprint-data.py --write # rewrite the files
|
||||
|
||||
`--check` exits non-zero when anything would be dropped;
|
||||
python/tests/test_shipped_data.py asserts the same thing, so a data refresh that
|
||||
reintroduces a bad row fails CI rather than shipping.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
from collections import Counter
|
||||
from pathlib import Path
|
||||
|
||||
PYTHON_SRC = Path(__file__).resolve().parents[1] / 'src'
|
||||
sys.path.insert(0, str(PYTHON_SRC))
|
||||
|
||||
from camoufox import coherence # noqa: E402
|
||||
from camoufox.fingerprints import from_preset # noqa: E402
|
||||
from camoufox.webgl import firefox_gpus # noqa: E402
|
||||
|
||||
PRESET_FILES = (
|
||||
PYTHON_SRC / 'camoufox' / 'fingerprint-presets.json',
|
||||
PYTHON_SRC / 'camoufox' / 'fingerprint-presets-v150.json',
|
||||
)
|
||||
OS_KEY = {'macos': 'mac', 'windows': 'win', 'linux': 'lin'}
|
||||
# The Firefox version only decides the UA rewrite, which no rule reads.
|
||||
FF_VERSION = '152'
|
||||
|
||||
|
||||
def preset_violations(preset, os_name):
|
||||
"""What this preset breaks, as a launch would see it."""
|
||||
try:
|
||||
config = from_preset(preset, FF_VERSION)
|
||||
except Exception as exc: # a row too malformed to convert is itself a defect
|
||||
return [coherence.Violation('unconvertible', f'{type(exc).__name__}: {exc}')]
|
||||
violations = coherence.validate(config, OS_KEY[os_name])
|
||||
gpu = (preset.get('webgl', {}).get('unmaskedVendor'), preset.get('webgl', {}).get('unmaskedRenderer'))
|
||||
if gpu not in firefox_gpus(os_name):
|
||||
violations.append(coherence.Violation(
|
||||
'gpu-without-webgl-data', f'fpgen has never seen Firefox on {os_name} report {gpu[1]!r}'))
|
||||
return violations
|
||||
|
||||
|
||||
def clean_presets(path, write):
|
||||
data = json.loads(path.read_text())
|
||||
dropped = Counter()
|
||||
kept_total = dropped_total = 0
|
||||
for os_name, presets in data['presets'].items():
|
||||
kept = []
|
||||
for index, preset in enumerate(presets):
|
||||
violations = preset_violations(preset, os_name)
|
||||
if violations:
|
||||
dropped_total += 1
|
||||
for violation in violations:
|
||||
dropped[violation.rule] += 1
|
||||
print(f' drop {path.name} {os_name}[{index}]: '
|
||||
f'{"; ".join(v.detail for v in violations)[:120]}')
|
||||
else:
|
||||
kept.append(preset)
|
||||
kept_total += len(kept)
|
||||
data['presets'][os_name] = kept
|
||||
if write and dropped_total:
|
||||
# Keep the file's own formatting. Rewriting 2-space-indented JSON
|
||||
# compactly turns a 38-row deletion into a 22,546-line diff that no
|
||||
# reviewer can read, which is how a data change stops being reviewable.
|
||||
# ensure_ascii=False as well: the originals hold 'Amélie' as itself,
|
||||
# and escaping it would rewrite lines no preset touched.
|
||||
# A no-op run must be byte-identical, so the diff of a real run is the
|
||||
# dropped rows and nothing else: 2-space indent, non-ASCII kept as
|
||||
# itself ('Amélie'), and no trailing newline, which is how these
|
||||
# files are written.
|
||||
path.write_text(json.dumps(data, indent=2, ensure_ascii=False))
|
||||
print(f'{path.name}: {dropped_total} dropped, {kept_total} kept'
|
||||
+ (f' {dict(dropped)}' if dropped else ''))
|
||||
return dropped_total
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--write', action='store_true', help='rewrite the data files')
|
||||
parser.add_argument('--check', action='store_true', help='exit non-zero if anything is unclean')
|
||||
args = parser.parse_args()
|
||||
|
||||
total = sum(clean_presets(path, args.write) for path in PRESET_FILES)
|
||||
|
||||
if not total:
|
||||
print('\nEvery shipped identity is coherent.')
|
||||
return 0
|
||||
if args.write:
|
||||
print(f'\n{total} incoherent entr(ies) removed.')
|
||||
return 0
|
||||
print(f'\n{total} incoherent entr(ies). Run with --write to remove them.')
|
||||
return 1 if args.check else 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
Executable
+45
@@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Install the pinned fpgen model before anything imports fpgen.
|
||||
|
||||
The launcher does this itself on first use (python/src/camoufox/fpgen_model.py,
|
||||
which explains why fpgen's own download is not used). This entry point exists
|
||||
for jobs that need the model in place before they start: CI, image builds, and
|
||||
the TypeScript golden recorder.
|
||||
|
||||
Usage:
|
||||
python3 python/scripts/pin-fpgen-model.py # install if not already pinned
|
||||
python3 python/scripts/pin-fpgen-model.py --check # verify only; non-zero if not pinned
|
||||
python3 python/scripts/pin-fpgen-model.py --force # re-download and reinstall
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
try:
|
||||
from camoufox.fpgen_model import PIN, ensure_fpgen_model, fpgen_data_dir, is_pinned
|
||||
except ImportError:
|
||||
sys.exit('camoufox is not installed; `pip install -e python` from the repo root first')
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(description=__doc__,
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
ap.add_argument('--check', action='store_true', help='verify only; non-zero if not pinned')
|
||||
ap.add_argument('--force', action='store_true', help='reinstall even if already pinned')
|
||||
args = ap.parse_args()
|
||||
|
||||
if args.check:
|
||||
if is_pinned():
|
||||
print(f'OK: fpgen model pinned to {PIN["tag"]}')
|
||||
return 0
|
||||
print(f'fpgen model is NOT pinned to {PIN["tag"]}; run python/scripts/pin-fpgen-model.py',
|
||||
file=sys.stderr)
|
||||
return 1
|
||||
|
||||
ensure_fpgen_model(force=args.force)
|
||||
print(f'OK: fpgen model {PIN["tag"]} pinned in {fpgen_data_dir()}', file=sys.stderr)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user