Skip to content

Instantly share code, notes, and snippets.

@vsajip
Created January 26, 2022 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vsajip/a72723d2909acf89d89c95ef9cb89b61 to your computer and use it in GitHub Desktop.
Save vsajip/a72723d2909acf89d89c95ef9cb89b61 to your computer and use it in GitHub Desktop.
Test files for diagnosing pynsist issue #243
#import distlib.scripts
import io
import os.path as osp
import sys
# from distlib import __version__ as distlibversion
from pathlib import Path
from zipfile import ZipFile
SCRIPT = """import sys
if __name__ == '__main__':
print(sys.version)
print(sys.argv)
print(sys.executable)
sys.exit(0)
"""
WSCRIPT = r'''
import ctypes
import sys
MB_OK = 0
s = '\n'.join([sys.version, str(sys.argv), sys.executable])
ctypes.windll.user32.MessageBoxW(0, s, 'System Info', MB_OK)
'''
def find_exe(bitness=32, console=True):
# distlib_dir = osp.dirname(distlib.scripts.__file__)
distlib_dir = '.'
name = "td" if console else "wd"
return osp.join(distlib_dir, f"{name}{bitness}.exe")
def build(name, commands):
for bitness, console in commands:
suffix = "" if console else "w"
nsuffix = 't' if console else 'w'
# exe_path = Path(".") / f"{name}-distlib{distlibversion}-{suffix}{bitness}.exe"
exe_path = f"{name}-{nsuffix}{bitness}.exe"
# 1. Get the base launcher exe from distlib
with open(find_exe(bitness, console), "rb") as f:
launcher_b = f.read()
# 2. Shebang
shebang = f"#!<launcher_dir>\\python{bitness}\\python{suffix}.exe\r\n".encode("utf-8")
# 3. Script contents
zip_bio = io.BytesIO()
with ZipFile(zip_bio, "w") as zf:
script = SCRIPT if console else WSCRIPT
zf.writestr("__main__.py", script.encode("utf-8"))
# Put the pieces together
with open(exe_path, "wb") as f:
f.write(launcher_b)
f.write(shebang)
f.write(zip_bio.getvalue())
if __name__ == "__main__":
build("test", (
(32, True),
(32, False),
(64, True),
(64, False),
))
@echo off
xcopy /y /d C:\Users\Vinay\Projects\simple_launcher\Debug\CLISimpleLauncher.exe td32.exe
xcopy /y /d C:\Users\Vinay\Projects\simple_launcher\Debug\GUISimpleLauncher.exe wd32.exe
xcopy /y /d C:\Users\Vinay\Projects\simple_launcher\x64\Debug\CLISimpleLauncher.exe td64.exe
xcopy /y /d C:\Users\Vinay\Projects\simple_launcher\x64\Debug\GUISimpleLauncher.exe wd64.exe
python3 build.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2022 Red Dove Consultants Limited. This script is MIT Licensed.
#
import argparse
import logging
import os
import subprocess
import sys
DEBUGGING = 'PY_DEBUG' in os.environ
logger = logging.getLogger(__name__)
def main():
fn = os.path.basename(__file__)
fn = os.path.splitext(fn)[0]
lfn = os.path.expanduser('~/logs/%s.log' % fn)
if os.path.isdir(os.path.dirname(lfn)):
logging.basicConfig(level=logging.DEBUG, filename=lfn, filemode='w',
format='%(message)s')
adhf = argparse.ArgumentDefaultsHelpFormatter
ap = argparse.ArgumentParser(formatter_class=adhf, prog=fn)
aa = ap.add_argument
aa('program', metavar='EXECUTABLE', help='Executable to run')
aa('args', metavar='ARG', nargs='*', help='Arguments')
options = ap.parse_args()
cmd = [options.program] + options.args
p = subprocess.Popen(cmd)
p.wait()
print('Program returned %s' % p.returncode)
if __name__ == '__main__':
try:
rc = main()
except KeyboardInterrupt:
rc = 2
except Exception as e:
if DEBUGGING:
s = ' %s:' % type(e).__name__
else:
s = ''
sys.stderr.write('Failed:%s %s\n' % (s, e))
if DEBUGGING: import traceback; traceback.print_exc()
rc = 1
sys.exit(rc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment