util.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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/", ".git/"}
  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):
  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. for root, directories, filenames in os.walk(tor_topdir):
  16. # Remove all the directories that are excluded.
  17. directories[:] = [ d for d in directories
  18. if _norm(os.path.join(root,d)) not in exclude_dirs ]
  19. directories.sort()
  20. filenames.sort()
  21. for filename in filenames:
  22. # We only care about .c and .h files
  23. if not (filename.endswith(".c") or filename.endswith(".h")):
  24. continue
  25. if filename in EXCLUDE_FILES:
  26. continue
  27. full_path = os.path.join(root,filename)
  28. files_list.append(full_path)
  29. return files_list