mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2025-06-23 01:08:53 +00:00
Merge branch 'master' into 3399.mypy
This commit is contained in:
@ -1,4 +1,16 @@
|
||||
"""Directory Node implementation."""
|
||||
"""Directory Node implementation.
|
||||
|
||||
Ported to Python 3.
|
||||
"""
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from future.utils import PY2
|
||||
if PY2:
|
||||
# Skip dict so it doesn't break things.
|
||||
from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, list, object, range, str, max, min # noqa: F401
|
||||
from past.builtins import unicode
|
||||
|
||||
import time
|
||||
@ -37,6 +49,8 @@ from eliot.twisted import (
|
||||
|
||||
NAME = Field.for_types(
|
||||
u"name",
|
||||
# Make sure this works on Python 2; with str, it gets Future str which
|
||||
# breaks Eliot.
|
||||
[unicode],
|
||||
u"The name linking the parent to this node.",
|
||||
)
|
||||
@ -179,7 +193,7 @@ class Adder(object):
|
||||
def modify(self, old_contents, servermap, first_time):
|
||||
children = self.node._unpack_contents(old_contents)
|
||||
now = time.time()
|
||||
for (namex, (child, new_metadata)) in self.entries.iteritems():
|
||||
for (namex, (child, new_metadata)) in list(self.entries.items()):
|
||||
name = normalize(namex)
|
||||
precondition(IFilesystemNode.providedBy(child), child)
|
||||
|
||||
@ -205,8 +219,8 @@ class Adder(object):
|
||||
return new_contents
|
||||
|
||||
def _encrypt_rw_uri(writekey, rw_uri):
|
||||
precondition(isinstance(rw_uri, str), rw_uri)
|
||||
precondition(isinstance(writekey, str), writekey)
|
||||
precondition(isinstance(rw_uri, bytes), rw_uri)
|
||||
precondition(isinstance(writekey, bytes), writekey)
|
||||
|
||||
salt = hashutil.mutable_rwcap_salt_hash(rw_uri)
|
||||
key = hashutil.mutable_rwcap_key_hash(salt, writekey)
|
||||
@ -221,7 +235,7 @@ def _encrypt_rw_uri(writekey, rw_uri):
|
||||
def pack_children(childrenx, writekey, deep_immutable=False):
|
||||
# initial_children must have metadata (i.e. {} instead of None)
|
||||
children = {}
|
||||
for (namex, (node, metadata)) in childrenx.iteritems():
|
||||
for (namex, (node, metadata)) in list(childrenx.items()):
|
||||
precondition(isinstance(metadata, dict),
|
||||
"directory creation requires metadata to be a dict, not None", metadata)
|
||||
children[normalize(namex)] = (node, metadata)
|
||||
@ -245,18 +259,19 @@ def _pack_normalized_children(children, writekey, deep_immutable=False):
|
||||
If deep_immutable is True, I will require that all my children are deeply
|
||||
immutable, and will raise a MustBeDeepImmutableError if not.
|
||||
"""
|
||||
precondition((writekey is None) or isinstance(writekey, str), writekey)
|
||||
precondition((writekey is None) or isinstance(writekey, bytes), writekey)
|
||||
|
||||
has_aux = isinstance(children, AuxValueDict)
|
||||
entries = []
|
||||
for name in sorted(children.keys()):
|
||||
assert isinstance(name, unicode)
|
||||
assert isinstance(name, str)
|
||||
entry = None
|
||||
(child, metadata) = children[name]
|
||||
child.raise_error()
|
||||
if deep_immutable and not child.is_allowed_in_immutable_directory():
|
||||
raise MustBeDeepImmutableError("child %s is not allowed in an immutable directory" %
|
||||
quote_output(name, encoding='utf-8'), name)
|
||||
raise MustBeDeepImmutableError(
|
||||
"child %r is not allowed in an immutable directory" % (name,),
|
||||
name)
|
||||
if has_aux:
|
||||
entry = children.get_aux(name)
|
||||
if not entry:
|
||||
@ -264,26 +279,26 @@ def _pack_normalized_children(children, writekey, deep_immutable=False):
|
||||
assert isinstance(metadata, dict)
|
||||
rw_uri = child.get_write_uri()
|
||||
if rw_uri is None:
|
||||
rw_uri = ""
|
||||
assert isinstance(rw_uri, str), rw_uri
|
||||
rw_uri = b""
|
||||
assert isinstance(rw_uri, bytes), rw_uri
|
||||
|
||||
# should be prevented by MustBeDeepImmutableError check above
|
||||
assert not (rw_uri and deep_immutable)
|
||||
|
||||
ro_uri = child.get_readonly_uri()
|
||||
if ro_uri is None:
|
||||
ro_uri = ""
|
||||
assert isinstance(ro_uri, str), ro_uri
|
||||
ro_uri = b""
|
||||
assert isinstance(ro_uri, bytes), ro_uri
|
||||
if writekey is not None:
|
||||
writecap = netstring(_encrypt_rw_uri(writekey, rw_uri))
|
||||
else:
|
||||
writecap = ZERO_LEN_NETSTR
|
||||
entry = "".join([netstring(name.encode("utf-8")),
|
||||
entry = b"".join([netstring(name.encode("utf-8")),
|
||||
netstring(strip_prefix_for_ro(ro_uri, deep_immutable)),
|
||||
writecap,
|
||||
netstring(json.dumps(metadata))])
|
||||
netstring(json.dumps(metadata).encode("utf-8"))])
|
||||
entries.append(netstring(entry))
|
||||
return "".join(entries)
|
||||
return b"".join(entries)
|
||||
|
||||
@implementer(IDirectoryNode, ICheckable, IDeepCheckable)
|
||||
class DirectoryNode(object):
|
||||
@ -352,9 +367,9 @@ class DirectoryNode(object):
|
||||
# cleartext. The 'name' is UTF-8 encoded, and should be normalized to NFC.
|
||||
# The rwcapdata is formatted as:
|
||||
# pack("16ss32s", iv, AES(H(writekey+iv), plaintext_rw_uri), mac)
|
||||
assert isinstance(data, str), (repr(data), type(data))
|
||||
assert isinstance(data, bytes), (repr(data), type(data))
|
||||
# an empty directory is serialized as an empty string
|
||||
if data == "":
|
||||
if data == b"":
|
||||
return AuxValueDict()
|
||||
writeable = not self.is_readonly()
|
||||
mutable = self.is_mutable()
|
||||
@ -373,7 +388,7 @@ class DirectoryNode(object):
|
||||
# Therefore we normalize names going both in and out of directories.
|
||||
name = normalize(namex_utf8.decode("utf-8"))
|
||||
|
||||
rw_uri = ""
|
||||
rw_uri = b""
|
||||
if writeable:
|
||||
rw_uri = self._decrypt_rwcapdata(rwcapdata)
|
||||
|
||||
@ -384,8 +399,8 @@ class DirectoryNode(object):
|
||||
# ro_uri is treated in the same way for consistency.
|
||||
# rw_uri and ro_uri will be either None or a non-empty string.
|
||||
|
||||
rw_uri = rw_uri.rstrip(' ') or None
|
||||
ro_uri = ro_uri.rstrip(' ') or None
|
||||
rw_uri = rw_uri.rstrip(b' ') or None
|
||||
ro_uri = ro_uri.rstrip(b' ') or None
|
||||
|
||||
try:
|
||||
child = self._create_and_validate_node(rw_uri, ro_uri, name)
|
||||
@ -468,7 +483,7 @@ class DirectoryNode(object):
|
||||
exists a child of the given name, False if not."""
|
||||
name = normalize(namex)
|
||||
d = self._read()
|
||||
d.addCallback(lambda children: children.has_key(name))
|
||||
d.addCallback(lambda children: name in children)
|
||||
return d
|
||||
|
||||
def _get(self, children, name):
|
||||
@ -543,7 +558,7 @@ class DirectoryNode(object):
|
||||
else:
|
||||
pathx = pathx.split("/")
|
||||
for p in pathx:
|
||||
assert isinstance(p, unicode), p
|
||||
assert isinstance(p, str), p
|
||||
childnamex = pathx[0]
|
||||
remaining_pathx = pathx[1:]
|
||||
if remaining_pathx:
|
||||
@ -555,8 +570,8 @@ class DirectoryNode(object):
|
||||
return d
|
||||
|
||||
def set_uri(self, namex, writecap, readcap=None, metadata=None, overwrite=True):
|
||||
precondition(isinstance(writecap, (str,type(None))), writecap)
|
||||
precondition(isinstance(readcap, (str,type(None))), readcap)
|
||||
precondition(isinstance(writecap, (bytes, type(None))), writecap)
|
||||
precondition(isinstance(readcap, (bytes, type(None))), readcap)
|
||||
|
||||
# We now allow packing unknown nodes, provided they are valid
|
||||
# for this type of directory.
|
||||
@ -569,16 +584,16 @@ class DirectoryNode(object):
|
||||
# this takes URIs
|
||||
a = Adder(self, overwrite=overwrite,
|
||||
create_readonly_node=self._create_readonly_node)
|
||||
for (namex, e) in entries.iteritems():
|
||||
assert isinstance(namex, unicode), namex
|
||||
for (namex, e) in entries.items():
|
||||
assert isinstance(namex, str), namex
|
||||
if len(e) == 2:
|
||||
writecap, readcap = e
|
||||
metadata = None
|
||||
else:
|
||||
assert len(e) == 3
|
||||
writecap, readcap, metadata = e
|
||||
precondition(isinstance(writecap, (str,type(None))), writecap)
|
||||
precondition(isinstance(readcap, (str,type(None))), readcap)
|
||||
precondition(isinstance(writecap, (bytes,type(None))), writecap)
|
||||
precondition(isinstance(readcap, (bytes,type(None))), readcap)
|
||||
|
||||
# We now allow packing unknown nodes, provided they are valid
|
||||
# for this type of directory.
|
||||
@ -779,7 +794,7 @@ class DirectoryNode(object):
|
||||
# in the nodecache) seem to consume about 2000 bytes.
|
||||
dirkids = []
|
||||
filekids = []
|
||||
for name, (child, metadata) in sorted(children.iteritems()):
|
||||
for name, (child, metadata) in sorted(children.items()):
|
||||
childpath = path + [name]
|
||||
if isinstance(child, UnknownNode):
|
||||
walker.add_node(child, childpath)
|
||||
|
Reference in New Issue
Block a user