Skip to content

Commit

Permalink
@ewels implementation of nf-core#936
Browse files Browse the repository at this point in the history
  • Loading branch information
ewels committed Mar 22, 2021
1 parent c51f8f0 commit b3b99ea
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 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,35 @@ 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}'")
# Raise UnicodeDecodeError as that's the worst case scenario anyway
# Means we only need to write the copy code once
try:
# Just copy certain file extensions
for e in binary_extensions:
if os.path.basename(template_fn_path).split(".")[-1] in binary_extensions:
raise UnicodeDecodeError

# 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 UnicodeDecodeError

# 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 UnicodeDecodeError as e:
log.debug(f"Copying file without Jinja: '{output_path}'")
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 b3b99ea

Please sign in to comment.