Skip to content

Commit f7a5ea6

Browse files
committed
Pushing and packaging single resource file, disable additional cleanup by default as before
1 parent 565a00a commit f7a5ea6

File tree

3 files changed

+65
-15
lines changed

3 files changed

+65
-15
lines changed

toolchain/toolchain/python/icmtoolchain/device.py

+53-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import re
33
import socket
44
import subprocess
5-
from os.path import basename, join, relpath
5+
from os.path import basename, isdir, isfile, join, relpath
66
from typing import Any, Dict, List, Optional, Tuple
77

88
from . import GLOBALS
@@ -149,26 +149,31 @@ def ls(path: str, *args: str) -> Tuple[List[str], List[str]]:
149149
(directories if is_directory else files).append(filename)
150150
return directories, files
151151

152-
def push_everything() -> int:
152+
def push_everything(push_unchanged: bool = True, cleanup_remote: bool = True) -> int:
153153
destination_directory = get_modpack_push_directory()
154154
if not destination_directory:
155155
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)
158158

159159
shell = Shell()
160160
shell.inline_flushing = True
161161

162162
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)
164164
if result != 0:
165165
return result
166166
for linked_resource in GLOBALS.LINKED_RESOURCE_STORAGE.iterate_resources():
167167
project_path = GLOBALS.MAKE_CONFIG.get_path(linked_resource["relative_path"])
168168
remote_path = destination_directory + "/" + linked_resource["output_path"]
169169
remote_push_unchanged = linked_resource["push_unchanged"] if "push_unchanged" in linked_resource else push_unchanged
170170
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.")
172177
if result != 0:
173178
return result
174179
if len(shell.interactables) == 0:
@@ -179,16 +184,54 @@ def push_everything() -> int:
179184
GLOBALS.OUTPUT_STORAGE.save()
180185
return 0
181186

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:
183226
items = [
184227
relpath(path, directory) for path in glob(directory + "/*") \
185228
if push_unchanged or GLOBALS.OUTPUT_STORAGE.is_path_changed(path)
186229
]
187230
if len(items) == 0:
188231
return 0
189-
directory_name = basename(directory)
232+
directory_basename = basename(directory)
190233
if shell:
191-
progress = Progress(f"Pushing {directory_name}")
234+
progress = Progress(f"Pushing {directory_basename}")
192235
shell.interactables.append(progress)
193236

194237
destination_directory = destination_directory.replace("\\", "/")
@@ -225,7 +268,7 @@ def push(directory: str, destination_directory: str, push_unchanged: bool = True
225268
error(cause)
226269
return result.returncode
227270

228-
Progress.notify(shell, progress, 1, f"Pushed {directory_name}")
271+
Progress.notify(shell, progress, 1, f"Pushed {directory_basename}")
229272
return 0
230273

231274
def make_locks(*locks: str) -> int:

toolchain/toolchain/python/icmtoolchain/resources.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
import os
3-
from os.path import basename, exists, relpath, isfile, join
3+
from os.path import basename, exists, isdir, isfile, join
44
from shutil import make_archive
55

66
from . import GLOBALS
@@ -110,7 +110,7 @@ def build_additional_resources() -> int:
110110
warn(f"* Skipped non-existing additional resource {additional_dir['source']!r}!")
111111
continue
112112
push_unchanged = additional_dir["pushUnchangedFiles"] if "pushUnchangedFiles" in additional_dir else None
113-
cleanup_remote = additional_dir["cleanupRemote"] if "cleanupRemote" in additional_dir else None
113+
cleanup_remote = additional_dir["cleanupRemote"] if "cleanupRemote" in additional_dir else False
114114

115115
for additional_path in additional_files:
116116
relative_path = GLOBALS.MAKE_CONFIG.get_relative_path(additional_path)
@@ -181,8 +181,14 @@ def build_package() -> int:
181181

182182
copy_directory(GLOBALS.MOD_STRUCTURE.directory, output_package_directory)
183183
for linked_resource in GLOBALS.LINKED_RESOURCE_STORAGE.iterate_resources():
184-
output_package_resource_directory = join(output_package_directory, linked_resource["output_path"])
185-
copy_directory(GLOBALS.MAKE_CONFIG.get_path(linked_resource["relative_path"]), output_package_resource_directory)
184+
input_resource = GLOBALS.MAKE_CONFIG.get_path(linked_resource["relative_path"])
185+
output_package_resource = join(output_package_directory, linked_resource["output_path"])
186+
if isfile(input_resource):
187+
copy_file(input_resource, output_package_resource)
188+
elif isdir(input_resource):
189+
copy_directory(input_resource, output_package_resource)
190+
else:
191+
warn(f"* We cannot copy {linked_resource['relative_path']} resource because we could not determine its type.")
186192
for path in GLOBALS.MAKE_CONFIG.get_list("excludeFromRelease"):
187193
for excluded_path in GLOBALS.MAKE_CONFIG.get_paths(join(output_package_directory, path)):
188194
remove_tree(excluded_path)

toolchain/toolchain/python/icmtoolchain/task.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ def task_update_includes() -> int:
229229
description="Copies predefined resources consisting of textures, in-game packs, etc."
230230
)
231231
def task_resources() -> int:
232-
from .resources import build_additional_resources, build_pack_graphics, build_resources
232+
from .resources import (build_additional_resources, build_pack_graphics,
233+
build_resources)
233234
if not GLOBALS.MAKE_CONFIG.has_value("manifest"):
234235
overall_result = build_resources()
235236
else:

0 commit comments

Comments
 (0)