file_util.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. """File utilities."""
  10. import glob
  11. from pathlib import Path
  12. from materialize import MZ_ROOT, spawn
  13. def get_recursive_file_list(path: str | Path, root_dir: Path = MZ_ROOT) -> list[str]:
  14. result = spawn.capture(["find", path, "-type", "f"], cwd=root_dir)
  15. return _file_list_output_to_list(result)
  16. def resolve_path_with_wildcard(path: str, root_dir: str | Path = MZ_ROOT) -> set[str]:
  17. return set(glob.glob(path, root_dir=root_dir))
  18. def resolve_paths_with_wildcard(
  19. paths: set[str], root_dir: str | Path = MZ_ROOT
  20. ) -> set[str]:
  21. result = set()
  22. for path in paths:
  23. result = result.union(resolve_path_with_wildcard(path, root_dir))
  24. return result
  25. def _file_list_output_to_list(output: str) -> list[str]:
  26. files = output.split("\n")
  27. return [file for file in files if file != ""]