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
4
  • Possible duplicate of Get location of the .py source file Commented Sep 24, 2019 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. Commented Sep 24, 2019 at 15:26
  • Maybe look at setuptools_scm and specifically its write_to option. Commented Sep 27, 2019 at 9:12
  • Did you manage to solve this? I have a similar problem. Commented Dec 19, 2020 at 10:03

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.