diff --git a/mitogen/parent.py b/mitogen/parent.py index f301a42c5..637389d5d 100644 --- a/mitogen/parent.py +++ b/mitogen/parent.py @@ -1385,10 +1385,6 @@ def __repr__(self): # with a custom argv. # * Optimized for minimum byte count after minification & compression. # The script preamble_size.py measures this. - # * 'CONTEXT_NAME' and 'PREAMBLE_COMPRESSED_LEN' are substituted with - # their respective values. - # * CONTEXT_NAME must be prefixed with the name of the Python binary in - # order to allow virtualenvs to detect their install prefix. # # macOS tweaks for Python 2.7 must be kept in sync with the the Ansible # module test_echo_module, used by the integration tests. @@ -1424,20 +1420,20 @@ def _first_stage(): os.close(r) os.close(W) os.close(w) - if os.uname()[0]=='Darwin'and os.uname()[2][:2]<'19'and sys.executable=='/usr/bin/python':sys.executable='/usr/bin/python2.7' - if os.uname()[0]=='Darwin'and os.uname()[2][:2]in'2021'and sys.version[:3]=='2.7':os.environ['PYTHON_LAUNCHED_FROM_WRAPPER']='1' + if os.uname()[0]+sys.executable=='Darwin/usr/bin/python'and os.uname()[2][:2]<'19':sys.executable+='2.7' + if os.uname()[0]+sys.version[:3]=='Darwin2.7'and os.uname()[2][:2]in'2021':os.environ['PYTHON_LAUNCHED_FROM_WRAPPER']='1' os.environ['ARGV0']=sys.executable - os.execl(sys.executable,sys.executable+'(mitogen:CONTEXT_NAME)') + os.execl(sys.executable,sys.executable+'(mitogen:%s)'%sys.argv[2]) os.write(1,'MITO000\n'.encode()) - fp=os.fdopen(0,'rb') - C=zlib.decompress(fp.read(PREAMBLE_COMPRESSED_LEN)) - fp.close() - fp=os.fdopen(W,'wb',0) - fp.write(C) - fp.close() - fp=os.fdopen(w,'wb',0) - fp.write(C) - fp.close() + f=os.fdopen(0,'rb') + C=zlib.decompress(f.read(int(sys.argv[3]))) + f.close() + f=os.fdopen(W,'wb',0) + f.write(C) + f.close() + f=os.fdopen(w,'wb',0) + f.write(C) + f.close() os.write(1,'MITO001\n'.encode()) os.close(2) @@ -1458,11 +1454,8 @@ def get_boot_command(self): source = inspect.getsource(self._first_stage) source = textwrap.dedent('\n'.join(source.strip().split('\n')[2:])) source = source.replace(' ', ' ') - source = source.replace('CONTEXT_NAME', self.options.remote_name) - preamble_compressed = self.get_preamble() - source = source.replace('PREAMBLE_COMPRESSED_LEN', - str(len(preamble_compressed))) - compressed = zlib.compress(source.encode(), 9) + compressor = zlib.compressobj(9, zlib.DEFLATED, -15, 9) + compressed = compressor.compress(source.encode()) + compressor.flush() encoded = binascii.b2a_base64(compressed).replace(b('\n'), b('')) # Just enough to decode, decompress, and exec the first stage. @@ -1472,7 +1465,10 @@ def get_boot_command(self): return self.get_python_argv() + [ '-c', 'import sys;sys.path=[p for p in sys.path if p];import binascii,os,zlib;' - 'exec(zlib.decompress(binascii.a2b_base64("%s")))' % (encoded.decode(),), + 'exec(zlib.decompress(binascii.a2b_base64(sys.argv[1]),-15))', + encoded.decode(), + self.options.remote_name, + str(len(self.get_preamble())), ] def get_econtext_config(self):