mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-10 13:13:44 +01:00
add_c_file: Rename variables based on the type of path
* fname for generic file paths * tor_fname for paths relative to the top-level tor directory * src_fname for paths relative to tor's src directory With prefixes as required to disambiguate different paths of the same type. Part of 32962.
This commit is contained in:
parent
b828588499
commit
eb336e23a6
@ -29,17 +29,17 @@ import os
|
|||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
|
|
||||||
def tordir_file(name):
|
def tordir_file(fname):
|
||||||
"""Make name relative to the current directory, which should be the
|
"""Make fname relative to the current directory, which should be the
|
||||||
top-level tor directory. Also performs basic path simplifications."""
|
top-level tor directory. Also performs basic path simplifications."""
|
||||||
return os.path.normpath(os.path.relpath(name))
|
return os.path.normpath(os.path.relpath(fname))
|
||||||
|
|
||||||
def srcdir_file(name):
|
def srcdir_file(tor_fname):
|
||||||
"""Make name relative to tor's "src" directory.
|
"""Make tor_fname relative to tor's "src" directory.
|
||||||
Also performs basic path simplifications.
|
Also performs basic path simplifications.
|
||||||
(This function takes paths relative to the top-level tor directory,
|
(This function takes paths relative to the top-level tor directory,
|
||||||
but outputs a path that is relative to tor's src directory.)"""
|
but outputs a path that is relative to tor's src directory.)"""
|
||||||
return os.path.normpath(os.path.relpath(name, 'src'))
|
return os.path.normpath(os.path.relpath(tor_fname, 'src'))
|
||||||
|
|
||||||
def guard_macro(src_fname):
|
def guard_macro(src_fname):
|
||||||
"""Return the guard macro that should be used for the header file
|
"""Return the guard macro that should be used for the header file
|
||||||
@ -48,13 +48,13 @@ def guard_macro(src_fname):
|
|||||||
td = src_fname.replace(".", "_").replace("/", "_").upper()
|
td = src_fname.replace(".", "_").replace("/", "_").upper()
|
||||||
return "TOR_{}".format(td)
|
return "TOR_{}".format(td)
|
||||||
|
|
||||||
def makeext(name, new_extension):
|
def makeext(fname, new_extension):
|
||||||
"""Replace the extension for the file called 'name' with 'new_extension'.
|
"""Replace the extension for the file called 'fname' with 'new_extension'.
|
||||||
This function takes and returns paths relative to either the top-level
|
This function takes and returns paths relative to either the top-level
|
||||||
tor directory, or tor's src directory, and returns the same kind
|
tor directory, or tor's src directory, and returns the same kind
|
||||||
of path.
|
of path.
|
||||||
"""
|
"""
|
||||||
base = os.path.splitext(name)[0]
|
base = os.path.splitext(fname)[0]
|
||||||
return base + "." + new_extension
|
return base + "." + new_extension
|
||||||
|
|
||||||
def instantiate_template(template, tor_fname):
|
def instantiate_template(template, tor_fname):
|
||||||
@ -154,11 +154,11 @@ class AutomakeChunk:
|
|||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def insertMember(self, member):
|
def insertMember(self, new_tor_fname):
|
||||||
"""
|
"""
|
||||||
Add a new member to this chunk. Try to insert it in alphabetical
|
Add a new file name new_tor_fname to this chunk. Try to insert it in
|
||||||
order with matching indentation, but don't freak out too much if the
|
alphabetical order with matching indentation, but don't freak out too
|
||||||
source isn't consistent.
|
much if the source isn't consistent.
|
||||||
|
|
||||||
Assumes that this chunk is of the form:
|
Assumes that this chunk is of the form:
|
||||||
FOOBAR = \
|
FOOBAR = \
|
||||||
@ -175,20 +175,21 @@ class AutomakeChunk:
|
|||||||
m = re.match(r'(\s+)(\S+)(\s+)\\', line)
|
m = re.match(r'(\s+)(\S+)(\s+)\\', line)
|
||||||
if not m:
|
if not m:
|
||||||
continue
|
continue
|
||||||
prespace, fname, postspace = m.groups()
|
prespace, cur_tor_fname, postspace = m.groups()
|
||||||
if fname > member:
|
if cur_tor_fname > new_tor_fname:
|
||||||
self.insert_before(lineno, member, prespace, postspace)
|
self.insert_before(lineno, new_tor_fname, prespace, postspace)
|
||||||
return
|
return
|
||||||
self.insert_at_end(member, prespace, postspace)
|
self.insert_at_end(new_tor_fname, prespace, postspace)
|
||||||
|
|
||||||
def insert_before(self, lineno, member, prespace, postspace):
|
def insert_before(self, lineno, new_tor_fname, prespace, postspace):
|
||||||
self.lines.insert(lineno,
|
self.lines.insert(lineno,
|
||||||
"{}{}{}\\\n".format(prespace, member, postspace))
|
"{}{}{}\\\n".format(prespace, new_tor_fname,
|
||||||
|
postspace))
|
||||||
|
|
||||||
def insert_at_end(self, member, prespace, postspace):
|
def insert_at_end(self, new_tor_fname, prespace, postspace):
|
||||||
lastline = self.lines[-1].strip()
|
lastline = self.lines[-1].strip()
|
||||||
self.lines[-1] = '{}{}{}\\\n'.format(prespace, lastline, postspace)
|
self.lines[-1] = '{}{}{}\\\n'.format(prespace, lastline, postspace)
|
||||||
self.lines.append("{}{}\n".format(prespace, member))
|
self.lines.append("{}{}\n".format(prespace, new_tor_fname))
|
||||||
|
|
||||||
def dump(self, f):
|
def dump(self, f):
|
||||||
"""Write all the lines in this chunk to the file 'f'."""
|
"""Write all the lines in this chunk to the file 'f'."""
|
||||||
@ -215,15 +216,15 @@ class ParsedAutomake:
|
|||||||
self.chunks.append(chunk)
|
self.chunks.append(chunk)
|
||||||
self.by_type[chunk.kind.lower()] = chunk
|
self.by_type[chunk.kind.lower()] = chunk
|
||||||
|
|
||||||
def add_file(self, fname, kind):
|
def add_file(self, tor_fname, kind):
|
||||||
"""Insert a file of kind 'kind' to the appropriate section of this
|
"""Insert a file tor_fname of kind 'kind' to the appropriate
|
||||||
file. Return True if we added it.
|
section of this file. Return True if we added it.
|
||||||
|
|
||||||
This function operates on paths relative to the top-level tor
|
This function operates on paths relative to the top-level tor
|
||||||
directory.
|
directory.
|
||||||
"""
|
"""
|
||||||
if kind.lower() in self.by_type:
|
if kind.lower() in self.by_type:
|
||||||
self.by_type[kind.lower()].insertMember(fname)
|
self.by_type[kind.lower()].insertMember(tor_fname)
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
@ -233,9 +234,9 @@ class ParsedAutomake:
|
|||||||
for chunk in self.chunks:
|
for chunk in self.chunks:
|
||||||
chunk.dump(f)
|
chunk.dump(f)
|
||||||
|
|
||||||
def get_include_am_location(fname):
|
def get_include_am_location(tor_fname):
|
||||||
"""Find the right include.am file for introducing a new file. Return None
|
"""Find the right include.am file for introducing a new file
|
||||||
if we can't guess one.
|
tor_fname. Return None if we can't guess one.
|
||||||
|
|
||||||
Note that this function is imperfect because our include.am layout is
|
Note that this function is imperfect because our include.am layout is
|
||||||
not (yet) consistent.
|
not (yet) consistent.
|
||||||
@ -243,20 +244,20 @@ def get_include_am_location(fname):
|
|||||||
This function operates on paths relative to the top-level tor directory.
|
This function operates on paths relative to the top-level tor directory.
|
||||||
"""
|
"""
|
||||||
# Strip src for pattern matching, but add it back when returning the path
|
# Strip src for pattern matching, but add it back when returning the path
|
||||||
td = srcdir_file(fname)
|
src_fname = srcdir_file(tor_fname)
|
||||||
m = re.match(r'^(lib|core|feature|app)/([a-z0-9_]*)/', td)
|
m = re.match(r'^(lib|core|feature|app)/([a-z0-9_]*)/', src_fname)
|
||||||
if m:
|
if m:
|
||||||
return "src/{}/{}/include.am".format(m.group(1),m.group(2))
|
return "src/{}/{}/include.am".format(m.group(1),m.group(2))
|
||||||
|
|
||||||
if re.match(r'^test/', td):
|
if re.match(r'^test/', src_fname):
|
||||||
return "src/test/include.am"
|
return "src/test/include.am"
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def run(fn):
|
def run(fname):
|
||||||
"""
|
"""
|
||||||
Create a new C file and H file corresponding to the filename "fn", and
|
Create a new C file and H file corresponding to the filename "fname",
|
||||||
add them to the corresponding include.am.
|
and add them to the corresponding include.am.
|
||||||
|
|
||||||
This function operates on paths relative to the top-level tor directory.
|
This function operates on paths relative to the top-level tor directory.
|
||||||
"""
|
"""
|
||||||
@ -266,31 +267,31 @@ def run(fn):
|
|||||||
assert(os.path.isdir("src"))
|
assert(os.path.isdir("src"))
|
||||||
|
|
||||||
# Make the file name relative to the top-level tor directory
|
# Make the file name relative to the top-level tor directory
|
||||||
fn = tordir_file(fn)
|
tor_fname = tordir_file(fname)
|
||||||
# And check that we're adding files to the "src" directory,
|
# And check that we're adding files to the "src" directory,
|
||||||
# with canonical paths
|
# with canonical paths
|
||||||
assert(fn[:4] == "src/")
|
assert(tor_fname[:4] == "src/")
|
||||||
|
|
||||||
cf = makeext(fn, "c")
|
c_tor_fname = makeext(tor_fname, "c")
|
||||||
hf = makeext(fn, "h")
|
h_tor_fname = makeext(tor_fname, "h")
|
||||||
|
|
||||||
if os.path.exists(cf):
|
if os.path.exists(c_tor_fname):
|
||||||
print("{} already exists".format(cf))
|
print("{} already exists".format(c_tor_fname))
|
||||||
return 1
|
return 1
|
||||||
if os.path.exists(hf):
|
if os.path.exists(h_tor_fname):
|
||||||
print("{} already exists".format(hf))
|
print("{} already exists".format(h_tor_fname))
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
with open(cf, 'w') as f:
|
with open(c_tor_fname, 'w') as f:
|
||||||
f.write(instantiate_template(C_FILE_TEMPLATE, cf))
|
f.write(instantiate_template(C_FILE_TEMPLATE, c_tor_fname))
|
||||||
|
|
||||||
with open(hf, 'w') as f:
|
with open(h_tor_fname, 'w') as f:
|
||||||
f.write(instantiate_template(HEADER_TEMPLATE, hf))
|
f.write(instantiate_template(HEADER_TEMPLATE, h_tor_fname))
|
||||||
|
|
||||||
iam = get_include_am_location(cf)
|
iam = get_include_am_location(c_tor_fname)
|
||||||
if iam is None or not os.path.exists(iam):
|
if iam is None or not os.path.exists(iam):
|
||||||
print("Made files successfully but couldn't identify include.am for {}"
|
print("Made files successfully but couldn't identify include.am for {}"
|
||||||
.format(cf))
|
.format(c_tor_fname))
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
amfile = ParsedAutomake()
|
amfile = ParsedAutomake()
|
||||||
@ -302,8 +303,8 @@ def run(fn):
|
|||||||
cur_chunk = AutomakeChunk()
|
cur_chunk = AutomakeChunk()
|
||||||
amfile.addChunk(cur_chunk)
|
amfile.addChunk(cur_chunk)
|
||||||
|
|
||||||
amfile.add_file(cf, "sources")
|
amfile.add_file(c_tor_fname, "sources")
|
||||||
amfile.add_file(hf, "headers")
|
amfile.add_file(h_tor_fname, "headers")
|
||||||
|
|
||||||
with open(iam+".tmp", 'w') as f:
|
with open(iam+".tmp", 'w') as f:
|
||||||
amfile.dump(f)
|
amfile.dump(f)
|
||||||
|
Loading…
Reference in New Issue
Block a user