-
Notifications
You must be signed in to change notification settings - Fork 1
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
Tarball all needed templates from different folder #214
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,9 @@ | |
import shlex | ||
from datetime import datetime | ||
import logging | ||
import shutil | ||
from pathlib import Path | ||
from tqdm import tqdm | ||
from utilix.x509 import _validate_x509_proxy | ||
from Pegasus.api import ( | ||
Arch, | ||
|
@@ -23,7 +25,7 @@ | |
) | ||
from alea.runner import Runner | ||
from alea.submitter import Submitter | ||
from alea.utils import load_yaml, dump_yaml | ||
from alea.utils import TEMPLATE_RECORDS, load_yaml, dump_yaml | ||
|
||
|
||
DEFAULT_IMAGE = "/cvmfs/singularity.opensciencegrid.org/xenonnt/base-environment:latest" | ||
|
@@ -42,12 +44,12 @@ class SubmitterHTCondor(Submitter): | |
def __init__(self, *args, **kwargs): | ||
# General start | ||
self.htcondor_configurations = kwargs.get("htcondor_configurations", {}) | ||
self.template_path = self.htcondor_configurations.pop("template_path", None) | ||
self.singularity_image = self.htcondor_configurations.pop( | ||
"singularity_image", DEFAULT_IMAGE | ||
) | ||
self.top_dir = TOP_DIR | ||
self.work_dir = WORK_DIR | ||
self.template_path = self.htcondor_configurations.pop("template_path", None) | ||
self.combine_n_outputs = self.htcondor_configurations.pop("combine_n_outputs", 100) | ||
|
||
# A flag to check if limit_threshold is added to the rc | ||
|
@@ -68,6 +70,7 @@ def __init__(self, *args, **kwargs): | |
self.dagman_maxjobs = self.htcondor_configurations.pop("dagman_maxjobs", 100_000) | ||
|
||
super().__init__(*args, **kwargs) | ||
TEMPLATE_RECORDS.lock() | ||
|
||
# Job input configurations | ||
self.config_file_path = os.path.abspath(self.config_file_path) | ||
|
@@ -80,6 +83,7 @@ def __init__(self, *args, **kwargs): | |
self.runs_dir = os.path.join(self.workflow_dir, "runs") | ||
self.outputs_dir = os.path.join(self.workflow_dir, "outputs") | ||
self.scratch_dir = os.path.join(self.workflow_dir, "scratch") | ||
self.templates_tarball_dir = os.path.join(self.generated_dir, "templates") | ||
|
||
@property | ||
def template_tarball(self): | ||
|
@@ -123,41 +127,29 @@ def requirements(self): | |
|
||
return _requirements | ||
|
||
def _validate_template_path(self): | ||
"""Validate the template path.""" | ||
if self.template_path is None: | ||
raise ValueError("Please provide a template path.") | ||
# This path must exists locally, and it will be used to stage the input files | ||
if not os.path.exists(self.template_path): | ||
raise ValueError(f"Path {self.template_path} does not exist.") | ||
|
||
# Printout the template path file structure | ||
logger.info("Template path file structure:") | ||
for dirpath, dirnames, filenames in os.walk(self.template_path): | ||
for filename in filenames: | ||
logger.info(f"File: {filename} in {dirpath}") | ||
if self._contains_subdirectories(self.template_path): | ||
logger.warning( | ||
"The template path contains subdirectories. All templates files will be tarred." | ||
) | ||
|
||
def _tar_h5_files(self, directory, template_tarball="templates.tar.gz"): | ||
"""Tar all .h5 templates in the directory and its subdirectories into a tarball.""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The docstring should be updated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you are right. |
||
# Create a tar.gz archive | ||
with tarfile.open(template_tarball, "w:gz") as tar: | ||
# Walk through the directory | ||
for dirpath, dirnames, filenames in os.walk(directory): | ||
for filename in filenames: | ||
if filename.endswith(".h5"): | ||
# Get the full path to the file | ||
filepath = os.path.join(dirpath, filename) | ||
# Add the file to the tar | ||
# Specify the arcname to store relative path within the tar | ||
tar.add(filepath, arcname=os.path.basename(filename)) | ||
tar.add(directory, arcname=os.path.basename(directory)) | ||
hammannr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def _make_template_tarball(self): | ||
"""Make tarball of the templates if not exists.""" | ||
self._tar_h5_files(self.template_path, self.template_tarball) | ||
if not TEMPLATE_RECORDS.uniqueness: | ||
raise RuntimeError("All files in the template path must have unique basenames.") | ||
os.makedirs(self.templates_tarball_dir, exist_ok=True) | ||
if os.listdir(self.templates_tarball_dir): | ||
raise RuntimeError( | ||
f"Directory {self.templates_tarball_dir} is not empty. " | ||
"Please remove it before running the script." | ||
) | ||
|
||
logger.info(f"Copying templates into {self.templates_tarball_dir}") | ||
for record in tqdm(TEMPLATE_RECORDS): | ||
# Copy each file to the destination folder | ||
shutil.copy(record, self.templates_tarball_dir) | ||
self._tar_h5_files(self.templates_tarball_dir, self.template_tarball) | ||
logger.info(f"Tarbal made at {self.template_tarball}") | ||
|
||
def _modify_yaml(self): | ||
"""Modify the statistical model config file to correct the 'template_filename' fields. | ||
|
@@ -674,19 +666,6 @@ def _plan_and_submit(self): | |
**self.pegasus_config, | ||
) | ||
|
||
def _check_filename_unique(self): | ||
"""Check if all the files in the template path are unique. | ||
|
||
We assume two levels of the template folder. | ||
|
||
""" | ||
all_files = [] | ||
for _, _, filenames in os.walk(self.template_path): | ||
for filename in filenames: | ||
all_files.append(filename) | ||
if len(all_files) != len(set(all_files)): | ||
raise RuntimeError("All files in the template path must have unique names.") | ||
|
||
def submit(self, **kwargs): | ||
"""Serve as the main function to submit the workflow.""" | ||
if os.path.exists(self.workflow_dir): | ||
|
@@ -704,8 +683,6 @@ def submit(self, **kwargs): | |
self._modify_yaml() | ||
|
||
# Handling templates as part of the inputs | ||
self._validate_template_path() | ||
self._check_filename_unique() | ||
self._make_template_tarball() | ||
|
||
self._generate_workflow() | ||
|
@@ -715,6 +692,6 @@ def submit(self, **kwargs): | |
self.wf.graph( | ||
output=os.path.join(self.generated_dir, "workflow_graph.dot"), label="xform-id" | ||
) | ||
self.wf.graph( | ||
output=os.path.join(self.generated_dir, "workflow_graph.svg"), label="xform-id" | ||
) | ||
# self.wf.graph( | ||
# output=os.path.join(self.generated_dir, "workflow_graph.svg"), label="xform-id" | ||
# ) | ||
hammannr marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_contains_subdirectories
is no longer used after this PR sop we might as well delete it or am I missing something?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you are right.