os_tools.py 1.2 KB

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