Fix multiple process access the same log file issue. (#1232)

This commit is contained in:
LiliDeng 2021-03-18 18:14:28 +08:00 коммит произвёл GitHub
Родитель 6dc843bff7
Коммит 35c97d57de
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 49 добавлений и 5 удалений

Просмотреть файл

@ -4,8 +4,8 @@
import glob
import itertools
import os
import platform
import re
import shutil
import time
from functools import partial
from pathlib import Path
@ -22,6 +22,13 @@ from lisa.util import InitializableMixin, LisaException, constants, run_in_threa
from lisa.util.logger import Logger, create_file_handler, get_logger, remove_handler
from lisa.util.process import Process
# uses to prevent read conflict on log files
if platform.system() == "Windows":
import msvcrt
import win32file # type: ignore
# TestResults\2021-02-07-18-04-09-OX53\LISAv2-Test-OX53.log
ROOT_LOG_FILE_PATTERN = re.compile(
r"TestResults[\\/][\d-]{20}.{4}[\\/]LISAv2-Test-.{4}\.log$"
@ -51,6 +58,9 @@ class LegacyRunner(BaseRunner):
return constants.TESTCASE_TYPE_LEGACY
def __init__(self, *args: Any, **kwargs: Any) -> None:
if platform.system() != "Windows":
raise LisaException("LegacyRunner uses PowerShell, runs on Windows only.")
super().__init__(*args, **kwargs)
self.exit_code: int = 0
# leverage Node logic to run local processes.
@ -549,11 +559,29 @@ class LogParser(InitializableMixin):
V2 opens log file frequently to write content, copying may be failed due to
conflict. So retry to make it more stable.
"""
temp_file_name = f"{self._runner_log_path}_temp.log"
shutil.copy2(self._runner_log_path, temp_file_name)
with open(temp_file_name, "r") as file:
# refer from http://thepythoncorner.com/dev/how-to-open-file-without-locking-it/
# that's cool!
handle = win32file.CreateFile(
self._runner_log_path,
win32file.GENERIC_READ,
win32file.FILE_SHARE_DELETE
| win32file.FILE_SHARE_READ
| win32file.FILE_SHARE_WRITE,
None,
win32file.OPEN_EXISTING,
0,
None,
)
# detach the handle
detached_handle = handle.Detach()
# get a file descriptor associated to the handle
file_descriptor = msvcrt.open_osfhandle(detached_handle, os.O_RDONLY)
# open the file descriptor
with open(file_descriptor) as file:
content = file.read()
os.remove(temp_file_name)
return content
def _line_iter(self) -> Iterable[str]:

15
poetry.lock сгенерированный
Просмотреть файл

@ -782,6 +782,17 @@ category = "main"
optional = false
python-versions = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*"
[[package]]
name = "pypiwin32"
version = "223"
description = ""
category = "main"
optional = false
python-versions = "*"
[package.dependencies]
pywin32 = ">=223"
[[package]]
name = "pytest"
version = "6.2.2"
@ -1475,6 +1486,10 @@ pyparsing = [
{file = "pyparsing-2.4.7-py2.py3-none-any.whl", hash = "sha256:ef9d7589ef3c200abe66653d3f1ab1033c3c419ae9b9bdb1240a85b024efc88b"},
{file = "pyparsing-2.4.7.tar.gz", hash = "sha256:c203ec8783bf771a155b207279b9bccb8dea02d8f0c9e5f8ead507bc3246ecc1"},
]
pypiwin32 = [
{file = "pypiwin32-223-py3-none-any.whl", hash = "sha256:67adf399debc1d5d14dffc1ab5acacb800da569754fafdc576b2a039485aa775"},
{file = "pypiwin32-223.tar.gz", hash = "sha256:71be40c1fbd28594214ecaecb58e7aa8b708eabfa0125c8a109ebd51edbd776a"},
]
pytest = [
{file = "pytest-6.2.2-py3-none-any.whl", hash = "sha256:b574b57423e818210672e07ca1fa90aaf194a4f63f3ab909a2c67ebb22913839"},
{file = "pytest-6.2.2.tar.gz", hash = "sha256:9d1edf9e7d0b84d72ea3dbcdfd22b35fb543a5e8f2a60092dd578936bf63d7f9"},

Просмотреть файл

@ -24,6 +24,7 @@ func-timeout = "^4.3.5"
assertpy = "^1.1"
pytest-html = "^3.1.1"
pluggy = "^0.13.1"
pypiwin32 = {version = "^223", platform = "win32"}
PyGObject = {version = "^3.38.0", platform = 'linux'}
[tool.poetry.dev-dependencies]