-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsetup.py
132 lines (116 loc) · 4.36 KB
/
setup.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import contextlib
import os
import re
import subprocess
from setuptools import setup
from setuptools.command.sdist import sdist
DATA_ROOTS = []
PROJECT = 'pytest-raises'
VERSION_FILE = 'pytest_raises/__init__.py'
def _get_output_or_none(args):
try:
return subprocess.check_output(args).decode('utf-8').strip()
except subprocess.CalledProcessError:
return None
def _get_git_description():
return _get_output_or_none(['git', 'describe'])
def _get_git_branches_for_this_commit():
branches = _get_output_or_none(['git', 'branch', '-r', '--contains', 'HEAD'])
split = branches.split('\n') if branches else []
return [branch.strip() for branch in split]
def _is_on_releasable_branch(branches):
return any([branch == 'origin/master' or branch.startswith('origin/hotfix') for branch in branches])
def _git_to_version(git):
match = re.match(r'(?P<tag>[\d\.]+)-(?P<offset>[\d]+)-(?P<sha>\w{8})', git)
if not match:
version = git
else:
version = "{tag}.post0.dev{offset}".format(**match.groupdict())
return version
def _get_version_from_git():
git_description = _get_git_description()
git_branches = _get_git_branches_for_this_commit()
version = _git_to_version(git_description) if git_description else None
if git_branches and not _is_on_releasable_branch(git_branches):
print("Forcing version to 0.0.1 because this commit is on branches {} and not a whitelisted branch".format(git_branches))
version = '0.0.1'
return version
VERSION_REGEX = re.compile(r'__version__ = "(?P<version>[\w\.]+)"')
def _get_version_from_file():
with open(VERSION_FILE, 'r') as f:
content = f.read()
match = VERSION_REGEX.match(content)
if not match:
raise Exception("Failed to pull version out of '{}'".format(content))
version = match.group(1)
return version
@contextlib.contextmanager
def write_version():
# pylint: disable=fixme
# TODO: don't use `git` on GitHub Actions, tags are not pulled
# with history included (`git describe` will fail).
# version = _get_version_from_git()
# if version:
# with open(VERSION_FILE, 'r') as version_file:
# old_contents = version_file.read()
# with open(VERSION_FILE, 'w') as version_file:
# new_contents = '__version__ = "{}"\n'.format(version)
# version_file.write(new_contents)
# print("Wrote {} with {}".format(VERSION_FILE, new_contents))
yield
# if version:
# with open(VERSION_FILE, 'w') as version_file:
# version_file.write(old_contents)
# print("Reverted {} to old contents".format(VERSION_FILE))
def get_version():
file_version = _get_version_from_file()
if file_version != 'development':
return file_version
git_version = _get_version_from_git()
return git_version if (file_version == 'development' and git_version) else file_version
def get_data_files():
data_files = []
for data_root in DATA_ROOTS:
for root, _, files in os.walk(data_root):
data_files.append((os.path.join(PROJECT, root), [os.path.join(root, f) for f in files]))
return data_files
class CustomSDistCommand(sdist): # pylint: disable=no-init, unused-variable
def run(self):
with write_version():
sdist.run(self)
def main():
setup(
name="pytest-raises",
version=get_version(),
description="An implementation of pytest.raises as a pytest.mark fixture",
url="https://github.com/Authentise/pytest-raises",
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
author="Authentise, Inc.",
author_email="[email protected]",
cmdclass={
'sdist': CustomSDistCommand,
},
# NOTE: if `install_requires` or `extras_require['develop']` change,
# make sure to update the Dockerfile to install things.
install_requires=[
'pytest>=3.2.2'
],
extras_require={
'develop': [
'pylint',
'pytest-cov'
],
},
packages=[
"pytest_raises",
],
entry_points={
'pytest11': [
'raises = pytest_raises.pytest_raises',
],
},
include_package_data=True
)
if __name__ == "__main__":
main()