2
2
import re
3
3
import socket
4
4
import subprocess
5
- from os .path import basename , join , relpath
5
+ from os .path import basename , isdir , isfile , join , relpath
6
6
from typing import Any , Dict , List , Optional , Tuple
7
7
8
8
from . import GLOBALS
@@ -149,26 +149,31 @@ def ls(path: str, *args: str) -> Tuple[List[str], List[str]]:
149
149
(directories if is_directory else files ).append (filename )
150
150
return directories , files
151
151
152
- def push_everything () -> int :
152
+ def push_everything (push_unchanged : bool = True , cleanup_remote : bool = True ) -> int :
153
153
destination_directory = get_modpack_push_directory ()
154
154
if not destination_directory :
155
155
return 1
156
- push_unchanged = GLOBALS .PREFERRED_CONFIG .get_value ("adb.pushUnchangedFiles" , True )
157
- cleanup_remote = GLOBALS .PREFERRED_CONFIG .get_value ("adb.cleanupRemote" , True )
156
+ push_unchanged = GLOBALS .PREFERRED_CONFIG .get_value ("adb.pushUnchangedFiles" , push_unchanged )
157
+ cleanup_remote = GLOBALS .PREFERRED_CONFIG .get_value ("adb.cleanupRemote" , cleanup_remote )
158
158
159
159
shell = Shell ()
160
160
shell .inline_flushing = True
161
161
162
162
with shell :
163
- result = push (GLOBALS .MOD_STRUCTURE .directory , destination_directory , push_unchanged = push_unchanged , cleanup_remote = cleanup_remote , shell = shell )
163
+ result = push_directory (GLOBALS .MOD_STRUCTURE .directory , destination_directory , push_unchanged = push_unchanged , cleanup_remote = cleanup_remote , shell = shell )
164
164
if result != 0 :
165
165
return result
166
166
for linked_resource in GLOBALS .LINKED_RESOURCE_STORAGE .iterate_resources ():
167
167
project_path = GLOBALS .MAKE_CONFIG .get_path (linked_resource ["relative_path" ])
168
168
remote_path = destination_directory + "/" + linked_resource ["output_path" ]
169
169
remote_push_unchanged = linked_resource ["push_unchanged" ] if "push_unchanged" in linked_resource else push_unchanged
170
170
remote_cleanup_remote = linked_resource ["cleanup_remote" ] if "cleanup_remote" in linked_resource else cleanup_remote
171
- result = push (project_path , remote_path , push_unchanged = remote_push_unchanged , cleanup_remote = remote_cleanup_remote , shell = shell )
171
+ if isfile (project_path ):
172
+ result = push_file (project_path , remote_path , push_unchanged = remote_push_unchanged , cleanup_remote = remote_cleanup_remote , shell = shell )
173
+ elif isdir (project_path ):
174
+ result = push_directory (project_path , remote_path , push_unchanged = remote_push_unchanged , cleanup_remote = remote_cleanup_remote , shell = shell )
175
+ else :
176
+ warn (f"* We cannot push { linked_resource ['relative_path' ]} resource because we could not determine its type." )
172
177
if result != 0 :
173
178
return result
174
179
if len (shell .interactables ) == 0 :
@@ -179,16 +184,54 @@ def push_everything() -> int:
179
184
GLOBALS .OUTPUT_STORAGE .save ()
180
185
return 0
181
186
182
- def push (directory : str , destination_directory : str , push_unchanged : bool = True , cleanup_remote : bool = True , shell : Optional [Shell ] = None ) -> int :
187
+ def push_file (file : str , destination_file : str , push_unchanged : bool = True , cleanup_remote : bool = True , shell : Optional [Shell ] = None ) -> int :
188
+ if not push_unchanged and not GLOBALS .OUTPUT_STORAGE .is_path_changed (file ):
189
+ return 0
190
+ file_basename = basename (file )
191
+ if shell :
192
+ progress = Progress (f"Pushing { file_basename } " )
193
+ shell .interactables .append (progress )
194
+
195
+ destination_file = destination_file .replace ("\\ " , "/" )
196
+ if not destination_file .startswith ("/" ):
197
+ destination_file = "/" + destination_file
198
+ sources_file = file .replace ("\\ " , "/" )
199
+ if shell and progress :
200
+ progress .seek (1 , f"Pushing { file_basename } " )
201
+ shell .render ()
202
+ try :
203
+ if cleanup_remote :
204
+ subprocess .call (GLOBALS .ADB_COMMAND + [
205
+ "shell" , "rm" , "-r" , destination_file
206
+ ], stderr = DEVNULL , stdout = DEVNULL )
207
+ result = subprocess .run (GLOBALS .ADB_COMMAND + [
208
+ "push" , sources_file , destination_file
209
+ ], capture_output = True , text = True )
210
+ except KeyboardInterrupt :
211
+ Progress .notify (shell , progress , 1 , "Pushing aborted." )
212
+ return 1
213
+
214
+ if result .returncode != 0 :
215
+ Progress .notify (shell , progress , 1 , f"Failed pushing { file_basename } " )
216
+ cause = result .stdout .splitlines ()[- 1 ]
217
+ if cause and len (cause ) > 0 :
218
+ if shell :
219
+ shell .leave ()
220
+ error (cause )
221
+ else :
222
+ Progress .notify (shell , progress , 1 , f"Pushed { file_basename } " )
223
+ return result .returncode
224
+
225
+ def push_directory (directory : str , destination_directory : str , push_unchanged : bool = True , cleanup_remote : bool = True , shell : Optional [Shell ] = None ) -> int :
183
226
items = [
184
227
relpath (path , directory ) for path in glob (directory + "/*" ) \
185
228
if push_unchanged or GLOBALS .OUTPUT_STORAGE .is_path_changed (path )
186
229
]
187
230
if len (items ) == 0 :
188
231
return 0
189
- directory_name = basename (directory )
232
+ directory_basename = basename (directory )
190
233
if shell :
191
- progress = Progress (f"Pushing { directory_name } " )
234
+ progress = Progress (f"Pushing { directory_basename } " )
192
235
shell .interactables .append (progress )
193
236
194
237
destination_directory = destination_directory .replace ("\\ " , "/" )
@@ -225,7 +268,7 @@ def push(directory: str, destination_directory: str, push_unchanged: bool = True
225
268
error (cause )
226
269
return result .returncode
227
270
228
- Progress .notify (shell , progress , 1 , f"Pushed { directory_name } " )
271
+ Progress .notify (shell , progress , 1 , f"Pushed { directory_basename } " )
229
272
return 0
230
273
231
274
def make_locks (* locks : str ) -> int :
0 commit comments