diff options
author | teor <teor@torproject.org> | 2020-01-16 08:36:45 +1000 |
---|---|---|
committer | teor <teor@torproject.org> | 2020-01-16 09:25:58 +1000 |
commit | 207d2625ed1485fd66dd9fd8df936d46252802ec (patch) | |
tree | 494d29332da8812f67952cba57a6c8ec86f64db3 /scripts/maint | |
parent | 4f45ad1394c4744f2c7a3deec45a32e3712e24bc (diff) | |
download | tor-207d2625ed1485fd66dd9fd8df936d46252802ec.tar.gz tor-207d2625ed1485fd66dd9fd8df936d46252802ec.zip |
add_c_file: Improve path handling and canonicalisation
* distinguish between paths relative to the top-level tor directory,
and paths relative to tor's src directory
* canonicalise paths before using them
* check that the script is run from the top-level tor directory
* check that the file is being created in tor's src directory
Part of 32962.
Diffstat (limited to 'scripts/maint')
-rwxr-xr-x | scripts/maint/add_c_file.py | 32 |
1 files changed, 23 insertions, 9 deletions
diff --git a/scripts/maint/add_c_file.py b/scripts/maint/add_c_file.py index 66a4fdcd9e..f242a8b4e9 100755 --- a/scripts/maint/add_c_file.py +++ b/scripts/maint/add_c_file.py @@ -18,14 +18,20 @@ import os import re import time -def topdir_file(name): - """Strip opening "src" from a filename""" - return os.path.relpath(name, './src') +def tordir_file(name): + """Make name relative to the current directory, which should be the + top-level tor directory. Also performs basic path simplifications.""" + return os.path.normpath(os.path.relpath(name)) + +def srcdir_file(name): + """Make name relative to tor's "src" directory. + Also performs basic path simplifications.""" + return os.path.normpath(os.path.relpath(name, 'src')) def guard_macro(name): """Return the guard macro that should be used for the header file 'name'. """ - td = topdir_file(name).replace(".", "_").replace("/", "_").upper() + td = srcdir_file(name).replace(".", "_").replace("/", "_").upper() return "TOR_{}".format(td) def makeext(name, new_extension): @@ -41,9 +47,9 @@ def instantiate_template(template, output_fname): """ names = { # The relative location of the header file. - 'header_path' : makeext(topdir_file(output_fname), "h"), + 'header_path' : makeext(srcdir_file(output_fname), "h"), # The relative location of the C file file. - 'c_file_path' : makeext(topdir_file(output_fname), "c"), + 'c_file_path' : makeext(srcdir_file(output_fname), "c"), # The truncated name of the file. 'short_name' : os.path.basename(output_fname), # The current year, for the copyright notice @@ -200,7 +206,8 @@ def get_include_am_location(fname): Note that this function is imperfect because our include.am layout is not (yet) consistent. """ - td = topdir_file(fname) + # Strip src for pattern matching, but add it back when returning the path + td = srcdir_file(fname) m = re.match(r'^(lib|core|feature|app)/([a-z0-9_]*)/', td) if m: return "src/{}/{}/include.am".format(m.group(1),m.group(2)) @@ -216,8 +223,15 @@ def run(fn): add them to include.am. """ - if fn.startswith("./"): - fn = fn[2:] + # Make sure we're in the top-level tor directory, + # which contains the src directory + assert(os.path.isdir("src")) + + # Make the file name relative to the top-level tor directory + fn = tordir_file(fn) + # And check that we're adding files to the "src" directory, + # with canonical paths + assert(fn[:4] == "src/") cf = makeext(fn, "c") hf = makeext(fn, "h") |