util.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Future imports for Python 2.7, mandatory in 3.0
  2. from __future__ import division
  3. from __future__ import print_function
  4. from __future__ import unicode_literals
  5. import os
  6. # We don't want to run metrics for unittests, automatically-generated C files,
  7. # external libraries or git leftovers.
  8. EXCLUDE_SOURCE_DIRS = {"src/test/", "src/trunnel/", "src/ext/" }
  9. EXCLUDE_FILES = {"orconfig.h"}
  10. def _norm(p):
  11. return os.path.normcase(os.path.normpath(p))
  12. def get_tor_c_files(tor_topdir, include_dirs=None):
  13. """
  14. Return a list with the .c and .h filenames we want to get metrics of.
  15. """
  16. files_list = []
  17. exclude_dirs = { _norm(os.path.join(tor_topdir, p)) for p in EXCLUDE_SOURCE_DIRS }
  18. if include_dirs is None:
  19. topdirs = [ tor_topdir ]
  20. else:
  21. topdirs = [ os.path.join(tor_topdir, inc) for inc in include_dirs ]
  22. for topdir in topdirs:
  23. for root, directories, filenames in os.walk(topdir):
  24. # Remove all the directories that are excluded.
  25. directories[:] = [ d for d in directories
  26. if _norm(os.path.join(root,d)) not in exclude_dirs ]
  27. directories.sort()
  28. filenames.sort()
  29. for filename in filenames:
  30. # We only care about .c and .h files
  31. if not (filename.endswith(".c") or filename.endswith(".h")):
  32. continue
  33. if filename in EXCLUDE_FILES:
  34. continue
  35. # Avoid editor temporary files
  36. bname = os.path.basename(filename)
  37. if bname.startswith("."):
  38. continue
  39. if bname.startswith("#"):
  40. continue
  41. full_path = os.path.join(root,filename)
  42. files_list.append(full_path)
  43. return files_list
  44. class NullFile:
  45. """A file-like object that we can us to suppress output."""
  46. def __init__(self):
  47. pass
  48. def write(self, s):
  49. pass