Skip to content

Commit

Permalink
Optimize path handling
Browse files Browse the repository at this point in the history
realpath() resolves any and all symlinks in the path; in situations
where we just need to store or print an absolute path, we should use
abspath() instead.

Signed-off-by: John Pennycook <[email protected]>
  • Loading branch information
Pennycook committed Oct 10, 2024
1 parent 20d11b4 commit 95a280f
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions codebasin/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,14 @@ def main():
args.reports = ["all"]

# Determine the root directory based on where codebasin is run.
rootdir = os.path.realpath(os.getcwd())
rootdir = os.path.abspath(os.getcwd())

# Set up a default configuration object.
configuration = {}

# Load the analysis file if it exists.
if args.analysis_file is not None:
path = os.path.realpath(args.analysis_file)
path = os.path.abspath(args.analysis_file)
if os.path.exists(path):
if not os.path.splitext(path)[1] == ".toml":
raise RuntimeError(f"Analysis file {path} must end in .toml.")
Expand Down
8 changes: 4 additions & 4 deletions codebasin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def load_database(dbpath, rootdir):

# Include paths may be specified relative to root
include_paths = [
os.path.realpath(os.path.join(rootdir, f)) for f in include_paths
os.path.abspath(os.path.join(rootdir, f)) for f in include_paths
]

# Files may be specified:
Expand All @@ -336,15 +336,15 @@ def load_database(dbpath, rootdir):
if os.path.isabs(command.directory):
filedir = command.directory
else:
filedir = os.path.realpath(
filedir = os.path.abspath(
rootdir,
os.path.join(command.directory),
)

if os.path.isabs(command.filename):
path = os.path.realpath(command.filename)
path = os.path.abspath(command.filename)
else:
path = os.path.realpath(os.path.join(filedir, command.filename))
path = os.path.abspath(os.path.join(filedir, command.filename))

# Compilation database may contain files that don't
# exist without running make
Expand Down
2 changes: 1 addition & 1 deletion codebasin/file_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class FileParser:
"""

def __init__(self, _filename):
self._filename = os.path.realpath(_filename)
self._filename = os.path.abspath(_filename)

@staticmethod
def handle_directive(out_tree, groups, logical_line):
Expand Down
2 changes: 1 addition & 1 deletion codebasin/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def find_include_file(self, filename, this_path, is_system_include=False):

# Determine the path to the include file, if it exists
for path in local_paths + self._include_paths:
test_path = os.path.realpath(os.path.join(path, filename))
test_path = os.path.abspath(os.path.join(path, filename))
if os.path.isfile(test_path):
include_file = test_path
self.found_incl[filename] = include_file
Expand Down

0 comments on commit 95a280f

Please sign in to comment.