| 1 |
import argparse |
| 2 |
import json |
| 3 |
import os |
| 4 |
from collections import defaultdict |
| 5 |
from typing import Any, Dict, Iterable, List, Text |
| 6 |
|
| 7 |
from .manifest import load_and_update, Manifest |
| 8 |
from .log import get_logger |
| 9 |
|
| 10 |
wpt_root = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)) |
| 11 |
|
| 12 |
logger = get_logger() |
| 13 |
|
| 14 |
|
| 15 |
def abs_path(path: str) -> str: |
| 16 |
return os.path.abspath(os.path.expanduser(path)) |
| 17 |
|
| 18 |
|
| 19 |
def create_parser() -> argparse.ArgumentParser: |
| 20 |
parser = argparse.ArgumentParser() |
| 21 |
parser.add_argument( |
| 22 |
"-p", "--path", type=abs_path, help="Path to manifest file.") |
| 23 |
parser.add_argument( |
| 24 |
"--src-root", type=abs_path, help="Path to root of sourcetree.") |
| 25 |
parser.add_argument( |
| 26 |
"--tests-root", type=abs_path, default=wpt_root, help="Path to root of tests.") |
| 27 |
parser.add_argument( |
| 28 |
"--no-update", dest="update", action="store_false", |
| 29 |
help="Don't update manifest before continuing") |
| 30 |
parser.add_argument( |
| 31 |
"-r", "--rebuild", action="store_true", |
| 32 |
help="Force a full rebuild of the manifest.") |
| 33 |
parser.add_argument( |
| 34 |
"--url-base", default="/", |
| 35 |
help="Base url to use as the mount point for tests in this manifest.") |
| 36 |
parser.add_argument( |
| 37 |
"--cache-root", default=os.path.join(wpt_root, ".wptcache"), |
| 38 |
help="Path in which to store any caches (default <tests_root>/.wptcache/)") |
| 39 |
parser.add_argument( |
| 40 |
"--json", action="store_true", |
| 41 |
help="Output as JSON") |
| 42 |
parser.add_argument( |
| 43 |
"test_ids", nargs="+", |
| 44 |
help="Test ids for which to get paths") |
| 45 |
return parser |
| 46 |
|
| 47 |
|
| 48 |
def get_path_id_map(src_root: Text, tests_root: Text, manifest_file: Manifest, test_ids: Iterable[Text]) -> Dict[Text, List[Text]]: |
| 49 |
test_ids = set(test_ids) |
| 50 |
path_id_map: Dict[Text, List[Text]] = defaultdict(list) |
| 51 |
|
| 52 |
compute_rel_path = src_root != tests_root |
| 53 |
|
| 54 |
for item_type, path, tests in manifest_file: |
| 55 |
for test in tests: |
| 56 |
if test.id in test_ids: |
| 57 |
if compute_rel_path: |
| 58 |
rel_path = os.path.relpath(os.path.join(tests_root, path), |
| 59 |
src_root) |
| 60 |
else: |
| 61 |
rel_path = path |
| 62 |
path_id_map[rel_path].append(test.id) |
| 63 |
return path_id_map |
| 64 |
|
| 65 |
|
| 66 |
def get_paths(**kwargs: Any) -> Dict[Text, List[Text]]: |
| 67 |
tests_root = kwargs["tests_root"] |
| 68 |
assert tests_root is not None |
| 69 |
path = kwargs["path"] |
| 70 |
if path is None: |
| 71 |
path = os.path.join(kwargs["tests_root"], "MANIFEST.json") |
| 72 |
src_root = kwargs["src_root"] |
| 73 |
if src_root is None: |
| 74 |
src_root = tests_root |
| 75 |
|
| 76 |
manifest_file = load_and_update(tests_root, |
| 77 |
path, |
| 78 |
kwargs["url_base"], |
| 79 |
update=kwargs["update"], |
| 80 |
rebuild=kwargs["rebuild"], |
| 81 |
cache_root=kwargs["cache_root"]) |
| 82 |
|
| 83 |
return get_path_id_map(src_root, tests_root, manifest_file, kwargs["test_ids"]) |
| 84 |
|
| 85 |
|
| 86 |
def write_output(path_id_map: Dict[Text, List[Text]], as_json: bool) -> None: |
| 87 |
if as_json: |
| 88 |
print(json.dumps(path_id_map)) |
| 89 |
else: |
| 90 |
for path, test_ids in sorted(path_id_map.items()): |
| 91 |
print(path) |
| 92 |
for test_id in sorted(test_ids): |
| 93 |
print(" " + test_id) |
| 94 |
|
| 95 |
|
| 96 |
def run(**kwargs: Any) -> None: |
| 97 |
path_id_map = get_paths(**kwargs) |
| 98 |
write_output(path_id_map, as_json=kwargs["json"]) |