From d6abab533d989495bded9d85cf8ea9797488840a Mon Sep 17 00:00:00 2001 From: Jonas Bushart Date: Thu, 13 Feb 2020 23:05:50 +0100 Subject: [PATCH] Fix shebang handling for languages with non-# comments The shebang always starts with the sequence "#!" independend of the comment symbol of the programming language. Add a test with Rust, which uses "//" as the comment symbol. --- jupytext/header.py | 4 ++-- tests/test_read_simple_rust.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/jupytext/header.py b/jupytext/header.py index c23ff7259..99e89bb73 100644 --- a/jupytext/header.py +++ b/jupytext/header.py @@ -47,7 +47,7 @@ def encoding_and_executable(notebook, metadata, ext): jupytext_metadata = metadata.get('jupytext', {}) if comment is not None and 'executable' in jupytext_metadata: - lines.append(comment + '!' + jupytext_metadata.pop('executable')) + lines.append('#!' + jupytext_metadata.pop('executable')) if 'encoding' in jupytext_metadata: lines.append(jupytext_metadata.pop('encoding')) @@ -144,7 +144,7 @@ def header_to_metadata_and_cell(lines, header_prefix, ext=None): encoding_re = re.compile(r'^[ \t\f]*{}.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)'.format(comment)) for i, line in enumerate(lines): - if i == 0 and line.startswith(comment + '!'): + if i == 0 and line.startswith('#!'): metadata.setdefault('jupytext', {})['executable'] = line[2:] start = i + 1 continue diff --git a/tests/test_read_simple_rust.py b/tests/test_read_simple_rust.py index b8c1b1f68..72dad3730 100644 --- a/tests/test_read_simple_rust.py +++ b/tests/test_read_simple_rust.py @@ -43,3 +43,26 @@ def test_read_simple_file(text='''println!("Hello world"); ::std::mem::drop''') ])) compare(jupytext.writes(nb, 'rs'), text) + + +def test_read_write_script_with_metadata_241(no_jupytext_version_number, rsnb="""#!/usr/bin/env scriptisto +// --- +// jupyter: +// jupytext: +// text_representation: +// extension: .rs +// format_name: light +// kernelspec: +// display_name: Rust +// language: rust +// name: rust +// --- + +let mut a: i32 = 2; +a += 1; +"""): + nb = jupytext.reads(rsnb, 'rs') + assert 'executable' in nb.metadata['jupytext'] + rsnb2 = jupytext.writes(nb, 'rs') + + compare(rsnb, rsnb2)