os_tools.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import os
  2. from typing import List, Callable, Set
  3. filter_any = lambda x: True
  4. filter_xml = lambda x: x.endswith('.xml')
  5. # For a given list of files and or directories,
  6. # recursively find all the files that adhere to an optional filename filter,
  7. # merge the results while eliminating duplicates.
  8. def get_files(paths: List[str], filter: Callable[[str], bool] = filter_any) -> List[str]:
  9. already_have: Set[str] = set()
  10. src_files = []
  11. def add_file(path):
  12. if path not in already_have:
  13. already_have.add(path)
  14. src_files.append(path)
  15. for p in paths:
  16. if os.path.isdir(p):
  17. # recursively scan directories
  18. for r, dirs, files in os.walk(p):
  19. files.sort()
  20. for f in files:
  21. if filter(f):
  22. add_file(os.path.join(r,f))
  23. elif os.path.isfile(p):
  24. add_file(p)
  25. else:
  26. print("%s: not a file or a directory, skipped." % p)
  27. return src_files
  28. # Drop file extension
  29. def dropext(file):
  30. return os.path.splitext(file)[0]
  31. # Ensure path of directories exists
  32. def make_dirs(file):
  33. os.makedirs(os.path.dirname(file), exist_ok=True)