From 46eab744cdafacbd749c56cbdade4e1e8d4156a0 Mon Sep 17 00:00:00 2001 From: Chris Livingston Date: Tue, 21 Nov 2017 12:19:22 -0800 Subject: [PATCH] Working package example for hello2 --- .../hello/hello2/.gitignore | 13 ++ .../hello/hello2/.travis.yml | 16 +++ .../python_distribution/hello/hello2/BUILD | 8 ++ .../hello/hello2/LICENSE.txt | 19 +++ .../hello/hello2/MANIFEST.in | 5 + .../hello/hello2/README.rst | 25 ++++ .../hello/hello2/c/super_greet.c | 15 +++ .../hello/hello2/data/data_file | 1 + .../hello/hello2/sample/__init__.py | 7 ++ .../hello/hello2/sample/lol.py | 4 + .../hello/hello2/sample/package_data.dat | 1 + .../hello/hello2/setup.cfg | 5 + .../python_distribution/hello/hello2/setup.py | 115 ++++++++++++++++++ .../hello/hello2/tests/__init__.py | 3 + .../hello/hello2/tests/test_simple.py | 7 ++ .../python_distribution/hello/hello2/tox.ini | 33 +++++ .../hello/superhello/c/super_greet.c | 15 +++ .../hello/superhello/cpp/greet.cpp | 27 ---- 18 files changed, 292 insertions(+), 27 deletions(-) create mode 100644 examples/src/python/example/python_distribution/hello/hello2/.gitignore create mode 100644 examples/src/python/example/python_distribution/hello/hello2/.travis.yml create mode 100644 examples/src/python/example/python_distribution/hello/hello2/BUILD create mode 100644 examples/src/python/example/python_distribution/hello/hello2/LICENSE.txt create mode 100644 examples/src/python/example/python_distribution/hello/hello2/MANIFEST.in create mode 100644 examples/src/python/example/python_distribution/hello/hello2/README.rst create mode 100644 examples/src/python/example/python_distribution/hello/hello2/c/super_greet.c create mode 100644 examples/src/python/example/python_distribution/hello/hello2/data/data_file create mode 100644 examples/src/python/example/python_distribution/hello/hello2/sample/__init__.py create mode 100644 examples/src/python/example/python_distribution/hello/hello2/sample/lol.py create mode 100644 examples/src/python/example/python_distribution/hello/hello2/sample/package_data.dat create mode 100644 examples/src/python/example/python_distribution/hello/hello2/setup.cfg create mode 100644 examples/src/python/example/python_distribution/hello/hello2/setup.py create mode 100644 examples/src/python/example/python_distribution/hello/hello2/tests/__init__.py create mode 100644 examples/src/python/example/python_distribution/hello/hello2/tests/test_simple.py create mode 100644 examples/src/python/example/python_distribution/hello/hello2/tox.ini create mode 100644 examples/src/python/example/python_distribution/hello/superhello/c/super_greet.c delete mode 100644 examples/src/python/example/python_distribution/hello/superhello/cpp/greet.cpp diff --git a/examples/src/python/example/python_distribution/hello/hello2/.gitignore b/examples/src/python/example/python_distribution/hello/hello2/.gitignore new file mode 100644 index 00000000000..e81f084204c --- /dev/null +++ b/examples/src/python/example/python_distribution/hello/hello2/.gitignore @@ -0,0 +1,13 @@ +# general things to ignore +build/ +dist/ +*.egg-info/ +*.egg +*.py[cod] +__pycache__/ +*.so +*~ + +# due to using tox and pytest +.tox +.cache diff --git a/examples/src/python/example/python_distribution/hello/hello2/.travis.yml b/examples/src/python/example/python_distribution/hello/hello2/.travis.yml new file mode 100644 index 00000000000..73cfbf58736 --- /dev/null +++ b/examples/src/python/example/python_distribution/hello/hello2/.travis.yml @@ -0,0 +1,16 @@ +# this file is *not* meant to cover or endorse the use of travis, but rather to +# help confirm pull requests to this project. + +language: python + +env: + - TOXENV=py27 + - TOXENV=py33 + - TOXENV=py34 + +install: pip install tox + +script: tox + +notifications: + email: false diff --git a/examples/src/python/example/python_distribution/hello/hello2/BUILD b/examples/src/python/example/python_distribution/hello/hello2/BUILD new file mode 100644 index 00000000000..0be85286a9b --- /dev/null +++ b/examples/src/python/example/python_distribution/hello/hello2/BUILD @@ -0,0 +1,8 @@ +# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md). +# Licensed under the Apache License, Version 2.0 (see LICENSE). + +# Like Hello world, but built with Pants. This time, with both C++ and Py sources + +python_distribution( + name='sample' +) diff --git a/examples/src/python/example/python_distribution/hello/hello2/LICENSE.txt b/examples/src/python/example/python_distribution/hello/hello2/LICENSE.txt new file mode 100644 index 00000000000..c74aceda037 --- /dev/null +++ b/examples/src/python/example/python_distribution/hello/hello2/LICENSE.txt @@ -0,0 +1,19 @@ +Copyright (c) 2016 The Python Packaging Authority (PyPA) + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/examples/src/python/example/python_distribution/hello/hello2/MANIFEST.in b/examples/src/python/example/python_distribution/hello/hello2/MANIFEST.in new file mode 100644 index 00000000000..a4fa0c0da8f --- /dev/null +++ b/examples/src/python/example/python_distribution/hello/hello2/MANIFEST.in @@ -0,0 +1,5 @@ +# Include the license file +include LICENSE.txt + +# Include the data files +recursive-include data * diff --git a/examples/src/python/example/python_distribution/hello/hello2/README.rst b/examples/src/python/example/python_distribution/hello/hello2/README.rst new file mode 100644 index 00000000000..c315711d650 --- /dev/null +++ b/examples/src/python/example/python_distribution/hello/hello2/README.rst @@ -0,0 +1,25 @@ +A sample Python project +======================= + +A sample project that exists as an aid to the `Python Packaging User Guide +`_'s `Tutorial on Packaging and Distributing +Projects `_. + +This projects does not aim to cover best practices for Python project +development as a whole. For example, it does not provide guidance or tool +recommendations for version control, documentation, or testing. + +---- + +This is the README file for the project. + +The file should use UTF-8 encoding and be written using `reStructuredText +`_. It +will be used to generate the project webpage on PyPI and will be displayed as +the project homepage on common code-hosting services, and should be written for +that purpose. + +Typical contents for this file would include an overview of the project, basic +usage examples, etc. Generally, including the project changelog in here is not +a good idea, although a simple "What's New" section for the most recent version +may be appropriate. diff --git a/examples/src/python/example/python_distribution/hello/hello2/c/super_greet.c b/examples/src/python/example/python_distribution/hello/hello2/c/super_greet.c new file mode 100644 index 00000000000..0c5d955f32b --- /dev/null +++ b/examples/src/python/example/python_distribution/hello/hello2/c/super_greet.c @@ -0,0 +1,15 @@ + +#include + +static PyObject * super_greet(PyObject *self, PyObject *args) { + return Py_BuildValue("s", "Super hello"); +} + +static PyMethodDef Methods[] = { + {"super_greet", super_greet, METH_VARARGS, "A super greeting"}, + {NULL, NULL, 0, NULL} +}; + +PyMODINIT_FUNC initsuper_greet(void) { + (void) Py_InitModule("super_greet", Methods); +} diff --git a/examples/src/python/example/python_distribution/hello/hello2/data/data_file b/examples/src/python/example/python_distribution/hello/hello2/data/data_file new file mode 100644 index 00000000000..7c0646bfd53 --- /dev/null +++ b/examples/src/python/example/python_distribution/hello/hello2/data/data_file @@ -0,0 +1 @@ +some data \ No newline at end of file diff --git a/examples/src/python/example/python_distribution/hello/hello2/sample/__init__.py b/examples/src/python/example/python_distribution/hello/hello2/sample/__init__.py new file mode 100644 index 00000000000..1b6bb2ac74a --- /dev/null +++ b/examples/src/python/example/python_distribution/hello/hello2/sample/__init__.py @@ -0,0 +1,7 @@ +import super_greet +def main(): + """Entry point for the application script""" + print("Call your main application code here") + +def ext(): + print(super_greet.super_greet()) \ No newline at end of file diff --git a/examples/src/python/example/python_distribution/hello/hello2/sample/lol.py b/examples/src/python/example/python_distribution/hello/hello2/sample/lol.py new file mode 100644 index 00000000000..3747fd053c1 --- /dev/null +++ b/examples/src/python/example/python_distribution/hello/hello2/sample/lol.py @@ -0,0 +1,4 @@ +import super_greet + +def lol(): + print(super_greet.super_greet()) \ No newline at end of file diff --git a/examples/src/python/example/python_distribution/hello/hello2/sample/package_data.dat b/examples/src/python/example/python_distribution/hello/hello2/sample/package_data.dat new file mode 100644 index 00000000000..7c0646bfd53 --- /dev/null +++ b/examples/src/python/example/python_distribution/hello/hello2/sample/package_data.dat @@ -0,0 +1 @@ +some data \ No newline at end of file diff --git a/examples/src/python/example/python_distribution/hello/hello2/setup.cfg b/examples/src/python/example/python_distribution/hello/hello2/setup.cfg new file mode 100644 index 00000000000..79bc67848ff --- /dev/null +++ b/examples/src/python/example/python_distribution/hello/hello2/setup.cfg @@ -0,0 +1,5 @@ +[bdist_wheel] +# This flag says that the code is written to work on both Python 2 and Python +# 3. If at all possible, it is good practice to do this. If you cannot, you +# will need to generate wheels for each Python version that you support. +universal=1 diff --git a/examples/src/python/example/python_distribution/hello/hello2/setup.py b/examples/src/python/example/python_distribution/hello/hello2/setup.py new file mode 100644 index 00000000000..d4c5cc21df6 --- /dev/null +++ b/examples/src/python/example/python_distribution/hello/hello2/setup.py @@ -0,0 +1,115 @@ +"""A setuptools based setup module. + +See: +https://packaging.python.org/en/latest/distributing.html +https://github.com/pypa/sampleproject +""" + +# Always prefer setuptools over distutils +from setuptools import setup, find_packages +# To use a consistent encoding +from codecs import open +from os import path +from distutils.core import Extension +here = path.abspath(path.dirname(__file__)) + +# Get the long description from the README file +with open(path.join(here, 'README.rst'), encoding='utf-8') as f: + long_description = f.read() + +c_module = Extension(str('super_greet'), sources=[str('c/super_greet.c')]) + +setup( + name='sample', + + # Versions should comply with PEP440. For a discussion on single-sourcing + # the version across setup.py and the project code, see + # https://packaging.python.org/en/latest/single_source_version.html + version='1.2.0', + ext_modules=[c_module], + description='A sample Python project', + long_description=long_description, + + # The project's main homepage. + url='https://github.com/pypa/sampleproject', + + # Author details + author='The Python Packaging Authority', + author_email='pypa-dev@googlegroups.com', + + # Choose your license + license='MIT', + + # See https://pypi.python.org/pypi?%3Aaction=list_classifiers + classifiers=[ + # How mature is this project? Common values are + # 3 - Alpha + # 4 - Beta + # 5 - Production/Stable + 'Development Status :: 3 - Alpha', + + # Indicate who your project is intended for + 'Intended Audience :: Developers', + 'Topic :: Software Development :: Build Tools', + + # Pick your license as you wish (should match "license" above) + 'License :: OSI Approved :: MIT License', + + # Specify the Python versions you support here. In particular, ensure + # that you indicate whether you support Python 2, Python 3 or both. + 'Programming Language :: Python :: 2', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.3', + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: 3.5', + ], + + # What does your project relate to? + keywords='sample setuptools development', + + # You can just specify the packages manually here if your project is + # simple. Or you can use find_packages(). + packages=find_packages(exclude=['contrib', 'docs', 'tests']), + + # Alternatively, if you want to distribute just a my_module.py, uncomment + # this: + # py_modules=["my_module"], + + # List run-time dependencies here. These will be installed by pip when + # your project is installed. For an analysis of "install_requires" vs pip's + # requirements files see: + # https://packaging.python.org/en/latest/requirements.html + #install_requires=['peppercorn'], + + # List additional groups of dependencies here (e.g. development + # dependencies). You can install these using the following syntax, + # for example: + # $ pip install -e .[dev,test] + extras_require={ + 'dev': ['check-manifest'], + 'test': ['coverage'], + }, + + # If there are data files included in your packages that need to be + # installed, specify them here. If using Python 2.6 or less, then these + # have to be included in MANIFEST.in as well. + package_data={ + 'sample': ['package_data.dat'], + }, + + # Although 'package_data' is the preferred approach, in some case you may + # need to place data files outside of your packages. See: + # http://docs.python.org/3.4/distutils/setupscript.html#installing-additional-files # noqa + # In this case, 'data_file' will be installed into '/my_data' + data_files=[('my_data', ['data/data_file'])], + + # To provide executable scripts, use entry points in preference to the + # "scripts" keyword. Entry points provide cross-platform support and allow + # pip to create the appropriate form of executable for the target platform. + entry_points={ + 'console_scripts': [ + 'sample=sample:main', + ], + }, +) diff --git a/examples/src/python/example/python_distribution/hello/hello2/tests/__init__.py b/examples/src/python/example/python_distribution/hello/hello2/tests/__init__.py new file mode 100644 index 00000000000..43d959a6d39 --- /dev/null +++ b/examples/src/python/example/python_distribution/hello/hello2/tests/__init__.py @@ -0,0 +1,3 @@ +# the inclusion of the tests module is not meant to offer best practices for +# testing in general, but rather to support the `find_packages` example in +# setup.py that excludes installing the "tests" package diff --git a/examples/src/python/example/python_distribution/hello/hello2/tests/test_simple.py b/examples/src/python/example/python_distribution/hello/hello2/tests/test_simple.py new file mode 100644 index 00000000000..61e44ebc659 --- /dev/null +++ b/examples/src/python/example/python_distribution/hello/hello2/tests/test_simple.py @@ -0,0 +1,7 @@ +# the inclusion of the tests module is not meant to offer best practices for +# testing in general, but rather to support the `find_packages` example in +# setup.py that excludes installing the "tests" package + + +def test_success(): + assert True diff --git a/examples/src/python/example/python_distribution/hello/hello2/tox.ini b/examples/src/python/example/python_distribution/hello/hello2/tox.ini new file mode 100644 index 00000000000..3d85a0da7d2 --- /dev/null +++ b/examples/src/python/example/python_distribution/hello/hello2/tox.ini @@ -0,0 +1,33 @@ +# this file is *not* meant to cover or endorse the use of tox or pytest or +# testing in general, +# +# It's meant to show the use of: +# +# - check-manifest +# confirm items checked into vcs are in your sdist +# - python setup.py check (using the readme_renderer extension) +# confirms your long_description will render correctly on pypi +# +# and also to help confirm pull requests to this project. + +[tox] +envlist = py{27,33,34} + +[testenv] +basepython = + py27: python2.7 + py33: python3.3 + py34: python3.4 +deps = + check-manifest + readme_renderer + flake8 + pytest +commands = + check-manifest --ignore tox.ini,tests* + python setup.py check -m -r -s + flake8 . + py.test tests +[flake8] +exclude = .tox,*.egg,build,data +select = E,W,F diff --git a/examples/src/python/example/python_distribution/hello/superhello/c/super_greet.c b/examples/src/python/example/python_distribution/hello/superhello/c/super_greet.c new file mode 100644 index 00000000000..0c5d955f32b --- /dev/null +++ b/examples/src/python/example/python_distribution/hello/superhello/c/super_greet.c @@ -0,0 +1,15 @@ + +#include + +static PyObject * super_greet(PyObject *self, PyObject *args) { + return Py_BuildValue("s", "Super hello"); +} + +static PyMethodDef Methods[] = { + {"super_greet", super_greet, METH_VARARGS, "A super greeting"}, + {NULL, NULL, 0, NULL} +}; + +PyMODINIT_FUNC initsuper_greet(void) { + (void) Py_InitModule("super_greet", Methods); +} diff --git a/examples/src/python/example/python_distribution/hello/superhello/cpp/greet.cpp b/examples/src/python/example/python_distribution/hello/superhello/cpp/greet.cpp deleted file mode 100644 index 0b54553ee45..00000000000 --- a/examples/src/python/example/python_distribution/hello/superhello/cpp/greet.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include - -string greet() { - return "Super hello"; -} - -// You are in charge of any adapter code in terms of translating C/C++ methods into Python -// You can optionally instead use an external library's helper methods, or you can manually define -// them in a c/cpp file like below - -PyObject* greet_impl(PyObject *) { - string h = greet(); - return h; -} - -static PyMethodDef greet_methods[] = { - // The first property is the name exposed to python, the second is the C++ function name - { "greet", (PyCFunction)greet_impl, METH_O, nullptr }, - - // Terminate the array with an object containing nulls. - { nullptr, nullptr, 0, nullptr } -}; - -PyMODINIT_FUNC PyInit_superhello() { - return PyModule_Create(&greet_module); -}