3

Is there a way to find the original source directory path in setup.py while install the package being from the source directory? For example my source code is in

cd /home/jumbo/project/ ls -ltr Pipfile Pipfile.lock README.md bin src_code setup.py

Being in the above directory, i run 'pip3 install .' In setup.py, i want to capture the git source directory path (/home/jumbo/project/) and write the commit hash of the git code to a file.

The git source path is not constant as it changes for each user whoever installing the setup.

git -C /home/jumbo/project/ rev-parse HEAD > hash.txt

Thanks for checking.

This is my setup.py code

import os.path
import subprocess
from setuptools import setup
from setuptools.command.install import install


class IW(install):
    def run(self):
        repo_path = os.path.dirname(os.path.realpath(__file__))
        print ("REPO_PATH:", repo_path)
        command = 'git -C ' + repo_path + ' rev-parse HEAD > hash.txt'
        execute_command = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
        execute_command.communicate()
        if execute_command.returncode != 0:
            raise OSError("Command %s failed" % command)
        install.run(self)

setup(name='jumbo_deploy',
      version='1.1.0',
      url='https://github.com/src/jumbo-deploy',
      license='Copyright Jumbo 2018',
      packages=['jumbo_deploy'],
      install_requires=[
          'argparse',
          'requests',
      ],
      zip_safe=False,
      package_data={'jumbo_deploy': ['hash.txt']},
      include_package_data=True,
      scripts=['bin/jumbo_deploy'],
      cmdclass={
          'install': IW,
      }
      )

+++++ END of my setup.py ++++


Currently with the above setup.py, my function run(self) is being executed after creating and changing the directory to some random 

user1 $ cd /home/jumbo/project/
user1 $ pip3 install . --upgrade -v
Created temporary directory: /private/var/folders/_w/sv2ms8pd0zl38l3lyy6f787w005lxf/T/pip-ephem-wheel-cache-w28h4dpd
Created temporary directory: /private/var/folders/_w/sv2ms8pd0zl38l3lyy6f787w005lxf/T/pip-req-tracker-pc07b4yn
Created requirements tracker '/private/var/folders/_w/sv2ms8pd0zl38l3lyy6f787w005lxf/T/pip-req-tracker-pc07b4yn'
Created temporary directory: /private/var/folders/_w/sv2ms8pd0zl38l3lyy6f787w005lxf/T/pip-install-wqohpdxt
Processing /home/jumbo/project
  Created temporary directory: /private/var/folders/_w/sv2ms8pd0zl38l3lyy6f787w005lxf/T/pip-req-build-1df74t7f
  Added file:////home/jumbo/project/ to build tracker '/private/var/folders/_w/sv2ms8pd0zl38l3lyy6f787w005lxf/T/pip-req-tracker-pc07b4yn'
  Running setup.py (path:/private/var/folders/_w/sv2ms8pd0zl38l3lyy6f787w005lxf/T/pip-req-build-1df74t7f/setup.py) egg_info for package from file:///home/jumbo/project/
    Running command python setup.py egg_info

REPO_PATH:/private/var/folders/_w/sv2ms8pd0zl38l3lyy6f787w005lxf/T/pip-req-build-1df74t7f 

========
I'm expecting REPO_PATH:/home/jumbo/project
but seems before my setup code runs, it already changed the directory to /private/var/folders/_w/sv2ms8pd0zl38l3lyy6f787w005lxf/T/pip-req-build-1df74t7f
raffiqu
  • 71
  • 6
  • Possible duplicate of [Get location of the .py source file](https://stackoverflow.com/questions/7162366/get-location-of-the-py-source-file) – user2653663 Sep 24 '19 at 14:32
  • seems before the setup.py runs, the files are copied over from source directory to another locations while installing. so os.path.dirname(os.path.abspath(__file__)) will only return the target location but i need source location path. – raffiqu Sep 24 '19 at 15:26
  • Maybe look at [setuptools_scm](https://pypi.org/project/setuptools-scm/) and specifically its `write_to` option. – sinoroc Sep 27 '19 at 09:12
  • Did you manage to solve this? I have a similar problem. – Peter Dec 19 '20 at 10:03

1 Answers1

0

I am pretty sure you can not do this reliably with a custom setuptools command, and even more unlikely with a custom install command. Indeed (as you correctly noticed) you have little control over where, when this command actually runs.

You probably should look more into customizing the sdist, build, and develop commands. These are usually run directly from within the original source directory. You will need to get at least these 3, probably more, to hit all the cases, and that might not even be enough.

Next you could try with a custom egg_info command (if I understood right, more or less all commands will run egg_info at some point), but I haven't looked much into it and it might be more tricky than it looks to get all the cases right.

Also look at the setuptools documentation on "Extending and Reusing Setuptools" for more ideas where to hook up your custom code.

Finally you might have better luck with setuptools-scm and in particular its write_to option, either using it directly or looking at its code for inspiration.

sinoroc
  • 18,409
  • 2
  • 39
  • 70