Skip to content

Commit

Permalink
Move from setup.py to pyproject.toml for creation of zcbor packages
Browse files Browse the repository at this point in the history
setup.py is deprecated.

This involved restructuring a bit to also avoid the data_files
feature which was used to include the src and include directories,
but which is discouraged/deprecated, and needed lots of resolving
logic in zcbor to locate in different types of installations.
  • Loading branch information
oyvindronningstad committed Dec 7, 2023
1 parent 89a9d0f commit b679ccf
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 171 deletions.
8 changes: 4 additions & 4 deletions .github/actions/install_zcbor/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ runs:
shell: sh
run: |
pip3 install -U pip
pip3 install -U setuptools
pip3 install -U build
pip3 install -U -r scripts/requirements.txt
- name: Install zcbor package
if: ${{ inputs.zcbor_package == 'bdist_wheel' }}
shell: sh
run: |
python3 setup.py sdist bdist_wheel
python3 -m build
pip3 install dist/zcbor-*.tar.gz
pip3 uninstall -y zcbor
pip3 install dist/zcbor-*.whl
Expand All @@ -27,10 +27,10 @@ runs:
if: ${{ inputs.zcbor_package == 'setup_install' }}
shell: sh
run: |
python3 setup.py install
pip3 install .
- name: Install zcbor package
if: ${{ inputs.zcbor_package == 'setup_develop' }}
shell: sh
run: |
python3 setup.py develop
pip3 install -e .
5 changes: 2 additions & 3 deletions .github/workflows/run-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,12 @@ jobs:
- name: Install west and dependencies
run: |
pip install -U pip
pip install -U setuptools
pip install -U wheel
pip install -U build
pip install -U -r scripts/requirements.txt
- name: Generate and install zcbor package
run: |
python setup.py bdist_wheel
python build bdist_wheel
pip install dist/zcbor-0.7.99-py3-none-any.whl
pip uninstall -y zcbor
pip install -e .
Expand Down
1 change: 0 additions & 1 deletion MANIFEST.in

This file was deleted.

12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,8 @@ options:
-c CDDL, --cddl CDDL Path to one or more input CDDL file(s). Passing
multiple files is equivalent to concatenating them.
--no-prelude Exclude the standard CDDL prelude from the build. The
prelude can be viewed at zcbor/cddl/prelude.cddl in
the repo, or together with the script.
prelude can be viewed at zcbor/prelude.cddl in the
repo, or together with the script.
-v, --verbose Print more information while parsing CDDL and
generating code.
--default-max-qty DEFAULT_MAX_QTY, --dq DEFAULT_MAX_QTY
Expand Down Expand Up @@ -540,8 +540,8 @@ options:
-c CDDL, --cddl CDDL Path to one or more input CDDL file(s). Passing
multiple files is equivalent to concatenating them.
--no-prelude Exclude the standard CDDL prelude from the build. The
prelude can be viewed at zcbor/cddl/prelude.cddl in
the repo, or together with the script.
prelude can be viewed at zcbor/prelude.cddl in the
repo, or together with the script.
-v, --verbose Print more information while parsing CDDL and
generating code.
-i INPUT, --input INPUT
Expand Down Expand Up @@ -593,8 +593,8 @@ options:
-c CDDL, --cddl CDDL Path to one or more input CDDL file(s). Passing
multiple files is equivalent to concatenating them.
--no-prelude Exclude the standard CDDL prelude from the build. The
prelude can be viewed at zcbor/cddl/prelude.cddl in
the repo, or together with the script.
prelude can be viewed at zcbor/prelude.cddl in the
repo, or together with the script.
-v, --verbose Print more information while parsing CDDL and
generating code.
-i INPUT, --input INPUT
Expand Down
14 changes: 14 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env python3
#
# Copyright (c) 2021 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: Apache-2.0
#

from pathlib import Path

from .zcbor.zcbor import (
CddlValidationError,
DataTranslator,
main
)
40 changes: 40 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[build-system]
requires = ['setuptools']
build-backend = 'setuptools.build_meta'

