forked from Amourspirit/python_libre_pythonista_ext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
229 lines (189 loc) · 7.7 KB
/
Copy pathbuild.py
File metadata and controls
229 lines (189 loc) · 7.7 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
from __future__ import annotations
import os
import shutil
from .config import Config
from . import file_util
from .build_args import BuildArgs
from .processing.token import Token
from .processing.packages import Packages
from .processing.req_packages import ReqPackages
from .processing.update import Update
from .processing.json_config import JsonConfig
from .processing.default_resource import DefaultResource
from .processing.locale.descriptions import Descriptions
from .processing.locale.publisher import Publisher
from .processing.locale.publisher_update import PublisherUpdate
from .processing.locale.name import Name
from .processing.bz2_process import BZ2Processor
from .install.pre_install_pure import PreInstallPure
from .processing.idl.idl_rdb import IdlRdb
from .processing.idl.idl_manifest import IdlManifest
class Build:
"""Builds the project."""
def __init__(self, args: BuildArgs) -> None:
self._config = Config()
self._build_path = self._config.build_path
self._args = args
self._src_path = self._config.root_path / args.oxt_src
if not self._src_path.exists():
raise FileNotFoundError(f"Oxt source directory '{self._src_path}' not found")
self._dist_path = self._config.root_path / self._config.dist_dir_name
self._dist_path.mkdir(parents=True, exist_ok=True)
def build(self) -> None:
"""Builds the project."""
# sourcery skip: extract-method
if self._args.clean:
self.clean()
# self._ensure_build()
self._copy_src_dest()
self._rename_lo_pip()
if self._args.process_tokens:
self._process_tokens()
self._process_config()
self._copy_py_req_packages()
self._copy_py_req_files()
self._clear_req_cache()
self._zip_req_python_path()
if self._args.process_py_packages:
pythonpath = self._build_path / self._config.py_pkg_dir
if pythonpath.exists():
shutil.rmtree(pythonpath)
self._copy_py_packages()
self._copy_py_files()
self._clear_cache()
self._zip_python_path()
if self._args.pre_install_pure_packages:
self._pre_install_pure_packages()
if self._args.compile_idl:
self._build_idl()
self._write_xml()
self._process_bz2()
self._ensure_default_resource()
if self._args.make_dist:
self._zip_build()
self._process_update()
def process_tokens(self, text: str) -> str:
"""Processes the tokens in the given text."""
token = Token()
return token.process(text)
def _write_xml(self) -> None:
"""Writes the descriptions."""
descriptions = Descriptions()
descriptions.write()
name = Name()
name.write()
publisher = Publisher()
publisher.write()
idl_manifest = IdlManifest()
idl_manifest.write()
def clean(self) -> None:
"""Cleans the project."""
if self._build_path.exists():
shutil.rmtree(self._build_path)
def _rename_lo_pip(self) -> None:
"""Renames the lo_pip folder."""
token = Token()
src_dir = self._build_path / "___lo_pip___"
if not src_dir.exists():
raise FileNotFoundError(f"lo_pip folder '{src_dir}' not found")
dest_dir = self._build_path / token.get_token_value("lo_pip")
os.rename(src_dir, dest_dir)
def _ensure_build(self) -> None:
"""Ensures the build directory exists."""
if not self._build_path.exists():
self._build_path.mkdir(parents=True, exist_ok=True)
def _ensure_default_resource(self) -> None:
"""Ensures the default resource file exists."""
default_resource = DefaultResource()
default_resource.ensure_default()
def _copy_src_dest(self) -> None:
"""Copies the source files to the build directory."""
shutil.copytree(src=self._src_path, dst=self._build_path)
def _process_tokens(self) -> None:
"""Processes the tokens in the dest files."""
files = file_util.find_files_matching_patterns(
self._build_path, self._config.token_file_ext, *self._config.token_files
)
for file in files:
text = file_util.read_file(file)
text = self.process_tokens(text)
file_util.write_string_to_file(file, text)
# process py_runner.py
file = self._build_path / "py_runner.py"
if file.exists():
py_file = str(file)
text = file_util.read_file(py_file)
text = self.process_tokens(text)
file_util.write_string_to_file(py_file, text)
def _process_config(self) -> None:
token = Token()
config_file = self._build_path / token.get_token_value("lo_pip") / "config.json"
json_config = JsonConfig()
json_config.update_json_config(config_file)
def _process_bz2(self) -> None:
"""Processes the bz2 files."""
bz2 = BZ2Processor()
bz2.process()
def _copy_py_packages(self) -> None:
"""Copies the python packages to the build directory."""
packages = Packages()
packages.copy_packages(self._build_path / self._config.py_pkg_dir)
def _copy_py_files(self) -> None:
"""Copies the python files to the build directory."""
packages = Packages()
packages.copy_files(self._build_path / self._config.py_pkg_dir)
def _copy_py_req_packages(self) -> None:
"""Copies the python packages to the build directory."""
packages = ReqPackages()
packages.copy_packages(self._build_path / f"req_{self._config.py_pkg_dir}")
def _copy_py_req_files(self) -> None:
"""Copies the python files to the build directory."""
packages = ReqPackages()
packages.copy_files(self._build_path / f"req_{self._config.py_pkg_dir}")
def _clear_cache(self) -> None:
"""Cleans the cache."""
packages = Packages()
if not packages.has_modules():
return
packages.clear_cache(self._build_path / self._config.py_pkg_dir)
def _clear_req_cache(self) -> None:
"""Cleans the cache."""
packages = ReqPackages()
packages.clear_cache(self._build_path / f"req_{self._config.py_pkg_dir}")
def _zip_python_path(self) -> None:
"""Zips the python path."""
pth = self._build_path / self._config.py_pkg_dir
if not pth.exists():
return
file_util.zip_folder(folder=pth)
shutil.rmtree(pth)
def _pre_install_pure_packages(self) -> None:
"""Installs the pure python packages."""
pre_install = PreInstallPure()
pre_install.install()
def _zip_req_python_path(self) -> None:
"""Zips the required packages path."""
pth = self._build_path / f"req_{self._config.py_pkg_dir}"
if not pth.exists():
return
file_util.zip_folder(folder=pth)
shutil.rmtree(pth)
def _zip_build(self) -> None:
"""Zips the build directory."""
old_file = self._dist_path / f"{self._config.build_dir_name}.zip"
new_file = self._dist_path / f"{self._config.otx_name}.oxt"
if new_file.exists():
os.remove(new_file)
file_util.zip_folder(folder=self._build_path, dest_dir=self._dist_path)
os.rename(old_file, new_file)
def _build_idl(self) -> None:
"""Builds the idl files."""
idl = IdlRdb()
if idl.has_files:
idl.compile()
def _process_update(self) -> None:
"""Processes the update file."""
update = Update()
update.process()
publisher_update = PublisherUpdate()
publisher_update.write()