forked from strictdoc-project/strictdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks_wine.py
152 lines (131 loc) · 5.05 KB
/
tasks_wine.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Invoke is broken on Python 3.11
# https://github.com/pyinvoke/invoke/issues/833#issuecomment-1293148106
import inspect
import os
import platform
import re
from enum import Enum
if not hasattr(inspect, "getargspec"):
inspect.getargspec = inspect.getfullargspec
import invoke # pylint: disable=wrong-import-position
from invoke import task # pylint: disable=wrong-import-position
# A flag that stores which virtual environment is used for executing tasks.
VENV_FOLDER = "VENV_FOLDER"
# A flag that is used to allow checking the readiness of virtual environment
# only once independently of which task or a sequence of tasks is executed.
VENV_DEPS_CHECK_PASSED = "VENV_DEPS_CHECK_PASSED"
COMMAND_SETUP_DEPS = "pip install .[development]"
def one_line_command(string):
return re.sub("\\s+", " ", string).strip()
def get_venv_command(venv_path: str, reset_path=True):
venv_command_activate = (
f". {venv_path}/bin/activate"
if platform.system() != "Windows"
else rf"{venv_path}\Scripts\activate"
)
venv_command = f"""python -m venv {venv_path} &&
{venv_command_activate}
"""
if reset_path:
venv_command += f'&& export PATH="{venv_path}/bin:/usr/bin:/bin"'
# TODO: Doing VER>NUL & could be eliminated.
if platform.system() == "Windows":
venv_command = "VER>NUL"
return one_line_command(venv_command)
# To prevent all tasks from building to the same virtual environment.
class VenvFolderType(str, Enum):
RELEASE_DEFAULT = "default"
RELEASE_LOCAL = "release-local"
RELEASE_PYPI = "release-pypi"
RELEASE_PYPI_TEST = "release-pypi-test"
RELEASE_PYINSTALLER = "release-pyinstaller"
def run_invoke_cmd(
context, cmd, warn: bool = False, reset_path: bool = True
) -> invoke.runners.Result:
postfix = (
context[VENV_FOLDER]
if VENV_FOLDER in context
else VenvFolderType.RELEASE_DEFAULT
)
venv_path = os.path.join(os.getcwd(), f".venv-{postfix}")
with context.prefix(get_venv_command(venv_path, reset_path=reset_path)):
# FIXME: pip3 uninstall strictdoc -y" is here because I don't know how
# to make pip install only the dependencies from the pyproject.toml but
# not the project itself.
if VENV_DEPS_CHECK_PASSED not in context:
result = context.run(
one_line_command(
"""
pip3 install toml &&
python check_environment.py &&
pip3 uninstall strictdoc -y
"""
),
env=None,
hide=False,
warn=True,
pty=False,
echo=True,
)
if result.exited == 11:
result = context.run(
one_line_command(COMMAND_SETUP_DEPS),
env=None,
hide=False,
warn=False,
pty=False,
echo=True,
)
if result.exited != 0:
return result
elif result.exited != 0:
raise invoke.exceptions.UnexpectedExit(result)
context[VENV_DEPS_CHECK_PASSED] = True
return context.run(
one_line_command(cmd),
env=None,
hide=False,
warn=warn,
pty=False,
echo=True,
)
@task
def setup_development_deps(context):
run_invoke_cmd(context, COMMAND_SETUP_DEPS)
@task
def release_pyinstaller(context):
context[VENV_FOLDER] = VenvFolderType.RELEASE_PYINSTALLER
setup_development_deps(context)
path_to_pyi_dist = "Z:\\tmp\\strictdoc_windows"
# The --additional-hooks-dir is a workaround against the following error
# that is produced by pybibtex:
# """
# pkg_resources.DistributionNotFound: The 'six' distribution was not found
# and is required by the application.
# """
# Solution found here:
# https://stackoverflow.com/a/64473931/598057
#
# The --hidden-import strictdoc.server.app flag is needed because without
# it, the following is produced:
# ERROR: Error loading ASGI app. Could not import
# module "strictdoc.server.app".
# Solution found here: https://stackoverflow.com/a/71340437/598057
# This behavior is not surprising because that's how the uvicorn loads the
# application separately from the parent process.
command = f"""
pip install pyinstaller &&
pyinstaller
--clean
--name strictdoc
--noconfirm
--additional-hooks-dir developer\\pyinstaller_hooks
--distpath {path_to_pyi_dist}
--hidden-import strictdoc.server.app
--add-data strictdoc\\export\\html\\templates;templates\\html
--add-data strictdoc\\export\\rst\\templates;templates\\rst
--add-data strictdoc\\export\\html\\_static;_static
--add-data strictdoc\\export\\html\\_static_extra;_static_extra
strictdoc\\cli\\main.py
"""
run_invoke_cmd(context, command)