使用自定义选项

时间:2017-12-03 15:17:20

标签: python setuptools

我通过子类化setuptools.Command在setup.py中使用自定义安装选项,但是在调用父运行函数时会抛出异常。这是setup.py代码

from setuptools import setup
from setuptools import Command
import os, json, sys

class vboxcustom(Command):
    user_options = [
            ("disk-path=", "d", "Location where vbox disk files will be stored."),
            ]

    def initialize_options(self):
        self.disk_path = None

    def finalize_options(self):
        if self.disk_path is None:
            self.disk_path = os.path.expanduser("~/vbox")

    def run(self):
        settings_file = os.path.expanduser("~/.vbox")

        settings = {"disk_path": self.disk_path}

        f = open(settings_file, 'w')
        f.write(json.dumps(settings))

        super(vboxcustom, self).run()

setup(
        name="vbox",
        version="1.0",
        author="Ravi",
        author_email="email@email",
        python_requires=">=3",
        install_requires=["dnspython"],
        packages=["vboxhelper"],
        scripts=["scripts/vbox"],
        cmdclass={
            "install": vboxcustom,
            }
        )

抛出的异常是这样的: RuntimeError: abstract method -- subclass <class '__main__.vboxcustom'> must override。看来我必须覆盖一个方法,但是什么方法(我只是猜测,错误是非常非特定的)

堆栈跟踪是:

Traceback (most recent call last):
  File "setup.py", line 37, in <module>
    "install": vboxcustom,
  File "/home/ravi/work/virenvs/testvbox/lib/python3.6/site-packages/setuptools/__init__.py", line 129, in setup
    return distutils.core.setup(**attrs)
  File "/usr/lib/python3.6/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/usr/lib/python3.6/distutils/dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "/usr/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "setup.py", line 25, in run
    super(vboxcustom, self).run()
  File "/usr/lib/python3.6/distutils/cmd.py", line 176, in run
    % self.__class__)
RuntimeError: abstract method -- subclass <class '__main__.vboxcustom'> must override

我想我不应该从setuptools.Command子类来覆盖install命令,所以我从setuptools.command.install.install继承了,现在我得到了一个不同的错误。抛出的新例外是:

distutils.errors.DistutilsGetoptError: invalid negative alias 'no-compile': option 'no-compile' not defined

1 个答案:

答案 0 :(得分:1)

您需要保留班级user_options中的install

class vboxcustom(install):
    user_options = install.user_options + [
        ("disk-path=", "d", "Location where vbox disk files will be stored."),
    ]