~aleteoryx/muditaos

ref: 6059ac716e0db2e30e5d04c7009c406ac7720a7d muditaos/test/get_os_log.py -rwxr-xr-x 2.2 KiB
6059ac71 — Lefucjusz [BH-2034] Various cleanups and refactors across the OS 1 year, 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
# Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

import sys
import os
import atexit

from harness.harness import Harness
from harness.request import TransactionError
from harness.interface.error import TestError, Error
from harness.interface.defs import status
from harness.api.developermode import PhoneModeLock
from harness.api.security import GetPhoneLockStatus
from harness.api.filesystem import get_log_file, get_log_file_with_path
from harness.api.device_info import *

def is_phone_locked(harness: Harness):
    try:
        GetPhoneLockStatus().run(harness)
    except TransactionError as error:
        return error.status == status["Forbidden"]
    else:
        return False

def set_passcode(harness: Harness, flag: bool):
    '''
    on exit -> restore PhoneModeLock
    '''
    PhoneModeLock(flag).run(harness)

def get_log_files(harness: Harness, fileList: DiagnosticsFileList, log_save_dir: str):
    '''
    Get list of log files and get them one by one
    '''
    ret = GetDiagnosticFilesList(fileList).run(harness)
    if not ret.files:
        print("No files to download")
        return

    for diag_file in ret.files:
        print(f'Downloading {diag_file} file:')
        get_log_file_with_path(harness, diag_file, log_save_dir)

def main():
    if len(sys.argv) == 1:
        print(
            f'Please pass log storage directory as the parameter: \'python {sys.argv[0]} <log dir>\' ')
        raise TestError(Error.OTHER_ERROR)

    log_save_dir = os.path.join(os.path.abspath(str(sys.argv[1])), '')

    if (not os.path.exists(log_save_dir)):
        print(f'Log storage directory {log_save_dir} not found')
        os.makedirs(log_save_dir, exist_ok = True)
        # raise TestError(Error.OTHER_ERROR)

    harness = Harness.from_detect()

    if is_phone_locked(harness):
        atexit.register(set_passcode, harness, True)
        set_passcode(harness, False)

    get_log_files(harness, DiagnosticsFileList.LOGS, log_save_dir)
    get_log_files(harness, DiagnosticsFileList.CRASH_DUMPS, log_save_dir)
    exit(0)


if __name__ == "__main__":
    try:
        main()
    except TestError as err:
        print(err)
        exit(err.get_error_code())