Passed
Push — master ( 017a18...c71ac2 )
by Oleksandr
49s
created

setup.maybe_get_shell_output()   A

Complexity

Conditions 3

Size

Total Lines 7
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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