Skip to content

Commit

Permalink
Add 'hash' API method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Rossi committed Jan 3, 2013
1 parent 8106286 commit 6ecfc87
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
30 changes: 29 additions & 1 deletion acidfs/__init__.py
Expand Up @@ -257,6 +257,17 @@ def open(self, path, mode='r', buffering=-1, encoding=None, errors=None,


raise ValueError("Bad mode: %s" % mode) raise ValueError("Bad mode: %s" % mode)


def hash(self, path=''):
"""
Returns the sha1 hash of the object referred to by `path`. If `path` is
omitted the current working directory is used.
"""
session = self._session()
obj = session.find(self._mkpath(path))
if not obj:
raise _NoSuchFileOrDirectory(path)
return obj.hash()

def listdir(self, path=''): def listdir(self, path=''):
""" """
Return list of files in indicated directory. If `path` is omitted, the Return list of files in indicated directory. If `path` is omitted, the
Expand Down Expand Up @@ -831,10 +842,12 @@ class _TreeNode(object):
parent = None parent = None
name = None name = None
dirty = False dirty = False
oid = None


@classmethod @classmethod
def read(cls, db, oid): def read(cls, db, oid):
node = cls(db) node = cls(db)
node.oid = oid
contents = node.contents contents = node.contents
with _popen(['git', 'ls-tree', oid], with _popen(['git', 'ls-tree', oid],
stdout=subprocess.PIPE, cwd=db) as lstree: stdout=subprocess.PIPE, cwd=db) as lstree:
Expand Down Expand Up @@ -902,6 +915,7 @@ def set(self, name, entry):
def set_dirty(self): def set_dirty(self):
node = self node = self
while node and not node.dirty: while node and not node.dirty:
node.oid = None
node.dirty = True node.dirty = True
node = node.parent node = node.parent


Expand Down Expand Up @@ -930,6 +944,7 @@ def save(self):
proc.stdin.write(b'\n') proc.stdin.write(b'\n')
proc.stdin.close() proc.stdin.close()
oid = proc.stdout.read().strip() oid = proc.stdout.read().strip()
self.oid = _s(oid)
return oid return oid


def empty(self): def empty(self):
Expand All @@ -938,6 +953,11 @@ def empty(self):
def __contains__(self, name): def __contains__(self, name):
return name in self.contents return name in self.contents


def hash(self):
if not self.oid:
self.save()
return self.oid



class _Blob(object): class _Blob(object):


Expand All @@ -952,6 +972,9 @@ def find(self, path):
if not path: if not path:
return self return self


def hash(self):
return self.oid



class _NewBlob(io.RawIOBase): class _NewBlob(io.RawIOBase):


Expand All @@ -978,7 +1001,7 @@ def close(self):
if retcode != 0: if retcode != 0:
raise subprocess.CalledProcessError( raise subprocess.CalledProcessError(
retcode, 'git hash-object -w --stdin') retcode, 'git hash-object -w --stdin')
self.parent.contents[self.name] = (b'blob', oid, None) self.parent.contents[self.name] = (b'blob', _s(oid), None)


def writable(self): def writable(self):
return True return True
Expand All @@ -988,6 +1011,11 @@ def open(self):
return self.prev.open() return self.prev.open()
raise _NoSuchFileOrDirectory(_object_path(self)) raise _NoSuchFileOrDirectory(_object_path(self))


def hash(self):
if self.prev:
return self.prev.hash()
raise _NoSuchFileOrDirectory(_object_path(self))

def find(self, path): def find(self, path):
if not path: if not path:
return self return self
Expand Down
16 changes: 16 additions & 0 deletions acidfs/tests.py
Expand Up @@ -100,12 +100,20 @@ def assertDirectoryNotEmpty(self, path):


def test_read_write_file(self): def test_read_write_file(self):
fs = self.make_one() fs = self.make_one()
self.assertEqual(fs.hash(),
'4b825dc642cb6eb9a060e54bf8d69288fbee4904')
with self.assertNoSuchFileOrDirectory('foo'):
fs.hash('foo')
with fs.open('foo', 'wb') as f: with fs.open('foo', 'wb') as f:
self.assertTrue(f.writable()) self.assertTrue(f.writable())
fprint(f, b'Hello') fprint(f, b'Hello')
with self.assertNoSuchFileOrDirectory('foo'): with self.assertNoSuchFileOrDirectory('foo'):
fs.open('foo', 'rb') fs.open('foo', 'rb')
with self.assertNoSuchFileOrDirectory('foo'):
fs.hash('foo')
self.assertEqual(fs.open('foo', 'rb').read(), b'Hello\n') self.assertEqual(fs.open('foo', 'rb').read(), b'Hello\n')
self.assertEqual(fs.hash('foo'),
'e965047ad7c57865823c7d992b1d046ea66edf78')
actual_file = os.path.join(self.tmp, 'foo') actual_file = os.path.join(self.tmp, 'foo')
self.assertFalse(os.path.exists(actual_file)) self.assertFalse(os.path.exists(actual_file))
transaction.commit() transaction.commit()
Expand Down Expand Up @@ -139,10 +147,14 @@ def test_read_write_file_in_subfolder(self):
self.assertFalse(fs.isdir('foo')) self.assertFalse(fs.isdir('foo'))
fs.mkdir('foo') fs.mkdir('foo')
self.assertTrue(fs.isdir('foo')) self.assertTrue(fs.isdir('foo'))
self.assertEqual(fs.hash('foo'),
'4b825dc642cb6eb9a060e54bf8d69288fbee4904')
with fs.open('foo/bar', 'wb') as f: with fs.open('foo/bar', 'wb') as f:
fprint(f, b'Hello') fprint(f, b'Hello')
with fs.open('foo/bar', 'rb') as f: with fs.open('foo/bar', 'rb') as f:
self.assertEqual(f.read(), b'Hello\n') self.assertEqual(f.read(), b'Hello\n')
self.assertEqual(fs.hash('foo'),
'c57c02051dae8e2e4803530c217ac38121f393d3')
actual_file = os.path.join(self.tmp, 'foo', 'bar') actual_file = os.path.join(self.tmp, 'foo', 'bar')
self.assertFalse(os.path.exists(actual_file)) self.assertFalse(os.path.exists(actual_file))
transaction.commit() transaction.commit()
Expand Down Expand Up @@ -268,7 +280,11 @@ def test_modify_file(self):
with fs.open('foo', 'wb') as f: with fs.open('foo', 'wb') as f:
fprint(f, b"Hello!") fprint(f, b"Hello!")
self.assertEqual(fs.open('foo', 'rb').read(), b'Howdy!\n') self.assertEqual(fs.open('foo', 'rb').read(), b'Howdy!\n')
self.assertEqual(fs.hash('foo'),
'c564dac563c1974addaa0ac0ae028fc92b2370f1')
self.assertEqual(fs.open('foo', 'rb').read(), b'Hello!\n') self.assertEqual(fs.open('foo', 'rb').read(), b'Hello!\n')
self.assertEqual(fs.hash('foo'),
'10ddd6d257e01349d514541981aeecea6b2e741d')
self.assertEqual(open(path, 'rb').read(), b'Howdy!\n') self.assertEqual(open(path, 'rb').read(), b'Howdy!\n')
transaction.commit() transaction.commit()


Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Expand Up @@ -168,6 +168,7 @@ API
.. automethod:: empty .. automethod:: empty
.. automethod:: get_base .. automethod:: get_base
.. automethod:: set_base .. automethod:: set_base
.. automethod:: hash


.. toctree:: .. toctree::
:maxdepth: 2 :maxdepth: 2

0 comments on commit 6ecfc87

Please sign in to comment.