【发布时间】:2020-01-24 17:04:29
【问题描述】:
在从源目录安装软件包时,有没有办法在 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
【问题讨论】:
-
似乎在 setup.py 运行之前,文件在安装时从源目录复制到另一个位置。所以 os.path.dirname(os.path.abspath(file)) 只会返回目标位置,但我需要源位置路径。
-
也许看看setuptools_scm,特别是它的
write_to选项。 -
你解决了吗?我也有类似的问题。
标签: python-3.x pip setup.py