tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

spec.py (2680B)


      1 #!/usr/bin/env python3
      2 import argparse
      3 import os
      4 from typing import Any, Optional, Text
      5 
      6 from . import vcs
      7 from .manifest import compute_manifest_spec_items, InvalidCacheError, Manifest, write
      8 from .log import get_logger, enable_debug_logging
      9 
     10 
     11 here = os.path.dirname(__file__)
     12 
     13 wpt_root = os.path.abspath(os.path.join(here, os.pardir, os.pardir))
     14 
     15 logger = get_logger()
     16 
     17 
     18 def update_spec(tests_root: Text,
     19                manifest_path: Text,
     20                url_base: Text,
     21                cache_root: Optional[Text] = None,
     22                working_copy: bool = True,
     23                parallel: bool = True
     24                ) -> None:
     25 
     26    manifest = Manifest(tests_root, url_base)
     27 
     28    logger.info("Updating SPEC_MANIFEST")
     29    try:
     30        tree = vcs.get_tree(tests_root, manifest, manifest_path, cache_root,
     31                            None, working_copy, True)
     32        changed = manifest.update(tree, parallel, compute_manifest_spec_items)
     33    except InvalidCacheError:
     34        logger.error("Manifest cache in spec.py was invalid.")
     35        return
     36 
     37    if changed:
     38        write(manifest, manifest_path)
     39    tree.dump_caches()
     40 
     41 
     42 def update_from_cli(**kwargs: Any) -> None:
     43    tests_root = kwargs["tests_root"]
     44    path = kwargs["path"]
     45    assert tests_root is not None
     46 
     47    update_spec(tests_root,
     48                path,
     49                kwargs["url_base"],
     50                cache_root=kwargs["cache_root"],
     51                parallel=kwargs["parallel"])
     52 
     53 
     54 def abs_path(path: str) -> str:
     55    return os.path.abspath(os.path.expanduser(path))
     56 
     57 
     58 def create_parser() -> argparse.ArgumentParser:
     59    parser = argparse.ArgumentParser()
     60    parser.add_argument(
     61        "-v", "--verbose", action="store_true",
     62        help="Turn on verbose logging")
     63    parser.add_argument(
     64        "-p", "--path", type=abs_path, help="Path to manifest file.")
     65    parser.add_argument(
     66        "--tests-root", type=abs_path, default=wpt_root, help="Path to root of tests.")
     67    parser.add_argument(
     68        "--url-base", default="/",
     69        help="Base url to use as the mount point for tests in this manifest.")
     70    parser.add_argument(
     71        "--cache-root", default=os.path.join(wpt_root, ".wptcache"),
     72        help="Path in which to store any caches (default <tests_root>/.wptcache/)")
     73    parser.add_argument(
     74        "--no-parallel", dest="parallel", action="store_false",
     75        help="Do not parallelize building the manifest")
     76    return parser
     77 
     78 
     79 def run(*args: Any, **kwargs: Any) -> None:
     80    if kwargs["path"] is None:
     81        kwargs["path"] = os.path.join(kwargs["tests_root"], "SPEC_MANIFEST.json")
     82    if kwargs["verbose"]:
     83        enable_debug_logging()
     84    update_from_cli(**kwargs)