Skip to content

Commit

Permalink
Merge pull request #91 from shivaniikum/cupid-clear
Browse files Browse the repository at this point in the history
"cupid-clear" Command Implementation Addressing Issue #85
  • Loading branch information
mnlevy1981 authored Apr 11, 2024
2 parents dec3cef + 1648661 commit bf65cdd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ $ cupid-build config.yml # Will build HTML from Jupyter Book
After the last step is finished, you can use Jupyter to view generated notebooks in `${CUPID_ROOT}/examples/coupled-model/computed_notebooks/quick-run`
or you can view `${CUPID_ROOT}/examples/coupled-model/computed_notebooks/quick-run/_build/html/index.html` in a web browser.

Furthermore, to clear the `computed_notebooks` folder which was generated by the `cupid-run` and `cupid-build` commands, you can run the following command:

``` bash
$ cupid-clear config.yml
```

This will clear the `computed_notebooks` folder which is at the location pointed to by the `run_dir` variable in the `config.yml` file.

### CUPiD Options

Most of CUPiD's configuration is done via the `config.yml` file, but there are a few command line options as well:
Expand Down Expand Up @@ -92,3 +100,4 @@ client

### Timeseries File Generation
CUPiD also has the capability to generate single variable timeseries files from history files for all components. To run timeseries, edit the `config.yml` file's timeseries section to fit your preferences, and then run `cupid-run config.yml -ts`.

36 changes: 36 additions & 0 deletions cupid/clear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python
import os
import click
import cupid.util
import shutil

def readConfigFile(config_path):
#Given the file path to config.yml, this function reads the config file content and
#returns the val of the run_dir string with '/computed_notebooks' appended to it

#Obtain the contents of the config.yml file and extract the run_dir variable
control = cupid.util.get_control_dict(config_path)
run_dir = control['data_sources'].get('run_dir', None)

if run_dir:
#Append '/computed_notebooks' to the run_dir value if it is not empty
fullPath = os.path.join(run_dir, 'computed_notebooks')
return fullPath

else: #run_dir is empty/wasn't found in config file so return error
raise ValueError("'run_dir' was empty/not found in the config file.")

@click.command()
@click.argument('config_path')
#Entry point to this script
def clear(config_path):
"""Clears the contents of the 'computed_notebooks' folder at the location specified by the 'run_dir' variable in the 'config.yml' file.
Args: config_path - The path to the config.yml file.
"""

run_dir = readConfigFile(config_path)
#Delete the 'computed_notebooks' folder and all the contents inside of it
shutil.rmtree(run_dir)
print(f"All contents in {run_dir} have been cleared.")
11 changes: 8 additions & 3 deletions cupid/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pathlib
import subprocess
import json
import sys
import yaml
import jupyter_client
import papermill as pm
Expand Down Expand Up @@ -74,9 +75,13 @@ def execute_managed_notebook(cls, nb_man, kernel_name, **kwargs):


def get_control_dict(config_path):
with open(config_path, "r") as fid:
control = yaml.safe_load(fid)

try:
with open(config_path, "r") as fid:
control = yaml.safe_load(fid)
except FileNotFoundError:
print(f"ERROR: {config_path} not found")
sys.exit(1)

# theoretically ploomber should manage this kernel checking by itself, but this seems to add
# the default kernel to info where necessary. currently a bit messy with copy pasting in
# script stuff.
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ documentation = "https://nbscuid.readthedocs.io"
[project.scripts]
cupid-run = "cupid.run:run"
cupid-build = "cupid.build:build"
cupid-clear = "cupid.clear:clear"

0 comments on commit bf65cdd

Please sign in to comment.