如何在pip包中打包shell脚本

时间:2018-06-11 09:08:30

标签: python pip

我在所有模块中都使用了通用shell脚本。

所有模块都安装一个名为common的公共点。 脚本是这样的

common
   utils
     scripts
       build
         generic_build.sh

我看到有一种方法可以通过使用setup.pyscripts参数添加到console_scripts来安装python脚本。但是sh文件似乎被忽略了。这意味着它们不会在已安装的软件包中结束。

任何想法?

2 个答案:

答案 0 :(得分:1)

Shell脚本可以通过将它们添加到scripts列表中而捆绑在分发中,就像它们是python脚本一样。例如:

# bash_scripts/spam.sh
#!/usr/bin/env bash
echo Running "$0"

用于打包spam.sh的简单设置脚本:

# setup.py
from setuptools import setup

setup(
    name='spam',
    scripts=['bash_scripts/spam.sh']
)

构建源文件和二进制文件列表包括spam.sh

$ python setup.py bdist_wheel | grep spam.sh
copying bash_scripts/spam.sh -> build/scripts-3.6
copying build/scripts-3.6/spam.sh -> build/bdist.linux-aarch64/wheel/spam-0.0.0.data/scripts
changing mode of build/bdist.linux-aarch64/wheel/spam-0.0.0.data/scripts/spam.sh to 755
adding 'spam-0.0.0.data/scripts/spam.sh'

$ python setup.py sdist | grep spam.sh
copying bash_scripts/spam.sh -> spam-0.0.0/bash_scripts

安装后,检查包含的shell脚本:

$ pip install dist/spam-0.0.0.tar.gz
...
$ pip show -f spam
Name: spam
Version: 0.0.0
Summary: UNKNOWN
Home-page: UNKNOWN
Author: UNKNOWN
Author-email: UNKNOWN
License: UNKNOWN
Location: /data/gentoo64/home/u0_a82/.local/lib64/python3.6/site-packages
Requires:
Files:
  ../../../bin/spam.sh
  spam-0.0.0.dist-info/DESCRIPTION.rst
  spam-0.0.0.dist-info/INSTALLER
  spam-0.0.0.dist-info/METADATA
  spam-0.0.0.dist-info/RECORD
  spam-0.0.0.dist-info/WHEEL
  spam-0.0.0.dist-info/metadata.json
  spam-0.0.0.dist-info/top_level.txt

答案 1 :(得分:0)

这取决于您配置setup.py文件的方式。 尝试将其添加为资源或其他类型的依赖项

 data_files=[('.',['INFO.txt']),
            ('additionalstuff',['additionalstuff/moredata.txt'])]