From 973f61504ec71e0ec925b592f12825aa0c9ab9d9 Mon Sep 17 00:00:00 2001 From: Daniel Firth Date: Tue, 10 Dec 2013 17:14:04 +0000 Subject: Removed unused CacheDir class --- morphlib/__init__.py | 1 - morphlib/cachedir.py | 86 ------------------------------ morphlib/cachedir_tests.py | 128 --------------------------------------------- 3 files changed, 215 deletions(-) delete mode 100644 morphlib/cachedir.py delete mode 100644 morphlib/cachedir_tests.py diff --git a/morphlib/__init__.py b/morphlib/__init__.py index fcfcee54..67fb944d 100644 --- a/morphlib/__init__.py +++ b/morphlib/__init__.py @@ -55,7 +55,6 @@ import buildcommand import buildenvironment import buildsystem import builder2 -import cachedir import cachedrepo import cachekeycomputer import extractedtarball diff --git a/morphlib/cachedir.py b/morphlib/cachedir.py deleted file mode 100644 index de1b87ff..00000000 --- a/morphlib/cachedir.py +++ /dev/null @@ -1,86 +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 hashlib -import os - -import morphlib - - -class CacheDir(object): - - '''Manage Baserock cached binaries.''' - - def __init__(self, dirname): - self.dirname = os.path.abspath(dirname) - - def key(self, dict_key): - '''Create a string key from a dictionary key. - - The string key can be used as a filename, or as part of one. - The dictionary key is a dict that maps any set of strings to - another set of strings. - - The same string key is guaranteed to be returned for a given - dictionary key. It is highly unlikely that two different dictionary - keys result in the same string key. - - ''' - - data = ''.join(key + value for key, value in dict_key.iteritems()) - return hashlib.sha256(data).hexdigest() - - def name(self, dict_key): - '''Return a filename for an object described by dictionary key. - - It is the caller's responsibility to set the fields in the - dictionary key suitably. For example, if there is a field - specifying a commit id, it should be the full git SHA-1 - identifier, not something ephemeral like HEAD. - - If the field 'kind' has a value, it is used as a suffix for - the filename. - - ''' - - key = self.key(dict_key) - if 'kind' in dict_key and dict_key['kind']: - suffix = '.%s' % dict_key['kind'] - else: - suffix = '' - - return os.path.join(self.dirname, key + suffix) - - def open(self, relative_name_or_cache_key, suffix='', **kwargs): - '''Open a file for writing in the cache. - - The file will be written with a temporary name, and renamed to - the final name when the file is closed. Additionally, if the - caller decides, mid-writing, that they don't want to write the - file after all (e.g., a log file), then the ``abort`` method - in the returned file handle can be called to remove the - temporary file. - - ''' - - if type(relative_name_or_cache_key) is dict: - path = self.name(relative_name_or_cache_key) - else: - path = os.path.join(self.dirname, relative_name_or_cache_key) - path += suffix - if 'mode' not in kwargs: - kwargs['mode'] = 'w' - return morphlib.savefile.SaveFile(path, **kwargs) diff --git a/morphlib/cachedir_tests.py b/morphlib/cachedir_tests.py deleted file mode 100644 index f02f80e1..00000000 --- a/morphlib/cachedir_tests.py +++ /dev/null @@ -1,128 +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 tempfile -import unittest - -import morphlib - - -class CacheDirTests(unittest.TestCase): - - def cat(self, relative_name): - with open(os.path.join(self.cachedir.dirname, relative_name)) as f: - return f.read() - - def setUp(self): - self.dirname = tempfile.mkdtemp() - self.cachedir = morphlib.cachedir.CacheDir(self.dirname) - - def tearDown(self): - shutil.rmtree(self.dirname) - - def test_sets_dirname_attribute(self): - self.assertEqual(self.cachedir.dirname, os.path.abspath(self.dirname)) - - def test_generates_string_key_for_arbitrary_dict_key(self): - key = self.cachedir.key({ - 'foo': 'bar', - 'xyzzy': 'plugh', - }) - self.assertEqual(type(key), str) - self.assertNotEqual(key, '') - - def test_generates_same_string_key_twice(self): - dict_key = { - 'foo': 'bar', - 'xyzzy': 'plugh', - } - self.assertEqual(self.cachedir.key(dict_key), - self.cachedir.key(dict_key)) - - def test_generates_different_string_keys(self): - dict_key_1 = { - 'foo': 'bar', - 'xyzzy': 'plugh', - } - dict_key_2 = { - 'foo': 'foobar', - 'xyzzy': 'stevenage', - } - self.assertNotEqual(self.cachedir.key(dict_key_1), - self.cachedir.key(dict_key_2)) - - def test_returns_a_chunk_pathname_in_cache_directory(self): - dict_key = { - 'kind': 'chunk', - 'ref': 'DEADBEEF', - 'repo': 'git://git.baserock.org/hello/', - 'arch': 'armv7', - } - pathname = self.cachedir.name(dict_key) - self.assert_(pathname.startswith(self.cachedir.dirname + '/')) - self.assert_(pathname.endswith('.chunk')) - - def test_returns_a_stratum_pathname_in_cache_directory(self): - dict_key = { - 'kind': 'stratum', - 'ref': 'DEADBEEF', - 'repo': 'git://git.baserock.org/hello/', - 'arch': 'armv7', - } - pathname = self.cachedir.name(dict_key) - self.assert_(pathname.startswith(self.cachedir.dirname + '/')) - self.assert_(pathname.endswith('.stratum')) - - def test_returns_a_valid_pathname_in_cache_directory(self): - dict_key = { - 'ref': 'DEADBEEF', - 'repo': 'git://git.baserock.org/hello/', - 'arch': 'armv7', - } - pathname = self.cachedir.name(dict_key) - self.assert_(pathname.startswith(self.cachedir.dirname + '/')) - - def test_allows_file_to_be_written_via_basename(self): - f = self.cachedir.open('foo') - f.write('bar') - f.close() - self.assertEqual(self.cat('foo'), 'bar') - - def test_allows_file_to_be_written_via_basename_and_suffix(self): - f = self.cachedir.open('foo', '.blip') - f.write('bar') - f.close() - self.assertEqual(self.cat('foo.blip'), 'bar') - - def test_allows_file_to_be_written_via_dict_key(self): - dict_key = { - 'kind': 'chunk', - 'meh': 'moo', - } - name = self.cachedir.name(dict_key) - f = self.cachedir.open(dict_key) - f.write('bar') - f.close() - self.assertEqual(self.cat(name), 'bar') - - def test_allows_file_to_be_aborted(self): - f = self.cachedir.open('foo') - f.write('bar') - f.abort() - pathname = os.path.join(self.cachedir.dirname, 'foo') - self.assertFalse(os.path.exists(pathname)) -- cgit v1.2.1