From e007c943e7868389b5dd2d0a96b7c2a463fcd5c5 Mon Sep 17 00:00:00 2001 From: lianghuikang <505519763@qq.com> Date: Fri, 16 Jul 2021 13:43:37 +0800 Subject: [PATCH] fix static check --- configure.py | 2 +- convert_tf2npu/file_op.py | 33 ++++++++----------- .../interface_checker/check_interface.py | 14 ++++++++ .../estimator/npu/mnist_softmax_npu.py | 3 +- tf_adapter_2.x/configure.py | 2 +- tf_adapter_2.x/examples/basic_tests.py | 14 ++++++++ 6 files changed, 46 insertions(+), 22 deletions(-) diff --git a/configure.py b/configure.py index 843220b06..30fdd70c3 100755 --- a/configure.py +++ b/configure.py @@ -126,7 +126,7 @@ def setup_python(): def setup_ascend(env_path): """Get ascend install path.""" - default_ascend_path = "/usr/local/Ascend" + default_ascend_path = os.path.realpath("/usr/local/Ascend") custom_ascend_path = env_path while True: if not custom_ascend_path: diff --git a/convert_tf2npu/file_op.py b/convert_tf2npu/file_op.py index b1f39505a..98a7e02db 100644 --- a/convert_tf2npu/file_op.py +++ b/convert_tf2npu/file_op.py @@ -39,15 +39,13 @@ def mkdir_and_copyfile(srcfile, dstpath, file_name): shutil.copyfile(os.path.join(srcfile, file_name), os.path.join(dstpath, file_name)) def write_output_after_conver(out_file, dst_content): - file = open(out_file, 'w') - file.write(dst_content) - file.close() + with open(out_file, 'w') as file: + file.write(dst_content) def write_report_after_conver(new_file_path, report_file, dst_content): mkdir(new_file_path) - file = open(os.path.join(new_file_path, report_file), 'w') - file.write(dst_content) - file.close() + with open(os.path.join(new_file_path, report_file), 'w') as file: + file.write(dst_content) def get_bit_val(value, index): if value & (1 << index): @@ -63,29 +61,26 @@ def write_report_terminator(content): if get_bit_val(value, times - 1): file = util_global.get_value('report_file')[times - 1] if os.path.exists(os.path.join(report_path, file)): - file = open(os.path.join(report_path, file), 'a') - file.write(content) - file.write("\r\n") - file.write("\r\n") - file.close() + with open(os.path.join(report_path, file), 'a') as file: + file.write(content) + file.write("\r\n") + file.write("\r\n") times = times - 1 util_global.set_value('report_file_status', 0) def write_conver_report(content, file): report_path = util_global.get_value('report') mkdir(report_path) - file = open(os.path.join(report_path, file), 'a') - file.write(content) - file.write("\r\n") - file.close() + with open(os.path.join(report_path, file), 'a') as file: + file.write(content) + file.write("\r\n") def write_analysis_report(content, file): report_path = util_global.get_value('report') mkdir(report_path) - file = open(os.path.join(report_path, file), 'a') - file.write(content) - file.write("\r\n") - file.close() + with open(os.path.join(report_path, file), 'a') as file: + file.write(content) + file.write("\r\n") def abs_join(abs1, abs2): abs2 = os.fspath(abs2) diff --git a/tf_adapter/interface_checker/check_interface.py b/tf_adapter/interface_checker/check_interface.py index 9b8adaa78..43d90d58b 100644 --- a/tf_adapter/interface_checker/check_interface.py +++ b/tf_adapter/interface_checker/check_interface.py @@ -1,5 +1,19 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +# Copyright (C) 2021. Huawei Technologies 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. +# ============================================================================== + """ check interface """ diff --git a/tf_adapter/python/npu_bridge/estimator/npu/mnist_softmax_npu.py b/tf_adapter/python/npu_bridge/estimator/npu/mnist_softmax_npu.py index da00912f2..4d185bf87 100644 --- a/tf_adapter/python/npu_bridge/estimator/npu/mnist_softmax_npu.py +++ b/tf_adapter/python/npu_bridge/estimator/npu/mnist_softmax_npu.py @@ -24,6 +24,7 @@ from __future__ import division from __future__ import print_function import argparse +import os import sys import tensorflow as tf @@ -101,7 +102,7 @@ if __name__ == '__main__': parser.add_argument( '--data_dir', type=str, - default='/tmp/tensorflow/mnist/input_data', + default=os.path.realpath('/tmp/tensorflow/mnist/input_data'), help='Directory for storing input data') parser.add_argument( '--npu', type=bool, default=True, help='Turn npu on') diff --git a/tf_adapter_2.x/configure.py b/tf_adapter_2.x/configure.py index 5d93bfa81..890ea6e83 100644 --- a/tf_adapter_2.x/configure.py +++ b/tf_adapter_2.x/configure.py @@ -107,7 +107,7 @@ def setup_python(env_path): def setup_ascend(env_path): """Get ascend install path.""" - default_ascend_path = "/usr/local/Ascend" + default_ascend_path = os.path.realpath("/usr/local/Ascend") ask_ascend_path = ('Please specify the location of ascend. [Default is ' '%s]\n(You can make this quiet by set env [ASCEND_INSTALLED_PATH]): ') % default_ascend_path custom_ascend_path = env_path diff --git a/tf_adapter_2.x/examples/basic_tests.py b/tf_adapter_2.x/examples/basic_tests.py index 52cb9d023..4e9cd68a3 100644 --- a/tf_adapter_2.x/examples/basic_tests.py +++ b/tf_adapter_2.x/examples/basic_tests.py @@ -1,5 +1,19 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +# Copyright (C) 2021. Huawei Technologies 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. +# ============================================================================== + import unittest import os import tensorflow as tf -- Gitee