File: __init__.py

package info (click to toggle)
depthcharge-tools 0.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 760 kB
  • sloc: python: 6,286; sh: 650; makefile: 12
file content (73 lines) | stat: -rw-r--r-- 1,950 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#! /usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-or-later

# depthcharge-tools __init__.py
# Copyright (C) 2020-2021 Alper Nebi Yasak <alpernebiyasak@gmail.com>
# See COPYRIGHT and LICENSE files for full copyright information.

import glob
import logging
import pathlib
import re
import subprocess
from importlib import metadata, resources

from packaging.version import InvalidVersion, Version

logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())


def get_version():
    version = None
    pkg_path = resources.files(__name__)

    try:
        version = metadata.version(__name__)

    except metadata.PackageNotFoundError:
        if isinstance(pkg_path, pathlib.Path):
            setup_py = pkg_path.parent / "setup.py"
            if setup_py.exists():
                version = re.findall(
                    'version=(\'.+\'|".+"),',
                    setup_py.read_text(),
                )[0].strip('"\'')

    if (
        isinstance(pkg_path, pathlib.Path)
        and (pkg_path.parent / ".git").exists()
    ):
        proc = subprocess.run(
            ["git", "-C", pkg_path, "describe"],
            stdout=subprocess.PIPE,
            encoding="utf-8",
            check=False,
        )
        if proc.returncode == 0:
            tag, *local = proc.stdout.split("-")

            if local:
                git_version = "{}+{}".format(tag, ".".join(local))
            else:
                git_version = tag
            try:
                return Version(git_version)
            except InvalidVersion:
                pass

    if version is not None:
        return Version(version)

__version__ = get_version()

config_ini = resources.files(__name__).joinpath("config.ini").read_text()

boards_ini = resources.files(__name__).joinpath("boards.ini").read_text()

config_files = [
    *glob.glob("/etc/depthcharge-tools/config"),
    *glob.glob("/etc/depthcharge-tools/config.d/*"),
]