util.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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
  35. class NullFile:
  36. """A file-like object that we can us to suppress output."""
  37. def __init__(self):
  38. pass
  39. def write(self, s):
  40. pass