• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2025 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import argparse
17import os
18import shutil
19import subprocess
20import sys
21import tarfile
22
23
24def copy_files(source_path, dest_path, is_file=False):
25    try:
26        if is_file:
27            os.makedirs(os.path.dirname(dest_path), exist_ok=True)
28            shutil.copy(source_path, dest_path)
29        else:
30            shutil.copytree(source_path, dest_path, dirs_exist_ok=True,
31                symlinks=True)
32    except Exception as err:
33        raise Exception("Copy files failed. Error: " + str(err)) from err
34
35
36def run_cmd(cmd, execution_path=None):
37    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
38                           stdin=subprocess.PIPE,
39                           stderr=subprocess.PIPE,
40                           cwd=execution_path)
41    stdout, stderr = proc.communicate(timeout=1000)
42    if proc.returncode != 0:
43        raise Exception(stdout.decode() + stderr.decode())
44
45
46def build(options):
47    build_cmd = [options.npm, 'run', 'compile:tsc']
48    run_cmd(build_cmd, options.source_path)
49
50
51def copy_output(options):
52    run_cmd(['rm', '-rf', options.output_path])
53    copy_files(os.path.join(options.source_path, 'build/lib'),
54               os.path.join(options.output_path, 'build/lib'))
55
56    copy_files(os.path.join(options.source_path, 'koalaui'),
57            os.path.join(options.output_path, 'koalaui'))
58
59    copy_files(os.path.join(options.source_path, 'package.json'),
60               os.path.join(options.output_path, 'package.json'), True)
61
62    if options.current_os == "mingw" :
63        copy_files(os.path.join(options.root_out_dir, 'libes2panda.dll'),
64                os.path.join(options.output_path, 'build/native/es2panda.node'), True)
65        copy_files(os.path.join(options.root_out_dir, 'libes2panda.dll'),
66            os.path.join(options.source_path, 'build/native/es2panda.node'), True)
67
68    if options.current_os == "linux" or options.current_os == "mac" :
69        copy_files(os.path.join(options.root_out_dir, 'libes2panda.node'),
70                os.path.join(options.output_path, 'build/native/es2panda.node'), True)
71        copy_files(os.path.join(options.root_out_dir, 'libes2panda.node'),
72                os.path.join(options.source_path, 'build/native/es2panda.node'), True)
73
74
75def parse_args():
76    parser = argparse.ArgumentParser()
77    parser.add_argument('--npm', help='path to a npm exetuable')
78    parser.add_argument('--source_path', help='path to build system source')
79    parser.add_argument('--output_path', help='path to output')
80    parser.add_argument('--root_out_dir', help='path to root out')
81    parser.add_argument('--current_os', help='current_os')
82
83    options = parser.parse_args()
84    return options
85
86
87def main():
88    options = parse_args()
89
90    build(options)
91    copy_output(options)
92
93
94if __name__ == '__main__':
95    sys.exit(main())