def stat(self, path, verbose=False): stats = defaultdict(list) assert os.path.exists(path) if os.path.isfile(path): self.update_stat(path, stats) elif os.path.isdir(path): for dirpath, dirnames, filenames in os.walk(path): for fname in filenames: self.update_stat(os.path.join(dirpath, fname), stats) insize, infiles, indistinct = 0, 0, 0 outsize, outfiles, outdistinct = 0, 0, 0 for md5, files in stats.items(): for i, (path, size, in_catalog) in enumerate(files): if i == 0: if in_catalog: indistinct += 1 else: outdistinct += 1 if verbose: print(path) if in_catalog: insize += size infiles += 1 else: outsize += size outfiles += 1 print('uploaded: {0} in {1} files ({2} distinct)'.format( File.format_size(insize), infiles, indistinct)) print('todo: {0} in {1} files ({2} distinct)'.format( File.format_size(outsize), outfiles, outdistinct)) return stats
def add_bitstream(self, fname=None, **kw): f = File(fname) self.bitstreams.append( Mock( _properties={ 'bitstreamid': f.clean_name, 'filesize': f.size, 'checksum': f.md5, 'checksum-algorithm': 'MD5', 'content-type': 'text/plain', 'created': 5, 'last-modified': 7 }))
class TestFile(TestCase): def setUp(self): from pycdstar.media import File with NamedTemporaryFile(delete=False, suffix='ä ö,ü.ß') as fp: fp.write(b"""test""") self.file = File(fp.name) def test_clean_name(self): self.assertTrue(self.file.clean_name.endswith('a_o_u.ss')) def test_size(self): self.assertEqual(self.file.size, 4) self.assertEqual(self.file.size_h, '4.0B') self.assertEqual(self.file.format_size(1000000000000000000000000000), '827.2YiB') def test_md5(self): self.assertEqual(self.file.md5, '098f6bcd4621d373cade4e832627b4f6') def test_create_object(self): self.file.temporary = True obj, md, bitstreams = self.file.create_object(Mock()) self.assertIn('original', bitstreams) self.assertFalse(os.path.exists(self.file.path)) def test_create_object_fail(self): """Make sure obj.delete() is called when adding a bitstream fails.""" class Object(Mock): def add_bitstream(self, *args, **kw): raise ValueError obj = Object() with self.assertRaises(ValueError): self.file.create_object(Mock(get_object=lambda: obj)) self.assertTrue(obj.delete.called) def tearDown(self): if os.path.exists(self.file.path): os.remove(self.file.path)
def setUp(self): from pycdstar.media import File with NamedTemporaryFile(delete=False, suffix='ä ö,ü.ß') as fp: fp.write(b"""test""") self.file = File(fp.name)
def size_h(self): return File.format_size(self.size)