Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom location for .pypirc file

Does setuptools allow for the .pypirc file to be specified in a custom location rather than $HOME/.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.

like image 763
Srikanth Avatar asked Jun 15 '16 20:06

Srikanth


People also ask

Where do I put .pypirc file?

This file must be placed in $HOME/. pypirc for pip/twine to use it. Keep in mind, pypi.org and test.pypi.org are not integrated, so you'll need to have a separate account created on each site. One thing to notice above is that the [pypi] section does not have repository configured, but the testpypi section does.

What is .pypirc file?

pypirc file. A . pypirc file allows you to define the configuration for package indexes (referred to here as “repositories”), so that you don't have to enter the URL, username, or password whenever you upload a package with twine or flit.

What is twine Python?

Twine is a utility for publishing Python packages to PyPI and other repositories. It provides build system independent uploads of source and binary distribution artifacts for both new and existing projects.


3 Answers

This is doable by overloading the default command classes used for communication with the PyPI or your custom repository server. This can be done in your setup.py script. 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 e.g.

$ python setup.py sdist register upload

the server credentials will be taken from .pypirc in the project directory instead of the default ~/.pypirc one.

So basically, all you need to do is to replace the register and upload commands with your own implementations. These implementations redefine the _get_rc_file method with your own logic. After that, don't forget to adjust the cmdclass argument in your setup function so your own command classes are used instead of the standard ones.

Of course, as both classes redefine the same method, you could prettify the code by e.g. 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. Anyway, you should get the basic idea of command overloading from the snippet above.

like image 156
hoefling Avatar answered Sep 16 '22 11:09

hoefling


You might want to check out twine. This tool allows you to specify a custom .pypirc file via the option --config-file

like image 30
mattmilten Avatar answered Sep 18 '22 11:09

mattmilten


In the last version of setuptools (version 29.0.1), the .pypirc file is loaded from the $HOME directory (or %USERPROFILE% on Windows).

This is done in the setuptools.package_index.PyPIConfig class. See the source code on 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.

like image 41
Laurent LAPORTE Avatar answered Sep 18 '22 11:09

Laurent LAPORTE