Skip to content

Commit

Permalink
Merge pull request #394 from Fortran-FOSS-Programmers/fix-exclude-dirs
Browse files Browse the repository at this point in the history
Fix `exclude_dirs`
  • Loading branch information
ZedThree authored Mar 14, 2022
2 parents ccbea6e + 74bd57f commit 3a30856
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 76 deletions.
2 changes: 2 additions & 0 deletions example/example-project-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ extra_mods: json_module: http://jacobwilliams.github.io/json-fortran/
license: by-nc
extra_filetypes: sh #
max_frontpage_items: 4
exclude: src/excluded_file.f90
exclude_dir: src/excluded_directory
---

Hi, my name is ${USER}.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
program excluded_program
end program excluded_program
2 changes: 2 additions & 0 deletions example/src/excluded_file.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
program this_program_will_be_excluded
end program this_program_will_be_excluded
13 changes: 6 additions & 7 deletions ford/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,6 @@ def parse_arguments(
base_dir = pathlib.Path(directory).absolute()
proj_data["base_dir"] = base_dir

def normalise_path(path):
"""Tidy up path, making it absolute, relative to base_dir"""
return (base_dir / os.path.expandvars(path)).absolute()

for var in [
"page_dir",
"output_dir",
Expand All @@ -447,18 +443,21 @@ def normalise_path(path):
"css",
"mathjax_config",
"src_dir",
"exclude",
"exclude_dir",
"include",
]:
if proj_data[var] is None:
continue
if isinstance(proj_data[var], list):
proj_data[var] = [normalise_path(p) for p in proj_data[var]]
proj_data[var] = [
ford.utils.normalise_path(base_dir, p) for p in proj_data[var]
]
else:
proj_data[var] = normalise_path(proj_data[var])
proj_data[var] = ford.utils.normalise_path(base_dir, proj_data[var])

if proj_data["favicon"].strip() != DEFAULT_SETTINGS["favicon"]:
proj_data["favicon"] = normalise_path(proj_data["favicon"])
proj_data["favicon"] = ford.utils.normalise_path(base_dir, proj_data["favicon"])

proj_data["display"] = [item.lower() for item in proj_data["display"]]
proj_data["creation_date"] = datetime.now().strftime(proj_data["creation_date"])
Expand Down
100 changes: 32 additions & 68 deletions ford/fortran_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#

import os
import pathlib
import toposort
import ford.utils
import ford.sourceform
Expand Down Expand Up @@ -80,56 +81,37 @@ def __init__(self, settings):
# Get all files within topdir, recursively
srcdir_list = self.make_srcdir_list(settings["exclude_dir"])
for curdir in srcdir_list:
for item in [
f
for f in os.listdir(curdir)
if not os.path.isdir(os.path.join(curdir, f))
]:
ext = item.split(".")[-1]
if (
ext in self.extensions or ext in self.fixed_extensions
) and item not in settings["exclude"]:
for item in [f for f in curdir.iterdir() if f.is_file()]:
if item in settings["exclude"]:
continue

filename = curdir / item
relative_path = os.path.relpath(filename)
extension = str(item.suffix)[1:] # Don't include the initial '.'
if extension in self.extensions or extension in self.fixed_extensions:
# Get contents of the file
print(
"Reading file {}".format(
os.path.relpath(os.path.join(curdir, item))
)
)
if item.split(".")[-1] in settings["fpp_extensions"]:
print(f"Reading file {relative_path}")
if extension in settings["fpp_extensions"]:
preprocessor = settings["preprocessor"]
else:
preprocessor = None
if settings["dbg"]:
try:
self.files.append(
ford.sourceform.FortranSourceFile(
os.path.join(curdir, item),
str(filename),
settings,
preprocessor,
ext in self.fixed_extensions,
extension in self.fixed_extensions,
incl_src=html_incl_src,
encoding=self.encoding,
)
)
else:
try:
self.files.append(
ford.sourceform.FortranSourceFile(
os.path.join(curdir, item),
settings,
preprocessor,
ext in self.fixed_extensions,
incl_src=html_incl_src,
encoding=self.encoding,
)
)
except Exception as e:
print(
"Warning: Error parsing {}.\n\t{}".format(
os.path.relpath(os.path.join(curdir, item)),
e.args[0],
)
)
continue
except Exception as e:
if not settings["dbg"]:
raise e

print(f"Warning: Error parsing {relative_path}.\n\t{e.args[0]}")
continue
for module in self.files[-1].modules:
self.modules.append(module)
for submod in self.files[-1].submodules:
Expand All @@ -145,36 +127,18 @@ def __init__(self, settings):
self.programs.append(program)
for block in self.files[-1].blockdata:
self.blockdata.append(block)
elif (
item.split(".")[-1] in self.extra_filetypes
and item not in settings["exclude"]
):
print(
"Reading file {}".format(
os.path.relpath(os.path.join(curdir, item))
)
)
if settings["dbg"]:
elif extension in self.extra_filetypes:
print(f"Reading file {relative_path}")
try:
self.extra_files.append(
ford.sourceform.GenericSource(
os.path.join(curdir, item), settings
)
ford.sourceform.GenericSource(str(filename), settings)
)
else:
try:
self.extra_files.append(
ford.sourceform.GenericSource(
os.path.join(curdir, item), settings
)
)
except Exception as e:
print(
"Warning: Error parsing {}.\n\t{}".format(
os.path.relpath(os.path.join(curdir, item)),
e.args[0],
)
)
continue
except Exception as e:
if not settings["dbg"]:
raise e

print(f"Warning: Error parsing {relative_path}.\n\t{e.args[0]}")
continue

@property
def allfiles(self):
Expand Down Expand Up @@ -408,8 +372,8 @@ def make_srcdir_list(self, exclude_dirs):
def recursive_dir_list(self, topdir, skip):
dir_list = []
for entry in os.listdir(topdir):
abs_entry = os.path.join(topdir, entry)
if os.path.isdir(abs_entry) and (abs_entry not in skip):
abs_entry = ford.utils.normalise_path(topdir, entry)
if abs_entry.is_dir() and (abs_entry not in skip):
dir_list.append(abs_entry)
dir_list += self.recursive_dir_list(abs_entry, skip)
return dir_list
Expand Down
8 changes: 8 additions & 0 deletions ford/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from urllib.request import urlopen, URLError
from urllib.parse import urljoin
import pathlib
from typing import Union


NOTE_TYPE = {
Expand Down Expand Up @@ -527,3 +528,10 @@ def str_to_bool(text):
raise ValueError(
f"Could not convert string to bool: expected 'true'/'false', got '{text}'"
)


def normalise_path(
base_dir: pathlib.Path, path: Union[str, pathlib.Path]
) -> pathlib.Path:
"""Tidy up path, making it absolute, relative to base_dir"""
return (base_dir / os.path.expandvars(path)).absolute()
43 changes: 42 additions & 1 deletion test/test_project.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ford.sourceform import FortranSourceFile
from ford.fortran_project import Project
from ford import DEFAULT_SETTINGS
from ford.utils import normalise_path

from copy import deepcopy

Expand All @@ -17,7 +18,7 @@ def copy_file(data):
with open(filename, "w") as f:
f.write(data)
settings = deepcopy(DEFAULT_SETTINGS)
settings["src_dir"] = [str(src_dir)]
settings["src_dir"] = [src_dir]
return settings

return copy_file
Expand Down Expand Up @@ -565,3 +566,43 @@ def test_display_internal_procedures(copy_fortran_file):
assert subroutine1.variables == []
assert len(subroutine2.variables) == 1
assert subroutine2.variables[0].name == "local_variable"


def test_exclude_dir(tmp_path):
exclude_dir = tmp_path / "sub1" / "sub2"
exclude_dir.mkdir(parents=True)
src = tmp_path / "src"
src.mkdir()

with open(src / "include.f90", "w") as f:
f.write("program foo\nend program")
with open(exclude_dir / "exclude.f90", "w") as f:
f.write("program bar\nend program")

settings = deepcopy(DEFAULT_SETTINGS)
settings["src_dir"] = [tmp_path]
settings["exclude_dir"] = [normalise_path(tmp_path, "sub1")]
project = Project(settings)

program_names = {program.name for program in project.programs}
assert program_names == {"foo"}


def test_exclude(tmp_path):
exclude_dir = tmp_path / "sub1" / "sub2"
exclude_dir.mkdir(parents=True)
src = tmp_path / "src"
src.mkdir()

with open(src / "include.f90", "w") as f:
f.write("program foo\nend program")
with open(exclude_dir / "exclude.f90", "w") as f:
f.write("program bar\nend program")

settings = deepcopy(DEFAULT_SETTINGS)
settings["src_dir"] = [tmp_path]
settings["exclude"] = [normalise_path(tmp_path, "sub1/sub2/exclude.f90")]
project = Project(settings)

program_names = {program.name for program in project.programs}
assert program_names == {"foo"}

0 comments on commit 3a30856

Please sign in to comment.