Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor work_dir creation #171

Merged
merged 6 commits into from
Oct 11, 2023
Merged

Refactor work_dir creation #171

merged 6 commits into from
Oct 11, 2023

Conversation

ccarouge
Copy link
Collaborator

@ccarouge ccarouge commented Oct 5, 2023

Fixes #150

Added a mkdir() wrapper to use throughout the code:

  • benchcab to create src/, hence removing the setup_src_dir() function.
  • fluxsite to create the task directories within setup_task()
  • workdir to create the fluxsite run directory tree.

Defined FLUXSITE_DIRS: a dictionary in internal.py that contains all the individual directories for the fluxsite runs.

Change the creation and deletion of fluxsite directory tree and source directory to use relative paths instead of absolute paths.

@codecov
Copy link

codecov bot commented Oct 5, 2023

Codecov Report

Merging #171 (f083d3e) into main (56448d4) will decrease coverage by 0.40%.
Report is 1 commits behind head on main.
The diff coverage is 95.83%.

@@            Coverage Diff             @@
##             main     #171      +/-   ##
==========================================
- Coverage   88.97%   88.58%   -0.40%     
==========================================
  Files          26       26              
  Lines        1660     1603      -57     
==========================================
- Hits         1477     1420      -57     
  Misses        183      183              
Files Coverage Δ
benchcab/comparison.py 61.36% <ø> (ø)
benchcab/internal.py 90.69% <100.00%> (+0.22%) ⬆️
benchcab/utils/fs.py 100.00% <100.00%> (ø)
benchcab/workdir.py 100.00% <100.00%> (ø)
tests/test_benchcab.py 100.00% <ø> (ø)
tests/test_comparison.py 100.00% <100.00%> (ø)
tests/test_fluxsite.py 100.00% <100.00%> (ø)
tests/test_fs.py 100.00% <100.00%> (ø)
tests/test_workdir.py 100.00% <100.00%> (ø)
benchcab/fluxsite.py 83.13% <87.50%> (+0.09%) ⬆️
... and 1 more

benchcab/workdir.py Outdated Show resolved Hide resolved
benchcab/workdir.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@SeanBryan51 SeanBryan51 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I just have a question/comment on the use of chdir

Comment on lines 166 to 167
with chdir(self.root_dir):
mkdir(internal.SRC_DIR, exist_ok=True, verbose=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably remove the chdir here? I notice you have used chdir elsewhere in the code. Was it required to make the tests pass?

Suggested change
with chdir(self.root_dir):
mkdir(internal.SRC_DIR, exist_ok=True, verbose=True)
mkdir(internal.SRC_DIR, exist_ok=True, verbose=True)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the current instantiation of the tests, without mkdir the paths will be created where the tests are run and not in the temporary location. The paths are deleted right away in the tests so nobody will see them but it is still an undesired side effect. Especially if something goes wrong and they are not deleted.

With the redesign of the tests, since we have a fixture that changes the CWD for the whole test, it will work without chdir.

I'm happy to wait for the tests redesign to go in first.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised the files are not being created in the temporary location since there is a run_around_tests() autouse fixture which changes the CWD. But I'm happy for this to wait until #172 goes in.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll double check...

@SeanBryan51
Copy link
Collaborator

Doing this made me wonder if we shouldn't define a "FLUXSITE_DIR" dictionary in internal.py instead of creating the list when running benchcab... But it might wait for a redesign of internal.py instead.

Yep it is basically a constant so it could go in internal.py. We could define the relative path list in internal.py and iterate over that?

@ccarouge
Copy link
Collaborator Author

ccarouge commented Oct 9, 2023

@SeanBryan51 I think we need to discuss our strategy to create/remove directories. For the moment, we are going towards using relative paths to create the fluxsite run tree. But the clean function uses absolute paths:

def clean_directory_tree(root_dir=internal.CWD):
    """Remove pre-existing directories in current working directory."""
    src_dir = Path(root_dir, internal.SRC_DIR)
    if src_dir.exists():
        shutil.rmtree(src_dir)

    run_dir = Path(root_dir, internal.RUN_DIR)
    if run_dir.exists():
        shutil.rmtree(run_dir)

This makes it harder to remember what they do, in particular when writing tests as root_dir becomes redundant in some cases but not others.

Since internal.py contains the relative paths SRC_DIR and RUN_DIR, should we instead write clean_directory_tree with relative paths and handle the location we call it from the outside? Or is it safer to have functions work on absolute paths so they work always the same way no matter how they are called?

@SeanBryan51
Copy link
Collaborator

Since internal.py contains the relative paths SRC_DIR and RUN_DIR, should we instead write clean_directory_tree with relative paths and handle the location we call it from the outside?

Yes. I think ideally we should use relative paths whenever paths are prefixed by the CWD. Maybe we can take a look at this when we fix issue #162?

tests/test_workdir.py Outdated Show resolved Hide resolved
tests/test_workdir.py Outdated Show resolved Hide resolved
@@ -57,23 +57,27 @@
CABLE_AUX_DIR / "core" / "biogeochem" / "pftlookup_csiro_v16_17tiles.csv"
)

# Fluxsite directory tree
FLUXSITE_DIRS = {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would a list be better here? I.e.

FLUXSITE_DIRS = [
    FLUXSITE_RUN_DIR,
    FLUXSITE_LOG_DIR,
    ...
]

I don't mind either way, but it seems using a list would have less changes in the code base.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like having 2 places with the same constants: the constant with its name and then a list with all of them that is used only once in the code after that. Hence the idea of a dictionary so that references to these paths are always handled in the same way throughout the code.

@ccarouge ccarouge merged commit fdcab39 into main Oct 11, 2023
3 of 4 checks passed
@ccarouge ccarouge deleted the 150-refactor-work_dir-creation branch October 11, 2023 23:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor work_dir creation
2 participants