• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2024 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
17
18
19# 插入新版宏内容
20def updated_version(file_path: str, new_version: int):
21    version_define = '#define OH_API_VERSION_'
22    version_current_define = '#define OH_CURRENT_API_VERSION OH_API_VERSION_'
23    # 需要插入的内容
24    new_content = ''
25    # 插入的标志位置
26    insert_after_line = '#define SDK_VERSION_9 9'
27    # 旧版本
28    old_version = 9
29    # 更新需插入的内容
30    for i in range(new_version - old_version):
31        new_content += '{}{} {}\n'.format(version_define, old_version + i + 1, old_version + i + 1)
32    new_content_tag = '{}{}\n'.format(version_current_define, new_version)
33    new_content += new_content_tag
34
35    with open(file_path, 'r') as fp:
36        lines = fp.readlines()
37
38    # 查找插入位置的索引
39    insert_position = None
40    for i, line in enumerate(lines):
41        if insert_after_line in line:
42            insert_position = i + 1  # 插入在该行之后
43            break
44
45    # 如果找到插入位置
46    if (insert_position is not None) and (new_content_tag not in lines):
47        # 插入新内容
48        lines.insert(insert_position, new_content)
49        # 更新修改后的内容
50        with open(file_path, 'w') as fp:
51            fp.writelines(lines)
52    elif insert_position is None:
53        raise Exception('未找到插入位置: {},写入失败'.format(insert_after_line))
54
55
56# 入口
57def process_target_version():
58    try:
59        parser = argparse.ArgumentParser(description='Updated version')
60        # 定义命令行参数
61        parser.add_argument('-p', '--path', type=str, required=True, help='Path to the input file')
62        parser.add_argument('-v', '--version', type=str, required=True, help='Version of the api')
63        args = parser.parse_args()
64        # 获取文件路径和版本
65        input_file = args.path
66        api_version = int(args.version)
67        # 写入新版宏内容
68        updated_version(input_file, api_version)
69    except Exception as e:
70        raise e
71
72
73if __name__ == '__main__':
74    process_target_version()
75