setup.py examples?

后端 未结 8 1789
庸人自扰
庸人自扰 2020-12-12 11:06

After studying this page:

http://docs.python.org/distutils/builtdist.html

I am hoping to find some setup.py files to study so as to make my own (with the goa

8条回答
  •  感动是毒
    2020-12-12 11:17

    Look at this complete example https://github.com/marcindulak/python-mycli of a small python package. It is based on packaging recommendations from https://packaging.python.org/en/latest/distributing.html, uses setup.py with distutils and in addition shows how to create RPM and deb packages.

    The project's setup.py is included below (see the repo for the full source):

    #!/usr/bin/env python
    
    import os
    import sys
    
    from distutils.core import setup
    
    name = "mycli"
    
    rootdir = os.path.abspath(os.path.dirname(__file__))
    
    # Restructured text project description read from file
    long_description = open(os.path.join(rootdir, 'README.md')).read()
    
    # Python 2.4 or later needed
    if sys.version_info < (2, 4, 0, 'final', 0):
        raise SystemExit, 'Python 2.4 or later is required!'
    
    # Build a list of all project modules
    packages = []
    for dirname, dirnames, filenames in os.walk(name):
            if '__init__.py' in filenames:
                packages.append(dirname.replace('/', '.'))
    
    package_dir = {name: name}
    
    # Data files used e.g. in tests
    package_data = {name: [os.path.join(name, 'tests', 'prt.txt')]}
    
    # The current version number - MSI accepts only version X.X.X
    exec(open(os.path.join(name, 'version.py')).read())
    
    # Scripts
    scripts = []
    for dirname, dirnames, filenames in os.walk('scripts'):
        for filename in filenames:
            if not filename.endswith('.bat'):
                scripts.append(os.path.join(dirname, filename))
    
    # Provide bat executables in the tarball (always for Win)
    if 'sdist' in sys.argv or os.name in ['ce', 'nt']:
        for s in scripts[:]:
            scripts.append(s + '.bat')
    
    # Data_files (e.g. doc) needs (directory, files-in-this-directory) tuples
    data_files = []
    for dirname, dirnames, filenames in os.walk('doc'):
            fileslist = []
            for filename in filenames:
                fullname = os.path.join(dirname, filename)
                fileslist.append(fullname)
            data_files.append(('share/' + name + '/' + dirname, fileslist))
    
    setup(name='python-' + name,
          version=version,  # PEP440
          description='mycli - shows some argparse features',
          long_description=long_description,
          url='https://github.com/marcindulak/python-mycli',
          author='Marcin Dulak',
          author_email='X.Y@Z.com',
          license='ASL',
          # https://pypi.python.org/pypi?%3Aaction=list_classifiers
          classifiers=[
              'Development Status :: 1 - Planning',
              'Environment :: Console',
              'License :: OSI Approved :: Apache Software License',
              'Natural Language :: English',
              'Operating System :: OS Independent',
              'Programming Language :: Python :: 2',
              'Programming Language :: Python :: 2.4',
              'Programming Language :: Python :: 2.5',
              'Programming Language :: Python :: 2.6',
              'Programming Language :: Python :: 2.7',
              'Programming Language :: Python :: 3',
              'Programming Language :: Python :: 3.2',
              'Programming Language :: Python :: 3.3',
              'Programming Language :: Python :: 3.4',
          ],
          keywords='argparse distutils cli unittest RPM spec deb',
          packages=packages,
          package_dir=package_dir,
          package_data=package_data,
          scripts=scripts,
          data_files=data_files,
          )
    

    and and RPM spec file which more or less follows Fedora/EPEL packaging guidelines may look like:

    # Failsafe backport of Python2-macros for RHEL <= 6
    %{!?python_sitelib: %global python_sitelib      %(%{__python} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")}
    %{!?python_sitearch:    %global python_sitearch     %(%{__python} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(1))")}
    %{!?python_version: %global python_version      %(%{__python} -c "import sys; sys.stdout.write(sys.version[:3])")}
    %{!?__python2:      %global __python2       %{__python}}
    %{!?python2_sitelib:    %global python2_sitelib     %{python_sitelib}}
    %{!?python2_sitearch:   %global python2_sitearch    %{python_sitearch}}
    %{!?python2_version:    %global python2_version     %{python_version}}
    
    %{!?python2_minor_version: %define python2_minor_version %(%{__python} -c "import sys ; print sys.version[2:3]")}
    
    %global upstream_name mycli
    
    
    Name:           python-%{upstream_name}
    Version:        0.0.1
    Release:        1%{?dist}
    Summary:        A Python program that demonstrates usage of argparse
    %{?el5:Group:       Applications/Scientific}
    License:        ASL 2.0
    
    URL:            https://github.com/marcindulak/%{name}
    Source0:        https://github.com/marcindulak/%{name}/%{name}-%{version}.tar.gz
    
    %{?el5:BuildRoot:   %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)}
    BuildArch:      noarch
    
    %if 0%{?suse_version}
    BuildRequires:      python-devel
    %else
    BuildRequires:      python2-devel
    %endif
    
    
    %description
    A Python program that demonstrates usage of argparse.
    
    
    %prep
    %setup -qn %{name}-%{version}
    
    
    %build
    %{__python2} setup.py build
    
    
    %install
    %{?el5:rm -rf $RPM_BUILD_ROOT}
    %{__python2} setup.py install --skip-build --prefix=%{_prefix} \
       --optimize=1 --root $RPM_BUILD_ROOT
    
    
    %check
    export PYTHONPATH=`pwd`/build/lib
    export PATH=`pwd`/build/scripts-%{python2_version}:${PATH}
    %if 0%{python2_minor_version} >= 7
    %{__python2} -m unittest discover -s %{upstream_name}/tests -p '*.py'
    %endif
    
    
    %clean
    %{?el5:rm -rf $RPM_BUILD_ROOT}
    
    
    %files
    %doc LICENSE README.md
    %{_bindir}/*
    %{python2_sitelib}/%{upstream_name}
    %{?!el5:%{python2_sitelib}/*.egg-info}
    
    
    %changelog
    * Wed Jan 14 2015 Marcin Dulak  - 0.0.1-1
    - initial version
    

提交回复
热议问题