From b8574f9c51a4314bcadf7f0d6ccd1b12a41955c4 Mon Sep 17 00:00:00 2001 From: chiourung_huang Date: Wed, 19 Feb 2020 09:26:45 +0000 Subject: [PATCH] Don't return error when execute "portstat -d" If execute "portstat -d" without execute "portstat" first, then this command would fail due to "No such file or directory" Before fix: root@as5812-54x:/home/admin# portstat -d Traceback (most recent call last): File "/usr/bin/portstat", line 411, in main() File "/usr/bin/portstat", line 355, in main if os.listdir(cnstat_dir) == []: OSError: [Errno 2] No such file or directory: '/tmp/portstat-0' Signed-off-by: chiourung_huang --- scripts/portstat | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/scripts/portstat b/scripts/portstat index 1fe00af2cc..72e53c64a2 100755 --- a/scripts/portstat +++ b/scripts/portstat @@ -382,26 +382,32 @@ Examples: cnstat_fqn_file = cnstat_dir + "/" + cnstat_file if delete_all_stats: - for file in os.listdir(cnstat_dir): - os.remove(cnstat_dir + "/" + file) + if os.path.isdir(cnstat_dir): + for file in os.listdir(cnstat_dir): + os.remove(cnstat_dir + "/" + file) - try: - os.rmdir(cnstat_dir) + try: + os.rmdir(cnstat_dir) + sys.exit(0) + except IOError as e: + print e.errno, e + sys.exit(e) + else: sys.exit(0) - except IOError as e: - print(e.errno, e) - sys.exit(e) if delete_saved_stats: - try: - os.remove(cnstat_fqn_file) - except IOError as e: - if e.errno != ENOENT: - print(e.errno, e) - sys.exit(1) - finally: - if os.listdir(cnstat_dir) == []: - os.rmdir(cnstat_dir) + if os.path.exists(cnstat_fqn_file): + try: + os.remove(cnstat_fqn_file) + except IOError as e: + if e.errno != ENOENT: + print e.errno, e + sys.exit(1) + finally: + if os.listdir(cnstat_dir) == []: + os.rmdir(cnstat_dir) + sys.exit(0) + else: sys.exit(0) intf_list = parse_interface_in_filter(intf_fs)