代码之家  ›  专栏  ›  技术社区  ›  christophe31

如何使用python/distutils定义系统命令?

  •  2
  • christophe31  · 技术社区  · 16 年前

    我正在寻找一种最优雅的方式来通知我的库的用户,他们需要一个特定的unix命令来确保它可以正常工作。。。

    我的lib何时会出错:

    • 安装?
    • 当我的应用程序调用命令时?

    以及如何检测到命令丢失( if not commands.getoutput("which CommandIDependsOn"): raise Exception("you need CommandIDependsOn")

    我需要建议。

    2 回复  |  直到 16 年前
        1
  •  4
  •   habnabit dwc    16 年前

    我根本就没有支票。文档说明您的库需要此命令,如果用户尝试使用库中需要它的任何部分,则运行此命令的任何部分都会引发异常。即使只提供了一部分功能,也应该可以导入并使用库。

    commands 是旧的和坏的,不应该在新代码中使用。 subprocess 是热门的新事物。)

        2
  •  5
  •   ohe    14 年前

    如果您使用distutils分发软件包,要安装它,必须执行以下操作:

    pythonsetup.py 建造 pythonsetup.py 安装

    或者只是

    pythonsetup.py 安装(在这种情况下是pythonsetup.py 构建是隐式的)

    要检查是否安装了*nix命令,可以在setup.py 这样地:

    from distutils.core import setup
    from distutils.command.build import build as _build
    
    class build(_build):
    
        description = "Custom Build Process"
        user_options= _build.user_options[:]
        # You can also define extra options like this : 
        #user_options.extend([('opt=', None, 'Name of optionnal option')])
    
        def initialize_options(self):   
    
            # Initialize here you're extra options... Not needed in your case
            #self.opt = None
            _build.initialize_options(self)
    
        def finalize_options(self):
            # Finalize your options, you can modify value
            if self.opt is None :
                self.opt = "default value"
    
            _build.finalize_options(self)
    
        def run(self):
            # Extra Check
            # Enter your code here to verify if the *nix command is present
            .................
    
            # Start "classic" Build command
            _build.run(self)
    
    setup(
            ....
            # Don't forget to register your custom build command
            cmdclass         = {'build' : build},
            ....
         )
    

    但是如果用户在安装包之后卸载所需的命令呢?要解决这个问题,唯一“好”的解决方案是使用诸如deb或rpm之类的打包系统,并在命令和包之间建立依赖关系。

    希望这有帮助

    推荐文章