Exemplo n.º 1
0
class TestFileCache(unittest.TestCase):
    def setUp(self):
        self.conf = get_test_config()
        self.fc = FileCache(self.conf)

    def tearDown(self):
        self.conf.cleanup()

    def testFileCache(self):
        cf1 = self.fc.newfile('ar123.tar')
        self.assertEqual(cf1.filename, 'ar123.tar')
        with open(cf1.fullpath(), 'wb') as f:
            f.write("Hello test world")
        self.assertEqual(self.fc.compute_size(), len("Hello test world"))

    def testCleanup(self):
        def makefile():
            cf1 = self.fc.newfile('ar123.tar')
            with open(cf1.fullpath(), 'wb') as f:
                f.write("Hello test world")
            return cf1.fullpath()

        fullpath = makefile()

        # Now the file should be unused
        self.assertTrue('ar123.tar' in self.fc.can_delete)
        self.fc.cleanup()
        self.assertFalse(os.path.exists(fullpath))
Exemplo n.º 2
0
class LocalStorage(object):
    def __init__(self, cp, target_dir):
        self.cp = cp
        self.cache = FileCache(cp)

        self.target_dir = target_dir
        self.trees_dir = os.path.join(target_dir, 'trees')
        self.archives_dir = os.path.join(target_dir, 'archives')

        mkdir_p(self.trees_dir)
        mkdir_p(self.archives_dir)

    def get_stored_info(self, target):
        tree_fns = (os.path.join(self.trees_dir, fn) for fn in os.listdir(self.trees_dir))
        return RemoteStoredInfo([LocalStorageKey(fn) for fn in tree_fns
                    if os.path.isfile(fn) and not fn.endswith('.meta')])

    def save_tree(self, target, new_tree):
        now_dt = datetime.utcnow()
        now_dt_str = now_dt.strftime(TREE_DT_FMT)
        tree_local_fname = os.path.join(self.trees_dir,
                '{}-{}'.format(target, now_dt_str))
        with open(tree_local_fname, 'wb') as f:
            tree.save_tree(new_tree, f)
        with open('{}.meta'.format(tree_local_fname), 'wb') as f:
            json.dump({'pf:target': target, 'pf:saved_dt': now_dt_str}, f)

    def save_archive(self, filename):
        archive_id = hashlib.sha224(filename).hexdigest()
        shutil.copy(filename, os.path.join(self.archives_dir, archive_id))
        return archive_id

    def load_archive(self, ar_name):
        if self.cache.hasfile(ar_name):
            return self.cache.getfile(ar_name)
        afile = self.cache.newfile(ar_name)
        shutil.copy(os.path.join(self.archives_dir, ar_name), afile.fullpath())
        return afile