• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# -*- coding: utf-8 -*-
2# Copyright (c) 2023 Huawei Device Co., Ltd.
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#!/usr/bin/python3
15
16from subprocess import run
17import os
18import sqlite3
19
20import sys
21sys.path.append(os.path.dirname(os.path.realpath(__file__)) + os.sep)
22from common import *
23from apl_config import *
24log_tag = 'read_device'
25
26#从设备中导出数据库
27def download_from_device(cmd,sql_src,sql_des):
28    download_cmd=cmd+' '+sql_src+' '+sql_des
29    apl_set_log_content(LogLevel(2).name, log_tag, 'database start downloading!')
30    try:
31        result = os.popen(download_cmd)
32        stdout = result.read()
33        print(stdout)
34        if 'Fail' in stdout:
35            raise AplCompareException(stdout.replace('\n\n','').replace('[Fail]', ''))
36        #sql_file=sql_des+'\\'+sql_src.split('/').pop()
37        sql_file = sql_des+sql_src.split('/').pop()
38        apl_set_log_content(LogLevel(2).name, log_tag, '{} download successful!'.format(sql_file))
39        return sql_file
40    except Exception as e:
41        apl_set_log_content(LogLevel(1).name, log_tag, '{}'.format(e.msg))
42        return None
43
44
45def sql_connect(db):
46    try:
47        if not os.path.exists(db):
48            raise AplCompareException('{} is not exists!'.format(db))
49        conn = sqlite3.connect(db)
50        return conn
51    except AplCompareException as e:
52        apl_set_log_content(LogLevel(1).name, log_tag, '{}'.format(e.msg))
53        return None
54
55#数据库语句查询
56def query_records(db,sql):
57    log_content = ''
58    try:
59        conn = sql_connect(db)
60        if conn == None:
61            raise AplCompareException('{} cannot connect!'.format(db))
62        cursor = conn.cursor()
63        cursor.execute(sql)
64        results = cursor.fetchall()
65        conn.close()
66        apl_set_log_content(LogLevel(2).name, log_tag, '"{}" query successful!'.format(sql))
67        return results
68    except sqlite3.OperationalError as e:
69        apl_set_log_content(LogLevel(2).name, log_tag, 'database {}'.format(e.args[0]))
70        return None
71    except AplCompareException as e:
72        apl_set_log_content(LogLevel(1).name, log_tag, '{}'.format(e.msg))
73        return None
74
75#查询hap_token_info_table中的bundle_name和apl
76def query_hap_apl(db,sql):
77    results = query_records(db, sql)
78    return set_map(results)
79
80#查询native_token_info_table中的process_name和apl
81def query_native_apl(db,sql):
82    results = query_records(db, sql)
83    return set_map(results)
84