在从源目录安装软件包时,是否有办法在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
答案 0 :(得分:0)
我敢肯定,使用自定义setuptools
命令不能可靠地完成此操作,而使用自定义install
命令则更不可能。确实(如您所正确注意到的那样),您几乎无法控制此命令的实际运行时间。
您可能应该更多地考虑自定义sdist
,build
和develop
命令。这些通常直接在原始源目录中运行。您需要至少获得这3个(可能更多),才能处理所有情况,而这可能还不够。
接下来,您可以尝试使用自定义egg_info
命令(如果我理解正确的话,或多或少所有命令都会在某个时候运行egg_info
),但是我并没有对其进行过多研究要使所有情况都正确,可能要比看起来更棘手。
也请查看setuptools documentation on "Extending and Reusing Setuptools",以获取更多有关连接自定义代码的想法。
最后,您可以使用setuptools-scm,尤其是它的write_to
选项来获得更好的运气,要么直接使用它,要么查看其代码以获取灵感。