Skip to content

Commit

Permalink
[one-cmds] Introduce export_constant.py (Samsung#10516)
Browse files Browse the repository at this point in the history
* [one-cmds] Introduce export_constant.py

This commit introduces a script that exports CONSTANT value with given
format.

ONE-DCO-1.0-Signed-off-by: seongwoo <[email protected]>

* supplement the explanation of 'format' option.
  • Loading branch information
mhs4670go authored Mar 16, 2023
1 parent 79b942d commit fdd48b1
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions compiler/one-cmds/onelib/export_constant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env python

# Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from constant import CONSTANT

import argparse
import configparser


def main():
parser = argparse.ArgumentParser(
description='Export CONSTANT value with given file format.')
parser.add_argument(
'-c', '--constant', type=str, required=True, help='Constant name to export')
parser.add_argument(
'-f',
'--format',
type=str,
required=True,
choices=['cfg', 'txt'],
help=
'File format to export. The created cfg file contains CONSTANT under the one-optimize section.'
)
parser.add_argument(
'--exclusive',
action='store_true',
help='Exports the rest of the options except for the given constant')
parser.add_argument(
'-o', '--output_path', type=str, required=True, help='Path to output')

args = parser.parse_args()

if not hasattr(CONSTANT, args.constant):
raise NameError('Not found given constant name')

if args.exclusive:
constant_to_exclude = getattr(CONSTANT, args.constant)
constant_to_export = []
for opt in CONSTANT.OPTIMIZATION_OPTS:
if opt[0] in constant_to_exclude:
continue
constant_to_export.append(opt[0])
else:
constant_to_export = getattr(CONSTANT, args.constant)

if args.format == 'cfg':
SECTION_TO_EXPORT = 'one-optimize'
config = configparser.ConfigParser()
config[SECTION_TO_EXPORT] = dict()
for constant in constant_to_export:
config[SECTION_TO_EXPORT][constant] = 'True'

with open(args.output_path, 'w') as f:
config.write(f)

if args.format == 'txt':
with open(args.output_path, 'w') as f:
for constant in constant_to_export:
f.write(f"{constant}\n")


if __name__ == '__main__':
main()

0 comments on commit fdd48b1

Please sign in to comment.