Skip to content

Commit

Permalink
Merge pull request #936 from KevinMenden/fix-create
Browse files Browse the repository at this point in the history
Fixing bugs in nf-core create
  • Loading branch information
ewels authored Mar 23, 2021
2 parents 3dd6427 + 3261493 commit e0f0d1b
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions nf_core/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def render_template(self):
loader=jinja2.PackageLoader("nf_core", "pipeline-template"), keep_trailing_newline=True
)
template_dir = os.path.join(os.path.dirname(__file__), "pipeline-template")
binary_ftypes = ["image", "application/java-archive"]
binary_ftypes = ["image", "application/java-archive", "application/x-java-archive"]
binary_extensions = [".jpeg", ".jpg", ".png", ".zip", ".gz", ".jar", ".tar"]
object_attrs = vars(self)
object_attrs["nf_core_version"] = nf_core.__version__

Expand All @@ -106,23 +107,33 @@ def render_template(self):
output_path = os.path.join(self.outdir, template_fn)
os.makedirs(os.path.dirname(output_path), exist_ok=True)

# Just copy binary files
(ftype, encoding) = mimetypes.guess_type(template_fn_path)
if encoding is not None or (ftype is not None and any([ftype.startswith(ft) for ft in binary_ftypes])):
log.debug(f"Copying binary file: '{output_path}'")
try:
# Just copy certain file extensions
filename, file_extension = os.path.splitext(template_fn_path)
if file_extension in binary_extensions:
raise AttributeError(f"File extension: {file_extension}")

# Try to detect binary files
(ftype, encoding) = mimetypes.guess_type(template_fn_path, strict=False)
if encoding is not None or (ftype is not None and any([ftype.startswith(ft) for ft in binary_ftypes])):
raise AttributeError(f"Encoding: {encoding}")

# Got this far - render the template
log.debug(f"Rendering template file: '{template_fn}'")
j_template = env.get_template(template_fn)
rendered_output = j_template.render(object_attrs)

# Write to the pipeline output file
with open(output_path, "w") as fh:
log.debug(f"Writing to output file: '{output_path}'")
fh.write(rendered_output)

# Copy the file directly instead of using Jinja
except (AttributeError, UnicodeDecodeError) as e:
log.debug(f"Copying file without Jinja: '{output_path}' - {e}")
shutil.copy(template_fn_path, output_path)
continue

# Render the template
log.debug(f"Rendering template file: '{template_fn}'")
j_template = env.get_template(template_fn)
rendered_output = j_template.render(object_attrs)

# Write to the pipeline output file
with open(output_path, "w") as fh:
log.debug(f"Writing to output file: '{output_path}'")
fh.write(rendered_output)

# Make a logo and save it
self.make_pipeline_logo()

Expand Down

0 comments on commit e0f0d1b

Please sign in to comment.