在我的python软件包中,安装后需要执行一些活动,例如需要更改文件的权限,而且pip安装也将使用--target选项运行,因此安装后脚本需要知道此安装目录路径(已通过使用--target),以便可以在正确的路径上进行权限更改。我的安装后脚本将得到扩展,但基础是在安装后内部查找安装路径
我尝试使用pkg_resources
from pkg_resources import resource_filename
class Post_Install(install):
def run(self):
print("Post install steps")
install.run(self)
package_dir = resource_filename('myproject',"config.json")
但是在安装后的过程中,这显示的是相对路径,例如myproject / config.json,不确定此处是否正确(这不是--target传递的路径)。
这是我的setup.py
from setuptools import setup, find_packages
from setuptools.command.install import install
import os
import sys
from pkg_resources import resource_filename
class Post_Install(install):
"""Post installation activities"""
def run(self):
print("Post install steps")
install.run(self)
package_dir = resource_filename('myproject',"config.json")
print("package dir {}".format(package_dir))
from subprocess import call
call([sys.executable, 'post_install.py',package_dir])
setup(name='myproject',
version='1.1.0',
author='methecoder',
description='my python project'
long_description=__doc__,
packages=find_packages(exclude=['tests']),
include_package_data=True,
cmdclass={
'install': Post_Install,
},
data_files =[ ('.', ['config.json']),
('logs', []),
],
classifiers=[
'Development Status :: 4 - Beta',
'Environment :: Console',
'Intended Audience :: End Users',
'Intended Audience :: Developers',
'Operating System :: Microsoft :: Windows',
'Operating System :: POSIX',
'Programming Language :: Python',
'Topic :: Communications :: Email',
'Topic :: Office/Business',
]
)
__author__ = 'methecoder'
我的帖子安装脚本
import os, sys
module_path = sys.argv[1]
print("from post_install.py module")
print("module path {}".format(module_path))
os.system("chmod 700 "+module_path+"/config.json")
print("changed permission")
假设我的目标目录为/ home / user1 /,则pip安装为
$ pip install -t /home/user1/ myproject-1.1.0.tar.gz -v
然后安装后必须找到/ home / user1 /,以便可以对所需文件运行chmod。
注意:目标目录下的预期文件是
$ ls /home/user1/
myproject/
config.json
logs/