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

Rewrite modules create repo type check. #1391

Merged
merged 9 commits into from
Mar 2, 2022
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
* Removed mention of `--singularity_pull_docker_container` in pipeline `README.md`
* Replaced equals with ~ in nf-core headers, to stop false positive unresolved conflict errors when committing with VSCode.
* Add retry strategy for AWS megatests after releasing [nf-core/tower-action v2.2](https://github.com/nf-core/tower-action/releases/tag/v2.2)
* Added `.nf-core.yml` file with `repository_type: pipeline` for modules commands

## General

* Updated `nf-core download` to work with latest DSL2 syntax for containers ([#1379](https://github.com/nf-core/tools/issues/1379))
* Made `nf-core modules create` detect repository type with explicit `.nf-core.yml` or `--repo-type`, instead of random readme stuff ([#1391](https://github.com/nf-core/tools/pull/1391))

### Modules

Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ github.com:
git_protocol: <ssh or https are valid choices>
```

The easiest way to create this configuration file is through *GitHub CLI*: follow
The easiest way to create this configuration file is through _GitHub CLI_: follow
its [installation instructions](https://cli.github.com/manual/installation)
and then call:

Expand Down Expand Up @@ -1116,11 +1116,17 @@ This command creates a new nf-core module from the nf-core module template.
This ensures that your module follows the nf-core guidelines.
The template contains extensive `TODO` messages to walk you through the changes you need to make to the template.

You can create a new module using `nf-core modules create`. This will create the new module in the current working directory. To specify another directory, use `--dir <directory>`.
You can create a new module using `nf-core modules create`.

If writing a module for the shared [nf-core/modules](https://github.com/nf-core/modules) repository, the `<directory>` argument should be the path to the clone of your fork of the modules repository.
This command can be used both when writing a module for the shared [nf-core/modules](https://github.com/nf-core/modules) repository,
and also when creating local modules for a pipeline.

Alternatively, if writing a more niche module that does not make sense to share, `<directory>` should be the path to your pipeline.
Which type of repository you are working in is detected by the `repository_type` flag in a `.nf-core.yml` file in the root directory,
set to either `pipeline` or `modules`.
The command will automatically look through parent directories for this file to set the root path, so that you can run the command in a subdirectory.
It will start in the current working directory, or whatever is specified with `--dir <directory>`.

If you do not have a `.nf-core.yml` file you can specify the repository type with the `--repo-type` flag.

The `nf-core modules create` command will prompt you with the relevant questions in order to create all of the necessary module files.

Expand Down
5 changes: 3 additions & 2 deletions nf_core/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,8 @@ def remove(ctx, dir, tool):
@click.option("-n", "--no-meta", is_flag=True, default=False, help="Don't use meta map for sample information")
@click.option("-f", "--force", is_flag=True, default=False, help="Overwrite any files if they already exist")
@click.option("-c", "--conda-name", type=str, default=None, help="Name of the conda package to use")
def create_module(ctx, tool, dir, author, label, meta, no_meta, force, conda_name):
@click.option("-r", "--repo-type", type=click.Choice(["pipeline", "modules"]), default=None, help="Type of repository")
def create_module(ctx, tool, dir, author, label, meta, no_meta, force, conda_name, repo_type):
"""
Create a new DSL2 module from the nf-core template.

Expand All @@ -519,7 +520,7 @@ def create_module(ctx, tool, dir, author, label, meta, no_meta, force, conda_nam

# Run function
try:
module_create = nf_core.modules.ModuleCreate(dir, tool, author, label, has_meta, force, conda_name)
module_create = nf_core.modules.ModuleCreate(dir, tool, author, label, has_meta, force, conda_name, repo_type)
module_create.create()
except UserWarning as e:
log.critical(e)
Expand Down
2 changes: 1 addition & 1 deletion nf_core/modules/bump_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def bump_versions(self, module=None, all_modules=False, show_uptodate=False):
# Get list of all modules
_, nfcore_modules = nf_core.modules.module_utils.get_installed_modules(self.dir)

# Load the .nf-core-tools.config
# Load the .nf-core.yml config
self.tools_config = nf_core.utils.load_tools_config(self.dir)

# Prompt for module or all
Expand Down
66 changes: 48 additions & 18 deletions nf_core/modules/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@

class ModuleCreate(object):
def __init__(
self, directory=".", tool="", author=None, process_label=None, has_meta=None, force=False, conda_name=None
self,
directory=".",
tool="",
author=None,
process_label=None,
has_meta=None,
force=False,
conda_name=None,
repo_type=None,
):
self.directory = directory
self.tool = tool
Expand All @@ -36,7 +44,7 @@ def __init__(
self.subtool = None
self.tool_conda_name = conda_name
self.tool_licence = None
self.repo_type = None
self.repo_type = repo_type
self.tool_licence = ""
self.tool_description = ""
self.tool_doc_url = ""
Expand Down Expand Up @@ -75,9 +83,12 @@ def create(self):

# Check whether the given directory is a nf-core pipeline or a clone of nf-core/modules
try:
self.repo_type = self.get_repo_type(self.directory)
self.get_repo_type()
except LookupError as e:
raise UserWarning(e)
log.info(f"Repository type: [blue]{self.repo_type}")
if self.directory != ".":
log.info(f"Base directory: '{self.directory}'")

log.info(
"[yellow]Press enter to use default values [cyan bold](shown in brackets)[/] [yellow]or type your own responses. "
Expand Down Expand Up @@ -272,27 +283,46 @@ def render_template(self):
template_stat = os.stat(os.path.join(os.path.dirname(nf_core.__file__), "module-template", template_fn))
os.chmod(dest_fn, template_stat.st_mode)

def get_repo_type(self, directory):
def get_repo_type(self):
"""
Determine whether this is a pipeline repository or a clone of
nf-core/modules
"""
# Verify that the pipeline dir exists
if dir is None or not os.path.exists(directory):
raise UserWarning(f"Could not find directory: {directory}")

readme = os.path.join(directory, "README.md")
# Determine repository type
if os.path.exists(readme):
with open(readme) as fh:
if fh.readline().rstrip().startswith("# ![nf-core/modules]"):
return "modules"
else:
return "pipeline"
else:
if dir is None or not os.path.exists(self.directory):
raise UserWarning(f"Could not find directory: {self.directory}")

# Try to find the root directory
base_dir = os.path.abspath(self.directory)
config_path_yml = os.path.join(base_dir, ".nf-core.yml")
config_path_yaml = os.path.join(base_dir, ".nf-core.yaml")
grst marked this conversation as resolved.
Show resolved Hide resolved
while (
not os.path.exists(config_path_yml)
and not os.path.exists(config_path_yaml)
and base_dir != os.path.dirname(base_dir)
):
base_dir = os.path.dirname(base_dir)
config_path_yml = os.path.join(base_dir, ".nf-core.yml")
config_path_yaml = os.path.join(base_dir, ".nf-core.yaml")
# Reset self.directory if we found the config file (will be an absolute path)
if os.path.exists(config_path_yml) or os.path.exists(config_path_yaml):
self.directory = base_dir

# Figure out the repository type from the .nf-core.yml config file if we can
tools_config = nf_core.utils.load_tools_config(self.directory)
grst marked this conversation as resolved.
Show resolved Hide resolved
if tools_config.get("repository_type") in ["pipeline", "modules"]:
if self.repo_type is not None and self.repo_type != tools_config["repository_type"]:
raise UserWarning(
f"'--repo-type {self.repo_type}' conflicts with [i][red]repository_type[/]: [blue]pipeline[/][/] in '{os.path.relpath(config_path_yml)}'"
)
self.repo_type = tools_config["repository_type"]
grst marked this conversation as resolved.
Show resolved Hide resolved
return

# Could be set on the command line - throw an error if not
if not self.repo_type:
raise UserWarning(
f"This directory does not look like a clone of nf-core/modules or an nf-core pipeline: '{directory}'"
" Please point to a valid directory."
f"Can't find a '.nf-core.yml' file with 'repository_type' set to 'pipeline' or 'modules': '{self.directory}'"
"\nPlease use the '--repo-type' flag or create '.nf-core.yml'"
)

def get_module_dirs(self):
Expand Down
1 change: 1 addition & 0 deletions nf_core/pipeline-template/.nf-core.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
repository_type: pipeline