util.py 1.44 KB
Newer Older
1
2
import os

3
4
# We don't want to run metrics for unittests, automatically-generated C files,
# external libraries or git leftovers.
5
EXCLUDE_SOURCE_DIRS = {"src/test/", "src/trunnel/", "src/rust/",
6
                       "src/ext/" }
7

8
9
EXCLUDE_FILES = {"orconfig.h"}

10
11
def _norm(p):
    return os.path.normcase(os.path.normpath(p))
12

13
def get_tor_c_files(tor_topdir, include_dirs=None):
14
    """
15
    Return a list with the .c and .h filenames we want to get metrics of.
16
17
    """
    files_list = []
18
19
    exclude_dirs = { _norm(os.path.join(tor_topdir, p)) for p in EXCLUDE_SOURCE_DIRS }

20
21
22
23
    if include_dirs is None:
        topdirs = [ tor_topdir ]
    else:
        topdirs = [ os.path.join(tor_topdir, inc) for inc in include_dirs ]
24

25
    for topdir in topdirs:
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
        for root, directories, filenames in os.walk(topdir):
            # Remove all the directories that are excluded.
            directories[:] = [ d for d in directories
                               if _norm(os.path.join(root,d)) not in exclude_dirs ]
            directories.sort()
            filenames.sort()
            for filename in filenames:
                # We only care about .c and .h files
                if not (filename.endswith(".c") or filename.endswith(".h")):
                    continue
                if filename in EXCLUDE_FILES:
                    continue

                full_path = os.path.join(root,filename)

                files_list.append(full_path)
42
43

    return files_list