commandline_flag_requirements = {} for key, value in conditional_requirements.items(): if key == "commandline_flag": for flag_name, requirements_paths in value.items():
commandline_flag_requirements[flag_name] = [
os.path.join(base_dir, path) for path in requirements_paths] else: raise KeyError(
f'Unsupported conditional requirement key: {key}')
def load_commands():
rv = {} with open(os.path.join(here, "paths")) as f:
paths = [item.strip().replace("/", os.path.sep) for item in f if item.strip()] for path in paths:
abs_path = os.path.join(wpt_root, path, "commands.json")
base_dir = os.path.dirname(abs_path) with open(abs_path) as f:
data = json.load(f) for command, props in data.items(): assert"path"in props assert"script"in props
rv[command] = { "path": os.path.join(base_dir, props["path"]), "script": props["script"], "parser": props.get("parser"), "parse_known": props.get("parse_known", False), "help": props.get("help"), "virtualenv": props.get("virtualenv", True), "requirements": [os.path.join(base_dir, item) for item in props.get("requirements", [])]
}
if rv[command]["requirements"] or rv[command]["conditional_requirements"]: assert rv[command]["virtualenv"] return rv
def parse_args(argv, commands=load_commands()):
parser = argparse.ArgumentParser()
parser.add_argument("--venv", help="Path to an existing virtualenv to use")
parser.add_argument("--skip-venv-setup", action="store_true",
help="Whether to use the virtualenv as-is. Must set --venv as well")
parser.add_argument("--debug", action="store_true",
help="Run the debugger in case of an exception")
subparsers = parser.add_subparsers(dest="command") for command, props in commands.items():
subparsers.add_parser(command, help=props["help"], add_help=False)
ifnot argv:
parser.print_help() returnNone, None
args, extra = parser.parse_known_args(argv)
return args, extra
def import_command(prog, command, props): # This currently requires the path to be a module, # which probably isn't ideal but it means that relative # imports inside the script work
rel_path = os.path.relpath(props["path"], wpt_root)
parts = os.path.splitext(rel_path)[0].split(os.path.sep)
mod_name = ".".join(parts)
mod = __import__(mod_name) for part in parts[1:]:
mod = getattr(mod, part)
def create_complete_parser(): """Eagerly load all subparsers. This involves more work than is required for typical command-line usage. It is maintained for the purposes of
documentation generation as implemented in WPT's top-level `/docs`
directory."""
# We should already be in a virtual environment from the top-level # `wpt build-docs` command but we need to look up the environment to # find out where it's located.
venv_path = os.environ["VIRTUAL_ENV"]
venv = virtualenv.Virtualenv(venv_path, True)
for command in commands:
props = commands[command]
try:
venv.install_requirements(*props.get("requirements", [])) except Exception:
logging.warning(
f"Unable to install requirements ({props['requirements']!r}) for command {command}"
) continue
def setup_virtualenv(path, skip_venv_setup, props): if skip_venv_setup and path isNone: raise ValueError("Must set --venv when --skip-venv-setup is used")
should_skip_setup = path isnotNoneand skip_venv_setup if path isNone:
path = os.path.join(wpt_root, venv_dir())
venv = virtualenv.Virtualenv(path, should_skip_setup) ifnot should_skip_setup:
venv.start()
venv.install_requirements(*props.get("requirements", [])) return venv
def install_command_flag_requirements(venv, props, kwargs):
requirements = props["conditional_requirements"].get("commandline_flag", {})
install_paths = [] for command_flag_name, requirement_paths in requirements.items(): if command_flag_name in kwargs:
install_paths.extend(requirement_paths)
venv.install_requirements(*install_paths)
def main(prog=None, argv=None):
logging.basicConfig(level=logging.INFO) # Ensure we use the spawn start method for all multiprocessing try:
multiprocessing.set_start_method('spawn') except RuntimeError as e: # This can happen if we call back into wpt having already set the context
start_method = multiprocessing.get_start_method() if start_method != "spawn":
logging.critical("The multiprocessing start method was set to %s by a caller", start_method) raise e
if prog isNone:
prog = sys.argv[0] if argv isNone:
argv = sys.argv[1:]
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.