Implemented cleaning of temporary files and directories.
authorEd Bartosh <eduard.bartosh@intel.com>
Wed, 25 Jul 2012 09:34:50 +0000 (12:34 +0300)
committerEd Bartosh <eduard.bartosh@intel.com>
Wed, 25 Jul 2012 11:52:10 +0000 (14:52 +0300)
Implemented in utils.Temp class, which creates temporary file or
directory using tempfile.md[ds]temp and destroys it when object is
deleted(garbage collected when it's out of the scope or upon program
exit).  Fixes #184 and #175.

To avoid abnormal gbs exit(and not deleting temporaries) Exception is
catched in gbs main module.

Change-Id: I04d7067fc293e8b712f183a3d8260dc946058fa7

gitbuildsys/cmd_build.py
gitbuildsys/cmd_export.py
gitbuildsys/cmd_remotebuild.py
gitbuildsys/utils.py
tools/gbs

index 537902f..8ffab91 100644 (file)
@@ -401,8 +401,9 @@ def do(opts, args):
     # Only guess spec filename here, parse later when we have the correct
     # spec file at hand
     specfile = utils.guess_spec(workdir, opts.spec)
-    packaging_dir = os.path.join(workdir, 'packaging/')
-    export_dir = tempfile.mkdtemp(prefix=packaging_dir + 'build_')
+    packaging_dir = os.path.join(workdir, 'packaging/', 'build_')
+    tmpd = utils.Temp(prefix=packaging_dir, directory=True)
+    export_dir = tmpd.path
     with utils.Workdir(workdir):
         if opts.commit:
             commit = opts.commit
@@ -460,6 +461,3 @@ def do(opts, args):
         msger.info('keyboard interrupt, killing build ...')
         subprocess.call(cmd + ["--kill"])
         msger.error('interrupt from keyboard')
-    finally:
-        import shutil
-        shutil.rmtree(export_dir, ignore_errors=True)
index 8764520..b5c1172 100644 (file)
@@ -108,7 +108,8 @@ def do(opts, args):
     # Only guess spec filename here, parse later when we have the correct
     # spec file at hand
     specfile = utils.guess_spec(workdir, opts.spec)
-    export_dir = tempfile.mkdtemp(prefix='gbs_export_', dir=outdir)
+    tempd = Temp(prefix='gbs_export_', dirn=outdir, directory=True)
+    export_dir = tempd.path
     with utils.Workdir(workdir):
         if opts.commit:
             commit = opts.commit
index 97ffd29..dd14d7a 100644 (file)
@@ -120,18 +120,6 @@ def do(opts, args):
     if not os.access(tmpdir, os.W_OK|os.R_OK|os.X_OK):
         msger.error('No access permission to %s, please check' % tmpdir)
 
-    oscrc = OSCRC_TEMPLATE % {
-                "http_debug": 1 if msger.get_loglevel() == 'debug' else 0,
-                "debug": 1 if msger.get_loglevel() == 'verbose' else 0,
-                "apiurl": APISERVER,
-                "user": USER,
-                "passwdx": PASSWDX,
-            }
-    (fds, oscrcpath) = tempfile.mkstemp(dir=tmpdir, prefix='.oscrc')
-    os.close(fds)
-    with file(oscrcpath, 'w+') as foscrc:
-        foscrc.write(oscrc)
-
     # TODO: check ./packaging dir at first
     specs = glob.glob('%s/packaging/*.spec' % workdir)
     if not specs:
@@ -158,6 +146,18 @@ def do(opts, args):
     else:
         target_prj = opts.target_obsprj
 
+    # Create temporary oscrc
+    oscrc = OSCRC_TEMPLATE % {
+                "http_debug": 1 if msger.get_loglevel() == 'debug' else 0,
+                "debug": 1 if msger.get_loglevel() == 'verbose' else 0,
+                "apiurl": APISERVER,
+                "user": USER,
+                "passwdx": PASSWDX,
+            }
+
+    tmpf = Temp(dirn=tmpdir, prefix='.oscrc', content=oscrc)
+    oscrcpath = tmpf.path
+
     if opts.buildlog:
         bs = buildservice.BuildService(apiurl=APISERVER, oscrc=oscrcpath)
         archlist = []
@@ -243,7 +243,6 @@ def do(opts, args):
     except GitRepositoryError, exc:
         msger.error('failed to get commit info: %s' % exc)
 
-    os.unlink(oscrcpath)
     msger.info('local changes submitted to build server successfully')
     msger.info('follow the link to monitor the build progress:\n'
                '  %s/package/show?package=%s&project=%s' \
index 7ab2394..e2b9aa8 100644 (file)
@@ -61,6 +61,41 @@ def guess_spec(workdir, default_spec):
             specfile = specs[0]
     return specfile
 
+class Temp(object):
+    """
+    Create temporary file or directory.
+    Delete it automatically when object is destroyed.
+
+    """
+
+    def __init__(self, suffix='', prefix='tmp', dirn=None,
+                 directory=False, content=None):
+        """
+        Create file or directory using tempfile.mk[sd]temp.
+        If content is provided write it to the file.
+
+        """
+        self.directory = directory
+
+        if directory:
+            path = tempfile.mkdtemp(suffix, prefix, dirn)
+        else:
+            (fds, path) = tempfile.mkstemp(suffix, prefix, dirn)
+            os.close(fds)
+            if content:
+                with file(path, 'w+') as fobj:
+                    fobj.write(content)
+
+        self.path = path
+
+    def __del__(self):
+        """Remove it when object is destroyed."""
+        if os.path.exists(self.path):
+            if self.directory:
+                shutil.rmtree(self.path, True)
+            else:
+                os.unlink(self.path)
+
 class TempCopy(object):
     """Copy original file to temporary file in the same directory as
        original. Creates empty termporary file if original doesn't exist.
index 41641f9..3c83f79 100755 (executable)
--- a/tools/gbs
+++ b/tools/gbs
@@ -409,3 +409,7 @@ if __name__ == '__main__':
             msger.error(traceback.format_exc())
         else:
             msger.error('\n'+str(err))
+
+    except Exception:
+        import traceback
+        msger.error(traceback.format_exc())