写入文件和 运行 文件有时有效,主要是第一个

Writing to file and running the files sometimes works, mostly only first one

此代码大部分时间都会产生 WindowsError,很少(通常是第一次 运行)不会。

""" Running hexlified codes from codefiles module prepared previously """

import tempfile
import subprocess
import threading
import os

import codefiles

if __name__ == '__main__':
    for ind, c in enumerate(codefiles.exes):
        fn = tempfile.mktemp() + '.exe'
        # for some reason hexlified code is sometimes odd length and one nibble
        if len(c) & 1:
            c += '0'
        c = c.decode('hex')
        with open(fn, 'wb') as f:
            f.write(c)

        threading.Thread(target=lambda:subprocess.Popen("", executable=fn)).start()

""" One time works, one time WindowsError 32
>>> 
132096 c:\docume~1\admin\locals~1\temp\tmpkhhxxo.exe
991232 c:\docume~1\admin\locals~1\temp\tmp9ow6zz.exe
>>> ================================ RESTART ================================
>>> 
132096 c:\docume~1\admin\locals~1\temp\tmp3hb0cf.exe
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
    self.run()
  File "C:\Python27\lib\threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Documents and Settings\Admin\My Documents\Google Drive\Python\Tools\runner.pyw", line 18, in <lambda>
    threading.Thread(target=lambda:subprocess.Popen("", executable=fn)).start()
  File "C:\Python27\lib\subprocess.py", line 710, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 958, in _execute_child
    startupinfo)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process
991232 c:\docume~1\admin\locals~1\temp\tmpnkfuon.exe

>>> 
"""

Hexlification 是用这个脚本完成的,它有时似乎会产生奇数个半字节,这看起来也很奇怪。

# bootstrapper for running hexlified executables,
# hexlification can be done by running this file directly or calling function make
# running can be done by run function in runner module, which imports the code,
# runs them from temporary files

import os

modulename = 'codefiles.py'

def code_file_names(d='.', ext='.exe'):
    return [n for n in os.listdir(d)
                            if n.lower().endswith(ext)]

def make():
    codes = code_file_names(os.curdir)
    with open(modulename, 'a') as f:
        f.write('exes = (')
        hex_codes = [open(n, 'rb').read().encode('hex') for n in codes]
        assert all(len(c) & 1 == 0 for c in hex_codes)
        print len(hex_codes),map(len, hex_codes)
        hex_codes = [repr(c) for c in hex_codes]
        f.write(',\n'.join(hex_codes))
        f.write(')\n')


if __name__ == '__main__':
    import make_exe

    # prepare hexlified exes for exes  in directory if codefiles not prepared
    if modulename not in os.listdir('.'):
        try:
            os.remove(modulename)
        except:
            pass   
        make()

    # prepare script for py2_exe to execute the scripts by run from runner
    make_exe.py2_exe('runner.pyw', 'tools/dvd.ico')        

经过Martelli先生的建议,错误消失了,但仍然不是预期的结果。

我在新版本的代码中传递了 exe 文件的创建(如果存在)(在新创建例程中保存的文件名)。它在创建文件时启动两个代码(两个十六进制代码),但随后启动第一个代码的多个副本。

tempfile.mktemp(),除了因为它的安全问题多年来被弃用外,还保证仅在程序的单个 运行 中使用唯一的名称,因为根据 https://docs.python.org/2/library/tempfile.html#tempfile.mktemp ,"The module uses a global variable that tell it how to construct a temporary name"。所以在第二个 运行 上,正如非常清楚的错误消息告诉你的那样,"The process cannot access the file because it is being used by another process"(具体来说,这个过程由前一个 运行 开始,即你不能重写一个 .exe 当前 运行ning 在某些过程中)。

解决方法是确保每个 运行 都使用自己唯一的临时文件目录。请参阅 https://docs.python.org/2/library/tempfile.html#tempfile.mkdtemp 中的 mkdtemp。如何最终清理这些临时目录是一个不同的问题,因为只要任何进程在这样的目录中 运行ning 一个 .exe 文件就无法完成——你可能需要一个单独的 "clean-up script" 可以达到目的,运行 定期(例如在 Unix 中我会使用 cron)和一个存储库(例如一个小的 sqlite 数据库,或者甚至只是一个文件)来记录之前运行创建的临时目录中有哪些仍然存在需要清理(捕获还不能清理时看到的异常,以便在重试未来)。