15f9996aaSopenharmony_ci#!/usr/bin/env python
25f9996aaSopenharmony_ci# -*- coding: utf-8 -*-
35f9996aaSopenharmony_ci#
45f9996aaSopenharmony_ci# Copyright (c) 2021 Huawei Device Co., Ltd.
55f9996aaSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
65f9996aaSopenharmony_ci# you may not use this file except in compliance with the License.
75f9996aaSopenharmony_ci# You may obtain a copy of the License at
85f9996aaSopenharmony_ci#
95f9996aaSopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
105f9996aaSopenharmony_ci#
115f9996aaSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
125f9996aaSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
135f9996aaSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
145f9996aaSopenharmony_ci# See the License for the specific language governing permissions and
155f9996aaSopenharmony_ci# limitations under the License.
165f9996aaSopenharmony_ci#
175f9996aaSopenharmony_ci
185f9996aaSopenharmony_ciimport argparse
195f9996aaSopenharmony_ciimport stat
205f9996aaSopenharmony_ciimport os
215f9996aaSopenharmony_ciimport sys
225f9996aaSopenharmony_ciimport json
235f9996aaSopenharmony_cisys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
245f9996aaSopenharmony_cifrom utils import makedirs
255f9996aaSopenharmony_ci
265f9996aaSopenharmony_ci
275f9996aaSopenharmony_cidef get_args(args):
285f9996aaSopenharmony_ci    parser = argparse.ArgumentParser()
295f9996aaSopenharmony_ci    parser.add_argument('--depfile', help='Path to depfile (refer to `gn help depfile`)')
305f9996aaSopenharmony_ci    parser.add_argument('--output_dir', help='output directory')
315f9996aaSopenharmony_ci    parser.add_argument('--source_dir', help='source directory')
325f9996aaSopenharmony_ci    parser.add_argument('--target', help='name of target')
335f9996aaSopenharmony_ci    parser.add_argument('--target_label')
345f9996aaSopenharmony_ci    parser.add_argument('--test_type')
355f9996aaSopenharmony_ci    parser.add_argument('--module_list_file', help='file name of module list')
365f9996aaSopenharmony_ci    parser.add_argument('--sources_file_search_root_dir',
375f9996aaSopenharmony_ci                        help='root dir to search xx.sources files')
385f9996aaSopenharmony_ci    parser.add_argument('--sources',
395f9996aaSopenharmony_ci                        help='case sources path defined in test template')
405f9996aaSopenharmony_ci    options = parser.parse_args(args)
415f9996aaSopenharmony_ci    return options
425f9996aaSopenharmony_ci
435f9996aaSopenharmony_ci
445f9996aaSopenharmony_cidef main(args):
455f9996aaSopenharmony_ci    options = get_args(args)
465f9996aaSopenharmony_ci    print("test module_list_file = {}".\
475f9996aaSopenharmony_ci        format(os.path.dirname(options.module_list_file)))
485f9996aaSopenharmony_ci    if not os.path.exists(os.path.dirname(options.module_list_file)):
495f9996aaSopenharmony_ci        makedirs(os.path.dirname(options.module_list_file))
505f9996aaSopenharmony_ci
515f9996aaSopenharmony_ci    with os.fdopen(os.open(options.module_list_file,
525f9996aaSopenharmony_ci                           os.O_RDWR | os.O_CREAT, stat.S_IWUSR | stat.S_IRUSR),
535f9996aaSopenharmony_ci                   'w', encoding='utf-8') as module_list_file:
545f9996aaSopenharmony_ci        contents = json.dumps([{
555f9996aaSopenharmony_ci            'target': options.target,
565f9996aaSopenharmony_ci            'label': options.target_label,
575f9996aaSopenharmony_ci            'source_directory': options.source_dir,
585f9996aaSopenharmony_ci            'output_directory': options.output_dir,
595f9996aaSopenharmony_ci            'test_type': options.test_type
605f9996aaSopenharmony_ci        }])
615f9996aaSopenharmony_ci        module_list_file.write(contents)
625f9996aaSopenharmony_ci
635f9996aaSopenharmony_ci    # create xx.sources file
645f9996aaSopenharmony_ci    fold = os.path.join(options.sources_file_search_root_dir, \
655f9996aaSopenharmony_ci        options.source_dir[(options.source_dir.rfind("../") + len("../")):])
665f9996aaSopenharmony_ci    if not os.path.exists(fold):
675f9996aaSopenharmony_ci        makedirs(fold)
685f9996aaSopenharmony_ci    sources_file_name = fold[fold.rfind("/") + len("/"):] + ".sources"
695f9996aaSopenharmony_ci
705f9996aaSopenharmony_ci    arg_sources = options.sources[0: (len(options.sources) - len(","))]
715f9996aaSopenharmony_ci
725f9996aaSopenharmony_ci    with os.fdopen(os.open(os.path.join(fold, sources_file_name),
735f9996aaSopenharmony_ci                           os.O_RDWR | os.O_CREAT, stat.S_IWUSR | stat.S_IRUSR),
745f9996aaSopenharmony_ci                   'a', encoding='utf-8') as source_defined_file:
755f9996aaSopenharmony_ci        list_sources = arg_sources.split(",")
765f9996aaSopenharmony_ci        for source in list_sources:
775f9996aaSopenharmony_ci            content = "{}/{}\n".format(
785f9996aaSopenharmony_ci                os.path.dirname(options.source_dir), source)
795f9996aaSopenharmony_ci            source_defined_file.write(content)
805f9996aaSopenharmony_ci
815f9996aaSopenharmony_ci
825f9996aaSopenharmony_ciif __name__ == '__main__':
835f9996aaSopenharmony_ci    sys.exit(main(sys.argv[1:]))
84