(scriptdirname, extension, *, include_extensions=True)
| 391 | |
| 392 | |
| 393 | def list_scripts(scriptdirname, extension, *, include_extensions=True): |
| 394 | scripts = {} |
| 395 | |
| 396 | loaded_extensions = {ext.canonical_name: ext for ext in extensions.active()} |
| 397 | loaded_extensions_scripts = {ext.canonical_name: [] for ext in extensions.active()} |
| 398 | |
| 399 | # build script dependency map |
| 400 | root_script_basedir = os.path.join(paths.script_path, scriptdirname) |
| 401 | if os.path.exists(root_script_basedir): |
| 402 | for filename in sorted(os.listdir(root_script_basedir)): |
| 403 | if not os.path.isfile(os.path.join(root_script_basedir, filename)): |
| 404 | continue |
| 405 | |
| 406 | if os.path.splitext(filename)[1].lower() != extension: |
| 407 | continue |
| 408 | |
| 409 | script_file = ScriptFile(paths.script_path, filename, os.path.join(root_script_basedir, filename)) |
| 410 | scripts[filename] = ScriptWithDependencies(filename, script_file, [], [], []) |
| 411 | |
| 412 | if include_extensions: |
| 413 | for ext in extensions.active(): |
| 414 | extension_scripts_list = ext.list_files(scriptdirname, extension) |
| 415 | for extension_script in extension_scripts_list: |
| 416 | if not os.path.isfile(extension_script.path): |
| 417 | continue |
| 418 | |
| 419 | script_canonical_name = ("builtin/" if ext.is_builtin else "") + ext.canonical_name + "/" + extension_script.filename |
| 420 | relative_path = scriptdirname + "/" + extension_script.filename |
| 421 | |
| 422 | script = ScriptWithDependencies( |
| 423 | script_canonical_name=script_canonical_name, |
| 424 | file=extension_script, |
| 425 | requires=ext.metadata.get_script_requirements("Requires", relative_path, scriptdirname), |
| 426 | load_before=ext.metadata.get_script_requirements("Before", relative_path, scriptdirname), |
| 427 | load_after=ext.metadata.get_script_requirements("After", relative_path, scriptdirname), |
| 428 | ) |
| 429 | |
| 430 | scripts[script_canonical_name] = script |
| 431 | loaded_extensions_scripts[ext.canonical_name].append(script) |
| 432 | |
| 433 | for script_canonical_name, script in scripts.items(): |
| 434 | # load before requires inverse dependency |
| 435 | # in this case, append the script name into the load_after list of the specified script |
| 436 | for load_before in script.load_before: |
| 437 | # if this requires an individual script to be loaded before |
| 438 | other_script = scripts.get(load_before) |
| 439 | if other_script: |
| 440 | other_script.load_after.append(script_canonical_name) |
| 441 | |
| 442 | # if this requires an extension |
| 443 | other_extension_scripts = loaded_extensions_scripts.get(load_before) |
| 444 | if other_extension_scripts: |
| 445 | for other_script in other_extension_scripts: |
| 446 | other_script.load_after.append(script_canonical_name) |
| 447 | |
| 448 | # if After mentions an extension, remove it and instead add all of its scripts |
| 449 | for load_after in list(script.load_after): |
| 450 | if load_after not in scripts and load_after in loaded_extensions_scripts: |
no test coverage detected