使用PyInstaller onefile选项和清单将UAC设置为requireAdministrator

sid*_*rgt 5 python windows pyinstaller visual-studio

好吧,我一直在四处寻找这个问题.我正在GraphicScriptWizard.exe使用-i -F -w和-m选项构建一个使用PyInstaller 2.0版调用的应用程序.

我已经定义了与-m选项一起使用的清单文件,它GraphicScriptWizard.exe.manifest具有以下内容:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> 
  <assemblyIdentity version="1.0.0.0"
     processorArchitecture="x86"
     name="GraphicScriptWizard"
     type="win32"/> 

  <!-- Identify the application security requirements. -->
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel
          level="requireAdministrator"
          uiAccess="false"/>
        </requestedPrivileges>
       </security>
  </trustInfo>
</assembly>
Run Code Online (Sandbox Code Playgroud)

使用此清单和命令行选项,我没有得到提示提升的可执行文件.

为了完整起见,Pyinstaller生成的spec文件是:

# -*- mode: python -*-
a = Analysis(['GraphicScriptWizard.py'],
             pathex=[<Full Development Path>],
             hiddenimports=[],
             hookspath=None)
pyz = PYZ(a.pure)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name=os.path.join('dist', 'GraphicScriptWizard.exe'),
          debug=False,
          strip=None,
          upx=True,
          console=False , icon='SP.ico', manifest='GraphicScriptWizard.exe.manifest')
app = BUNDLE(exe,
             name=os.path.join('dist', 'GraphicScriptWizard.exe.app'))
Run Code Online (Sandbox Code Playgroud)

我尝试使用没有-m选项的pyinstaller进行编译,并使用命令嵌入mt:

mt.exe -manifest GraphicScriptWizard.exe.manifest -outputresource:GraphicScriptWizard.exe;#1
Run Code Online (Sandbox Code Playgroud)

当我这样做时,应用程序提示我提升,但程序运行时出错:

"Cannot open self <Full path to exe>\GraphicScriptWizard.exe or archive..."

我真的在我的智慧结束,我希望有更多熟悉Windows资源和清单的人可以为我提供更多的信息.我的清单XML错了吗?我的Pyinstaller方法是错误的吗?

小智 9

我自己就走了这条路,这是我的观察和经验.

首先要知道的是清单有两个有效位置:

  1. 嵌入在可执行文件(或dll)中作为资源

  2. 正如您当前所做的那样,在可执行文件(或dll)旁边.

阅读有关清单的信息:http://msdn.microsoft.com/en-us/library/windows/desktop/aa374191(v = vs.85).aspx

根据我的经验,您可以同时使用两者,但如果有任何重叠,嵌入式清单优先.这就是搞砸你的原因.Pyinstaller创建的可执行文件具有嵌入式清单,该清单将请求的执行级别设置为"asInvoker",该级别将覆盖您正在使用的清单中的级别.

--manifest(或EXE()的清单参数)仅修改Pyinstaller放置在可执行文件/ dll旁边的清单,而不是嵌入式清单.

因此,我们可以转向mt.exe来更改嵌入式清单,但正如您所发现的,这会导致应用程序无法运行.这是因为Pyinstaller创建的应用程序实际上是两部分; 一个小的可执行文件,它提取一个存档,然后在其捆绑的python环境中设置和启动你的代码,以及可执行文件所在的存档.这是有效的,因为可执行文件的规范允许将任意数据附加到可执行文件的末尾,但是在可执行文件本身之外,由可执行文件头中记录的大小定义.当我们在Pyinstaller创建的可执行文件上运行mt.exe时,它查看标题以获取大小,并忽略除此之外的任何内容,从而在使用新清单保存可执行文件时丢弃该存档数据,这会导致您出现错误看到.

我使用的解决方案是在将归档数据附加到可执行文件之前修改清单,这需要修改Pyinstaller源代码.Pyinstaller源具有更新可执行文件/ dll中的资源的实用程序,它将其用作构建过程的一部分.你会想看看build.py,winmanifest.py也许winresource.py在Pyinstaller位置.我在EXE类中添加了一个参数,然后在该类的汇编方法中添加了一个更新清单的步骤,我在它附加存档之前就这样做了.我添加的内容就像这样:

if self.manifest_override != None:
        print "Overriding default manifest"
        tmpnm = tempfile.mktemp()
        shutil.copy2(exe, tmpnm)
        os.chmod(tmpnm, 0755)
        winmanifest.UpdateManifestResourcesFromXMLFile(tmpnm, self.manifest_override, names=[1], languages=[1033])
        exe = tmpnm
        trash.append(tmpnm)
Run Code Online (Sandbox Code Playgroud)

我把它放在读取的行之前:exe = checkCache(exe, ... 它应该在上面,但是接近aprint "Appending archive to EXE"...

这个解决方案一直在为我工作,但我并不是很满意.我宁愿覆盖嵌入的默认清单,而不是更新它,但到目前为止我的努力一直没有结果.

对不起文字墙,但这里有很多事情要做.