• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding:utf-8 -*-
3# The build entrance of UniProton.
4
5#
6# Copyright (c) 2021-2023 Huawei Device Co., Ltd.
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11#     http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
19
20import os
21import re
22import sys
23import time
24import logging
25import globle
26
27
28logging.basicConfig(level=logging.NOTSET)
29
30class BuilderNolog():
31
32    def __init__(self,log_file):
33        self.log_file = log_file
34        self.loglevel = 'INFO'
35
36    def run(self, cmd, cwd=os.getcwd(), env=None):
37        exit_code = os.system('cd %s && %s' % (cwd, cmd))
38        if exit_code != 0:
39            with open(self.log_file) as file_handle:
40                for line in file_handle.readlines():
41                    logging.info(line)
42            logging.info("\n--[INFO] more message in logfile [%s] env: [%s]" ,self.log_file, env)
43        return exit_code
44
45    def log_format(self):
46        try:
47            sys.path.append(("%s%s") % (globle.home_path, "/../cmake"))
48            import logcode_format
49            formatter = (logcode_format.init_format(os.getcwd().split('/')[-1]))
50        except ImportError:
51            formatter = '%(asctime)s -- %(levelname)s [UniProton] -- : %(message)s'
52
53        logger_root = logging.getLogger()
54        for handler in logger_root.handlers:
55            logger_root.removeHandler(handler)
56
57        with open(self.log_file, 'r') as rd:
58            lines = rd.readlines()
59        os.remove(self.log_file)
60
61        logformat = logging.Formatter(formatter)
62        fh = logging.FileHandler(self.log_file, mode='a', encoding=sys.getdefaultencoding())
63        fh.setLevel(self.loglevel)
64        fh.setFormatter(logformat)
65
66        logger = logging.getLogger('UniProton')
67        logger.setLevel(self.loglevel)
68        for handler in logger.handlers:
69            logger.removeHandler(handler)
70        logger.addHandler(fh)
71
72        for line in lines:
73            logger.info(line.strip())
74        return
75
76
77def log_msg(level,msg):
78    logging.info('[%s] %s %s %s', level.upper(),'#'*20,msg,'#'*20)
79