2019-12-12 06:58:51 +01:00
|
|
|
# Future imports for Python 2.7, mandatory in 3.0
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2019-02-27 14:14:19 +01:00
|
|
|
import os
|
|
|
|
|
2019-02-28 11:09:10 +01:00
|
|
|
# We don't want to run metrics for unittests, automatically-generated C files,
|
|
|
|
# external libraries or git leftovers.
|
2019-07-17 14:30:12 +02:00
|
|
|
EXCLUDE_SOURCE_DIRS = {"src/test/", "src/trunnel/", "src/rust/",
|
2019-09-02 21:31:31 +02:00
|
|
|
"src/ext/" }
|
2019-07-17 14:30:12 +02:00
|
|
|
|
2019-08-05 16:31:02 +02:00
|
|
|
EXCLUDE_FILES = {"orconfig.h"}
|
|
|
|
|
2019-07-17 14:30:12 +02:00
|
|
|
def _norm(p):
|
|
|
|
return os.path.normcase(os.path.normpath(p))
|
2019-02-28 11:09:10 +01:00
|
|
|
|
2019-09-02 21:31:31 +02:00
|
|
|
def get_tor_c_files(tor_topdir, include_dirs=None):
|
2019-02-27 14:14:19 +01:00
|
|
|
"""
|
2019-08-05 16:31:02 +02:00
|
|
|
Return a list with the .c and .h filenames we want to get metrics of.
|
2019-02-27 14:14:19 +01:00
|
|
|
"""
|
|
|
|
files_list = []
|
2019-07-17 14:30:12 +02:00
|
|
|
exclude_dirs = { _norm(os.path.join(tor_topdir, p)) for p in EXCLUDE_SOURCE_DIRS }
|
|
|
|
|
2019-09-02 21:31:31 +02:00
|
|
|
if include_dirs is None:
|
|
|
|
topdirs = [ tor_topdir ]
|
|
|
|
else:
|
|
|
|
topdirs = [ os.path.join(tor_topdir, inc) for inc in include_dirs ]
|
2019-02-27 14:14:19 +01:00
|
|
|
|
2019-09-02 21:31:31 +02:00
|
|
|
for topdir in topdirs:
|
2019-09-02 21:40:03 +02:00
|
|
|
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
|
2019-11-14 07:12:19 +01:00
|
|
|
if filename in EXCLUDE_FILES:
|
|
|
|
continue
|
2019-10-28 04:09:58 +01:00
|
|
|
# Avoid editor temporary files
|
2019-11-14 07:12:19 +01:00
|
|
|
bname = os.path.basename(filename)
|
|
|
|
if bname.startswith("."):
|
2019-10-28 04:09:58 +01:00
|
|
|
continue
|
2019-11-14 07:12:19 +01:00
|
|
|
if bname.startswith("#"):
|
2019-09-02 21:40:03 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
full_path = os.path.join(root,filename)
|
|
|
|
|
|
|
|
files_list.append(full_path)
|
2019-02-27 14:14:19 +01:00
|
|
|
|
|
|
|
return files_list
|
2019-09-18 14:49:57 +02:00
|
|
|
|
|
|
|
class NullFile:
|
|
|
|
"""A file-like object that we can us to suppress output."""
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
def write(self, s):
|
|
|
|
pass
|