Passed
Push — master ( 35438f...8fb3c8 )
by Oleksandr
01:09
created

setup   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 111
dl 0
loc 167
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A maybe_get_shell_output() 0 7 3
A maybe_get_current_commit_hash() 0 2 1
C parse_requirements() 0 39 9
A maybe_get_current_branch_name() 0 2 1
1
import itertools
2
import os
3
import shlex
4
import sys
5
6
if sys.version_info >= (3, 9):
7
  List  = list
8
  Tuple = tuple
9
else:
10
  from typing import List
11
  from typing import Tuple
12
13
from pathlib import Path
14
from setuptools import setup
15
from subprocess import check_output
16
from typing import Optional
17
18
19
__here__ = Path(__file__).absolute().parent
20
21
22
version_file_path = __here__ / "il2fb" / "regiments" / "version.py"
23
exec(compile(version_file_path.read_text(encoding="utf-8"), version_file_path, "exec"))
24
25
26
def maybe_get_shell_output(command: str) -> str:
27
  try:
28
    args = shlex.split(command)
29
    with open(os.devnull, "w") as devnull:
30
      return check_output(args, stderr=devnull).strip().decode()
31
  except Exception:
32
    pass
33
34
35
def maybe_get_current_branch_name() -> Optional[str]:
36
  return maybe_get_shell_output("git rev-parse --abbrev-ref HEAD")
37
38
39
def maybe_get_current_commit_hash() -> Optional[str]:
40
  return maybe_get_shell_output("git rev-parse --short HEAD")
41
42
43
def parse_requirements(file_path: Path) -> Tuple[List[str], List[str]]:
44
  requirements, dependencies = list(), list()
45
46
  if not file_path.exists():
47
    return requirements, dependencies
48
49
  with file_path.open("rt") as f:
50
    for line in f:
51
      line = line.strip()
52
53
      # check if is comment or empty
54
      if not line or line.startswith("#"):
55
        continue
56
57
      # check if is URL
58
      if "://" in line:
59
        dependencies.append(line)
60
61
        egg = line.split("#egg=", 1)[1]
62
63
        # check if version is specified
64
        if "-" in egg:
65
          egg = egg.rsplit("-", 1)[0]
66
67
        requirements.append(egg)
68
69
      # check if is inclusion of other requirements file
70
      elif line.startswith("-r"):
71
        name = Path(line.split(" ", 1)[1])
72
        path = file_path.parent / name
73
        subrequirements, subdependencies = parse_requirements(path)
74
        requirements.extend(subrequirements)
75
        dependencies.extend(subdependencies)
76
77
      # assume is a standard requirement
78
      else:
79
        requirements.append(line)
80
81
  return requirements, dependencies
82
83
84
README    = (__here__ / "README.rst"   ).read_text(encoding="utf-8")
85
CHANGELOG = (__here__ / "CHANGELOG.rst").read_text(encoding="utf-8")
86
87
STABLE_BRANCH_NAME  = "master"
88
CURRENT_COMMIT_HASH = maybe_get_current_commit_hash()
89
CURRENT_BRANCH_NAME = maybe_get_current_branch_name()
90
IS_CURRENT_BRANCH_STABLE = (CURRENT_BRANCH_NAME == STABLE_BRANCH_NAME)
91
BUILD_TAG = (
92
  f".{CURRENT_BRANCH_NAME}.{CURRENT_COMMIT_HASH}"
93
  if not IS_CURRENT_BRANCH_STABLE and CURRENT_COMMIT_HASH
94
  else ""
95
)
96
97
REQUIREMENTS_DIR_PATH = __here__ / "requirements"
98
99
INSTALL_REQUIREMENTS, INSTALL_DEPENDENCIES = parse_requirements(
100
  file_path=(REQUIREMENTS_DIR_PATH / "dist.txt"),
101
)
102
SETUP_REQUIREMENTS, SETUP_DEPENDENCIES = parse_requirements(
103
  file_path=(REQUIREMENTS_DIR_PATH / "setup.txt"),
104
)
105
TEST_REQUIREMENTS, TEST_DEPENDENCIES = parse_requirements(
106
  file_path=(REQUIREMENTS_DIR_PATH / "test.txt"),
107
)
108
109
110
setup(
111
  name="il2fb-regiments",
112
  version=VERSION,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable VERSION does not seem to be defined.
Loading history...
113
  description=(
114
    "Access data about regiments from «IL-2 Sturmovik: Forgotten Battles» "
115
    "flight simulator"
116
  ),
117
  long_description=README + "\n\n" + CHANGELOG,
118
  long_description_content_type="text/x-rst",
119
  keywords=[
120
    "il2", "il-2", "fb", "forgotten battles", "regiments",
121
  ],
122
  license="MIT",
123
  url=f"https://github.com/IL2HorusTeam/il2fb-regiments/tree/v{VERSION}",
124
125
  author="Oleksandr Oblovatnyi",
126
  author_email="[email protected]",
127
128
  packages=[
129
    "il2fb.regiments",
130
  ],
131
  namespace_packages=[
132
    "il2fb",
133
  ],
134
  include_package_data=True,
135
136
  python_requires=">=3.7",
137
  dependency_links=list(set(itertools.chain(
138
    INSTALL_DEPENDENCIES,
139
    SETUP_DEPENDENCIES,
140
    TEST_DEPENDENCIES,
141
  ))),
142
  install_requires=INSTALL_REQUIREMENTS,
143
  setup_requires=SETUP_REQUIREMENTS,
144
  tests_require=TEST_REQUIREMENTS,
145
  test_suite="tests",
146
147
  classifiers=[
148
    "Development Status :: 5 - Production/Stable",
149
    "Intended Audience :: Developers",
150
    "License :: OSI Approved :: MIT License",
151
    "Natural Language :: English",
152
    "Operating System :: MacOS :: MacOS X",
153
    "Operating System :: Microsoft :: Windows",
154
    "Operating System :: POSIX",
155
    "Programming Language :: Python :: 3",
156
    "Topic :: Software Development :: Libraries",
157
  ],
158
159
  options={
160
    'egg_info': {
161
      'tag_build': BUILD_TAG,
162
      'tag_date':  False,
163
    },
164
  },
165
166
  zip_safe=False,
167
)
168