From b4605d415444fda3d2e7973dc456f585f0f4008a Mon Sep 17 00:00:00 2001 From: Ed Bartosh Date: Wed, 25 Jul 2012 12:34:50 +0300 Subject: [PATCH] Implemented cleaning of temporary files and directories. 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 | 8 +++----- gitbuildsys/cmd_export.py | 3 ++- gitbuildsys/cmd_remotebuild.py | 25 ++++++++++++------------- gitbuildsys/utils.py | 35 +++++++++++++++++++++++++++++++++++ tools/gbs | 4 ++++ 5 files changed, 56 insertions(+), 19 deletions(-) diff --git a/gitbuildsys/cmd_build.py b/gitbuildsys/cmd_build.py index 537902f..8ffab91 100644 --- a/gitbuildsys/cmd_build.py +++ b/gitbuildsys/cmd_build.py @@ -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) diff --git a/gitbuildsys/cmd_export.py b/gitbuildsys/cmd_export.py index 8764520..b5c1172 100644 --- a/gitbuildsys/cmd_export.py +++ b/gitbuildsys/cmd_export.py @@ -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 diff --git a/gitbuildsys/cmd_remotebuild.py b/gitbuildsys/cmd_remotebuild.py index 97ffd29..dd14d7a 100644 --- a/gitbuildsys/cmd_remotebuild.py +++ b/gitbuildsys/cmd_remotebuild.py @@ -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' \ diff --git a/gitbuildsys/utils.py b/gitbuildsys/utils.py index 7ab2394..e2b9aa8 100644 --- a/gitbuildsys/utils.py +++ b/gitbuildsys/utils.py @@ -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. diff --git a/tools/gbs b/tools/gbs index 41641f9..3c83f79 100755 --- 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()) -- 2.7.4