1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# Copyright (c) 2023 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17import os 18import sys 19import argparse 20import subprocess 21 22 23def input_path_check(arg): 24 """ 25 Argument check, which is used to check whether the specified arg is a path. 26 :param arg: the arg to check. 27 :return: Check result, which is False if the arg is invalid. 28 """ 29 if not os.path.isdir(arg) and not os.path.isfile(arg): 30 return False 31 return arg 32 33 34def jar_name_check(arg): 35 """ 36 Argument check, which is used to check whether the specified arg is none. 37 :param arg: the arg to check. 38 :return: Check result, which is False if the arg is invalid. 39 """ 40 if arg is None: 41 return False 42 return arg 43 44 45def build_jar(source_file, cls_out, manifest_file, jar_out, jar_name): 46 jar_file = os.path.join(jar_out, '%s.jar' % jar_name) 47 javac_cmd = ['javac', '-d', cls_out, source_file] 48 subprocess.call(javac_cmd, shell=False) 49 jar_cmd = ['jar', 'cvfm', jar_file, manifest_file, '-C', cls_out, '.'] 50 subprocess.call(jar_cmd, shell=False) 51 return True 52 53 54def main(argv): 55 """ 56 Entry function. 57 """ 58 parser = argparse.ArgumentParser() 59 60 parser.add_argument("-sf", "--source_file", type=input_path_check, 61 default=None, help="Source file path.") 62 parser.add_argument("-co", "--cls_out", type=input_path_check, 63 default=None, help="Class out path.") 64 parser.add_argument("-mf", "--manifest_file", type=input_path_check, 65 default=None, help="Manifest file path.") 66 parser.add_argument("-jo", "--jar_out", type=input_path_check, 67 default=None, help="Jar out path.") 68 parser.add_argument("-jn", "--jar_name", type=jar_name_check, 69 default=None, help="Jar name.") 70 71 args = parser.parse_args(argv) 72 73 # Generate the jar. 74 build_re = build_jar(args.source_file, args.cls_out, args.manifest_file, args.jar_out, args.jar_name) 75 76if __name__ == '__main__': 77 main(sys.argv[1:]) 78