py2exe使用教程(四)——设置文件属性

来源:互联网 发布:方正证券交易软件 编辑:程序博客网 时间:2024/06/17 02:51

文中资料来源于Py2exe安装包里的sample,C:\Python27\Lib\site-packages\py2exe\samples

一、文件属性

下面是一个非常简单的setup脚本,用于创建2个可执行文件。

hello.py是一个简单的“hello, world”式的程序,test_wx.py 则是一个简单的wxpython程序,不显示后台命令窗口的程序。在命令提示符后键入'setup.py py2exe' 或在 'python setup.py py2exe' 来执行打包过程。

如果一切顺利,你将得到一个“dist”子目录,包含hello.exe 和test_wx.exe 以及一些其他文件。

from distutils.core import setupimport py2exesetup(    version = "0.5.0",    description = "py2exe sample script",    name = "py2exe samples",    # targets to build    windows = ["test_wx.py"],    console = ["hello.py"],    )
前三个参数不是必须的,如果给定了一个'version' 参数,那么一个版本信息将会被创建并且嵌入到可执行文件的属性中。

如果设置了这些参数的话,可以把鼠标悬浮在可执行文件上,查看文件的版本信息,也可以右键在属性->详细中查看文件的详细信息(版本,描述等)。

参数 windows 用来创建一个图形用户界面程序;

参数 console 用来创建一个控制台程序。


二、用户访问控制(User Access Control)

下面这个setup脚本通过在 manifest 文件中设置不同的用户访问控制标志,创建的几个访问级别不同的可执行文件。

对于Python 2.6+, 创建一个无需通过 UAC(User Account Control,用户帐户控制) 控制权限的可执行文件,“asInvoker”级别下也是无需特殊权限。

尽管如此,对于2.5和更早版本,将会强制这个应用程序以兼容模式打开。(就像在生成的可执行文件中根本不存在 manifest 一样)

from distutils.core import setupimport py2exe# The targets to buildt1 = dict(script="hello.py",          dest_base="not_specified")# targets with different values for requestedExecutionLevelt2 = dict(script="hello.py",          dest_base="as_invoker",          uac_info="asInvoker")t3 = dict(script="hello.py",          dest_base="highest_available",          uac_info="highestAvailable")t4 = dict(script="hello.py",          dest_base="require_admin",          uac_info="requireAdministrator")console = [t1, t2, t3, t4]windows = [t1.copy(), t2.copy(), t3.copy(), t4.copy()]for t in windows:    t['dest_base'] += "_w"setup(    version = "0.5.0",    description = "py2exe user-access-control sample script",    name = "py2exe samples",    # targets to build    windows = windows,    console = console,    )

这里创建了四种不同访问级别的可执行文件,每种级别有控制台程序和图形用户界面程序(以_w结尾)两种。

参数 dest_base 指定了生成的可执行文件的名称;

参数 uac_info 设定了运行可执行程序所需要的权限。


补充:

上面提到了requestedExecutionLevel,查了一下资料,如下:

Vista 和 Windows 7 操作系统为了加强安全,增加了 UAC(用户账户控制) 的机制,如果 UAC 被打开,用户即使是以管理员权限登录,其应用程序默认情况下也无法对系统目录,系统注册表等可能影响系统运行的设置进行写操作。

这个机制大大增强了系统的安全性,但对应用程序开发者来说,我们不能强迫用户去关闭UAC,但有时我们开发的应用程序又需要以 Administrator 的方式运行,即 Win7 中 以 as administrator 方式运行,那么我们怎么来实现这样的功能呢?
 
在 win7 下运行一些安装程序时,会发现首先弹出一个对话框,让用户确认是否同意允许这个程序改变你的计算机配置,但我们编写的应用程序默认是不会弹出这个提示的,也无法以管理员权限运行。

以上的示例中介绍了如何在py2exe下通过 setup 参数来设置来提示用户以管理员权限运行。

我们可以看到这个配置中有一个 requestedExecutionLevel 项,这个项用于配置当前应用请求的执行权限级别。这个项有3个值可供选择,如下表所示:

ValueDescriptionCommentasInvokerThe application runs with the same access token as the parent process.Recommended for standard user applications. Do refractoring with internal elevation points, as per the guidance provided earlier in this document.highestAvailableThe application runs with the highest privileges the current user can obtain.Recommended for mixed-mode applications. Plan to refractor the application in a future release.requireAdministratorThe application runs only for administrators and requires that the application be launched with the full access token of an administrator.Recommended for administrator only applications. Internal elevation points are not needed. The application is already running elevated. 
asInvoker : 应用程序就是以当前的权限运行。
highestAvailable: 以当前用户可以获得的最高权限运行。
requireAdministrator: 仅以系统管理员权限运行。
默认情况下是 asInvoker。highestAvailable 和 requireAdministrator 这两个选项都可以提示用户获取系统管理员权限。

那么后两个选项的区别在哪里呢?

区别在于,如果我们不是以管理员帐号登录,那么如果应用程序设置为 requireAdministrator ,那么应用程序就直接运行失败,无法启动。而如果设置为 highestAvailable,则应用程序可以运行成功,但是是以当前帐号的权限运行而不是系统管理员权限运行。如果我们希望程序在非管理员帐号登录时也可以运行(这种情况下应该某些功能受限制) ,那么建议采用 highestAvailable 来配置。

这里我们需要知道这三个选项分别代表什么含义就可以了,如果想了解更多关于requestedExecutionLevel 设置,请参考下面链接:
Create and Embed an Application Manifest (UAC)

0 0
原创粉丝点击