[utils] fix tempdir ctxmgr to ensure we go back to a fine cwd (closes #3652139)

We go back to the wd we were at the time of entering the context.

This is required at least to build a RPM packages, otherwise the RPM generation will fail if there are more than one rpm distribution, because at the end of the build of a RPM, the cwd is a tmpdir which has been deleted (is keep_dir is false)...

authorDavid Douard <david.douard@logilab.fr>
changesetb8c098b5561c
branchdefault
phasepublic
hiddenno
parent revision#e38dd75bc109 Added tag 1.3.0, debian/1.3.0-1 for changeset fa9855b25771
child revision#6e7f7b4538c1 [test] fix unittest_pkginfo
files modified by this revision
logilab/packaging/lgp/build.py
logilab/packaging/lgp/utils.py
# HG changeset patch
# User David Douard <david.douard@logilab.fr>
# Date 1452272033 -3600
# Fri Jan 08 17:53:53 2016 +0100
# Node ID b8c098b5561ceada59775fd11758741fb8406d04
# Parent e38dd75bc109b95f961ca5a3175f1aeb48076cbf
[utils] fix tempdir ctxmgr to ensure we go back to a fine cwd (closes #3652139)

We go back to the wd we were at the time of entering the context.

This is required at least to build a RPM packages,
otherwise the RPM generation will fail if there are more than one rpm
distribution, because at the end of the build of a RPM, the cwd is a tmpdir
which has been deleted (is keep_dir is false)...

diff --git a/logilab/packaging/lgp/build.py b/logilab/packaging/lgp/build.py
@@ -178,10 +178,11 @@
1          # create the upstream tarball if necessary and move it to result directory
2          with tempdir(self.config.keep_tmpdir) as tmpdir:
3              self.make_orig_tarball(tmpdir)
4              try:
5                  for distrib in  self.distributions:
6 +                    self.logger.info('Attempting to build %s', distrib)
7                      with tempdir(self.config.keep_tmpdir) as src_tmpdir:
8                          if self.config.rpm or distrib.startswith(('fedora', 'epel')):
9                              specfile = self.get_rpm_specfile()
10                              if specfile is None:
11                                  # do not crash if a project does not (yet) provide a .spec file
diff --git a/logilab/packaging/lgp/utils.py b/logilab/packaging/lgp/utils.py
@@ -17,10 +17,11 @@
12  """
13 
14  import glob
15  import sys
16  import time
17 +import os
18  import os.path as osp
19  from subprocess import Popen, PIPE
20  from contextlib import contextmanager
21  import shutil
22  import tempfile
@@ -243,15 +244,19 @@
23 
24      See also tempfile.TemporaryDirectory in python >= 3.2
25      """
26      tmpdir = tempfile.mkdtemp()
27      logging.debug('using new build directory... (%s)', tmpdir)
28 +    cwd = os.getcwd()
29      try:
30          yield tmpdir
31      except Exception, exc:
32          raise
33      finally:
34 +        # ensure we pop to the dir we were (before entering the
35 +        # ctx), to prevent from sewing the branch we sit on...
36 +        os.chdir(cwd)
37          if not keep_tmpdir:
38              shutil.rmtree(tmpdir)
39          else:
40              logging.warn("build directory not deleted: %s", tmpdir)
41