File 0002-dev-hb_report-Using-Tempfile-class-to-manage-tempfil.patch of Package crmsh.12576

From 11d206b7320a1b8953cdecb5bc1815aded21df39 Mon Sep 17 00:00:00 2001
From: liangxin1300 <XLiang@suse.com>
Date: Wed, 3 Apr 2019 13:38:12 +0800
Subject: [PATCH 2/4] dev: hb_report: Using Tempfile class to manage tempfiles

---
 hb_report/constants.py |  1 -
 hb_report/hb_report.in |  6 ++--
 hb_report/utillib.py   | 67 ++++++++++++++++++++++++------------------
 3 files changed, 40 insertions(+), 34 deletions(-)

diff --git a/hb_report/constants.py b/hb_report/constants.py
index 5f1276ca..8afefb42 100644
--- a/hb_report/constants.py
+++ b/hb_report/constants.py
@@ -49,7 +49,6 @@ SSH_USER = ""
 SUDO = ""
 THIS_IS_NODE = 0
 TMP = None
-TMPFLIST = None
 TO_TIME = 0
 TRY_SSH = "root hacluster"
 # UNIQUE_MSG = "Mark:HB_REPORT:%d" % now_second
diff --git a/hb_report/hb_report.in b/hb_report/hb_report.in
index 5f55dd1a..40de4047 100755
--- a/hb_report/hb_report.in
+++ b/hb_report/hb_report.in
@@ -2,7 +2,6 @@
 # Copyright (C) 2017 Xin Liang <XLiang@suse.com>
 # See COPYING for license information.
 
-import atexit
 import getopt
 import multiprocessing
 import os
@@ -11,6 +10,7 @@ import sys
 import datetime
 import shutil
 
+sys.path.append(os.path.dirname(os.path.realpath(__file__)))
 import constants
 import utillib
 from crmsh import utils as crmutils
@@ -210,10 +210,8 @@ def parse_argument(argv):
 def run():
 
     utillib.check_env()
-    constants.TMPFLIST = utillib.create_tempfile()
-    atexit.register(utillib.drop_tempfiles)
     tmpdir = utillib.make_temp_dir()
-    utillib.add_tmpfiles(tmpdir)
+    utillib.add_tempfiles(tmpdir)
 
     #
     # get and check options; and the destination
diff --git a/hb_report/utillib.py b/hb_report/utillib.py
index 9612abc1..57ca0dc6 100644
--- a/hb_report/utillib.py
+++ b/hb_report/utillib.py
@@ -15,17 +15,47 @@ import stat
 import string
 import subprocess
 import sys
+import atexit
 import tempfile
 import contextlib
 from dateutil import tz
 from threading import Timer
 
+sys.path.append(os.path.dirname(os.path.realpath(__file__)))
 import constants
 import crmsh.config
 from crmsh import msg as crmmsg
 from crmsh import utils as crmutils
 
 
+class Tempfile(object):
+
+    def __init__(self):
+        self.file = create_tempfile()
+        log_debug("create tempfile \"{}\"".format(self.file))
+
+    def add(self, filename):
+        with open(self.file, 'a') as f:
+            f.write(filename + '\n')
+        log_debug("add tempfile \"{}\" to \"{}\"".format(filename, self.file))
+
+    def drop(self):
+        with open(self.file, 'r') as f:
+            for line in f.read().split('\n'):
+                if os.path.isdir(line):
+                    shutil.rmtree(line)
+                if os.path.isfile(line):
+                    os.remove(line)
+        os.remove(self.file)
+        log_debug("remove tempfile \"{}\"".format(self.file))
+
+
+def add_tempfiles(filename):
+    t = Tempfile()
+    t.add(filename)
+    atexit.register(t.drop)
+
+
 def _mkdir(directory):
     """
     from crmsh/tmpfiles.py
@@ -37,14 +67,6 @@ def _mkdir(directory):
             log_fatal("Failed to create directory: %s" % (err))
 
 
-def add_tmpfiles(contents):
-    """
-    add contents for removing when program exit
-    """
-    with open(constants.TMPFLIST, 'a') as f:
-        f.write(contents+'\n')
-
-
 def arch_logs(logf, from_time, to_time):
     """
     go through archived logs (timewise backwards) and see if there
@@ -450,19 +472,6 @@ def dlm_dump():
         crmutils.str2file(out_string, dlm_f)
 
 
-def drop_tempfiles():
-    """
-    tmp files business
-    """
-    with open(constants.TMPFLIST, 'r') as f:
-        for line in f.read().split('\n'):
-            if os.path.isdir(line):
-                shutil.rmtree(line)
-            if os.path.isfile(line):
-                os.remove(line)
-    os.remove(constants.TMPFLIST)
-
-
 def dump_log(logf, from_line, to_line):
     if not from_line:
         return
@@ -549,20 +558,19 @@ def find_files(dirs, from_time, to_time):
         log_warning("sorry, can't find files based on time if you don't supply time")
         return
 
-    from_stamp = create_tempfile(from_time)
-    add_tmpfiles(from_stamp)
-    findexp = "-newer %s" % from_stamp
+    file_with_stamp = create_tempfile(from_time)
+    findexp = "-newer %s" % file_with_stamp
 
     if crmutils.is_int(to_time) and to_time > 0:
-        to_stamp = create_tempfile(to_time)
-        add_tmpfiles(to_stamp)
-        findexp += " ! -newer %s" % to_stamp
+        file_with_stamp = create_tempfile(to_time)
+        findexp += " ! -newer %s" % file_with_stamp
 
     cmd = r"find %s -type f %s" % (dirs, findexp)
     cmd_res = get_command_info(cmd)[1].strip()
     if cmd_res:
         res = cmd_res.split('\n')
 
+    os.remove(file_with_stamp)
     return res
 
 
@@ -1358,7 +1366,8 @@ def print_logseg(logf, from_time, to_time):
     cat = find_decompressor(logf)
     if cat != "cat":
         tmp = create_tempfile()
-        add_tmpfiles(tmp)
+        add_tempfiles(tmp)
+
         cmd = "%s %s > %s" % (cat, logf, tmp)
         code, out, err = crmutils.get_stdout_stderr(cmd)
         if code != 0:
@@ -1444,7 +1453,7 @@ def sanitize_one(in_file, mode=None):
             return 0
 
     ref = create_tempfile()
-    add_tmpfiles(ref)
+    add_tempfiles(ref)
     touch_r(in_file, ref)
 
     with open_(in_file, 'w') as f:
-- 
2.22.1

openSUSE Build Service is sponsored by