Skip to content

Commit

Permalink
Merge pull request #2389 from mirpedrol/save-template-info
Browse files Browse the repository at this point in the history
Save template info
  • Loading branch information
mirpedrol authored Aug 7, 2023
2 parents 65b4b5f + 13cee01 commit 80f562f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/create-test-lint-wf-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ jobs:
# Try syncing it before we change anything
- name: nf-core sync
run: nf-core --log-file log.txt sync --dir my-prefix-testpipeline/ --template-yaml ${{ matrix.TEMPLATE }}
run: nf-core --log-file log.txt sync --dir my-prefix-testpipeline/

# Run code style linting
- name: Run Prettier --check
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Remove default false from nextflow_schema.json ([#2376](https://github.com/nf-core/tools/pull/2376))
- Add module MULTIQC to modules.config ([#2377](https://github.com/nf-core/tools/pull/2377))
- Update the Code of Conduct ([#2381](https://github.com/nf-core/tools/pull/2381))
- Save template information to `.nf-core.yml` and deprecate argument `--template-yaml` for `nf-core sync` ([#2388](https://github.com/nf-core/tools/pull/2388) and [#2389](https://github.com/nf-core/tools/pull/2389))

### Download

Expand Down
17 changes: 14 additions & 3 deletions nf_core/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(
default_branch=None,
):
self.template_params, skip_paths_keys, self.template_yaml = self.create_param_dict(
name, description, author, version, template_yaml_path, plain
name, description, author, version, template_yaml_path, plain, outdir if outdir else "."
)

skippable_paths = {
Expand Down Expand Up @@ -90,16 +90,28 @@ def __init__(
outdir = os.path.join(os.getcwd(), self.template_params["name_noslash"])
self.outdir = Path(outdir)

def create_param_dict(self, name, description, author, version, template_yaml_path, plain):
def create_param_dict(self, name, description, author, version, template_yaml_path, plain, pipeline_dir):
"""Creates a dictionary of parameters for the new pipeline.
Args:
name (str): Name for the pipeline.
description (str): Description for the pipeline.
author (str): Authors name of the pipeline.
version (str): Version flag.
template_yaml_path (str): Path to YAML file containing template parameters.
plain (bool): If true the pipeline template will be initialized plain, without customisation.
pipeline_dir (str): Path to the pipeline directory.
"""
# Try reading config file
_, config_yml = nf_core.utils.load_tools_config(pipeline_dir)

# Obtain template customization info from template yaml file or `.nf-core.yml` config file
try:
if template_yaml_path is not None:
with open(template_yaml_path, "r") as f:
template_yaml = yaml.safe_load(f)
elif "template" in config_yml:
template_yaml = config_yml["template"]
else:
template_yaml = {}
except FileNotFoundError:
Expand Down Expand Up @@ -169,7 +181,6 @@ def create_param_dict(self, name, description, author, version, template_yaml_pa
param_dict["logo_dark"] = f"{param_dict['name_noslash']}_logo_dark.png"
param_dict["version"] = version

_, config_yml = nf_core.utils.load_tools_config()
if (
"lint" in config_yml
and "nextflow_config" in config_yml["lint"]
Expand Down
45 changes: 30 additions & 15 deletions nf_core/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import shutil

import git
import questionary
import requests
import requests_cache
import rich
import yaml
from git import GitCommandError, InvalidGitRepositoryError

import nf_core
Expand Down Expand Up @@ -42,6 +44,7 @@ class PipelineSync:
make_pr (bool): Set this to `True` to create a GitHub pull-request with the changes
gh_username (str): GitHub username
gh_repo (str): GitHub repository name
template_yaml_path (str): Path to template.yml file for pipeline creation settings. DEPRECATED
Attributes:
pipeline_dir (str): Path to target pipeline directory
Expand All @@ -52,7 +55,6 @@ class PipelineSync:
required_config_vars (list): List of nextflow variables required to make template pipeline
gh_username (str): GitHub username
gh_repo (str): GitHub repository name
template_yaml (str): Path to template.yml file for pipeline creation settings.
"""

def __init__(
Expand Down Expand Up @@ -80,11 +82,28 @@ def __init__(
self.gh_repo = gh_repo
self.pr_url = ""

self.template_yaml_path = template_yaml_path
# Save contents of template.yml for using outside of git.
if self.template_yaml_path is not None:
with open(self.template_yaml_path, "r") as template_yaml:
self.template_yaml_cache = template_yaml.read()
self.config_yml_path, self.config_yml = nf_core.utils.load_tools_config(self.pipeline_dir)

# Throw deprecation warning if template_yaml_path is set
if template_yaml_path is not None:
log.warning(
f"The `template_yaml_path` argument is deprecated. Saving pipeline creation settings in .nf-core.yml instead. Please remove {template_yaml_path} file."
)
if "template" in self.config_yml:
overwrite_template = questionary.confirm(
f"A template section already exists in '{self.config_yml_path}'. Do you want to overwrite?",
style=nf_core.utils.nfcore_question_style,
default=False,
).unsafe_ask()
if overwrite_template or "template" not in self.config_yml:
with open(template_yaml_path, "r") as f:
self.config_yml["template"] = yaml.safe_load(f)
with open(self.config_yml_path, "w") as fh:
yaml.safe_dump(self.config_yml, fh)
log.info(f"Saved pipeline creation settings to '{self.config_yml_path}'")
raise SystemExit(
f"Please commit your changes and delete the {template_yaml_path} file. Then run the sync command again."
)

# Set up the API auth if supplied on the command line
self.gh_api = nf_core.utils.gh_api
Expand Down Expand Up @@ -213,7 +232,7 @@ def delete_template_branch_files(self):
# Delete everything
log.info("Deleting all files in 'TEMPLATE' branch")
for the_file in os.listdir(self.pipeline_dir):
if the_file == ".git" or the_file == self.template_yaml_path:
if the_file == ".git":
continue
file_path = os.path.join(self.pipeline_dir, the_file)
log.debug(f"Deleting {file_path}")
Expand All @@ -234,10 +253,10 @@ def make_template_pipeline(self):
# Only show error messages from pipeline creation
logging.getLogger("nf_core.create").setLevel(logging.ERROR)

# Re-write the template yaml from cache which may have been updated
if self.template_yaml_path and self.template_yaml_cache:
with open(self.template_yaml_path, "w") as template_path:
template_path.write(self.template_yaml_cache)
# Re-write the template yaml info from .nf-core.yml config
if "template" in self.config_yml:
with open(self.config_yml_path, "w") as config_path:
yaml.safe_dump(self.config_yml, config_path)

try:
nf_core.create.PipelineCreate(
Expand All @@ -248,13 +267,9 @@ def make_template_pipeline(self):
force=True,
outdir=self.pipeline_dir,
author=self.wf_config["manifest.author"].strip('"').strip("'"),
template_yaml_path=self.template_yaml_path,
plain=True,
).init_pipeline()
except Exception as err:
if self.template_yaml_path:
# If sync fails, remove template_yaml_path before raising error.
os.remove(self.template_yaml_path)
# Reset to where you were to prevent git getting messed up.
self.repo.git.reset("--hard")
raise SyncException(f"Failed to rebuild pipeline from template with error:\n{err}")
Expand Down

0 comments on commit 80f562f

Please sign in to comment.