Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-04 09:44:13

0001 #!/usr/bin/env python3
0002 
0003 from pathlib import Path
0004 import os
0005 import sys
0006 import subprocess
0007 
0008 EXCLUDE_PATHS = (
0009     ".devcontainer",
0010     ".git",
0011     ".github",
0012     ".idea",
0013     "CI",
0014     "cmake",
0015     # Used by traccc
0016     "Detray/detectors",
0017     # CLI tools
0018     "Detray/tests/tools",
0019     "Detray/python/core/tests",
0020     "Detray/python/io/tests",
0021     "Detray/python/tests/tests",
0022     "Detray/python/plugins/tests",
0023     # TODO: Remove the traccc part.
0024     "Traccc",
0025     "git",
0026     "Python",
0027     "Scripts",
0028     # cmake for "DD4hep-tests" looks a bit different
0029     "Tests/UnitTests/Plugins/DD4hep",
0030     "thirdparty",
0031     "white_papers/figures",
0032     "figures/white_papers",
0033 )
0034 EXCLUDE_FILES = (
0035     ".gersemirc",
0036     ".gitignore",
0037     ".kodiak.toml",
0038     ".merge-sentinel.yml",
0039     ".policy.yml",
0040     ".pre-commit-config.yaml",
0041     "acts_logo_colored.svg",
0042     "CITATION.cff",
0043     "CMakeLists.txt",
0044     "CMakePresets.json",
0045     "CODE_OF_CONDUCT.md",
0046     "CODEOWNERS",
0047     "codecov.yml",
0048     "pytest.ini",
0049     "README.md",
0050     "readthedocs.yml",
0051     "sonar-project.properties",
0052     # Filename not completed in source
0053     "vertexing_event_mu20_beamspot.csv",
0054     "vertexing_event_mu20_tracks.csv",
0055     "vertexing_event_mu20_vertices_AMVF.csv",
0056     "event000000001-MuonDriftCircle.csv",
0057     "event000000001-MuonSimHit.csv",
0058     # TODO Move the following files to a better place?
0059     "Magfield.ipynb",
0060     "SolenoidField.ipynb",
0061     # TODO Add README next to the following files?
0062     "generic-input-config.json",
0063     "generic-alignment-geo.json",
0064     # TODO Mention these files somewhere?
0065     "codegen/src/codegen/sympy_common.py",
0066     "codegen/src/codegen/detray_backend.py",
0067     "CompressedIO.h",
0068     "generate_particle_data_table.py",
0069     "GeometryModule.h",
0070     "lazy_autodoc.py",
0071     "runtime_geometry_modules.md",
0072     # Files for python binding generation
0073     "acts-version-manager.js",
0074     "bugs.md",
0075     "deprecated.md",
0076     "Python/conftest.py",
0077     "serve.py",
0078     "SNIPPETS.md",
0079     "tex-mml-chtml.js",
0080     "tgeo_aux.py.in",
0081     # Pytest collects these examples; Doxygen includes the group by ID.
0082     "docs/examples/test_generic.py",
0083     "docs/examples/test_performance_and_plotting.py",
0084     "docs/groups/python/performance_and_plotting.md",
0085     "todo.md",
0086     # Detray python tests for auto-generated code
0087     "Detray/codegen/detray-sympy/tests/test_assumptions_D.py",
0088     "Detray/codegen/detray-sympy/tests/test_matrices.py",
0089     # Used in traccc
0090     "Detray/tests/include/detray/test/utils/perigee_stopper.hpp",
0091     "Detray/tests/include/detray/test/validation/propagation_validation.hpp",
0092     # Build-time metadata generation
0093     "Detray/detectors/codegen/detray_metadata/impl/definitions.py",
0094     "Detray/detectors/codegen/detray_metadata/impl/type_helpers.py",
0095     # Python uv files
0096     "Detray/codegen/detray-sympy/uv.lock",
0097     # Generated PyPI readme, checked by Python/Examples/tests/test_pypi_readme.py
0098     "CI/pypi_readme.md",
0099     # Remove once the first in-tree user includes this header.
0100     "Core/include/Acts/Utilities/CompilerAttributes.hpp",
0101     # TODO: remove after file is gone
0102     "Core/include/Acts/Utilities/ProtoAxisHelpers.hpp",
0103 )
0104 SUFFIX_CPP = (
0105     ".hpp",
0106     ".cuh",
0107     ".sycl",
0108     ".hip",
0109     ".ipp",
0110     ".cpp",
0111     ".cu",
0112 )
0113 SUFFIX_IMAGE = (
0114     ".png",
0115     ".svg",
0116     ".jpg",
0117     ".gif",
0118 )
0119 SUFFIX_PYTHON = (".py",)
0120 SUFFIX_DOC = (
0121     ".md",
0122     ".rst",
0123     ".dox",
0124     ".html",
0125     ".bib",
0126 )
0127 SUFFIX_OTHER = (
0128     "",
0129     ".C",
0130     ".csv",
0131     ".css",
0132     ".gdml",
0133     ".hepmc3",
0134     ".lock",
0135     ".ico",
0136     ".in",
0137     ".ipynb",
0138     ".json",
0139     ".j2",
0140     ".onnx",
0141     ".root",
0142     ".toml",
0143     ".txt",
0144     ".yml",
0145     ".xml",
0146     ".sh",
0147 )
0148 
0149 
0150 def filter_paths(names, root, exclude_paths=(), exclude_files=()):
0151     """
0152     Filter names from os.walk() based on path substrings and file rules.
0153     Excludes entries if their full path matches exclude_paths or exclude_files.
0154     """
0155 
0156     def keep(name):
0157         p = Path(root) / name
0158         p_str = p.as_posix()
0159         return not any(ep in p_str for ep in exclude_paths) and not any(
0160             ef in p_str if "/" in ef else p.name == ef for ef in exclude_files
0161         )
0162 
0163     return [name for name in names if keep(name)]
0164 
0165 
0166 def file_can_be_removed(searchstring, scope):
0167     # Only existence matters: stop at the first match instead of scanning the
0168     # entire tree and collecting every matching line for each candidate.
0169     result = subprocess.run(["grep", "-IRq", "--", searchstring, *scope])
0170     if result.returncode not in (0, 1):
0171         result.check_returncode()
0172     return result.returncode == 1
0173 
0174 
0175 def count_files(path="."):
0176     count = 0
0177     for root, dirs, files in os.walk(path):
0178         dirs[:] = filter_paths(dirs, root, EXCLUDE_PATHS)
0179         files = filter_paths(files, root, EXCLUDE_PATHS, EXCLUDE_FILES)
0180 
0181         count += len(files)
0182 
0183     return count
0184 
0185 
0186 def check_wrong_extensions(walk_root):
0187     """
0188     Collect files with disallowed suffixes. Returns the number of problematic files.
0189     """
0190 
0191     suffix_allowed = (
0192         SUFFIX_CPP + SUFFIX_IMAGE + SUFFIX_PYTHON + SUFFIX_DOC + SUFFIX_OTHER
0193     )
0194 
0195     wrong_extension = []
0196     for root, dirs, files in os.walk(walk_root):
0197         dirs[:] = filter_paths(dirs, root, EXCLUDE_PATHS)
0198         files = filter_paths(files, root, EXCLUDE_PATHS, EXCLUDE_FILES)
0199 
0200         for f in files:
0201             p = Path(root) / f
0202             if p.suffix not in suffix_allowed:
0203                 wrong_extension.append(str(p))
0204 
0205     if len(wrong_extension) != 0:
0206         print(
0207             "\n\n\033[31mERROR\033[0m "
0208             + f"The following {len(wrong_extension)} files have an unsupported extension:\n\n"
0209             + "\033[31m"
0210             + "\n".join(wrong_extension)
0211             + "\033[0m"
0212             + "\nCheck if you can change the format to one of the following:\n"
0213             + "\n".join(suffix_allowed)
0214             + "\nIf you really need that specific extension, add it to the list above.\n"
0215         )
0216 
0217     return len(wrong_extension)
0218 
0219 
0220 def find_unused_by_suffix(walk_root, suffixes, search_key, search_scope):
0221     unused = []
0222 
0223     for root, dirs, files in os.walk(walk_root):
0224         dirs[:] = filter_paths(dirs, root, EXCLUDE_PATHS)
0225         files = filter_paths(files, root, EXCLUDE_PATHS, EXCLUDE_FILES)
0226 
0227         for f in files:
0228             p = Path(root) / f
0229             if p.suffix in suffixes and file_can_be_removed(
0230                 search_key(p), search_scope
0231             ):
0232                 unused.append(str(p))
0233 
0234     return unused
0235 
0236 
0237 def find_unused_python_files(walk_root, dirs_base):
0238     unused = []
0239 
0240     for root, dirs, files in os.walk(walk_root):
0241         dirs[:] = filter_paths(dirs, root, EXCLUDE_PATHS)
0242         files = filter_paths(files, root, EXCLUDE_PATHS, EXCLUDE_FILES)
0243 
0244         for f in files:
0245             p = Path(root) / f
0246             if p.suffix not in SUFFIX_PYTHON:
0247                 continue
0248 
0249             if not file_can_be_removed(r"import .*" + p.stem, dirs_base):
0250                 continue
0251 
0252             if not file_can_be_removed(r"from " + p.stem + r" import", dirs_base):
0253                 continue
0254 
0255             if file_can_be_removed(p.name, dirs_base):
0256                 unused.append(str(p))
0257 
0258     return unused
0259 
0260 
0261 def main():
0262     print("\033[32mINFO\033[0m Start check_unused_files.py ...")
0263 
0264     exit = 0
0265 
0266     dirs_base = next(os.walk("."))[1]
0267     dirs_base.append(".")
0268     dirs_base[:] = filter_paths(dirs_base, Path("."), EXCLUDE_PATHS)
0269     dirs_base_docs = ("docs",)
0270     dirs_base_code = filter_paths(dirs_base, Path("."), dirs_base_docs)
0271 
0272     exit += check_wrong_extensions(".")
0273 
0274     # Collector
0275     unused_files = []
0276 
0277     unused_files += find_unused_by_suffix(
0278         ".", SUFFIX_CPP, lambda p: p.name, dirs_base_code
0279     )
0280 
0281     unused_files += find_unused_python_files(".", dirs_base)
0282 
0283     # TODO find more reliable test for this
0284     unused_files += find_unused_by_suffix(
0285         ".", SUFFIX_DOC, lambda p: p.stem, dirs_base_docs
0286     )
0287 
0288     unused_files += find_unused_by_suffix(
0289         ".", SUFFIX_IMAGE + SUFFIX_OTHER, lambda p: p.name, dirs_base
0290     )
0291 
0292     if len(unused_files) != 0:
0293         print(
0294             "\n\n\033[31mERROR\033[0m "
0295             + f"The following {len(unused_files)} files seem to be unused:\n"
0296             + "\033[31m"
0297             + "\n".join(unused_files)
0298             + "\033[0m"
0299             + "\nYou have 3 options:"
0300             + "\n\t- Remove them"
0301             + "\n\t- Use them (check proper include)"
0302             + "\n\t- Modify the ignore list of this check\n"
0303         )
0304 
0305         exit += 1
0306 
0307     if exit == 0:
0308         print(
0309             "\n\n\033[32mINFO\033[0m Finished check_unused_files.py without any errors."
0310         )
0311 
0312     return exit
0313 
0314 
0315 if "__main__" == __name__:
0316     sys.exit(main())