140681896Sopenharmony_ci#!/usr/bin/env python3
240681896Sopenharmony_ci# -*- coding: utf-8 -*-
340681896Sopenharmony_ci
440681896Sopenharmony_ci# Copyright (c) 2023 Huawei Device Co., Ltd.
540681896Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
640681896Sopenharmony_ci# you may not use this file except in compliance with the License.
740681896Sopenharmony_ci# You may obtain a copy of the License at
840681896Sopenharmony_ci#
940681896Sopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0
1040681896Sopenharmony_ci#
1140681896Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
1240681896Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
1340681896Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1440681896Sopenharmony_ci# See the License for the specific language governing permissions and
1540681896Sopenharmony_ci# limitations under the License.
1640681896Sopenharmony_ci
1740681896Sopenharmony_ciimport os
1840681896Sopenharmony_ciimport sys
1940681896Sopenharmony_ciimport argparse
2040681896Sopenharmony_ciimport subprocess
2140681896Sopenharmony_ci
2240681896Sopenharmony_ci
2340681896Sopenharmony_cidef input_path_check(arg):
2440681896Sopenharmony_ci    """
2540681896Sopenharmony_ci    Argument check, which is used to check whether the specified arg is a path.
2640681896Sopenharmony_ci    :param arg: the arg to check.
2740681896Sopenharmony_ci    :return: Check result, which is False if the arg is invalid.
2840681896Sopenharmony_ci    """
2940681896Sopenharmony_ci    if not os.path.isdir(arg) and not os.path.isfile(arg):
3040681896Sopenharmony_ci        return False
3140681896Sopenharmony_ci    return arg
3240681896Sopenharmony_ci
3340681896Sopenharmony_ci
3440681896Sopenharmony_cidef jar_name_check(arg):
3540681896Sopenharmony_ci    """
3640681896Sopenharmony_ci    Argument check, which is used to check whether the specified arg is none.
3740681896Sopenharmony_ci    :param arg: the arg to check.
3840681896Sopenharmony_ci    :return: Check result, which is False if the arg is invalid.
3940681896Sopenharmony_ci    """
4040681896Sopenharmony_ci    if arg is None:
4140681896Sopenharmony_ci        return False
4240681896Sopenharmony_ci    return arg
4340681896Sopenharmony_ci
4440681896Sopenharmony_ci
4540681896Sopenharmony_cidef build_jar(source_file, cls_out, manifest_file, jar_out, jar_name):
4640681896Sopenharmony_ci    jar_file = os.path.join(jar_out, '%s.jar' % jar_name)
4740681896Sopenharmony_ci    javac_cmd = ['javac', '-d', cls_out, source_file]
4840681896Sopenharmony_ci    subprocess.call(javac_cmd, shell=False)
4940681896Sopenharmony_ci    jar_cmd = ['jar', 'cvfm', jar_file, manifest_file, '-C', cls_out, '.']
5040681896Sopenharmony_ci    subprocess.call(jar_cmd, shell=False)
5140681896Sopenharmony_ci    return True
5240681896Sopenharmony_ci
5340681896Sopenharmony_ci
5440681896Sopenharmony_cidef main(argv):
5540681896Sopenharmony_ci    """
5640681896Sopenharmony_ci    Entry function.
5740681896Sopenharmony_ci    """
5840681896Sopenharmony_ci    parser = argparse.ArgumentParser()
5940681896Sopenharmony_ci
6040681896Sopenharmony_ci    parser.add_argument("-sf", "--source_file", type=input_path_check,
6140681896Sopenharmony_ci                        default=None, help="Source file path.")
6240681896Sopenharmony_ci    parser.add_argument("-co", "--cls_out", type=input_path_check,
6340681896Sopenharmony_ci                        default=None, help="Class out path.")
6440681896Sopenharmony_ci    parser.add_argument("-mf", "--manifest_file", type=input_path_check,
6540681896Sopenharmony_ci                        default=None, help="Manifest file path.")
6640681896Sopenharmony_ci    parser.add_argument("-jo", "--jar_out", type=input_path_check,
6740681896Sopenharmony_ci                        default=None, help="Jar out path.")
6840681896Sopenharmony_ci    parser.add_argument("-jn", "--jar_name", type=jar_name_check,
6940681896Sopenharmony_ci                        default=None, help="Jar name.")
7040681896Sopenharmony_ci
7140681896Sopenharmony_ci    args = parser.parse_args(argv)
7240681896Sopenharmony_ci
7340681896Sopenharmony_ci    # Generate the jar.
7440681896Sopenharmony_ci    build_re = build_jar(args.source_file, args.cls_out, args.manifest_file, args.jar_out, args.jar_name)
7540681896Sopenharmony_ci
7640681896Sopenharmony_ciif __name__ == '__main__':
7740681896Sopenharmony_ci    main(sys.argv[1:])
78