Copy an application archive, modifying the shebang line.
(archive, new_archive, interpreter=None)
| 51 | |
| 52 | |
| 53 | def _copy_archive(archive, new_archive, interpreter=None): |
| 54 | """Copy an application archive, modifying the shebang line.""" |
| 55 | with _maybe_open(archive, 'rb') as src: |
| 56 | # Skip the shebang line from the source. |
| 57 | # Read 2 bytes of the source and check if they are #!. |
| 58 | first_2 = src.read(2) |
| 59 | if first_2 == b'#!': |
| 60 | # Discard the initial 2 bytes and the rest of the shebang line. |
| 61 | first_2 = b'' |
| 62 | src.readline() |
| 63 | |
| 64 | with _maybe_open(new_archive, 'wb') as dst: |
| 65 | _write_file_prefix(dst, interpreter) |
| 66 | # If there was no shebang, "first_2" contains the first 2 bytes |
| 67 | # of the source file, so write them before copying the rest |
| 68 | # of the file. |
| 69 | dst.write(first_2) |
| 70 | shutil.copyfileobj(src, dst) |
| 71 | |
| 72 | if interpreter and isinstance(new_archive, str): |
| 73 | os.chmod(new_archive, os.stat(new_archive).st_mode | stat.S_IEXEC) |
| 74 | |
| 75 | |
| 76 | def create_archive(source, target=None, interpreter=None, main=None, |
no test coverage detected
searching dependent graphs…