setuptools中自动安装多层嵌套目录下的data文件

setuptool可以帮我们很方便的安装package,但是对于非python文件则比较麻烦。虽然通过MANIFEST.in可以指定要安装的文件,但是其不支持多层嵌套目录的安装。一个解决方法就是在setup.py中主动发现文件。下面是个例子:

# -*- coding: utf-8 -*-

import fnmatch
import os

from setuptools import setup, find_packages


def get_file_path(*args):
    path = os.path.join(*args)
    return os.path.normpath(path)


def walk_helper(args, dir_name, files):
    if ".git" in dir_name:
        return

    names = []

    file_list, wildcards = args
    for wc in wildcards:
        wc_file_name = get_file_path(dir_name, wc)
        for file_ in files:
            if file_.endswith(".py") or file_.endswith(".pyc"):
                continue
            file_name = get_file_path(dir_name, file_)
            if (fnmatch.fnmatch(file_name, wc_file_name) and
                    not os.path.isdir(file_name)):
                names.append(file_name)

    if names:
        file_list.append((dir_name, names))

def get_data_files(src_dir, *wildcards):
    file_list = []
    os.path.walk(src_dir, walk_helper, (file_list, wildcards))
    return file_list


setup(
    name="hmonitor",
    version="0.10",
    description="A monitor control console",
    author="Qin TianHuan",
    author_email="tianhuan@bingotree.cn",
    url="",
    packages=find_packages(),
    data_files=get_data_files("hmonitor/", "*.*"),

    provides=["hmonitor.utils.executor",
              ],
    entry_points={
        "hmonitor.utils.executor": [
            'ssh = hmonitor.utils.executor.ssh:SSHExecutor',
        ],
        "console_scripts": [
            "hmonitor-server = hmonitor.hmonitor_server:main",
            "hmonitor-agent = hmonitor.hmonitor_agent:main",
        ],
    },
)

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*