Skip to content

Commit

Permalink
More time logging
Browse files Browse the repository at this point in the history
Summary: In `BuckPartScope` also log times to the console.

Reviewed By: NTillmann

Differential Revision: D48481598

fbshipit-source-id: 1309f38a8a5ba5d9f3701c829a8ac9ce477a8faa
  • Loading branch information
agampe authored and facebook-github-bot committed Nov 13, 2023
1 parent f3e288f commit 0049104
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 141 deletions.
33 changes: 23 additions & 10 deletions pyredex/buck.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
import os
import platform
import typing
from contextlib import contextmanager
from io import TextIOWrapper

from pyredex.utils import time_it


# Data class for a running step.
class _RunningPart:
Expand Down Expand Up @@ -172,13 +175,23 @@ def __exit__(self, *args: typing.Any) -> None:
_BUCK_CONNECTION.disconnect()


class BuckPartScope:
def __init__(self, name: str, desc: str) -> None:
self.name = name
self.desc = desc

def __enter__(self) -> None:
_BUCK_CONNECTION.start(self.name, self.desc)

def __exit__(self, *args: typing.Any) -> None:
_BUCK_CONNECTION.end()
@contextmanager
def BuckPartScope(
name: str, desc: str, timed: bool = True, timed_start: bool = True
) -> typing.Generator[int, None, None]:
if timed:
with time_it(
f"{desc} took {{time:.2f}} seconds",
**({"start": desc} if timed_start else {}),
):
_BUCK_CONNECTION.start(name, desc)
try:
yield 1 # Irrelevant
finally:
_BUCK_CONNECTION.end()
else:
_BUCK_CONNECTION.start(name, desc)
try:
yield 1 # Irrelevant
finally:
_BUCK_CONNECTION.end()
2 changes: 1 addition & 1 deletion pyredex/packer.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def _compress(
return

with _warning_timer(item.name, 1.0) as _:
logging.info("Compressing %s...", item.name)
logging.debug("Compressing %s...", item.name)

# If an output name is given, use it. If not, ensure that it is only
# one file.
Expand Down
56 changes: 45 additions & 11 deletions pyredex/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import timeit
import typing
import zipfile
from contextlib import contextmanager
from os.path import basename, dirname, isfile, join

from pyredex.logger import log
Expand Down Expand Up @@ -65,7 +66,10 @@ def with_temp_cleanup(
success = True
finally:
if success:
remove_temp_dirs()
from pyredex.buck import BuckPartScope # Circular dependency...

with BuckPartScope("Redex::TempDirs", "Cleaning up temporary directories"):
remove_temp_dirs()


class _FindAndroidBuildToolHelper:
Expand Down Expand Up @@ -499,18 +503,23 @@ def get_file_ext(file_name: str) -> str:


def _verify_dex(dex_file: str, cmd: str) -> bool:
logging.debug("Verifying %s...", dex_file)
try:
logging.info("Verifying %s...", dex_file)

res = subprocess.run(
f"{cmd} '{dex_file}'", shell=True, text=False, capture_output=True
)
if res.returncode == 0:
return True
res = subprocess.run(
f"{cmd} '{dex_file}'", shell=True, text=False, capture_output=True
)
if res.returncode == 0:
return True

try:
stderr_str = res.stderr.decode("utf-8")
except BaseException:
stderr_str = "<unable to decode, contains non-UTF8>"
try:
stderr_str = res.stderr.decode("utf-8")
except BaseException:
stderr_str = "<unable to decode, contains non-UTF8>"

except BaseException as e:
logging.exception("Error for dex file %s", dex_file)
stderr_str = f"{e}"

logging.error("Failed verification for %s:\n%s", dex_file, stderr_str)
return False
Expand Down Expand Up @@ -557,3 +566,28 @@ def get_xz_path() -> typing.Optional[str]:
logging.debug("Using command line xz")
xz = "xz"
return xz


_TIME_IT_DEPTH: int = 0


@contextmanager
def time_it(
fmt: str, *args: typing.Any, **kwargs: str
) -> typing.Generator[int, None, None]:
global _TIME_IT_DEPTH
this_depth = _TIME_IT_DEPTH
_TIME_IT_DEPTH += 1

if "start" in kwargs:
logging.info("-" * this_depth + kwargs["start"])

timer = timeit.default_timer
start_time = timer()
try:
yield 1 # Irrelevant
finally:
end_time = timer()
logging.info("-" * this_depth + fmt.format(time=end_time - start_time), *args)

_TIME_IT_DEPTH -= 1
Loading

0 comments on commit 0049104

Please sign in to comment.