5
5
import subprocess
6
6
import sys
7
7
import json
8
- from typing import Set , Dict , Union
8
+ from typing import Set , Dict , Union , Any
9
9
10
10
RepoManagerSetting = pathlib .Path ("/data/cogs/RepoManager/settings.json" )
11
11
DownloaderSetting = pathlib .Path ("/data/cogs/Downloader/settings.json" )
12
12
DownloaderLibFolder = pathlib .Path ("/data/cogs/Downloader/lib" )
13
13
RepoManagerRepoFolder = pathlib .Path ("/data/cogs/RepoManager/repos/pylav" )
14
14
CogManagerCogFolder = pathlib .Path ("/data/cogs/CogManager/cogs" )
15
15
CogRepoURL = "https://github.com/PyLav/Red-Cogs"
16
- PyLavHashFile = pathlib .Path ("/data/ pylav/.hashfile" )
16
+ PyLavHashFile = pathlib .Path ("/pylav/.hashfile" )
17
17
18
18
logging .basicConfig (level = logging .INFO , format = "%(asctime)s %(levelname)5s: %(message)s" , datefmt = "%Y-%m-%d %H:%M:%S" )
19
19
20
20
log = logging .getLogger ("PyLavSetup" )
21
21
22
- STORAGE_TYPE = os .environ .get ("STORAGE_TYPE" )
23
22
24
- if not STORAGE_TYPE :
25
- with pathlib .Path ("/data/config.json" ).open ("r" , encoding = "utf-8" ) as __f :
26
- STORAGE_TYPE = json .load (__f )["docker" ]["STORAGE_TYPE" ]
23
+ DEV_PYLAV = os .environ .get ("PYLAV__DEV_LIB" )
24
+ DEV_PYLAV_COGS = os .environ .get ("PYLAV__DEV_COG" )
27
25
28
26
29
- IS_JSON = STORAGE_TYPE .upper () == "JSON"
27
+ with pathlib .Path ("/data/config.json" ).open ("r" , encoding = "utf-8" ) as __f :
28
+ IS_JSON = json .load (__f )["docker" ]["STORAGE_TYPE" ].upper () == "JSON"
29
+
30
30
31
31
if not IS_JSON :
32
- RepoManagerRepoFolder = pathlib .Path ("/data/ pylav/cogs" )
32
+ RepoManagerRepoFolder = pathlib .Path ("/pylav/git- cogs" )
33
33
34
34
35
35
def get_git_env () -> Dict [str , str ]:
@@ -84,11 +84,20 @@ def clone_or_update_pylav_repo() -> str:
84
84
85
85
86
86
def get_pylav_cogs () -> Dict [str , pathlib .Path ]:
87
- return {
88
- cog .name : cog
89
- for cog in RepoManagerRepoFolder .iterdir ()
90
- if cog .is_dir () and (cog .name .startswith ("pl" ) or cog .name == "audio" )
91
- }
87
+
88
+ return (
89
+ {
90
+ cog .name : cog
91
+ for cog in RepoManagerRepoFolder .iterdir ()
92
+ if cog .is_dir () and (cog .name .startswith ("pl" ) or cog .name == "audio" )
93
+ }
94
+ if not DEV_PYLAV_COGS
95
+ else {
96
+ cog .name : cog
97
+ for cog in pathlib .Path (DEV_PYLAV_COGS ).iterdir ()
98
+ if cog .is_dir () and (cog .name .startswith ("pl" ) or cog .name == "audio" )
99
+ }
100
+ )
92
101
93
102
94
103
def copy_and_overwrite (from_path : Union [str , os .PathLike [str ]], to_path : Union [str , os .PathLike [str ]]) -> None :
@@ -111,11 +120,20 @@ def get_requirements_for_all_cogs(cogs: Dict[str, pathlib.Path]) -> Set[str]:
111
120
data = json .load (f )
112
121
if "requirements" in data :
113
122
for req in data ["requirements" ]:
123
+ if DEV_PYLAV and req .startswith ("Py-Lav" ):
124
+ continue
114
125
requirements .add (req )
115
126
return requirements
116
127
117
128
118
- def install_requirements (cogs : Dict [str , pathlib .Path ]) -> None | subprocess .Popen [str ]:
129
+ def install_requirements (
130
+ cogs : Dict [str , pathlib .Path ]
131
+ ) -> tuple [
132
+ subprocess .Popen [str ] | subprocess .Popen [str | bytes | Any ] | None ,
133
+ subprocess .Popen [str ] | subprocess .Popen [str | bytes | Any ] | None ,
134
+ ]:
135
+ proc = None
136
+ proc2 = None
119
137
if requirements := get_requirements_for_all_cogs (cogs ):
120
138
log .info ("Installing requirements: %s" , requirements )
121
139
proc = subprocess .Popen (
@@ -143,8 +161,38 @@ def install_requirements(cogs: Dict[str, pathlib.Path]) -> None | subprocess.Pop
143
161
log .info (line .strip ("\n " ))
144
162
if line .startswith ("Successfully installed" ):
145
163
break
146
- return proc
164
+
165
+ if DEV_PYLAV :
166
+ log .info ("Installing editable PyLav" )
167
+ proc2 = subprocess .Popen (
168
+ [
169
+ "/data/venv/bin/pip" ,
170
+ "install" ,
171
+ "--upgrade" ,
172
+ "--no-input" ,
173
+ "--no-warn-conflicts" ,
174
+ "--require-virtualenv" ,
175
+ "--upgrade-strategy" ,
176
+ "eager" ,
177
+ "--target" ,
178
+ DownloaderLibFolder ,
179
+ "--editable" ,
180
+ ".[all]" ,
181
+ ],
182
+ cwd = DEV_PYLAV ,
183
+ env = get_git_env (),
184
+ stdout = subprocess .PIPE ,
185
+ universal_newlines = True ,
186
+ )
187
+ while True :
188
+ line = proc2 .stdout .readline ()
189
+ if not line :
190
+ break
191
+ log .info (line .strip ("\n " ))
192
+ if line .startswith ("Successfully installed" ):
193
+ break
147
194
log .info ("Requirements installed" )
195
+ return proc , proc2
148
196
149
197
150
198
def generate_updated_downloader_setting (
@@ -181,32 +229,42 @@ def update_existing_commit(commit_hash: str) -> None:
181
229
for folder in (DownloaderLibFolder , RepoManagerRepoFolder , CogManagerCogFolder ):
182
230
if not folder .exists ():
183
231
folder .mkdir (parents = True , mode = 0o776 )
184
- current_commit = clone_or_update_pylav_repo ()
185
- existing_commit = get_existing_commit ()
232
+ if not DEV_PYLAV_COGS :
233
+ current_commit = clone_or_update_pylav_repo ()
234
+ existing_commit = get_existing_commit ()
235
+ else :
236
+ current_commit = 1
237
+ existing_commit = 0
186
238
cogs_mapping = get_pylav_cogs ()
187
239
if current_commit == existing_commit :
188
240
log .info ("PyLav is up to date" )
189
241
sys .exit (0 )
190
242
else :
191
- if IS_JSON :
192
- install_or_update_pylav_cogs (cogs_mapping )
193
- process = install_requirements (cogs_mapping )
243
+ install_or_update_pylav_cogs (cogs_mapping )
244
+ process , process2 = install_requirements (cogs_mapping )
194
245
try :
195
246
log .info ("Current PyLav-Cogs Commit: %s" , current_commit )
196
247
downloader_data = generate_updated_downloader_setting (cogs_mapping , current_commit )
197
- log .info ("Updated Downloader Data: %s" , downloader_data )
198
248
if IS_JSON :
249
+ log .info ("Updating Downloader Data: %s" , downloader_data )
199
250
create_or_update_downloader_setting (downloader_data )
200
251
create_or_update_repo_manager_setting ()
201
- update_existing_commit (current_commit )
252
+ if not DEV_PYLAV_COGS :
253
+ update_existing_commit (current_commit )
202
254
if process is not None :
203
255
log .info ("Waiting for requirements to finish installing" )
204
256
process .wait ()
257
+ if process2 is not None :
258
+ log .info ("Waiting for PyLav to finish installing" )
259
+ process2 .wait ()
205
260
log .info ("PyLav setup and update finished" )
206
261
except Exception as e :
207
262
log .info ("PyLav setup and update failed: %s" , e , exc_info = e )
208
263
finally :
209
264
if process is not None :
210
265
process .kill ()
211
266
process .terminate ()
267
+ if process2 is not None :
268
+ process2 .kill ()
269
+ process2 .terminate ()
212
270
sys .exit (0 )
0 commit comments