|
| 1 | +Packaging |
| 2 | +######### |
| 3 | +Python packages are used to organize and structure a Python library containing several modules and assets such as examples or binary extensions. |
| 4 | +They offer an easy, reliable and comprehensive way to distribute and install |
| 5 | +Python libraries on a variety of platforms and environments. |
| 6 | + |
| 7 | +Namespace Packaging |
| 8 | +------------------- |
| 9 | +PyAnsys libraries use the `namespace packaging`_. |
| 10 | +Namespace packages allow the user to easily split subpackages from a package into |
| 11 | +a single and an independent distribution. |
| 12 | + |
| 13 | +Three different approaches are currently available to create a namespace package: |
| 14 | + |
| 15 | +* `native namespace packages`_ |
| 16 | +* pkgutil-style namespace packages |
| 17 | +* pkg_resources-style namespace packages |
| 18 | + |
| 19 | +Required files |
| 20 | +-------------- |
| 21 | + |
| 22 | +* README.rst file is used to describe the purpose of the package. |
| 23 | + *The format of this file must be reStructuredText.* |
| 24 | + |
| 25 | +* LICENSE file to specify the copyrigths and required authorization. |
| 26 | + |
| 27 | +* setup.py file to provide package main information. |
| 28 | + The presence of this file indicate that the package was likely created using disutils |
| 29 | + which is the Python standard for building and distributing python package. |
| 30 | + |
| 31 | + |
| 32 | +Setup File |
| 33 | +---------- |
| 34 | +The `setup.py`_ file is the build script for ``setuptools``. It exposes dynamic metadata and contains |
| 35 | +package's main information such as description, author, and version. |
| 36 | +In this file, the ``setuptools`` module will be used to configure the metadata as opposed to ``distutils``, |
| 37 | + |
| 38 | +.. code:: python |
| 39 | +
|
| 40 | + import setuptools |
| 41 | + setuptools.setup(...) |
| 42 | +
|
| 43 | +This file gathers all namespace packages and files that must be included in the distributed |
| 44 | +package. |
| 45 | + |
| 46 | +.. code:: python |
| 47 | +
|
| 48 | + packages = [] |
| 49 | + for package in setuptools.find_namespace_packages(include='ansys*'): |
| 50 | + if package.startswith('ansys.tools.example_coverage'): |
| 51 | + packages.append(package) |
| 52 | +
|
| 53 | +
|
| 54 | +It also extracts the version number from the ``_version.py`` located in the ``ansys/<product>/library`` directory of the source code. |
| 55 | + |
| 56 | + |
| 57 | +Generate the Package and Upload it on PyPI |
| 58 | +------------------------------------------ |
| 59 | + |
| 60 | +The first time you want to upload a package on PyPI under the `ansys <https://pypi.org/user/ansys/>`_ account, you must perform the following |
| 61 | +process manually: |
| 62 | + |
| 63 | +Create the python package. |
| 64 | + |
| 65 | +.. code:: |
| 66 | +
|
| 67 | + python setup.py sdist |
| 68 | +
|
| 69 | +Verify the distribution's long description rendering with ``twine``. |
| 70 | + |
| 71 | +.. code:: |
| 72 | +
|
| 73 | + pip install twine |
| 74 | + twine check dist/* |
| 75 | +
|
| 76 | +Upload the package to PyPI using `` twine`` using the upload token generated for the `` ansys`` PyPI account. Contact [email protected] for the token. |
| 77 | + |
| 78 | +.. code:: |
| 79 | +
|
| 80 | + python -m twine upload -u __token__ -p <TOKEN_FOR_PYPI> --skip-existing dist/* |
| 81 | +
|
| 82 | +Then, for the next release upload, you can do it through the CI/CD workflow after generating a token just for this package. |
| 83 | +Create a `secret`_ in GitHub settings. |
| 84 | +Name it ``PYPI_TOKEN`` and assign it the token provided by PyPI. |
| 85 | +This token will be reused in the CI/CD workflow handling the package distribution. |
| 86 | + |
| 87 | +Tag a Release |
| 88 | +------------- |
| 89 | +In order to deploy a new package on PyPI, you must tag a release under a release branch. The PyAnsys project follows the `trunk-based development`_ source-control branching model, where the main development branch is always in a releasable state. |
| 90 | +To tag the release, you must update your main local branch. |
| 91 | + |
| 92 | +.. code:: |
| 93 | +
|
| 94 | + git checkout main |
| 95 | + git pull |
| 96 | +
|
| 97 | +Then, create the new release branch |
| 98 | + |
| 99 | +.. code:: |
| 100 | +
|
| 101 | + git checkout -b release/MAJOR.MINOR |
| 102 | +
|
| 103 | +Bump the version number in the ``_version`` file to ``MAJOR.MINOR.PATCH``. |
| 104 | +Commit and push your changes and then create the tag: |
| 105 | + |
| 106 | +.. code:: |
| 107 | +
|
| 108 | + git commit -am "Increase version to v<MAJOR.MINOR.PATCH>" |
| 109 | + git tag v<MAJOR.MINOR.PATCH> |
| 110 | + git push --tags |
| 111 | +
|
| 112 | +Finally, following this tag creation, the workflow responsible for the distribution |
| 113 | +will be automatically triggered. |
| 114 | + |
| 115 | +Install a package |
| 116 | +----------------- |
| 117 | + |
| 118 | +.. code:: |
| 119 | +
|
| 120 | + pip install ansys.<product>.<library> |
| 121 | +
|
| 122 | +Here is the minimal content of your python project to create a package complying with the above standards. |
| 123 | + |
| 124 | +.. code:: |
| 125 | +
|
| 126 | + ansys/<product>/<library>/__init__.py |
| 127 | + LICENSE |
| 128 | + README.rst |
| 129 | + setup.py |
| 130 | + tests/ |
| 131 | +
|
| 132 | +
|
| 133 | +.. _namespace packaging: https://packaging.python.org/guides/packaging-namespace-packages/ |
| 134 | +.. _native namespace packages: https://packaging.python.org/guides/packaging-namespace-packages/#native-namespace-packages |
| 135 | +.. _trunk-based development: https://trunkbaseddevelopment.com/ |
| 136 | +.. _secret: https://docs.github.com/en/actions/reference/encrypted-secrets |
| 137 | +.. _setup.py: https://packaging.python.org/tutorials/packaging-projects/#configuring-metadata |
0 commit comments