2019-05-20 17:52:45 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
"""
|
|
|
|
Add a C file with matching header to the Tor codebase. Creates
|
|
|
|
both files from templates, and adds them to the right include.am file.
|
|
|
|
|
2020-01-20 04:20:14 +01:00
|
|
|
This script takes paths relative to the top-level tor directory. It
|
|
|
|
expects to be run from that directory.
|
|
|
|
|
|
|
|
This script creates files, and inserts them into include.am, also
|
|
|
|
relative to the top-level tor directory.
|
2020-01-15 23:48:44 +01:00
|
|
|
|
|
|
|
But the template content in those files is relative to tor's src
|
|
|
|
directory. (This script strips "src" from the paths used to create
|
|
|
|
templated comments and macros.)
|
|
|
|
|
|
|
|
This script expects posix paths, so it should be run with a python
|
2020-01-20 04:20:14 +01:00
|
|
|
where os.path is posixpath. (Rather than ntpath.) This probably means
|
|
|
|
Linux, macOS, or BSD, although it might work on Windows if your python
|
|
|
|
was compiled with mingw, MSYS, or cygwin.
|
2020-01-15 23:48:44 +01:00
|
|
|
|
2019-05-20 17:52:45 +02:00
|
|
|
Example usage:
|
|
|
|
|
|
|
|
% add_c_file.py ./src/feature/dirauth/ocelot.c
|
|
|
|
"""
|
|
|
|
|
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-05-20 17:52:45 +02:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import time
|
|
|
|
|
2020-01-16 00:15:22 +01:00
|
|
|
def tordir_file(fname):
|
|
|
|
"""Make fname relative to the current directory, which should be the
|
2020-01-15 23:36:45 +01:00
|
|
|
top-level tor directory. Also performs basic path simplifications."""
|
2020-01-16 00:15:22 +01:00
|
|
|
return os.path.normpath(os.path.relpath(fname))
|
2020-01-15 23:36:45 +01:00
|
|
|
|
2020-01-16 00:15:22 +01:00
|
|
|
def srcdir_file(tor_fname):
|
|
|
|
"""Make tor_fname relative to tor's "src" directory.
|
2020-01-15 23:48:44 +01:00
|
|
|
Also performs basic path simplifications.
|
|
|
|
(This function takes paths relative to the top-level tor directory,
|
|
|
|
but outputs a path that is relative to tor's src directory.)"""
|
2020-01-16 00:15:22 +01:00
|
|
|
return os.path.normpath(os.path.relpath(tor_fname, 'src'))
|
2019-05-20 17:52:45 +02:00
|
|
|
|
2020-01-15 23:55:20 +01:00
|
|
|
def guard_macro(src_fname):
|
|
|
|
"""Return the guard macro that should be used for the header file
|
|
|
|
'src_fname'. This function takes paths relative to tor's src directory.
|
2019-05-20 17:52:45 +02:00
|
|
|
"""
|
2020-01-15 23:55:20 +01:00
|
|
|
td = src_fname.replace(".", "_").replace("/", "_").upper()
|
2019-05-20 17:52:45 +02:00
|
|
|
return "TOR_{}".format(td)
|
|
|
|
|
2020-01-16 00:15:22 +01:00
|
|
|
def makeext(fname, new_extension):
|
|
|
|
"""Replace the extension for the file called 'fname' with 'new_extension'.
|
2020-01-15 23:48:44 +01:00
|
|
|
This function takes and returns paths relative to either the top-level
|
|
|
|
tor directory, or tor's src directory, and returns the same kind
|
|
|
|
of path.
|
2019-05-20 17:52:45 +02:00
|
|
|
"""
|
2020-01-16 00:15:22 +01:00
|
|
|
base = os.path.splitext(fname)[0]
|
2019-05-20 17:52:45 +02:00
|
|
|
return base + "." + new_extension
|
|
|
|
|
2020-01-15 23:55:20 +01:00
|
|
|
def instantiate_template(template, tor_fname):
|
2019-05-20 17:52:45 +02:00
|
|
|
"""
|
|
|
|
Fill in a template with string using the fields that should be used
|
2020-01-15 23:55:20 +01:00
|
|
|
for 'tor_fname'.
|
2020-01-15 23:48:44 +01:00
|
|
|
|
|
|
|
This function takes paths relative to the top-level tor directory,
|
|
|
|
but the paths in the completed template are relative to tor's src
|
|
|
|
directory. (Except for one of the fields, which is just a basename).
|
2019-05-20 17:52:45 +02:00
|
|
|
"""
|
2020-01-15 23:55:20 +01:00
|
|
|
src_fname = srcdir_file(tor_fname)
|
2019-05-20 17:52:45 +02:00
|
|
|
names = {
|
|
|
|
# The relative location of the header file.
|
2020-01-15 23:55:20 +01:00
|
|
|
'header_path' : makeext(src_fname, "h"),
|
2019-05-20 17:52:45 +02:00
|
|
|
# The relative location of the C file file.
|
2020-01-15 23:55:20 +01:00
|
|
|
'c_file_path' : makeext(src_fname, "c"),
|
2019-05-20 17:52:45 +02:00
|
|
|
# The truncated name of the file.
|
2020-01-15 23:55:20 +01:00
|
|
|
'short_name' : os.path.basename(src_fname),
|
2019-05-20 17:52:45 +02:00
|
|
|
# The current year, for the copyright notice
|
|
|
|
'this_year' : time.localtime().tm_year,
|
|
|
|
# An appropriate guard macro, for the header.
|
2020-01-15 23:55:20 +01:00
|
|
|
'guard_macro' : guard_macro(src_fname),
|
2019-05-20 17:52:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return template.format(**names)
|
|
|
|
|
2020-01-15 23:48:44 +01:00
|
|
|
# This template operates on paths relative to tor's src directory
|
2019-05-20 17:52:45 +02:00
|
|
|
HEADER_TEMPLATE = """\
|
|
|
|
/* Copyright (c) 2001 Matej Pfajfar.
|
|
|
|
* Copyright (c) 2001-2004, Roger Dingledine.
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
|
|
|
* Copyright (c) 2007-{this_year}, The Tor Project, Inc. */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file {short_name}
|
|
|
|
* @brief Header for {c_file_path}
|
|
|
|
**/
|
|
|
|
|
|
|
|
#ifndef {guard_macro}
|
|
|
|
#define {guard_macro}
|
|
|
|
|
|
|
|
#endif /* !defined({guard_macro}) */
|
|
|
|
"""
|
|
|
|
|
2020-01-15 23:48:44 +01:00
|
|
|
# This template operates on paths relative to the tor's src directory
|
2019-05-20 17:52:45 +02:00
|
|
|
C_FILE_TEMPLATE = """\
|
|
|
|
/* Copyright (c) 2001 Matej Pfajfar.
|
|
|
|
* Copyright (c) 2001-2004, Roger Dingledine.
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
|
|
|
* Copyright (c) 2007-{this_year}, The Tor Project, Inc. */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file {short_name}
|
|
|
|
* @brief DOCDOC
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include "orconfig.h"
|
|
|
|
#include "{header_path}"
|
|
|
|
"""
|
|
|
|
|
|
|
|
class AutomakeChunk:
|
|
|
|
"""
|
|
|
|
Represents part of an automake file. If it is decorated with
|
|
|
|
an ADD_C_FILE comment, it has a "kind" based on what to add to it.
|
|
|
|
Otherwise, it only has a bunch of lines in it.
|
2020-01-15 23:48:44 +01:00
|
|
|
|
|
|
|
This class operates on paths relative to the top-level tor directory.
|
2019-05-20 17:52:45 +02:00
|
|
|
"""
|
|
|
|
pat = re.compile(r'# ADD_C_FILE: INSERT (\S*) HERE', re.I)
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.lines = []
|
|
|
|
self.kind = ""
|
2020-01-15 18:54:47 +01:00
|
|
|
self.hasBlank = False # true if we end with a blank line.
|
2019-05-20 17:52:45 +02:00
|
|
|
|
|
|
|
def addLine(self, line):
|
|
|
|
"""
|
|
|
|
Insert a line into this chunk while parsing the automake file.
|
2020-01-15 18:54:47 +01:00
|
|
|
|
|
|
|
Return True if we have just read the last line in the chunk, and
|
|
|
|
False otherwise.
|
2019-05-20 17:52:45 +02:00
|
|
|
"""
|
|
|
|
m = self.pat.match(line)
|
|
|
|
if m:
|
|
|
|
if self.lines:
|
|
|
|
raise ValueError("control line not preceded by a blank line")
|
|
|
|
self.kind = m.group(1)
|
|
|
|
|
|
|
|
if line.strip() == "":
|
2020-01-15 18:54:47 +01:00
|
|
|
self.hasBlank = True
|
2019-05-20 17:52:45 +02:00
|
|
|
return True
|
|
|
|
|
2020-01-15 18:54:47 +01:00
|
|
|
self.lines.append(line)
|
|
|
|
|
2019-05-20 17:52:45 +02:00
|
|
|
return False
|
|
|
|
|
2020-01-16 00:15:22 +01:00
|
|
|
def insertMember(self, new_tor_fname):
|
2019-05-20 17:52:45 +02:00
|
|
|
"""
|
2020-01-16 00:15:22 +01:00
|
|
|
Add a new file name new_tor_fname to this chunk. Try to insert it in
|
|
|
|
alphabetical order with matching indentation, but don't freak out too
|
|
|
|
much if the source isn't consistent.
|
2019-05-20 17:52:45 +02:00
|
|
|
|
|
|
|
Assumes that this chunk is of the form:
|
|
|
|
FOOBAR = \
|
|
|
|
X \
|
|
|
|
Y \
|
|
|
|
Z
|
2020-01-15 23:48:44 +01:00
|
|
|
|
|
|
|
This function operates on paths relative to the top-level tor
|
|
|
|
directory.
|
2019-05-20 17:52:45 +02:00
|
|
|
"""
|
2019-06-14 14:42:24 +02:00
|
|
|
prespace = "\t"
|
|
|
|
postspace = "\t\t"
|
2019-05-20 17:52:45 +02:00
|
|
|
for lineno, line in enumerate(self.lines):
|
|
|
|
m = re.match(r'(\s+)(\S+)(\s+)\\', line)
|
|
|
|
if not m:
|
|
|
|
continue
|
2020-01-16 00:15:22 +01:00
|
|
|
prespace, cur_tor_fname, postspace = m.groups()
|
|
|
|
if cur_tor_fname > new_tor_fname:
|
|
|
|
self.insert_before(lineno, new_tor_fname, prespace, postspace)
|
2019-05-20 17:52:45 +02:00
|
|
|
return
|
2020-01-16 00:15:22 +01:00
|
|
|
self.insert_at_end(new_tor_fname, prespace, postspace)
|
2019-05-20 17:52:45 +02:00
|
|
|
|
2020-01-16 00:15:22 +01:00
|
|
|
def insert_before(self, lineno, new_tor_fname, prespace, postspace):
|
2019-05-20 17:52:45 +02:00
|
|
|
self.lines.insert(lineno,
|
2020-01-16 00:15:22 +01:00
|
|
|
"{}{}{}\\\n".format(prespace, new_tor_fname,
|
|
|
|
postspace))
|
2019-05-20 17:52:45 +02:00
|
|
|
|
2020-01-16 00:15:22 +01:00
|
|
|
def insert_at_end(self, new_tor_fname, prespace, postspace):
|
2020-01-15 18:54:47 +01:00
|
|
|
lastline = self.lines[-1].strip()
|
|
|
|
self.lines[-1] = '{}{}{}\\\n'.format(prespace, lastline, postspace)
|
2020-01-16 00:15:22 +01:00
|
|
|
self.lines.append("{}{}\n".format(prespace, new_tor_fname))
|
2019-05-20 17:52:45 +02:00
|
|
|
|
|
|
|
def dump(self, f):
|
|
|
|
"""Write all the lines in this chunk to the file 'f'."""
|
|
|
|
for line in self.lines:
|
|
|
|
f.write(line)
|
|
|
|
if not line.endswith("\n"):
|
|
|
|
f.write("\n")
|
|
|
|
|
2020-01-15 18:54:47 +01:00
|
|
|
if self.hasBlank:
|
|
|
|
f.write("\n")
|
|
|
|
|
2019-05-20 17:52:45 +02:00
|
|
|
class ParsedAutomake:
|
|
|
|
"""A sort-of-parsed automake file, with identified chunks into which
|
|
|
|
headers and c files can be inserted.
|
2020-01-15 23:48:44 +01:00
|
|
|
|
|
|
|
This class operates on paths relative to the top-level tor directory.
|
2019-05-20 17:52:45 +02:00
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
self.chunks = []
|
|
|
|
self.by_type = {}
|
|
|
|
|
|
|
|
def addChunk(self, chunk):
|
|
|
|
"""Add a newly parsed AutomakeChunk to this file."""
|
|
|
|
self.chunks.append(chunk)
|
|
|
|
self.by_type[chunk.kind.lower()] = chunk
|
|
|
|
|
2020-01-16 00:15:22 +01:00
|
|
|
def add_file(self, tor_fname, kind):
|
|
|
|
"""Insert a file tor_fname of kind 'kind' to the appropriate
|
|
|
|
section of this file. Return True if we added it.
|
2020-01-15 23:48:44 +01:00
|
|
|
|
|
|
|
This function operates on paths relative to the top-level tor
|
|
|
|
directory.
|
2019-05-20 17:52:45 +02:00
|
|
|
"""
|
|
|
|
if kind.lower() in self.by_type:
|
2020-01-16 00:15:22 +01:00
|
|
|
self.by_type[kind.lower()].insertMember(tor_fname)
|
2019-05-20 17:52:45 +02:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def dump(self, f):
|
|
|
|
"""Write this file into a file 'f'."""
|
|
|
|
for chunk in self.chunks:
|
|
|
|
chunk.dump(f)
|
|
|
|
|
2020-01-16 00:15:22 +01:00
|
|
|
def get_include_am_location(tor_fname):
|
|
|
|
"""Find the right include.am file for introducing a new file
|
|
|
|
tor_fname. Return None if we can't guess one.
|
2019-05-20 17:52:45 +02:00
|
|
|
|
|
|
|
Note that this function is imperfect because our include.am layout is
|
|
|
|
not (yet) consistent.
|
2020-01-15 23:48:44 +01:00
|
|
|
|
|
|
|
This function operates on paths relative to the top-level tor directory.
|
2019-05-20 17:52:45 +02:00
|
|
|
"""
|
2020-01-15 23:36:45 +01:00
|
|
|
# Strip src for pattern matching, but add it back when returning the path
|
2020-01-16 00:15:22 +01:00
|
|
|
src_fname = srcdir_file(tor_fname)
|
|
|
|
m = re.match(r'^(lib|core|feature|app)/([a-z0-9_]*)/', src_fname)
|
2019-05-20 17:52:45 +02:00
|
|
|
if m:
|
2020-01-13 15:34:17 +01:00
|
|
|
return "src/{}/{}/include.am".format(m.group(1),m.group(2))
|
2019-05-20 17:52:45 +02:00
|
|
|
|
2020-01-16 00:15:22 +01:00
|
|
|
if re.match(r'^test/', src_fname):
|
2019-05-20 17:52:45 +02:00
|
|
|
return "src/test/include.am"
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2020-01-16 00:15:22 +01:00
|
|
|
def run(fname):
|
2019-05-20 17:52:45 +02:00
|
|
|
"""
|
2020-01-16 00:15:22 +01:00
|
|
|
Create a new C file and H file corresponding to the filename "fname",
|
|
|
|
and add them to the corresponding include.am.
|
2020-01-15 23:48:44 +01:00
|
|
|
|
|
|
|
This function operates on paths relative to the top-level tor directory.
|
2019-05-20 17:52:45 +02:00
|
|
|
"""
|
|
|
|
|
2020-01-15 23:36:45 +01:00
|
|
|
# Make sure we're in the top-level tor directory,
|
|
|
|
# which contains the src directory
|
2020-01-20 04:20:42 +01:00
|
|
|
if not os.path.isdir("src"):
|
|
|
|
raise RuntimeError("Could not find './src/'. "
|
|
|
|
"Run this script from the top-level tor source "
|
|
|
|
"directory.")
|
|
|
|
|
2020-01-20 04:04:02 +01:00
|
|
|
# And it looks like a tor/src directory
|
2020-01-20 04:20:42 +01:00
|
|
|
if not os.path.isfile("src/include.am"):
|
|
|
|
raise RuntimeError("Could not find './src/include.am'. "
|
|
|
|
"Run this script from the top-level tor source "
|
|
|
|
"directory.")
|
2020-01-15 23:36:45 +01:00
|
|
|
|
|
|
|
# Make the file name relative to the top-level tor directory
|
2020-01-16 00:15:22 +01:00
|
|
|
tor_fname = tordir_file(fname)
|
2020-01-15 23:36:45 +01:00
|
|
|
# And check that we're adding files to the "src" directory,
|
|
|
|
# with canonical paths
|
2020-01-20 04:20:42 +01:00
|
|
|
if tor_fname[:4] != "src/":
|
|
|
|
raise ValueError("Requested file path '{}' canonicalized to '{}', "
|
|
|
|
"but the canonical path did not start with 'src/'. "
|
|
|
|
"Please add files to the src directory."
|
|
|
|
.format(fname, tor_fname))
|
2020-01-15 18:58:52 +01:00
|
|
|
|
2020-01-16 00:15:22 +01:00
|
|
|
c_tor_fname = makeext(tor_fname, "c")
|
|
|
|
h_tor_fname = makeext(tor_fname, "h")
|
2019-05-20 17:52:45 +02:00
|
|
|
|
2020-01-16 00:15:22 +01:00
|
|
|
if os.path.exists(c_tor_fname):
|
|
|
|
print("{} already exists".format(c_tor_fname))
|
2019-05-20 17:52:45 +02:00
|
|
|
return 1
|
2020-01-16 00:15:22 +01:00
|
|
|
if os.path.exists(h_tor_fname):
|
|
|
|
print("{} already exists".format(h_tor_fname))
|
2019-05-20 17:52:45 +02:00
|
|
|
return 1
|
|
|
|
|
2020-01-16 00:15:22 +01:00
|
|
|
with open(c_tor_fname, 'w') as f:
|
|
|
|
f.write(instantiate_template(C_FILE_TEMPLATE, c_tor_fname))
|
2019-05-20 17:52:45 +02:00
|
|
|
|
2020-01-16 00:15:22 +01:00
|
|
|
with open(h_tor_fname, 'w') as f:
|
|
|
|
f.write(instantiate_template(HEADER_TEMPLATE, h_tor_fname))
|
2019-05-20 17:52:45 +02:00
|
|
|
|
2020-01-16 00:15:22 +01:00
|
|
|
iam = get_include_am_location(c_tor_fname)
|
2019-05-20 17:52:45 +02:00
|
|
|
if iam is None or not os.path.exists(iam):
|
|
|
|
print("Made files successfully but couldn't identify include.am for {}"
|
2020-01-16 00:15:22 +01:00
|
|
|
.format(c_tor_fname))
|
2019-05-20 17:52:45 +02:00
|
|
|
return 1
|
|
|
|
|
|
|
|
amfile = ParsedAutomake()
|
|
|
|
cur_chunk = AutomakeChunk()
|
|
|
|
with open(iam) as f:
|
|
|
|
for line in f:
|
|
|
|
if cur_chunk.addLine(line):
|
|
|
|
amfile.addChunk(cur_chunk)
|
|
|
|
cur_chunk = AutomakeChunk()
|
|
|
|
amfile.addChunk(cur_chunk)
|
|
|
|
|
2020-01-16 00:15:22 +01:00
|
|
|
amfile.add_file(c_tor_fname, "sources")
|
|
|
|
amfile.add_file(h_tor_fname, "headers")
|
2019-05-20 17:52:45 +02:00
|
|
|
|
|
|
|
with open(iam+".tmp", 'w') as f:
|
|
|
|
amfile.dump(f)
|
|
|
|
|
|
|
|
os.rename(iam+".tmp", iam)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
|
|
|
sys.exit(run(sys.argv[1]))
|