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

Save template info #2389

Merged
merged 5 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
7 changes: 6 additions & 1 deletion nf_core/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,16 @@ def create_param_dict(self, name, description, author, version, template_yaml_pa
Args:
template_yaml_path (str): Path to YAML file containing template parameters.
"""
# Try reading config file
_, config_yml = nf_core.utils.load_tools_config()

# 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 +175,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()

# 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 {template_yaml_path} file. Then run the sync command again."
mirpedrol marked this conversation as resolved.
Show resolved Hide resolved
)

# 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