Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setuptools. How to make package contain extra data folder and all folders inside

So i'm making setuptool package.

To include extra data files in package i need to specefy data_file parameter inside setup().

Code Example:

datadir = os.path.join('share','data')
datafiles = [(datadir, [f for f in glob.glob(os.path.join(datadir, '*'))])]
import metainfo # a file with relevant information
setup(
    name             = 'yourpackage',
    version          = metainfo.version,
    maintainer       = metainfo.maintainer,
    maintainer_email = metainfo.maintainer_email,
    author           = metainfo.authors,
    author_email     = metainfo.authors,
    description      = metainfo.description,
    keywords         = metainfo.keywords,
    long_description = metainfo.long_description,

    # package installation
    packages = find_packages('src'),
    package_dir  = package_dir,

    data_files = datafiles,
}

datafile variable in this example will contain data like this

[('share/data', ['share/data/addon.xml.tmplt', 'share/data/default.py.tmplt'])]

However, my example does not make list of folders and containing files recursively.

I need function which gets as parameter folder_path and returns list of tuple:

[
    ('share/data', ['share/data/addon.xml.tmplt', 'share/data/default.py.tmplt']),
    ('share/data/inside', ['share/data/inside/file.iside', 'share/data/inside/file2.inside']),
    .........

]
like image 780
Pol Avatar asked Nov 29 '12 15:11

Pol


People also ask

How do you include data in a Python package?

Place the files that you want to include in the package directory (in our case, the data has to reside in the roman/ directory). Add the field include_package_data=True in setup.py. Add the field package_data={'': [... patterns for files you want to include, relative to package dir...]} in setup.py .

What is package_data?

The package_data argument is a dictionary that maps from package names to lists of glob patterns. Note that the data files specified using the package_data option neither require to be included within a MANIFEST.in file, nor require to be added by a revision control system plugin.

What is MANIFEST in Python package?

A manifest file is a text file called MANIFEST.in . Place it in the project's root directory, next to README. txt and setup.py . Manifest files are not Python scripts; they are text files that contain a series of “commands” in a Distutils-defined format.

What is Pkg_resources?

pkg_resources is a module used to find and manage Python package/version dependencies and access bundled files and resources, including those inside of zipped . egg files.


1 Answers

Use os.walk() to generate that information:

datafiles = [(root, [os.path.join(root, f) for f in files])
    for root, dirs, files in os.walk(datadir)]

That'll produce absolute paths; you can process the root variable a little more to make them relative to the setup.py directory if needed.

like image 150
Martijn Pieters Avatar answered Oct 13 '22 21:10

Martijn Pieters