简体   繁体   English

.pypirc文件的自定义位置

[英]Custom location for .pypirc file

Does setuptools allow for the .pypirc file to be specified in a custom location rather than $HOME/.pypirc ? setuptools是否允许在自定义位置而不是$HOME/.pypirc指定.pypirc文件? I'm setting up a jenkins job to publish to an internal repository, and want the .pypirc file to be inside the job's workspace. 我正在设置jenkins作业以发布到内部存储库,并希望.pypirc文件位于作业的工作区内。

This is doable by overloading the default command classes used for communication with the PyPI or your custom repository server. 这可以通过重载用于与PyPI或自定义存储库服务器通信的默认命令类来实现。 This can be done in your setup.py script. 这可以在setup.py脚本中完成。 Let the code do the talk first: 让代码先说说话:

# this is your project's setup.py script

import os
from distutils.command.register import register as register_orig
from distutils.command.upload import upload as upload_orig

from setuptools import setup


class register(register_orig):

    def _get_rc_file(self):
        return os.path.join('.', '.pypirc')

class upload(upload_orig):

    def _get_rc_file(self):
        return os.path.join('.', '.pypirc')

setup(
    name='myproj',
    ...
    cmdclass={
        'register': register,
        'upload': upload,
    }
)

Now, if you call eg 现在,如果你打电话,例如

$ python setup.py sdist register upload

the server credentials will be taken from .pypirc in the project directory instead of the default ~/.pypirc one. 服务器证书将采取.pypirc在项目目录,而不是默认的~/.pypirc之一。

So basically, all you need to do is to replace the register and upload commands with your own implementations. 基本上,您需要做的就是用您自己的实现替换registerupload命令。 These implementations redefine the _get_rc_file method with your own logic. 这些实现使用您自己的逻辑重新定义_get_rc_file方法。 After that, don't forget to adjust the cmd_class argument in your setup function so your own command classes are used instead of the standard ones. 之后,不要忘记在setup函数中调整cmd_class参数,以便使用自己的命令类而不是标准类。

Of course, as both classes redefine the same method, you could prettify the code by eg writing a mixin class and reusing it etc etc. Also, if you are uploading the sphinx docs to PyPI via upload_docs command, you have to overload it the same way. 当然,由于这两个类都重新定义了相同的方法,你可以通过编写一个mixin类并重复使用等等来对代码进行美化。另外,如果你通过upload_docs命令将sphinx文档上传到PyPI,你必须重载它方式。 Anyway, you should get the basic idea of command overloading from the snippet above. 无论如何,你应该从上面的代码片段中获得命令重载的基本概念。

You might want to check out twine . 你可能想看看麻线 This tool allows you to specify a custom .pypirc file via the option --config-file 此工具允许您通过选项--config-file指定自定义.pypirc --config-file

In the last version of setuptools (version 29.0.1), the .pypirc file is loaded from the $HOME directory (or %USERPROFILE% on Windows). 在最新版本的setuptools (版本29.0.1)中, .pypirc文件是从$HOME目录(或Windows上的%USERPROFILE% )加载的。

This is done in the setuptools.package_index.PyPIConfig class. 这是在setuptools.package_index.PyPIConfig类中完成的。 See the source code on GitHub: 查看GitHub上的源代码

class PyPIConfig(configparser.RawConfigParser):
    def __init__(self):
        """
        Load from ~/.pypirc
        """
        defaults = dict.fromkeys(['username', 'password', 'repository'], '')
        configparser.RawConfigParser.__init__(self, defaults)

        rc = os.path.join(os.path.expanduser('~'), '.pypirc')
        if os.path.exists(rc):
            self.read(rc)

So, the only solution I see is to temporary redefine the HOME environment variable before running the Jenkins job. 因此,我看到的唯一解决方案是在运行Jenkins作业之前临时重新定义HOME环境变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM