Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Dropped WithUnpacking mixin, added temp_dir context manager

  • Loading branch information...
commit 322fe16b9e87ceb55b75655bb7e2cd11ea01d074 1 parent e2297e2
@dvarrazzo authored
View
29 pgxnclient/commands/__init__.py
@@ -409,33 +409,6 @@ def get_spec(self):
return super(WithSpecLocal, self).get_spec(_can_be_local=True)
-import shutil
-import tempfile
-from pgxnclient.utils.zip import unpack
-
-class WithUnpacking(object):
- """
- Mixin to implement commands that may deal with zip files.
- """
- def call_with_temp_dir(self, f, *args, **kwargs):
- """
- Call a function in the context of a temporary directory.
-
- Create the temp directory and pass its name as first argument to *f*.
- Other arguments and keywords are passed to *f* too. Upon exit delete
- the directory.
- """
- dir = tempfile.mkdtemp()
- try:
- return f(dir, *args, **kwargs)
- finally:
- shutil.rmtree(dir)
-
- def unpack(self, zipname, destdir):
- """Unpack the zip file *zipname* into *destdir*."""
- return unpack(zipname, destdir)
-
-
class WithPgConfig(object):
"""
Mixin to implement commands that should query :program:`pg_config`.
@@ -489,7 +462,7 @@ def get_pg_config(self):
import shlex
-class WithMake(WithPgConfig, WithUnpacking):
+class WithMake(WithPgConfig):
"""
Mixin to implement commands that should invoke :program:`make`.
"""
View
9 pgxnclient/commands/install.py
@@ -23,6 +23,8 @@
from pgxnclient.network import download
from pgxnclient.commands import Command, WithDatabase, WithMake, WithPgConfig
from pgxnclient.commands import WithSpec, WithSpecLocal, WithSudo
+from pgxnclient.utils.zip import unpack
+from pgxnclient.utils.temp import temp_dir
from pgxnclient.utils.strings import Identifier
logger = logging.getLogger('pgxnclient.commands')
@@ -93,18 +95,19 @@ class InstallUninstall(WithMake, WithSpecLocal, Command):
Base class to implement the ``install`` and ``uninstall`` commands.
"""
def run(self):
- return self.call_with_temp_dir(self._run)
+ with temp_dir() as dir:
+ return self._run(dir)
def _run(self, dir):
spec = self.get_spec()
if spec.is_dir():
pdir = os.path.abspath(spec.dirname)
elif spec.is_file():
- pdir = self.unpack(spec.filename, dir)
+ pdir = unpack(spec.filename, dir)
else: # download
self.opts.target = dir
fn = Download(self.opts).run()
- pdir = self.unpack(fn, dir)
+ pdir = unpack(fn, dir)
self.maybe_run_configure(pdir)
View
4 pgxnclient/tests/test_commands.py
@@ -409,7 +409,7 @@ def test_install_sudo(self):
self.assertCallArgs(['gksudo', '-d', 'hello world', self.make],
self.mock_popen.call_args_list[1][0][0][:4])
- @patch('pgxnclient.commands.unpack')
+ @patch('pgxnclient.commands.install.unpack')
def test_install_local_zip(self, mock_unpack):
from pgxnclient.utils.zip import unpack
mock_unpack.side_effect = unpack
@@ -580,7 +580,7 @@ def test_check_psql_options(self, mock_get):
args = self.mock_popen.call_args[0][0]
self.assertEqual('somewhere', args[args.index('--host') + 1])
- @patch('pgxnclient.commands.unpack')
+ @patch('pgxnclient.commands.install.unpack')
@patch('pgxnclient.api.get_file')
def test_load_local_zip(self, mock_get, mock_unpack):
mock_get.side_effect = lambda *args: self.fail('network invoked')
View
19 pgxnclient/utils/temp.py
@@ -0,0 +1,19 @@
+"""
+pgxnclient -- temp files utilities
+"""
+
+# Copyright (C) 2011-2012 Daniele Varrazzo
+
+# This file is part of the PGXN client
+
+import shutil
+import tempfile
+import contextlib
+
+@contextlib.contextmanager
+def temp_dir():
+ """Context manager to create a temp dir and delete after usage."""
+ dir = tempfile.mkdtemp()
+ yield dir
+ shutil.rmtree(dir)
+
Please sign in to comment.
Something went wrong with that request. Please try again.