[project]
name = 'zcbor'
description = 'Code generation and validation using CDDL schemas'
readme = 'README.md'
license = {text = 'Apache'}
requires-python = ">=3.8"
classifiers = [
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'License :: OSI Approved :: Apache Software License',
'Programming Language :: Python :: 3',
'Programming Language :: C',
'Topic :: File Formats :: JSON :: JSON Schema',
'Topic :: Software Development :: Build Tools',
'Topic :: Software Development :: Code Generators',
]
authors = [{name = 'Nordic Semiconductor ASA'}]
maintainers = [{name = 'Øyvind Rønningstad', email = '[email protected]'}]
dynamic = ['dependencies', 'version']

[project.urls]
Homepage = 'https://github.com/NordicSemiconductor/zcbor'

[project.scripts]
zcbor = 'zcbor:main'

[tool.setuptools]
packages = ['zcbor', 'zcbor.src', 'zcbor.include', 'zcbor.zcbor']
package-dir = {zcbor = '.'}

[tool.setuptools.package-data]
zcbor = ['src/*', 'include/*', 'zcbor/*']

[tool.setuptools.dynamic]
version = {file = 'zcbor/VERSION'}
dependencies = {file = ['scripts/requirements-base.txt']}
2 changes: 1 addition & 1 deletion add_helptext.py → scripts/add_helptext.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pathlib import Path
from sys import argv

p_root = Path(__file__).absolute().parents[0]
p_root = Path(__file__).absolute().parents[1]
p_README = Path(p_root, 'README.md')

pattern = r"""
Expand Down
91 changes: 0 additions & 91 deletions setup.py

This file was deleted.

7 changes: 3 additions & 4 deletions tests/scripts/test_repo_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
p_readme = p_root / "README.md"
p_architecture = p_root / "ARCHITECTURE.md"
p_release_notes = p_root / "RELEASE_NOTES.md"
p_init_py = p_root / 'zcbor' / '__init__.py'
p_init_py = p_root / '__init__.py'
p_zcbor_py = p_root / 'zcbor' / 'zcbor.py'
p_setup_py = p_root / 'setup.py'
p_add_helptext = p_root / 'add_helptext.py'
p_add_helptext = p_root / 'scripts' / 'add_helptext.py'
p_test_zcbor_py = p_tests / 'scripts' / 'test_zcbor.py'
p_test_versions_py = p_tests / 'scripts' / 'test_versions.py'
p_test_repo_files_py = p_tests / 'scripts' / 'test_repo_files.py'
Expand All @@ -51,7 +50,7 @@ def do_codestyle(self, files, **kwargs):

def test_codestyle(self):
"""Run codestyle tests on all Python scripts in the repo."""
self.do_codestyle([p_init_py, p_setup_py, p_test_versions_py, p_test_repo_files_py])
self.do_codestyle([p_init_py, p_test_versions_py, p_test_repo_files_py, p_add_helptext])
self.do_codestyle([p_zcbor_py], ignore=['W191', 'E101', 'W503'])
self.do_codestyle([p_test_zcbor_py], ignore=['E402', 'E501', 'W503'])

Expand Down
4 changes: 2 additions & 2 deletions tests/scripts/test_zcbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
except ImportError:
print("""
The zcbor package must be installed to run these tests.
During development, install with `python3 setup.py develop` to install in a way
During development, install with `pip3 install -e .` to install in a way
that picks up changes in the files without having to reinstall.
""")
exit(1)
Expand All @@ -48,7 +48,7 @@
p_yaml_compat_cddl = Path(p_tests, 'cases', 'yaml_compatibility.cddl')
p_yaml_compat_yaml = Path(p_tests, 'cases', 'yaml_compatibility.yaml')
p_README = Path(p_root, 'README.md')
p_prelude = Path(p_root, 'zcbor', 'cddl', 'prelude.cddl')
p_prelude = Path(p_root, 'zcbor', 'prelude.cddl')


class TestManifest(TestCase):
Expand Down
19 changes: 0 additions & 19 deletions zcbor/__init__.py

This file was deleted.

File renamed without changes.
53 changes: 13 additions & 40 deletions zcbor/zcbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,20 @@
import sys
from site import USER_BASE
from textwrap import wrap, indent

from importlib.metadata import version

regex_cache = {}
indentation = "\t"
newl_ind = "\n" + indentation

P_SCRIPT = Path(__file__).absolute().parent
P_REPO_ROOT = Path(__file__).absolute().parents[1]
VERSION_path = Path(P_SCRIPT, "VERSION")
PRELUDE_path = Path(P_SCRIPT, "cddl", "prelude.cddl")
SCRIPT_PATH = Path(__file__).absolute().parent
PACKAGE_PATH = Path(__file__).absolute().parents[1]
PRELUDE_PATH = SCRIPT_PATH / "prelude.cddl"
VERSION_PATH = SCRIPT_PATH / "VERSION"
C_SRC_PATH = PACKAGE_PATH / "src"
C_INCLUDE_PATH = PACKAGE_PATH / "include"

__version__ = VERSION_path.read_text(encoding="utf-8").strip()
__version__ = VERSION_PATH.read_text(encoding="utf-8").strip()

UINT8_MAX = 0xFF
UINT16_MAX = 0xFFFF
Expand All @@ -61,35 +63,6 @@ def getrp(pattern, flags=0):
return regex_cache[pattern_key]


def is_relative_to(path1, path2):
try:
path1.relative_to(path2)
except ValueError:
return False
return True


# The root of the non-generated c code (<c_code_root>/src and <c_code_root>/include)
if Path(__file__).name in sys.argv[0]:
# Running the script directly in the repo.
c_code_root = P_REPO_ROOT
elif any((getrp(r"zcbor-.*\.egg").match(p) for p in P_SCRIPT.parts)):
# Installed via setup.py install
c_code_root = Path(P_SCRIPT.parent, "lib", "zcbor")
elif is_relative_to(P_SCRIPT, (Path(sys.prefix, "local"))):
# Installed via pip as root.
c_code_root = Path(sys.prefix, "local", "lib", "zcbor")
elif is_relative_to(P_SCRIPT, (Path(sys.prefix))):
# Installed via pip as root.
c_code_root = Path(sys.prefix, "lib", "zcbor")
elif is_relative_to(P_SCRIPT, (Path(USER_BASE))):
# Installed via pip as user.
c_code_root = Path(USER_BASE, "lib", "zcbor")
else:
# Don't know where the C code is. Assume we are in the repo.
c_code_root = P_REPO_ROOT


# Size of "additional" field if num is encoded as int
def sizeof(num):
if num <= 23:
Expand Down Expand Up @@ -2852,7 +2825,7 @@ def parse_args():
parent_parser.add_argument(
"--no-prelude", required=False, action="store_true", default=False,
help=f"""Exclude the standard CDDL prelude from the build. The prelude can be viewed at
{PRELUDE_path.relative_to(P_REPO_ROOT)} in the repo, or together with the script.""")
{PRELUDE_PATH.relative_to(PACKAGE_PATH)} in the repo, or together with the script.""")
parent_parser.add_argument(
"-v", "--verbose", required=False, action="store_true", default=False,
help="Print more information while parsing CDDL and generating code.")
Expand Down Expand Up @@ -3027,7 +3000,7 @@ def parse_args():
args = parser.parse_args()

