misc.py 8.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# Copyright (c) 2020 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import os
import subprocess
from datetime import datetime
Y
yaoxiaoyu 已提交
22
from distutils.spawn import find_executable
L
lubinglun 已提交
23
from hb_internal.common.utils import exec_command
Y
yaoxiaoyu 已提交
24
from hb_internal.common.utils import hb_warning
25
from hb_internal.common.config import Config
26 27 28 29 30 31 32 33 34


class PreBuild:
    def __init__(self, config):
        self._root_path = config.root_path
        self._out_path = config.out_path
        self._log_path = config.log_path

    def set_ccache(self):
35
        ccache_local_dir = os.environ.get('CCACHE_LOCAL_DIR')
36
        ccache_log_suffix = os.environ.get('CCACHE_LOG_SUFFIX')
37 38
        if not ccache_local_dir:
            ccache_local_dir = '.ccache'
39
        ccache_base = os.environ.get('CCACHE_BASE')
L
lijunru 已提交
40 41 42 43 44 45 46

        # The default value is HOME for local users
        if not ccache_base:
            ccache_base = os.environ.get('HOME')
        ccache_base = os.path.join(ccache_base, ccache_local_dir)
        if not os.path.exists(ccache_base):
            os.makedirs(ccache_base)
47 48 49 50
        ccache_log_file_name = "ccache.log"
        if ccache_log_suffix:
            ccache_log_file_name = "ccache." + ccache_log_suffix + ".log"

L
lijunru 已提交
51
        logfile = os.path.join(ccache_base, ccache_log_file_name)
52
        if os.path.exists(logfile):
53
            oldfile_name = ccache_log_file_name + ".old"
L
lijunru 已提交
54
            oldfile = os.path.join(ccache_base, oldfile_name)
55 56 57 58
            if os.path.exists(oldfile):
                os.unlink(oldfile)
            os.rename(logfile, oldfile)

Y
yaoxiaoyu 已提交
59 60 61 62 63
        ccache_path = find_executable('ccache')
        if ccache_path is None:
            hb_warning('Failed to find ccache, ccache disabled.')
            return
        os.environ['CCACHE_EXEC'] = ccache_path
64 65 66
        os.environ['CCACHE_LOGFILE'] = logfile
        os.environ['USE_CCACHE'] = '1'
        os.environ['CCACHE_DIR'] = ccache_base
L
lijunru 已提交
67 68
        os.environ['CCACHE_UMASK'] = '002'
        os.environ['CCACHE_BASEDIR'] = self._root_path
69 70 71
        ccache_max_size = os.environ.get('CCACHE_MAXSIZE')
        if not ccache_max_size:
            ccache_max_size = '100G'
72

73
        cmd = ['ccache', '-M', ccache_max_size]
74 75 76
        exec_command(cmd, log_path=self._log_path)

    def set_pycache(self):
L
lijunru 已提交
77 78 79 80 81 82
        pycache_dir = os.environ.get('CCACHE_BASE')

        # The default value is HOME for local users
        if not pycache_dir:
            pycache_dir = os.environ.get('HOME')
        pycache_dir = os.path.join(pycache_dir, '.pycache')
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
        os.environ['PYCACHE_DIR'] = pycache_dir
        pyd_start_cmd = [
            'python3',
            '{}/build/scripts/util/pyd.py'.format(self._root_path),
            '--root',
            pycache_dir,
            '--start',
        ]
        cmd = ['/bin/bash', '-c', ' '.join(pyd_start_cmd), '&']
        subprocess.Popen(cmd)

    def rename_last_logfile(self):
        logfile = os.path.join(self._out_path, 'build.log')
        if os.path.exists(logfile):
            mtime = os.stat(logfile).st_mtime
Y
yaoxiaoyu 已提交
98
            os.rename(logfile, '{}/build.{}.log'.format(self._out_path, mtime))
99 100

    def prepare(self, args):
101
        actions = [self.set_ccache, self.rename_last_logfile]
102 103 104 105 106 107 108 109 110 111
        for action in actions:
            action()


class PostBuild:
    def __init__(self, config):
        self._root_path = config.root_path
        self._out_path = config.out_path
        self._log_path = config.log_path

112 113 114 115 116 117 118 119 120
    def clean(self, start_time, disable_post_build_args):
        if not disable_post_build_args or 'stat_ccache' not in disable_post_build_args:
                self.stat_ccache()
        if not disable_post_build_args or 'generate_ninja_trace' not in disable_post_build_args:
                self.generate_ninja_trace(start_time)
        if not disable_post_build_args or 'get_warning_list' not in disable_post_build_args:
                self.get_warning_list()
        if not disable_post_build_args or 'compute_overlap_rate' not in disable_post_build_args:
                self.compute_overlap_rate()
