0

I've been sitting on this for a while now and here is the question. Do you know if I'm able to reference a git branch in setup.py in any way? Is '@' sign supposed to do that? Or is it used solely for tags and commits? Here is an example of what I was trying to do.

# setup.py
...
install_requires=['Django==1.5.11']
dependency_links=['git+https://github.com/django-nonrel/django.git@nonrel-1.5#egg=Django-1.5.11']
...
#python setup.py develop
running develop
running egg_info
...
Processing dependencies for mypackage
Searching for Django==1.5.11
Best match: Django 1.5.11
Doing git clone from https://github.com/django-nonrel/django.git to c:\users\my_user_name\appdata\local\temp\easy_install-ci3vh1\django.git@nonrel-1.5
Checking out nonrel-1.5
fatal: Not a git repository (or any of the parent directories): .git

Above works without a problem when I'm not referencing any branch:

git+https://github.com/django-nonrel/django.git#egg=Django

And when I run it with pip:

pip install git+https://github.com/django-nonrel/django.git@nonrel-1.5

I included the package name for purpose, so you can look at their git repo setup. I'm not asking for the alternative ways of installing packages from git, as I'm aware of them. Just if it is possible to reference a branch in setup.py. Thanks in advance.

mfox
  • 13
  • 2

2 Answers2

0

Just put the egg at the end of git URL

pip install git+https://github.com/django-nonrel/django.git@nonrel-1.5#egg=Django

Mauro Baraldi
  • 6,346
  • 2
  • 32
  • 43
  • Well. It works well without it. What I'm asking though is why I can't achieve the same behavior inside setup.py. – mfox Feb 20 '16 at 03:07
0

I was digging down through the setuptools implementation (https://github.com/jaraco/setuptools keeps an up-to-date mirror for ease of browsing) and it looks like I've found where the problem is. If we look at the function responsible for downloading git repos defined in package_index.py:

    def _download_git(self, url, filename):
    filename = filename.split('#',1)[0]
    url, rev = self._vcs_split_rev_from_url(url, pop_prefix=True)

    self.info("Doing git clone from %s to %s", url, filename)
    os.system("git clone --quiet %s %s" % (url, filename))

    if rev is not None:
        self.info("Checking out %s", rev)
        os.system("(cd %s && git checkout --quiet %s)" % (
            filename,
            rev,
        ))

    return filename

we can see it's first cloning it to some directory (usually system temp) and then "cd git checkout" 'ing the branch. Unfortunately recently I'm forced to work on Windows, which I hate (long live Arch Linux!), and the problem with this checkout command is that cd on this system doesn't automatically switch the drive. So it's not going to work if your package is located on the other partition/drive than your temp directory. I checked that correcting it to:

(cd /d %s && git checkout --quiet %s)

solves the problem. This is system specific though, so we (probably) don't want to do PR to setuptools guys to modify it. Instead what I did was to create a temp directory in my package folder and add

import tempfile
tempfile.tempdir=os.getcwd()+"\\temp\\"

to my setup.py, which temporary changes the temp directory of easy_install. That's because I know that setuptools uses easy_install and that easy_install uses tempfile to obtain the location of temp dir. This is a good solution and I'm sticking with it, but for the sake of spreading information, I'd also mention that another thing I was trying was to create a temporary system alias for "cd" to "cd /d". On windows this is the doskey command. Unfortunately this is a local command and doesn't spread to subprocesses created with os.system(). Setting such alias globally though is a pain and you don't want to modify system registry of potential users to achieve that.

mfox
  • 13
  • 2