1#!/usr/bin/env python3
2#
3# Copyright (c) 2021 Nordic Semiconductor ASA
4#
5# SPDX-License-Identifier: Apache-2.0
6#
7
8import setuptools
9from pathlib import Path
10from re import sub
11
12
13P_REPO_ROOT = Path(__file__).absolute().parents[0]
14ZCBOR_URL = 'https://github.com/NordicSemiconductor/zcbor'
15
16
17def absolute_links(text):
18    def new_link(match):
19        match_text = match.group(0)
20        path = Path(P_REPO_ROOT, match_text)
21        try:
22            path_exists = path.exists()
23        except OSError:
24            path_exists = False
25        if path_exists:
26            path_comp = "tree" if path.is_dir() else "blob"
27            url_prefix = f"{ZCBOR_URL}/{path_comp}/{get_version()}/"
28            return url_prefix + match_text
29        else:
30            return match_text
31    new_text = sub(r"(?<=\]\().*?(?=\))", new_link, text)
32    return new_text
33
34
35def get_description(title_only=False):
36    """Use the README as the long description that appears on PyPI."""
37    p_readme = Path(P_REPO_ROOT, 'README.md').absolute()
38
39    if p_readme.is_file():
40        with p_readme.open(encoding='utf-8') as f:
41            return f.readline().strip() if title_only else absolute_links(f.read())
42    return ''
43
44
45def get_dependencies():
46    """Extract base requirement packages from file."""
47    p_base_reqs = Path(
48        P_REPO_ROOT, 'scripts', 'requirements-base.txt').absolute()
49
50    l_dependencies = list()
51    if p_base_reqs.is_file():
52        with p_base_reqs.open(encoding='utf-8') as f:
53            for line in f.readlines():
54                l_dependencies.append(line.strip())
55    return l_dependencies
56
57
58def get_version():
59    p_version = Path(P_REPO_ROOT, 'zcbor', 'VERSION').absolute()
60    return p_version.read_text(encoding='utf-8').strip()
61
62
63setuptools.setup(
64    name='zcbor',
65    version=get_version(),
66    description=get_description(title_only=True),
67    long_description=get_description(),
68    long_description_content_type='text/markdown',
69    url=ZCBOR_URL,
70    author='Nordic Semiconductor ASA',
71    license='Apache Software License',
72    classifiers=[
73        "Development Status :: 4 - Beta",
74        "License :: OSI Approved :: Apache Software License",
75        "Programming Language :: Python :: 3",
76        "Topic :: Software Development :: Build Tools",
77    ],
78    python_requires='>=3.7',
79    packages=setuptools.find_packages(),
80    install_requires=get_dependencies(),
81    entry_points={
82        'console_scripts': ['zcbor=zcbor.zcbor:main'],
83    },
84    include_package_data=True,
85    package_data={'': ['VERSION', 'cddl/prelude.cddl']},
86    data_files=[
87        ("lib/zcbor/include",
88            ("include/zcbor_decode.h", "include/zcbor_encode.h", "include/zcbor_common.h",
89                "include/zcbor_tags.h", "include/zcbor_debug.h")),
90        ("lib/zcbor/src", ("src/zcbor_decode.c", "src/zcbor_encode.c", "src/zcbor_common.c"))],
91)
92