鉴于以下(演示)项目布局:

MyProject/
    README
    LICENSE
    setup.py
    myproject/
        ... # packages
    extrastuff/
        ... # some extra data

我如何(以及在​​哪里)声明不同的分发类型?特别是我需要这两个选项:
  • 仅包含源
  • 的分发
  • 包含源文件和 (extrastuff) 下的所有数据文件的发行版

  • 理想情况下,如何声明上面的两个配置,而第二个配置取决于第一个?

    最佳答案

    我之前已经实现过类似的东西...... sdist 命令可以扩展为处理额外的命令行参数并基于这些来操作数据文件。如果您运行 python setup.py sdist --help ,它将在帮助中包含您的自定义命令行参数,这很好。使用以下配方:

    from distutils import log
    from distutils.core import setup
    from distutils.command.sdist import sdist
    
    class CustomSdist(sdist):
    
        user_options = [
            ('packaging=', None, "Some option to indicate what should be packaged")
        ] + sdist.user_options
    
        def __init__(self, *args, **kwargs):
            sdist.__init__(self, *args, **kwargs)
    
            self.packaging = "default value for this option"
    
        def get_file_list(self):
    
            log.info("Chosen packaging option: {self.packaging}".format(self=self))
    
            # Change the data_files list here based on the packaging option
            self.distribution.data_files = list(
              ('folder', ['file1', 'file2'])
            )
            sdist.get_file_list(self)
    
    if __name__ == "__main__":
    
        setup(
            name = "name",
            version = "version",
            author = "author",
            author_email = "author_email",
            url = "url",
            py_modules = [
                # ...
            ],
            packages = [
                # ...
            ],
    #        data_files = default data files for commands other than sdist if you wish
            cmdclass={
                'sdist': CustomSdist
            }
        )
    

    关于python - 使用 setup.py 创建不同的分发类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8064823/

    10-12 18:39