W
weifulun 已提交
121 122 123 124 125 126 127 128
            
    def package_image(self):
        image_path = os.path.join(self._out_path, 'packages/phone/images/')
        if os.path.exists(image_path):
            packaged_file_path = os.path.join(self._out_path, 'images.tar.gz')
            cmd = ['tar', '-zcvf', packaged_file_path, image_path]
            exec_command(cmd, log_path=self._log_path)
            
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    def stat_pycache(self):
        cmd = [
            'python3', '{}/build/scripts/util/pyd.py'.format(self._root_path),
            '--stat'
        ]
        exec_command(cmd, log_path=self._log_path)

    def manage_cache_data(self):
        cmd = [
            'python3', '{}/build/scripts/util/pyd.py'.format(self._root_path),
            '--manage'
        ]
        exec_command(cmd, log_path=self._log_path)

    def stop_pyd(self):
        cmd = [
            'python3', '{}/build/scripts/util/pyd.py'.format(self._root_path),
            '--stop'
        ]
        exec_command(cmd, log_path=self._log_path)

    def stat_ccache(self):
151 152 153
        ccache_path = find_executable('ccache')
        if ccache_path is None:
            return
Y
yinchuang 已提交
154 155 156 157 158

        ccache_log_file_name = "ccache.log"
        ccache_log_suffix = os.environ.get('CCACHE_LOG_SUFFIX')
        if ccache_log_suffix:
            ccache_log_file_name = "ccache." + ccache_log_suffix + ".log"
L
lijunru 已提交
159 160 161 162 163 164 165 166 167
        ccache_local_dir = os.environ.get('CCACHE_LOCAL_DIR')
        if not ccache_local_dir:
            ccache_local_dir = '.ccache'
        ccache_base = os.environ.get('CCACHE_BASE')

        # The default value is HOME for local users
        if not ccache_base:
            ccache_base = os.environ.get('HOME')
        ccache_base = os.path.join(ccache_base, ccache_local_dir)
168 169
        cmd = [
            'python3', '{}/build/scripts/summary_ccache_hitrate.py'.format(
L
lijunru 已提交
170
                self._root_path), '{}/{}'.format(ccache_base, ccache_log_file_name)
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
        ]
        exec_command(cmd, log_path=self._log_path)

    def get_warning_list(self):
        cmd = [
            'python3',
            '{}/build/scripts/get_warnings.py'.format(self._root_path),
            '--build-log-file',
            '{}/build.log'.format(self._out_path),
            '--warning-out-file',
            '{}/packages/WarningList.txt'.format(self._out_path),
        ]
        exec_command(cmd, log_path=self._log_path)

    def generate_ninja_trace(self, start_time):
        def get_unixtime(dt):
            epoch = datetime.utcfromtimestamp(0)
            unixtime = '%f' % ((dt - epoch).total_seconds() * 10**9)
            return str(unixtime)

        cmd = [
            'python3',
            '{}/build/scripts/ninja2trace.py'.format(self._root_path),
            '--ninja-log',
            '{}/.ninja_log'.format(self._out_path),
            "--trace-file",
            "{}/build.trace".format(self._out_path),
            "--ninja-start-time",
            get_unixtime(start_time),
            "--duration-file",
            "{}/sorted_action_duration.txt".format(self._out_path),
        ]
        exec_command(cmd, log_path=self._log_path)

    def compute_overlap_rate(self):
206 207
        conf = Config()
        subsystem_config_overlay_path = conf.product_path + '/subsystem_config_overlay.json'
208 209 210 211 212 213 214 215
        if os.path.isfile(subsystem_config_overlay_path):
            cmd = [
                'python3',
                '{}/build/ohos/statistics/build_overlap_statistics.py'.format(
                    self._root_path), "--build-out-dir", self._out_path,
                "--subsystem-config-file",
                "{}/build/subsystem_config.json".format(self._root_path),
                "--subsystem-config-overlay-file",
216
                "{}/subsystem_config_overlay.json".format(conf.product_path),
217 218 219 220 221 222 223 224 225 226 227
                "--root-source-dir", self._root_path
            ]
        else:
            cmd = [
                'python3',
                '{}/build/ohos/statistics/build_overlap_statistics.py'.format(
                    self._root_path), "--build-out-dir", self._out_path,
                "--subsystem-config-file",
                "{}/build/subsystem_config.json".format(self._root_path),
                "--root-source-dir", self._root_path
            ]
228
        exec_command(cmd, log_path=self._log_path)