forked from Kludex/python-multipart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
84 lines (64 loc) · 2.12 KB
/
tasks.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
import os
import re
import sys
from invoke import task, run
version_file = os.path.join('multipart', '__init__.py')
version_regex = re.compile(r'((?:\d+)\.(?:\d+)\.(?:\d+))')
# Get around Python 2.X's lack of 'nonlocal' keyword
class g:
test_success = False
@task
def test(ctx, all=False):
test_cmd = [
'pytest', # Test command
'--cov-report term-missing', # Print only uncovered lines to stdout
'--cov-config .coveragerc', # Use this file for configuration
'--cov multipart', # Test only this module
'--timeout=30' # Each test should timeout after 30 sec
]
# Test in this directory
test_cmd.append("tests")
# Run the command.
# TODO: why does this fail with pty=True?
res = run(' '.join(test_cmd), pty=False)
g.test_success = res.ok
@task
def bump(ctx, type):
# Read and parse version.
with open(version_file, 'r') as f:
file_data = f.read().replace('\r\n', '\n')
m = version_regex.search(file_data)
if m is None:
print(f"Could not find version in '{version_file}'!", file=sys.stderr)
return
version = m.group(0)
before = file_data[0:m.start(0)]
after = file_data[m.end(0):]
# Bump properly.
ver_nums = [int(x) for x in version.split('.')]
if type == 'patch':
ver_nums[2] += 1
elif type == 'minor':
ver_nums[1] += 1
elif type == 'major':
ver_nums[0] += 1
else:
print(f"Invalid version type: '{type}'", file=sys.stderr)
return
# Construct new data and write to file.
new_ver = ".".join(str(x) for x in ver_nums)
new_data = before + new_ver + after
with open(version_file, 'w') as f:
f.write(new_data)
# Print information.
print(f"Bumped version from: {version} --> {new_ver}")
@task(pre=[test])
def deploy(ctx):
if not g.test_success:
print("Tests must pass before deploying!", file=sys.stderr)
return
# # Build source distribution and wheel
run('hatch build')
#
# # Upload distributions from last step to pypi
run('hatch publish')