Merge branch 'fix-changelogs' into 'main'
[tor.git] / scripts / maint / practracker / util.py
blob6ab10a8de7a3728f8024017c71e12d9a5126ad99
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
6 import os
8 # We don't want to run metrics for unittests, automatically-generated C files,
9 # external libraries or git leftovers.
10 EXCLUDE_SOURCE_DIRS = {"src/test/", "src/trunnel/", "src/ext/" }
12 EXCLUDE_FILES = {"orconfig.h"}
14 def _norm(p):
15 return os.path.normcase(os.path.normpath(p))
17 def get_tor_c_files(tor_topdir, include_dirs=None):
18 """
19 Return a list with the .c and .h filenames we want to get metrics of.
20 """
21 files_list = []
22 exclude_dirs = { _norm(os.path.join(tor_topdir, p)) for p in EXCLUDE_SOURCE_DIRS }
24 if include_dirs is None:
25 topdirs = [ tor_topdir ]
26 else:
27 topdirs = [ os.path.join(tor_topdir, inc) for inc in include_dirs ]
29 for topdir in topdirs:
30 for root, directories, filenames in os.walk(topdir):
31 # Remove all the directories that are excluded.
32 directories[:] = [ d for d in directories
33 if _norm(os.path.join(root,d)) not in exclude_dirs ]
34 directories.sort()
35 filenames.sort()
36 for filename in filenames:
37 # We only care about .c and .h files
38 if not (filename.endswith(".c") or filename.endswith(".h")):
39 continue
40 if filename in EXCLUDE_FILES:
41 continue
42 # Avoid editor temporary files
43 bname = os.path.basename(filename)
44 if bname.startswith("."):
45 continue
46 if bname.startswith("#"):
47 continue
49 full_path = os.path.join(root,filename)
51 files_list.append(full_path)
53 return files_list
55 class NullFile:
56 """A file-like object that we can us to suppress output."""
57 def __init__(self):
58 pass
59 def write(self, s):
60 pass