From 6e4ca9f4d3b94cdf603d5a97b054125cf3e8e30d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Priit=20L=C3=A4tt?= Date: Mon, 11 Jan 2021 15:10:33 +0200 Subject: [PATCH] Do not store xcodebuild logs in memory (#62) --- CHANGELOG.md | 7 +++++++ src/codemagic/__version__.py | 2 +- src/codemagic/cli/cli_process.py | 27 ++++++++++++++++++++------- src/codemagic/models/xcodebuild.py | 11 +++++++++-- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fa63e83..08a5e14f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +Version 0.4.4 +------------- + +**Improvements** + +- Improvement: Reduce memory footprint for `xcode-process` by not storing xcodebuild logs in memory. Read them from file if need be. + Version 0.4.3 ------------- diff --git a/src/codemagic/__version__.py b/src/codemagic/__version__.py index 03c3e609..b8316e61 100644 --- a/src/codemagic/__version__.py +++ b/src/codemagic/__version__.py @@ -1,5 +1,5 @@ __title__ = "codemagic-cli-tools" __description__ = "CLI tools used in Codemagic builds" -__version__ = "0.4.3" +__version__ = "0.4.4" __url__ = 'https://github.com/codemagic-ci-cd/cli-tools' __licence__ = 'GNU General Public License v3.0' diff --git a/src/codemagic/cli/cli_process.py b/src/codemagic/cli/cli_process.py index d01c2e4f..31fc50b1 100644 --- a/src/codemagic/cli/cli_process.py +++ b/src/codemagic/cli/cli_process.py @@ -35,8 +35,16 @@ def __init__(self, command_args: Sequence[CommandArg], if safe_form is None: full_command = ' '.join(shlex.quote(str(arg)) for arg in command_args) self.safe_form = ObfuscatedCommand(full_command) - self.stdout = "" - self.stderr = "" + self._stdout = "" + self._stderr = "" + + @property + def stdout(self) -> str: + return self._stdout + + @property + def stderr(self) -> str: + return self._stderr @property def returncode(self) -> int: @@ -78,9 +86,9 @@ def _handle_streams(self, buffer_size: Optional[int] = None): if self._process is None: return if self._process.stdout: - self.stdout += self._handle_stream(self._process.stdout, sys.stdout, buffer_size) + self._stdout += self._handle_stream(self._process.stdout, sys.stdout, buffer_size) if self._process.stderr: - self.stderr += self._handle_stream(self._process.stderr, sys.stderr, buffer_size) + self._stderr += self._handle_stream(self._process.stderr, sys.stderr, buffer_size) def execute(self, stdout: Union[int, IO] = subprocess.PIPE, @@ -100,8 +108,13 @@ def execute(self, self._log_exec_completed() return self - def raise_for_returncode(self, success_code: int = 0): + def raise_for_returncode(self, success_code: int = 0, include_logs: bool = True): if self.returncode == success_code: return - raise subprocess.CalledProcessError( - self.returncode, self._command_args, self.stdout, self.stderr) + if include_logs: + stdout = self.stdout + stderr = self.stderr + else: + stdout = '' + stderr = '' + raise subprocess.CalledProcessError(self.returncode, self._command_args, stdout, stderr) diff --git a/src/codemagic/models/xcodebuild.py b/src/codemagic/models/xcodebuild.py index 48a06463..e6ecf930 100644 --- a/src/codemagic/models/xcodebuild.py +++ b/src/codemagic/models/xcodebuild.py @@ -252,7 +252,7 @@ def _run_command(self, if cli_app: process = XcodebuildCliProcess(command, xcpretty=self.xcpretty) cli_app.logger.info(f'Execute "%s"\n', process.safe_form) - process.execute().raise_for_returncode() + process.execute().raise_for_returncode(include_logs=False) else: subprocess.check_output(command) except subprocess.CalledProcessError as cpe: @@ -272,6 +272,14 @@ def __init__(self, *args, xcpretty: Optional[Xcpretty] = None, **kwargs): self._buffer: Optional[IO] = None self.xcpretty = xcpretty + @property + def stdout(self) -> str: + return self.log_path.read_text() + + @property + def stderr(self) -> str: + return '' + def _print_stream(self, chunk: str): if not self._print_streams: return @@ -286,7 +294,6 @@ def _handle_streams(self, buffer_size: Optional[int] = None): lines = self._buffer.readlines(buffer_size or -1) chunk = ''.join(lines) self._print_stream(chunk) - self.stdout += chunk def execute(self, *args, **kwargs) -> XcodebuildCliProcess: try: