From fbc2b8237a46d417f18be8938a1653945e189c54 Mon Sep 17 00:00:00 2001 From: Daniel Firth Date: Fri, 20 Dec 2013 15:47:10 +0000 Subject: Replaces Tempdir with fs.tempfs.TempFS --- morphlib/__init__.py | 1 - morphlib/cachedrepo_tests.py | 65 +++++++++++++------------- morphlib/plugins/artifact_inspection_plugin.py | 31 ++++++------ morphlib/tempdir.py | 48 ------------------- morphlib/tempdir_tests.py | 57 ---------------------- 5 files changed, 50 insertions(+), 152 deletions(-) delete mode 100644 morphlib/tempdir.py delete mode 100644 morphlib/tempdir_tests.py diff --git a/morphlib/__init__.py b/morphlib/__init__.py index 8f39cb30..fcfcee54 100644 --- a/morphlib/__init__.py +++ b/morphlib/__init__.py @@ -82,7 +82,6 @@ import stagingarea import stopwatch import sysbranchdir import systemmetadatadir -import tempdir import util import workspace diff --git a/morphlib/cachedrepo_tests.py b/morphlib/cachedrepo_tests.py index b1825eba..8afe2063 100644 --- a/morphlib/cachedrepo_tests.py +++ b/morphlib/cachedrepo_tests.py @@ -18,12 +18,11 @@ import logging import os import unittest +import fs.tempfs import cliapp import morphlib -from morphlib import cachedrepo - class CachedRepoTests(unittest.TestCase): @@ -114,7 +113,7 @@ class CachedRepoTests(unittest.TestCase): self.repo_name = 'foo' self.repo_url = 'git://foo.bar/foo.git' self.repo_path = '/tmp/foo' - self.repo = cachedrepo.CachedRepo( + self.repo = morphlib.cachedrepo.CachedRepo( object(), self.repo_name, self.repo_url, self.repo_path) self.repo._rev_parse = self.rev_parse self.repo._show_tree_hash = self.show_tree_hash @@ -123,10 +122,7 @@ class CachedRepoTests(unittest.TestCase): self.repo._checkout_ref = self.checkout_ref self.repo._ls_tree = self.ls_tree self.repo._clone_into = self.clone_into - self.tempdir = morphlib.tempdir.Tempdir() - - def tearDown(self): - self.tempdir.remove() + self.tempfs = fs.tempfs.TempFS() def test_constructor_sets_name_and_url_and_path(self): self.assertEqual(self.repo.original_name, self.repo_name) @@ -150,7 +146,7 @@ class CachedRepoTests(unittest.TestCase): self.assertEqual(tree, 'ffffffffffffffffffffffffffffffffffffffff') def test_fail_resolving_invalid_named_ref(self): - self.assertRaises(cachedrepo.InvalidReferenceError, + self.assertRaises(morphlib.cachedrepo.InvalidReferenceError, self.repo.resolve_ref, 'foo/bar') def test_resolve_sha1_ref(self): @@ -160,7 +156,7 @@ class CachedRepoTests(unittest.TestCase): self.assertEqual(tree, 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee') def test_fail_resolving_an_invalid_sha1_ref(self): - self.assertRaises(cachedrepo.InvalidReferenceError, + self.assertRaises(morphlib.cachedrepo.InvalidReferenceError, self.repo.resolve_ref, self.bad_sha1_known_to_rev_parse) @@ -170,9 +166,10 @@ class CachedRepoTests(unittest.TestCase): self.assertEqual(data, self.EXAMPLE_MORPH) def test_fail_cat_file_in_invalid_ref(self): - self.assertRaises(cachedrepo.InvalidReferenceError, self.repo.cat, - '079bbfd447c8534e464ce5d40b80114c2022ebf4', - 'doesnt-matter-whether-this-file-exists') + self.assertRaises( + morphlib.cachedrepo.InvalidReferenceError, self.repo.cat, + '079bbfd447c8534e464ce5d40b80114c2022ebf4', + 'doesnt-matter-whether-this-file-exists') def test_fail_cat_non_existent_file_in_existing_ref(self): self.assertRaises(IOError, self.repo.cat, @@ -180,37 +177,40 @@ class CachedRepoTests(unittest.TestCase): 'file-that-does-not-exist') def test_fail_cat_non_existent_file_in_invalid_ref(self): - self.assertRaises(cachedrepo.InvalidReferenceError, self.repo.cat, - '079bbfd447c8534e464ce5d40b80114c2022ebf4', - 'file-that-does-not-exist') + self.assertRaises( + morphlib.cachedrepo.InvalidReferenceError, self.repo.cat, + '079bbfd447c8534e464ce5d40b80114c2022ebf4', + 'file-that-does-not-exist') def test_fail_because_cat_in_named_ref_is_not_allowed(self): - self.assertRaises(cachedrepo.UnresolvedNamedReferenceError, + self.assertRaises(morphlib.cachedrepo.UnresolvedNamedReferenceError, self.repo.cat, 'master', 'doesnt-matter') def test_fail_clone_checkout_into_existing_directory(self): - self.assertRaises(cachedrepo.CheckoutDirectoryExistsError, + self.assertRaises(morphlib.cachedrepo.CheckoutDirectoryExistsError, self.repo.clone_checkout, 'e28a23812eadf2fce6583b8819b9c5dbd36b9fb9', - self.tempdir.dirname) + self.tempfs.root_path) def test_fail_checkout_due_to_clone_error(self): - self.assertRaises(cachedrepo.CloneError, self.repo.clone_checkout, - 'a4da32f5a81c8bc6d660404724cedc3bc0914a75', - self.tempdir.join('failed-checkout')) + self.assertRaises( + morphlib.cachedrepo.CloneError, self.repo.clone_checkout, + 'a4da32f5a81c8bc6d660404724cedc3bc0914a75', + self.tempfs.getsyspath('failed-checkout')) def test_fail_checkout_due_to_copy_error(self): - self.assertRaises(cachedrepo.CopyError, self.repo.checkout, + self.assertRaises(morphlib.cachedrepo.CopyError, self.repo.checkout, 'a4da32f5a81c8bc6d660404724cedc3bc0914a75', - self.tempdir.join('failed-checkout')) + self.tempfs.getsyspath('failed-checkout')) def test_fail_checkout_from_invalid_ref(self): - self.assertRaises(cachedrepo.CheckoutError, self.repo.checkout, - '079bbfd447c8534e464ce5d40b80114c2022ebf4', - self.tempdir.join('checkout-from-invalid-ref')) + self.assertRaises( + morphlib.cachedrepo.CheckoutError, self.repo.checkout, + '079bbfd447c8534e464ce5d40b80114c2022ebf4', + self.tempfs.getsyspath('checkout-from-invalid-ref')) def test_checkout_from_existing_ref_into_new_directory(self): - unpack_dir = self.tempdir.join('unpack-dir') + unpack_dir = self.tempfs.getsyspath('unpack-dir') self.repo.checkout('e28a23812eadf2fce6583b8819b9c5dbd36b9fb9', unpack_dir) self.assertTrue(os.path.exists(unpack_dir)) @@ -227,11 +227,12 @@ class CachedRepoTests(unittest.TestCase): self.assertEqual(data, ['foo.morph']) def test_fail_ls_tree_in_invalid_ref(self): - self.assertRaises(cachedrepo.InvalidReferenceError, self.repo.ls_tree, - '079bbfd447c8534e464ce5d40b80114c2022ebf4') + self.assertRaises( + morphlib.cachedrepo.InvalidReferenceError, self.repo.ls_tree, + '079bbfd447c8534e464ce5d40b80114c2022ebf4') def test_fail_because_ls_tree_in_named_ref_is_not_allowed(self): - self.assertRaises(cachedrepo.UnresolvedNamedReferenceError, + self.assertRaises(morphlib.cachedrepo.UnresolvedNamedReferenceError, self.repo.ls_tree, 'master') def test_successful_update(self): @@ -240,10 +241,10 @@ class CachedRepoTests(unittest.TestCase): def test_failing_update(self): self.repo._update = self.update_with_failure - self.assertRaises(cachedrepo.UpdateError, self.repo.update) + self.assertRaises(morphlib.cachedrepo.UpdateError, self.repo.update) def test_no_update_if_local(self): - self.repo = cachedrepo.CachedRepo( + self.repo = morphlib.cachedrepo.CachedRepo( object(), 'local:repo', 'file:///local/repo/', '/local/repo/') self.repo._update = self.update_with_failure self.repo.update() diff --git a/morphlib/plugins/artifact_inspection_plugin.py b/morphlib/plugins/artifact_inspection_plugin.py index 9181d488..6eeece77 100644 --- a/morphlib/plugins/artifact_inspection_plugin.py +++ b/morphlib/plugins/artifact_inspection_plugin.py @@ -19,6 +19,9 @@ import glob import json import os import re +import contextlib + +import fs.tempfs import morphlib @@ -109,25 +112,25 @@ class AutotoolsVersionGuesser(ProjectVersionGuesser): return None def _check_autoconf_package_version(self, repo, ref, filename, data): - tempdir = morphlib.tempdir.Tempdir(self.app.settings['tempdir']) - with open(tempdir.join(filename), 'w') as f: - f.write(data) - exit_code, output, errors = self.app.runcmd_unchecked( + with contextlib.closing(fs.tempfs.TempFS( + temp_dir=self.app.settings['tempdir'])) as tempdir: + with open(tempdir.getsyspath(filename), 'w') as f: + f.write(data) + exit_code, output, errors = self.app.runcmd_unchecked( ['autoconf', filename], ['grep', '^PACKAGE_VERSION='], ['cut', '-d=', '-f2'], ['sed', "s/'//g"], - cwd=tempdir.dirname) - tempdir.remove() - version = None - if output: - output = output.strip() - if output and output[0].isdigit(): - version = output - if exit_code != 0: - self.app.status( + cwd=tempdir.root_path) + version = None + if output: + output = output.strip() + if output and output[0].isdigit(): + version = output + if exit_code != 0: + self.app.status( msg='%(repo)s: Failed to detect version from ' - '%(ref)s:%(filename)s', + '%(ref)s:%(filename)s', repo=repo, ref=ref, filename=filename, chatty=True) return version diff --git a/morphlib/tempdir.py b/morphlib/tempdir.py deleted file mode 100644 index bfbabded..00000000 --- a/morphlib/tempdir.py +++ /dev/null @@ -1,48 +0,0 @@ -# Copyright (C) 2011-2012 Codethink Limited -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; version 2 of the License. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License along -# with this program; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - -import logging -import os -import shutil -import tempfile - - -class Tempdir(object): - - '''Temporary file handling for morph.''' - - def __init__(self, parent=None): - self.dirname = tempfile.mkdtemp(dir=parent) - logging.debug('Created temporary directory %s' % self.dirname) - - def remove(self): - '''Remove the temporary directory.''' - logging.debug('Removing temporary directory %s' % self.dirname) - shutil.rmtree(self.dirname) - self.dirname = None - - def join(self, relative): - '''Return full path to file in temporary directory. - - The relative path is given appended to the name of the - temporary directory. If the relative path is actually absolute, - it is forced to become relative. - - The returned path is normalized. - - ''' - - return os.path.normpath(os.path.join(self.dirname, './' + relative)) diff --git a/morphlib/tempdir_tests.py b/morphlib/tempdir_tests.py deleted file mode 100644 index 64ed214a..00000000 --- a/morphlib/tempdir_tests.py +++ /dev/null @@ -1,57 +0,0 @@ -# Copyright (C) 2011-2012 Codethink Limited -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; version 2 of the License. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License along -# with this program; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - -import os -import shutil -import unittest - -import morphlib - - -class TempdirTests(unittest.TestCase): - - def setUp(self): - self.parent = os.path.abspath('unittest-tempdir') - os.mkdir(self.parent) - self.tempdir = morphlib.tempdir.Tempdir(parent=self.parent) - - def tearDown(self): - shutil.rmtree(self.parent) - - def test_creates_the_directory(self): - self.assert_(os.path.isdir(self.tempdir.dirname)) - - def test_creates_subdirectory_of_parent(self): - self.assert_(self.tempdir.dirname.startswith(self.parent + '/')) - - def test_uses_default_if_parent_not_specified(self): - t = morphlib.tempdir.Tempdir() - shutil.rmtree(t.dirname) - self.assertNotEqual(t.dirname, None) - - def test_removes_itself(self): - dirname = self.tempdir.dirname - self.tempdir.remove() - self.assertEqual(self.tempdir.dirname, None) - self.assertFalse(os.path.exists(dirname)) - - def test_joins_filename(self): - self.assertEqual(self.tempdir.join('foo'), - os.path.join(self.tempdir.dirname, 'foo')) - - def test_joins_absolute_filename(self): - self.assertEqual(self.tempdir.join('/foo'), - os.path.join(self.tempdir.dirname, 'foo')) -- cgit v1.2.1