Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add 'hash' API method.

  • Loading branch information...
commit 6ecfc87d70a5dd76c7c6779886646599d15e19ad 1 parent 8106286
Chris Rossi chrisrossi authored

Showing 3 changed files with 46 additions and 1 deletion. Show diff stats Hide diff stats

  1. +29 1 acidfs/__init__.py
  2. +16 0 acidfs/tests.py
  3. +1 0  docs/index.rst
30 acidfs/__init__.py
@@ -257,6 +257,17 @@ def open(self, path, mode='r', buffering=-1, encoding=None, errors=None,
257 257
258 258 raise ValueError("Bad mode: %s" % mode)
259 259
  260 + def hash(self, path=''):
  261 + """
  262 + Returns the sha1 hash of the object referred to by `path`. If `path` is
  263 + omitted the current working directory is used.
  264 + """
  265 + session = self._session()
  266 + obj = session.find(self._mkpath(path))
  267 + if not obj:
  268 + raise _NoSuchFileOrDirectory(path)
  269 + return obj.hash()
  270 +
260 271 def listdir(self, path=''):
261 272 """
262 273 Return list of files in indicated directory. If `path` is omitted, the
@@ -831,10 +842,12 @@ class _TreeNode(object):
831 842 parent = None
832 843 name = None
833 844 dirty = False
  845 + oid = None
834 846
835 847 @classmethod
836 848 def read(cls, db, oid):
837 849 node = cls(db)
  850 + node.oid = oid
838 851 contents = node.contents
839 852 with _popen(['git', 'ls-tree', oid],
840 853 stdout=subprocess.PIPE, cwd=db) as lstree:
@@ -902,6 +915,7 @@ def set(self, name, entry):
902 915 def set_dirty(self):
903 916 node = self
904 917 while node and not node.dirty:
  918 + node.oid = None
905 919 node.dirty = True
906 920 node = node.parent
907 921
@@ -930,6 +944,7 @@ def save(self):
930 944 proc.stdin.write(b'\n')
931 945 proc.stdin.close()
932 946 oid = proc.stdout.read().strip()
  947 + self.oid = _s(oid)
933 948 return oid
934 949
935 950 def empty(self):
@@ -938,6 +953,11 @@ def empty(self):
938 953 def __contains__(self, name):
939 954 return name in self.contents
940 955
  956 + def hash(self):
  957 + if not self.oid:
  958 + self.save()
  959 + return self.oid
  960 +
941 961
942 962 class _Blob(object):
943 963
@@ -952,6 +972,9 @@ def find(self, path):
952 972 if not path:
953 973 return self
954 974
  975 + def hash(self):
  976 + return self.oid
  977 +
955 978
956 979 class _NewBlob(io.RawIOBase):
957 980
@@ -978,7 +1001,7 @@ def close(self):
978 1001 if retcode != 0:
979 1002 raise subprocess.CalledProcessError(
980 1003 retcode, 'git hash-object -w --stdin')
981   - self.parent.contents[self.name] = (b'blob', oid, None)
  1004 + self.parent.contents[self.name] = (b'blob', _s(oid), None)
982 1005
983 1006 def writable(self):
984 1007 return True
@@ -988,6 +1011,11 @@ def open(self):
988 1011 return self.prev.open()
989 1012 raise _NoSuchFileOrDirectory(_object_path(self))
990 1013
  1014 + def hash(self):
  1015 + if self.prev:
  1016 + return self.prev.hash()
  1017 + raise _NoSuchFileOrDirectory(_object_path(self))
  1018 +
991 1019 def find(self, path):
992 1020 if not path:
993 1021 return self
16 acidfs/tests.py
@@ -100,12 +100,20 @@ def assertDirectoryNotEmpty(self, path):
100 100
101 101 def test_read_write_file(self):
102 102 fs = self.make_one()
  103 + self.assertEqual(fs.hash(),
  104 + '4b825dc642cb6eb9a060e54bf8d69288fbee4904')
  105 + with self.assertNoSuchFileOrDirectory('foo'):
  106 + fs.hash('foo')
103 107 with fs.open('foo', 'wb') as f:
104 108 self.assertTrue(f.writable())
105 109 fprint(f, b'Hello')
106 110 with self.assertNoSuchFileOrDirectory('foo'):
107 111 fs.open('foo', 'rb')
  112 + with self.assertNoSuchFileOrDirectory('foo'):
  113 + fs.hash('foo')
108 114 self.assertEqual(fs.open('foo', 'rb').read(), b'Hello\n')
  115 + self.assertEqual(fs.hash('foo'),
  116 + 'e965047ad7c57865823c7d992b1d046ea66edf78')
109 117 actual_file = os.path.join(self.tmp, 'foo')
110 118 self.assertFalse(os.path.exists(actual_file))
111 119 transaction.commit()
@@ -139,10 +147,14 @@ def test_read_write_file_in_subfolder(self):
139 147 self.assertFalse(fs.isdir('foo'))
140 148 fs.mkdir('foo')
141 149 self.assertTrue(fs.isdir('foo'))
  150 + self.assertEqual(fs.hash('foo'),
  151 + '4b825dc642cb6eb9a060e54bf8d69288fbee4904')
142 152 with fs.open('foo/bar', 'wb') as f:
143 153 fprint(f, b'Hello')
144 154 with fs.open('foo/bar', 'rb') as f:
145 155 self.assertEqual(f.read(), b'Hello\n')
  156 + self.assertEqual(fs.hash('foo'),
  157 + 'c57c02051dae8e2e4803530c217ac38121f393d3')
146 158 actual_file = os.path.join(self.tmp, 'foo', 'bar')
147 159 self.assertFalse(os.path.exists(actual_file))
148 160 transaction.commit()
@@ -268,7 +280,11 @@ def test_modify_file(self):
268 280 with fs.open('foo', 'wb') as f:
269 281 fprint(f, b"Hello!")
270 282 self.assertEqual(fs.open('foo', 'rb').read(), b'Howdy!\n')
  283 + self.assertEqual(fs.hash('foo'),
  284 + 'c564dac563c1974addaa0ac0ae028fc92b2370f1')
271 285 self.assertEqual(fs.open('foo', 'rb').read(), b'Hello!\n')
  286 + self.assertEqual(fs.hash('foo'),
  287 + '10ddd6d257e01349d514541981aeecea6b2e741d')
272 288 self.assertEqual(open(path, 'rb').read(), b'Howdy!\n')
273 289 transaction.commit()
274 290
1  docs/index.rst
Source Rendered
@@ -168,6 +168,7 @@ API
168 168 .. automethod:: empty
169 169 .. automethod:: get_base
170 170 .. automethod:: set_base
  171 + .. automethod:: hash
171 172
172 173 .. toctree::
173 174 :maxdepth: 2

0 comments on commit 6ecfc87

Please sign in to comment.
Something went wrong with that request. Please try again.