if not args.no_prelude:
args.cddl.append(open(PRELUDE_path, 'r', encoding="utf-8"))
args.cddl.append(open(PRELUDE_PATH, 'r', encoding="utf-8"))

if hasattr(args, "decode") and not args.decode and not args.encode:
parser.error("Please specify at least one of --decode or --encode.")
Expand Down Expand Up @@ -3069,7 +3042,7 @@ def process_code(args):
if "zcbor.py" in sys.argv[0]:
git_args = ['git', 'rev-parse', '--verify', '--short', 'HEAD']
git_sha = Popen(
git_args, cwd=P_REPO_ROOT, stdout=PIPE).communicate()[0].decode('utf-8').strip()
git_args, cwd=PACKAGE_PATH, stdout=PIPE).communicate()[0].decode('utf-8').strip()
else:
git_sha = __version__

Expand Down Expand Up @@ -3115,8 +3088,8 @@ def add_mode_to_fname(filename, mode):
file_header=args.file_header
)

c_code_dir = Path(c_code_root, "src")
h_code_dir = Path(c_code_root, "include")
c_code_dir = C_SRC_PATH
h_code_dir = C_INCLUDE_PATH

if args.copy_sources:
new_c_code_dir = out_c_parent
Expand Down

0 comments on commit b679ccf

Please sign in to comment.