From 109fdc68cc20ee41c32642e14762f1d3657d64b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=A4=E6=9C=88?= <1724246050@qq.com> Date: Wed, 5 Mar 2025 13:16:56 +0800 Subject: [PATCH] Initial commit --- .github/workflows/release.yml | 31 +++++++ .github/workflows/test.yml | 27 ++++++ .gitignore | 14 +++ LICENSE.txt | 19 ++++ README.md | 39 ++++++++ noxfile.py | 53 +++++++++++ pyproject.toml | 162 ++++++++++++++++++++++++++++++++++ src/sample/__init__.py | 3 + src/sample/package_data.dat | 1 + src/sample/simple.py | 2 + tests/__init__.py | 2 + tests/test_simple.py | 17 ++++ 12 files changed, 370 insertions(+) create mode 100644 .github/workflows/release.yml create mode 100644 .github/workflows/test.yml create mode 100644 .gitignore create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 noxfile.py create mode 100644 pyproject.toml create mode 100644 src/sample/__init__.py create mode 100644 src/sample/package_data.dat create mode 100644 src/sample/simple.py create mode 100644 tests/__init__.py create mode 100644 tests/test_simple.py diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..5bde2fc --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,31 @@ +# this file is *not* meant to cover or endorse the use of GitHub Actions, but rather to +# help make automated releases for this project + +name: Release + +on: + push: + branches: + - main + +jobs: + build-and-publish: + runs-on: ubuntu-latest + permissions: + # IMPORTANT: this permission is mandatory for trusted publishing + id-token: write + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.x' + - name: Install build dependencies + run: python -m pip install -U setuptools wheel build + - name: Build + run: python -m build . + - name: Publish + uses: pypa/gh-action-pypi-publish@release/v1 + with: + skip-existing: true diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..64d0c4f --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,27 @@ +# this file is *not* meant to cover or endorse the use of GitHub Actions, but rather to +# help test this project + +name: Test + +on: [push, pull_request] + +jobs: + test: + strategy: + matrix: + python: ['3.9', '3.10', '3.11', '3.12', '3.13'] + platform: [ubuntu-latest, macos-latest, windows-latest] + runs-on: ${{ matrix.platform }} + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Set up Python ${{ matrix.python }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python }} + - name: Install test dependencies + run: python -m pip install --upgrade nox + - name: Test + run: python -m nox -s tests-${{ matrix.python }} + - name: Lint + run: python -m nox -s lint diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ddc2bd1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +# general things to ignore +build/ +dist/ +*.egg-info/ +*.egg +*.py[cod] +__pycache__/ +*.so +*~ +venv/ + +# due to using nox and pytest +.nox +.cache diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..c74aced --- /dev/null +++ b/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/README.md b/README.md new file mode 100644 index 0000000..d6c10eb --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# A sample Python project + +![Python Logo](https://www.python.org/static/community_logos/python-logo.png "Sample inline image") + +A sample project that exists as an aid to the [Python Packaging User +Guide][packaging guide]'s [Tutorial on Packaging and Distributing +Projects][distribution tutorial]. + +This project 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. + +[The source for this project is available here][src]. + +The metadata for a Python project is defined in the `pyproject.toml` file, +an example of which is included in this project. You should edit this file +accordingly to adapt this sample project to your needs. + +---- + +This is the README file for the project. + +The file should use UTF-8 encoding and can be written using +[reStructuredText][rst] or [markdown][md use] with the appropriate [key set][md +use]. 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. + +[packaging guide]: https://packaging.python.org +[distribution tutorial]: https://packaging.python.org/tutorials/packaging-projects/ +[src]: https://github.com/pypa/sampleproject +[rst]: http://docutils.sourceforge.net/rst.html +[md]: https://tools.ietf.org/html/rfc7764#section-3.5 "CommonMark variant" +[md use]: https://packaging.python.org/specifications/core-metadata/#description-content-type-optional diff --git a/noxfile.py b/noxfile.py new file mode 100644 index 0000000..d2ec8f7 --- /dev/null +++ b/noxfile.py @@ -0,0 +1,53 @@ +# this file is *not* meant to cover or endorse the use of nox 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 +# - readme_renderer (when using a reStructuredText README) +# confirms your long_description will render correctly on PyPI. +# +# and also to help confirm pull requests to this project. + +import os + +import nox + +nox.options.sessions = ["lint"] + +# Define the minimal nox version required to run +nox.options.needs_version = ">= 2024.3.2" + + +@nox.session +def lint(session): + session.install("flake8") + session.run( + "flake8", "--exclude", ".nox,*.egg,build,data", + "--select", "E,W,F", "." + ) + + +@nox.session +def build_and_check_dists(session): + session.install("build", "check-manifest >= 0.42", "twine") + # If your project uses README.rst, uncomment the following: + # session.install("readme_renderer") + + session.run("check-manifest", "--ignore", "noxfile.py,tests/**") + session.run("python", "-m", "build") + session.run("python", "-m", "twine", "check", "dist/*") + + +@nox.session(python=["3.9", "3.10", "3.11", "3.12", "3.13"]) +def tests(session): + session.install("pytest") + build_and_check_dists(session) + + generated_files = os.listdir("dist/") + generated_sdist = os.path.join("dist/", generated_files[1]) + + session.install(generated_sdist) + + session.run("py.test", "tests/", *session.posargs) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..d97bdab --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,162 @@ +# Guide (user-friendly): +# https://packaging.python.org/en/latest/guides/writing-pyproject-toml/ + +# Specification (technical, formal): +# https://packaging.python.org/en/latest/specifications/pyproject-toml/ + + +# Choosing a build backend: +# https://packaging.python.org/en/latest/tutorials/packaging-projects/#choosing-a-build-backend +[build-system] +# A list of packages that are needed to build your package: +requires = ["setuptools"] # REQUIRED if [build-system] table is used +# The name of the Python object that frontends will use to perform the build: +build-backend = "setuptools.build_meta" # If not defined, then legacy behavior can happen. + + +[project] +# This is the name of your project. The first time you publish this +# package, this name will be registered for you. It will determine how +# users can install this project, e.g.: +# +# $ pip install sampleproject +# +# And where it will live on PyPI: https://pypi.org/project/sampleproject/ +# +# There are some restrictions on what makes a valid project name +# specification here: +# https://packaging.python.org/specifications/core-metadata/#name +name = "sampleproject" # REQUIRED, is the only field that cannot be marked as dynamic. + +# Versions should comply with PEP 440: +# https://www.python.org/dev/peps/pep-0440/ +# +# For a discussion on single-sourcing the version, see +# https://packaging.python.org/guides/single-sourcing-package-version/ +version = "4.0.0" # REQUIRED, although can be dynamic + +# This is a one-line description or tagline of what your project does. This +# corresponds to the "Summary" metadata field: +# https://packaging.python.org/specifications/core-metadata/#summary +description = "A sample Python project" + +# This is an optional longer description of your project that represents +# the body of text which users will see when they visit PyPI. +# +# Often, this is the same as your README, so you can just read it in from +# that file directly. +# +# This field corresponds to the "Description" metadata field: +# https://packaging.python.org/specifications/core-metadata/#description-optional +readme = "README.md" + +# Specify which Python versions you support. In contrast to the +# 'Programming Language' classifiers in this file, 'pip install' will check this +# and refuse to install the project if the version does not match. See +# https://packaging.python.org/guides/distributing-packages-using-setuptools/#python-requires +requires-python = ">=3.9" + +# This is either text indicating the license for the distribution, or a file +# that contains the license. +# https://packaging.python.org/en/latest/specifications/core-metadata/#license +license = { file = "LICENSE.txt" } + +# This field adds keywords for your project which will appear on the +# project page. What does your project relate to? +# +# Note that this is a list of additional keywords, separated +# by commas, to be used to assist searching for the distribution in a +# larger catalog. +keywords = ["sample", "setuptools", "development"] + +# This should be your name or the name of the organization who originally +# authored the project, and a valid email address corresponding to the name +# listed. +authors = [{ name = "A. Random Developer", email = "author@example.com" }] + +# This should be your name or the names of the organization who currently +# maintains the project, and a valid email address corresponding to the name +# listed. +maintainers = [ + { name = "A. Great Maintainer", email = "maintainer@example.com" }, +] + +# Classifiers help users find your project by categorizing it. +# +# For a list of valid classifiers, see https://pypi.org/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 + "License :: OSI Approved :: MIT License", + + # Specify the Python versions you support here. In particular, ensure + # that you indicate you support Python 3. These classifiers are *not* + # checked by "pip install". See instead "requires-python" key in this file. + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3 :: Only", +] + +# This field lists other packages that your project depends on to run. +# Any package you put here will be installed by pip when your project is +# installed, so they must be valid existing projects. +# +# For an analysis of this field vs pip's requirements files see: +# https://packaging.python.org/discussions/install-requires-vs-requirements/ +dependencies = ["peppercorn"] + +# List additional groups of dependencies here (e.g. development +# dependencies). Users will be able to install these using the "extras" +# syntax, for example: +# +# $ pip install sampleproject[dev] +# +# Optional dependencies the project provides. These are commonly +# referred to as "extras". For a more extensive definition see: +# https://packaging.python.org/en/latest/specifications/dependency-specifiers/#extras +[project.optional-dependencies] +dev = ["check-manifest"] +test = ["coverage"] + +# List URLs that are relevant to your project +# +# This field corresponds to the "Project-URL" and "Home-Page" metadata fields: +# https://packaging.python.org/specifications/core-metadata/#project-url-multiple-use +# https://packaging.python.org/specifications/core-metadata/#home-page-optional +# +# Examples listed include a pattern for specifying where the package tracks +# issues, where the source is hosted, where to say thanks to the package +# maintainers, and where to support the project financially. The key is +# what's used to render the link text on PyPI. +[project.urls] +"Homepage" = "https://github.com/pypa/sampleproject" +"Bug Reports" = "https://github.com/pypa/sampleproject/issues" +"Funding" = "https://donate.pypi.org" +"Say Thanks!" = "http://saythanks.io/to/example" +"Source" = "https://github.com/pypa/sampleproject/" + +# The following would provide a command line executable called `sample` +# which executes the function `main` from this package when invoked. +[project.scripts] +sample = "sample:main" + + +# This is configuration specific to the `setuptools` build backend. +# If you are using a different build backend, you will need to change this. +[tool.setuptools] +# If there are data files included in your packages that need to be +# installed, specify them here. +package-data = { "sample" = ["*.dat"] } diff --git a/src/sample/__init__.py b/src/sample/__init__.py new file mode 100644 index 0000000..c8f1064 --- /dev/null +++ b/src/sample/__init__.py @@ -0,0 +1,3 @@ +def main(): + """Entry point for the application script""" + print("Call your main application code here") diff --git a/src/sample/package_data.dat b/src/sample/package_data.dat new file mode 100644 index 0000000..7c0646b --- /dev/null +++ b/src/sample/package_data.dat @@ -0,0 +1 @@ +some data \ No newline at end of file diff --git a/src/sample/simple.py b/src/sample/simple.py new file mode 100644 index 0000000..c929f88 --- /dev/null +++ b/src/sample/simple.py @@ -0,0 +1,2 @@ +def add_one(number): + return number + 1 diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..4eef093 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,2 @@ +# The inclusion of the tests module is not meant to offer best practices for +# testing in general. diff --git a/tests/test_simple.py b/tests/test_simple.py new file mode 100644 index 0000000..b7a0ee3 --- /dev/null +++ b/tests/test_simple.py @@ -0,0 +1,17 @@ +# 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 + +import unittest + +from sample.simple import add_one + + +class TestSimple(unittest.TestCase): + + def test_add_one(self): + self.assertEqual(add_one(5), 6) + + +if __name__ == '__main__': + unittest.main()