util.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import os
  2. # We don't want to run metrics for unittests, automatically-generated C files,
  3. # external libraries or git leftovers.
  4. EXCLUDE_SOURCE_DIRS = {"src/test/", "src/trunnel/", "src/rust/",
  5. "src/ext/" }
  6. EXCLUDE_FILES = {"orconfig.h"}
  7. def _norm(p):
  8. return os.path.normcase(os.path.normpath(p))
  9. def get_tor_c_files(tor_topdir, include_dirs=None):
  10. """
  11. Return a list with the .c and .h filenames we want to get metrics of.
  12. """
  13. files_list = []
  14. exclude_dirs = { _norm(os.path.join(tor_topdir, p)) for p in EXCLUDE_SOURCE_DIRS }
  15. if include_dirs is None:
  16. topdirs = [ tor_topdir ]
  17. else:
  18. topdirs = [ os.path.join(tor_topdir, inc) for inc in include_dirs ]
  19. for topdir in topdirs:
  20. for root, directories, filenames in os.walk(topdir):
  21. # Remove all the directories that are excluded.
  22. directories[:] = [ d for d in directories
  23. if _norm(os.path.join(root,d)) not in exclude_dirs ]
  24. directories.sort()
  25. filenames.sort()
  26. for filename in filenames:
  27. # We only care about .c and .h files
  28. if not (filename.endswith(".c") or filename.endswith(".h")):
  29. continue
  30. if filename in EXCLUDE_FILES:
  31. continue
  32. full_path = os.path.join(root,filename)
  33. files_list.append(full_path)
  34. return files_list