有没有办法在从源目录中安装软件包时找到 setup.py 中的原始源目录路径? 例如,我的源代码在
cd /home/jumbo/project/
ls -ltr
Pipfile Pipfile.lock README.md bin src_code setup.py
在上面的目录中,我运行'pip3 install .'
在 setup.py 中,我想捕获 git 源目录路径 (/home/jumbo/project/( 并将 git 代码的提交哈希写入文件。
git 源路径不是恒定的,因为它会因安装安装程序的每个用户而变化。
git -C /home/jumbo/project/ rev-parse HEAD > hash.txt
感谢您的检查。
这是我 setup.py 代码
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
我很确定您无法使用自定义setuptools
命令可靠地执行此操作,使用自定义install
命令更不可能。事实上(正如您正确注意到的那样(,您几乎无法控制此命令实际运行的位置和时间。
您可能应该更多地研究自定义sdist
、build
和develop
命令。这些通常直接从原始源目录中运行。您至少需要获得这 3 个,可能更多,才能击中所有情况,这甚至可能还不够。
接下来,您可以尝试使用自定义egg_info
命令(如果我理解正确,或多或少所有命令都会在某个时候egg_info
运行(,但我还没有深入研究它,它可能比看起来更棘手。
另请查看有关"扩展和重用安装工具"的setuptools文档,以获取在何处挂接自定义代码的更多想法。
最后,你可能会有更好的运气设置工具-scm,特别是它的write_to
选项,要么直接使用它,要么查看它的代码来寻找灵感。