Skip to content

Commit

Permalink
Do not store xcodebuild logs in memory (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
priitlatt authored Jan 11, 2021
1 parent e30ca58 commit 6e4ca9f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
-------------

Expand Down
2 changes: 1 addition & 1 deletion src/codemagic/__version__.py
Original file line number Diff line number Diff line change
@@ -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'
27 changes: 20 additions & 7 deletions src/codemagic/cli/cli_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand All @@ -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)
11 changes: 9 additions & 2 deletions src/codemagic/models/xcodebuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit 6e4ca9f

Please sign in to comment.