本文整理汇总了Python中twisted.python.filepath.FilePath.descendant方法的典型用法代码示例。如果您正苦于以下问题:Python FilePath.descendant方法的具体用法?Python FilePath.descendant怎么用?Python FilePath.descendant使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.python.filepath.FilePath
的用法示例。
在下文中一共展示了FilePath.descendant方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: FilesystemSynchronousBackend
# 需要导入模块: from twisted.python.filepath import FilePath [as 别名]
# 或者: from twisted.python.filepath.FilePath import descendant [as 别名]
class FilesystemSynchronousBackend(object):
"""A synchronous filesystem backend.
@see: L{IBackend}
@param base_path: the base filesystem path for this backend, any attempts to
read or write 'above' the specified path will be denied
@type base_path: C{bytes} or L{FilePath<twisted.python.filepath.FilePath>}
@param can_read: whether or not this backend should support reads
@type can_read: C{bool}
@param can_write: whether or not this backend should support writes
@type can_write: C{bool}
"""
def __init__(self, base_path, can_read=True, can_write=True):
try:
self.base = FilePath(base_path.path)
except AttributeError:
self.base = FilePath(base_path)
self.can_read, self.can_write = can_read, can_write
@deferred
def get_reader(self, file_name):
"""
@see: L{IBackend.get_reader}
@rtype: L{Deferred}, yielding a L{FilesystemReader}
"""
if not self.can_read:
raise Unsupported("Reading not supported")
try:
target_path = self.base.descendant(file_name.split(b"/"))
except InsecurePath as e:
raise AccessViolation("Insecure path: %s" % e)
return FilesystemReader(target_path)
@deferred
def get_writer(self, file_name):
"""
@see: L{IBackend.get_writer}
@rtype: L{Deferred}, yielding a L{FilesystemWriter}
"""
if not self.can_write:
raise Unsupported("Writing not supported")
try:
target_path = self.base.descendant(file_name.split(b"/"))
except InsecurePath as e:
raise AccessViolation("Insecure path: %s" % e)
return FilesystemWriter(target_path)
示例2: test_bootstrap_pyc
# 需要导入模块: from twisted.python.filepath import FilePath [as 别名]
# 或者: from twisted.python.filepath.FilePath import descendant [as 别名]
def test_bootstrap_pyc(self):
"""
``create_virtualenv`` creates links to the pyc files for all the
modules required for the virtualenv bootstrap process.
"""
target_path = FilePath(self.mktemp())
create_virtualenv(root=target_path)
py_files = []
for module_name in VIRTUALENV_REQUIRED_MODULES:
py_base = target_path.descendant(['lib', 'python2.7', module_name])
py = py_base.siblingExtension('.py')
pyc = py_base.siblingExtension('.pyc')
if py.exists() and False in (py.islink(), pyc.islink()):
py_files.append('PY: {} > {}\nPYC: {} > {}\n'.format(
'/'.join(py.segmentsFrom(target_path)),
py.realpath().path,
'/'.join(pyc.segmentsFrom(target_path)),
pyc.islink() and pyc.realpath().path or 'NOT A SYMLINK'
))
if py_files:
self.fail(
'Non-linked bootstrap pyc files in {}: \n{}'.format(
target_path, '\n'.join(py_files)
)
)
示例3: BackendSelection
# 需要导入模块: from twisted.python.filepath import FilePath [as 别名]
# 或者: from twisted.python.filepath.FilePath import descendant [as 别名]
class BackendSelection(unittest.TestCase):
test_data = """line1
line2
line3
"""
def setUp(self):
self.temp_dir = FilePath(tempfile.mkdtemp())
self.existing_file_name = self.temp_dir.descendant(("dir", "foo"))
self.existing_file_name.parent().makedirs()
self.existing_file_name.setContent(self.test_data)
@inlineCallbacks
def test_read_supported_by_default(self):
b = FilesystemSynchronousBackend(self.temp_dir.path)
reader = yield b.get_reader('dir/foo')
self.assertTrue(IReader.providedBy(reader))
@inlineCallbacks
def test_write_supported_by_default(self):
b = FilesystemSynchronousBackend(self.temp_dir.path)
writer = yield b.get_writer('dir/bar')
self.assertTrue(IWriter.providedBy(writer))
def test_read_unsupported(self):
b = FilesystemSynchronousBackend(self.temp_dir.path, can_read=False)
return self.assertFailure(b.get_reader('dir/foo'), Unsupported)
def test_write_unsupported(self):
b = FilesystemSynchronousBackend(self.temp_dir.path, can_write=False)
return self.assertFailure(b.get_writer('dir/bar'), Unsupported)
def test_insecure_reader(self):
b = FilesystemSynchronousBackend(self.temp_dir.path)
return self.assertFailure(
b.get_reader('../foo'), AccessViolation)
def test_insecure_writer(self):
b = FilesystemSynchronousBackend(self.temp_dir.path)
return self.assertFailure(
b.get_writer('../foo'), AccessViolation)
@inlineCallbacks
def test_read_ignores_leading_and_trailing_slashes(self):
b = FilesystemSynchronousBackend(self.temp_dir.path)
reader = yield b.get_reader('/dir/foo/')
segments_from_root = reader.file_path.segmentsFrom(self.temp_dir)
self.assertEqual(["dir", "foo"], segments_from_root)
@inlineCallbacks
def test_write_ignores_leading_and_trailing_slashes(self):
b = FilesystemSynchronousBackend(self.temp_dir.path)
writer = yield b.get_writer('/dir/bar/')
segments_from_root = writer.file_path.segmentsFrom(self.temp_dir)
self.assertEqual(["dir", "bar"], segments_from_root)
def tearDown(self):
shutil.rmtree(self.temp_dir.path)
示例4: FlockerDeployConfigureSSHTests
# 需要导入模块: from twisted.python.filepath import FilePath [as 别名]
# 或者: from twisted.python.filepath.FilePath import descendant [as 别名]
class FlockerDeployConfigureSSHTests(TestCase):
"""
Tests for ``DeployScript._configure_ssh``.
"""
@_require_installed
def setUp(self):
self.sshd_config = FilePath(self.mktemp())
self.server = create_ssh_server(self.sshd_config)
self.addCleanup(self.server.restore)
self.flocker_config = FilePath(self.mktemp())
self.local_user_ssh = FilePath(self.mktemp())
self.config = OpenSSHConfiguration(
ssh_config_path=self.local_user_ssh,
flocker_path=self.flocker_config)
self.configure_ssh = self.config.configure_ssh
# ``configure_ssh`` expects ``ssh`` to already be able to
# authenticate against the server. Set up an ssh-agent to
# help it do that against our testing server.
self.agent = create_ssh_agent(self.server.key_path, self)
def test_installs_public_sshkeys(self):
"""
``DeployScript._configure_ssh`` installs the cluster wide public ssh
keys on each node in the supplied ``Deployment``.
"""
deployment = Deployment(
nodes=frozenset([
Node(
hostname=str(self.server.ip),
applications=None
),
# Node(
# hostname='node2.example.com',
# applications=None
# )
])
)
script = DeployScript(
ssh_configuration=self.config, ssh_port=self.server.port)
result = script._configure_ssh(deployment)
local_key = self.local_user_ssh.child(b'id_rsa_flocker.pub')
authorized_keys = self.sshd_config.descendant([
b'home', b'.ssh', b'authorized_keys'])
def check_authorized_keys(ignored):
self.assertIn(local_key.getContent().rstrip(),
authorized_keys.getContent().splitlines())
result.addCallback(check_authorized_keys)
return result
示例5: test_absolute_args_no_box
# 需要导入模块: from twisted.python.filepath import FilePath [as 别名]
# 或者: from twisted.python.filepath.FilePath import descendant [as 别名]
def test_absolute_args_no_box(self):
"""
When invoked as `build-vagrant-box`, specifying a box is required.
"""
path = FilePath(self.mktemp())
path.createDirectory()
base_path = path.descendant(['bin', 'build-vagrant-box'])
options = BuildOptions(base_path=base_path, top_level=path)
self.assertRaises(UsageError, options.parseOptions, [])
示例6: test_relative_args_with_box
# 需要导入模块: from twisted.python.filepath import FilePath [as 别名]
# 或者: from twisted.python.filepath.FilePath import descendant [as 别名]
def test_relative_args_with_box(self):
"""
When invoked as `build`, no box can be specified.
"""
path = FilePath(self.mktemp())
path.createDirectory()
base_path = path.descendant(['somewhere', 'box-name', 'build'])
options = BuildOptions(base_path=base_path, top_level=path)
self.assertRaises(UsageError, options.parseOptions, ['--box', 'box'])
示例7: test_absolute_args
# 需要导入模块: from twisted.python.filepath import FilePath [as 别名]
# 或者: from twisted.python.filepath.FilePath import descendant [as 别名]
def test_absolute_args(self):
"""
When invoked as `build-vagrant-box`, :class:`BuildOption` takes the
path relative to the top-level, and the box name from the passed
argument.
"""
path = FilePath(self.mktemp())
path.createDirectory()
base_path = path.descendant(['bin', 'build-vagrant-box'])
options = BuildOptions(base_path=base_path, top_level=path)
options.parseOptions(['--box', 'box-name'])
self.assertEqual(options, {
'box': 'box-name',
'path': path.descendant(['vagrant', 'box-name']),
'build-server': 'http://build.clusterhq.com/',
'branch': None,
'flocker-version': flocker_version,
})
示例8: test_relative_args
# 需要导入模块: from twisted.python.filepath import FilePath [as 别名]
# 或者: from twisted.python.filepath.FilePath import descendant [as 别名]
def test_relative_args(self):
"""
When invoked as `build`, no box can be specified. BuildOption takes the
path from the parent of :file:`build`, and the box name from the name
of
that directory.
"""
path = FilePath(self.mktemp())
path.createDirectory()
base_path = path.descendant(['somewhere', 'box-name', 'build'])
options = BuildOptions(base_path=base_path, top_level=path)
options.parseOptions([])
self.assertEqual(options, {
'box': 'box-name',
'path': path.descendant(['somewhere', 'box-name']),
'build-server': 'http://build.clusterhq.com/',
'branch': None,
'flocker-version': flocker_version,
})
示例9: test_pythonpath
# 需要导入模块: from twisted.python.filepath import FilePath [as 别名]
# 或者: from twisted.python.filepath.FilePath import descendant [as 别名]
def test_pythonpath(self):
"""
``create_virtualenv`` installs a virtual python whose path does not
include the system python libraries.
"""
target_path = FilePath(self.mktemp())
create_virtualenv(root=target_path)
output = check_output([
target_path.descendant(['bin', 'python']).path,
'-c', r'import sys; sys.stdout.write("\n".join(sys.path))'
])
# We should probably check for lib64 as well here.
self.assertNotIn(
'/usr/lib/python2.7/site-packages', output.splitlines())
示例10: test_install
# 需要导入模块: from twisted.python.filepath import FilePath [as 别名]
# 或者: from twisted.python.filepath.FilePath import descendant [as 别名]
def test_install(self):
"""
``VirtualEnv.install`` accepts a ``PythonPackage`` instance and
installs it.
"""
virtualenv_dir = FilePath(self.mktemp())
virtualenv = create_virtualenv(root=virtualenv_dir)
package_dir = FilePath(self.mktemp())
package = canned_package(package_dir)
virtualenv.install(package_dir.path)
self.assertIn(
'{}-{}-py2.7.egg-info'.format(package.name, package.version),
[f.basename() for f in virtualenv_dir.descendant(
['lib', 'python2.7', 'site-packages']).children()]
)
示例11: test_run
# 需要导入模块: from twisted.python.filepath import FilePath [as 别名]
# 或者: from twisted.python.filepath.FilePath import descendant [as 别名]
def test_run(self):
"""
``CreateLinks.run`` generates symlinks in ``destination_path`` for all
the supplied ``links``.
"""
root = FilePath(self.mktemp())
bin_dir = root.descendant(['usr', 'bin'])
bin_dir.makedirs()
CreateLinks(
links=frozenset([
(FilePath('/opt/flocker/bin/flocker-foo'), bin_dir),
(FilePath('/opt/flocker/bin/flocker-bar'), bin_dir),
])
).run()
self.assertEqual(
set(FilePath('/opt/flocker/bin').child(script)
for script in ('flocker-foo', 'flocker-bar')),
set(child.realpath() for child in bin_dir.children())
)
示例12: test_steps
# 需要导入模块: from twisted.python.filepath import FilePath [as 别名]
# 或者: from twisted.python.filepath.FilePath import descendant [as 别名]
def test_steps(self):
"""
``build_in_docker`` returns a ``BuildSequence`` comprising
``DockerBuild`` and ``DockerRun`` instances.
"""
supplied_distribution = 'Foo'
expected_tag = 'clusterhq/build-%s' % (supplied_distribution,)
supplied_top_level = FilePath('/foo/bar')
expected_build_directory = supplied_top_level.descendant(
['admin', 'build_targets', supplied_distribution])
supplied_destination_path = FilePath('/baz/qux')
expected_volumes = {
FilePath('/output'): supplied_destination_path,
FilePath('/flocker'): supplied_top_level,
}
expected_package_uri = 'http://www.example.com/foo/bar/whl'
assert_equal_steps(
test_case=self,
expected=BuildSequence(
steps=[
DockerBuild(
tag=expected_tag,
build_directory=expected_build_directory
),
DockerRun(
tag=expected_tag,
volumes=expected_volumes,
command=[expected_package_uri]
),
]
),
actual=build_in_docker(
destination_path=supplied_destination_path,
distribution=supplied_distribution,
top_level=supplied_top_level,
package_uri=expected_package_uri
)
)
示例13: create_proxy_to
# 需要导入模块: from twisted.python.filepath import FilePath [as 别名]
# 或者: from twisted.python.filepath.FilePath import descendant [as 别名]
#.........这里部分代码省略.........
# Tag it as a flocker-created rule so we can recognize it later.
b"--match", b"comment", b"--comment", FLOCKER_PROXY_COMMENT_MARKER,
# If the filter matched, jump to the DNAT chain to handle doing the
# actual packet mangling. DNAT is a built-in chain that already
# knows how to do this. Pass an argument to the DNAT chain so it
# knows how to mangle the packet - rewrite the destination IP of
# the address to the target we were told to use.
b"--jump", b"DNAT", b"--to-destination", encoded_ip,
])
# Bonus round! Having performed DNAT (changing the destination) during
# prerouting we are now prepared to send the packet on somewhere else.
# On its way out of this system it is also necessary to further
# modify and then track that packet. We want it to look like it
# comes from us (the downstream client will be *very* confused if
# the node we're passing the packet on to replies *directly* to them;
# and by confused I mean it will be totally broken, of course) so we
# also need to "masquerade" in the postrouting chain. This changes
# the source address (ip and port) of the packet to the address of
# the external interface the packet is exiting upon. Doing SNAT here
# would be a little bit more efficient because the kernel could avoid
# looking up the external interface's address for every single packet.
# But it requires this code to know that address and it requires that
# if it ever changes the rule gets updated and it may require some
# steps to do port allocation (not sure what they are yet). So we'll
# just masquerade for now.
iptables(logger, [
# All NAT stuff happens in the netfilter NAT table.
b"--table", b"nat",
# As described above, this transformation happens after routing
# decisions have been made and the packet is on its way out of the
# system. Therefore, append the rule to the POSTROUTING chain.
b"--append", b"POSTROUTING",
# We'll stick to matching the same kinds of packets we matched in
# the earlier stage. We might want to change the factoring of this
# code to avoid the duplication - particularly in case we want to
# change the specifics of the filter.
#
# This omits the LOCAL addrtype check, though, because at this
# point the packet is definitely leaving this host.
b"--protocol", b"tcp", b"--destination-port", encoded_port,
# Do the masquerading.
b"--jump", b"MASQUERADE",
])
# Secret level!! Traffic that originates *on* the host bypasses the
# PREROUTING chain. Instead, it passes through the OUTPUT chain. If
# we want connections from localhost to the forwarded port to be
# affected then we need a rule in the OUTPUT chain to do the same kind
# of DNAT that we did in the PREROUTING chain.
iptables(logger, [
# All NAT stuff happens in the netfilter NAT table.
b"--table", b"nat",
# As mentioned, this rule is for the OUTPUT chain.
b"--append", b"OUTPUT",
# Matching the exact same kinds of packets as the PREROUTING rule
# matches.
b"--protocol", b"tcp",
b"--destination-port", encoded_port,
b"--match", b"addrtype", b"--dst-type", b"LOCAL",
# Do the same DNAT as we did in the rule for the PREROUTING chain.
b"--jump", b"DNAT", b"--to-destination", encoded_ip,
])
iptables(logger, [
b"--table", b"filter",
b"--insert", b"FORWARD",
b"--destination", encoded_ip,
b"--protocol", b"tcp", b"--destination-port", encoded_port,
b"--jump", b"ACCEPT",
])
# The network stack only considers forwarding traffic when certain
# system configuration is in place.
#
# https://www.kernel.org/doc/Documentation/networking/ip-sysctl.txt
# will explain the meaning of these in (very slightly) more detail.
conf = FilePath(b"/proc/sys/net/ipv4/conf")
descendant = conf.descendant([b"default", b"forwarding"])
with descendant.open("wb") as forwarding:
forwarding.write(b"1")
# In order to have the OUTPUT chain DNAT rule affect routing decisions,
# we also need to tell the system to make routing decisions about
# traffic from or to localhost.
for path in conf.children():
with path.child(b"route_localnet").open("wb") as route_localnet:
route_localnet.write(b"1")
return Proxy(ip=ip, port=port)
示例14: FlockerDeployConfigureSSHTests
# 需要导入模块: from twisted.python.filepath import FilePath [as 别名]
# 或者: from twisted.python.filepath.FilePath import descendant [as 别名]
class FlockerDeployConfigureSSHTests(TestCase):
"""
Tests for ``DeployScript._configure_ssh``.
"""
@_require_installed
def setUp(self):
self.sshd_config = FilePath(self.mktemp())
self.server = create_ssh_server(self.sshd_config)
self.addCleanup(self.server.restore)
self.flocker_config = FilePath(self.mktemp())
self.local_user_ssh = FilePath(self.mktemp())
self.config = OpenSSHConfiguration(
ssh_config_path=self.local_user_ssh,
flocker_path=self.flocker_config)
self.configure_ssh = self.config.configure_ssh
# ``configure_ssh`` expects ``ssh`` to already be able to
# authenticate against the server. Set up an ssh-agent to
# help it do that against our testing server.
self.agent = create_ssh_agent(self.server.key_path, self)
def test_installs_public_sshkeys(self):
"""
``DeployScript._configure_ssh`` installs the cluster wide public ssh
keys on each node in the supplied ``Deployment``.
"""
deployment = Deployment(
nodes=frozenset([
Node(
hostname=str(self.server.ip),
applications=None
),
# Node(
# hostname='node2.example.com',
# applications=None
# )
])
)
script = DeployScript(
ssh_configuration=self.config, ssh_port=self.server.port)
result = script._configure_ssh(deployment)
local_key = self.local_user_ssh.child(b'id_rsa_flocker.pub')
authorized_keys = self.sshd_config.descendant([
b'home', b'.ssh', b'authorized_keys'])
def check_authorized_keys(ignored):
self.assertIn(local_key.getContent().rstrip(),
authorized_keys.getContent().splitlines())
result.addCallback(check_authorized_keys)
return result
def test_sshkey_installation_failure(self):
"""
``DeployScript._configure_ssh`` fires with an errback if one of the
configuration attempts fails.
"""
def fail(host, port):
raise ZeroDivisionError()
self.config.configure_ssh = fail
deployment = Deployment(
nodes=frozenset([
Node(
hostname=str(self.server.ip),
applications=None
),
])
)
script = DeployScript(
ssh_configuration=self.config, ssh_port=self.server.port)
result = script._configure_ssh(deployment)
result.addErrback(lambda f: f.value.subFailure)
result = self.assertFailure(result, ZeroDivisionError)
# Handle errors logged by gather_deferreds
self.addCleanup(self.flushLoggedErrors, ZeroDivisionError)
return result
def test_sshkey_installation_ssh_process_failure(self):
"""
``DeployScript._configure_ssh`` fires with a ``SystemExit`` errback
containing the SSH process output if one of the configuration
attempts fails.
"""
def fail(host, port):
raise CalledProcessError(1, "ssh", output=b"onoes")
self.config.configure_ssh = fail
deployment = Deployment(
nodes=frozenset([
Node(
hostname=str(self.server.ip),
applications=None
),
])
#.........这里部分代码省略.........
示例15: GetExtensionsTest
# 需要导入模块: from twisted.python.filepath import FilePath [as 别名]
# 或者: from twisted.python.filepath.FilePath import descendant [as 别名]
class GetExtensionsTest(TestCase):
"""
Tests for L{dist.getExtensions}.
"""
setupTemplate = (
"from twisted.python.dist import ConditionalExtension\n"
"extensions = [\n"
" ConditionalExtension(\n"
" '%s', ['twisted/some/thing.c'],\n"
" condition=lambda builder: True)\n"
" ]\n")
def setUp(self):
self.basedir = FilePath(self.mktemp()).child("twisted")
self.basedir.makedirs()
self.addCleanup(os.chdir, os.getcwd())
os.chdir(self.basedir.parent().path)
def writeSetup(self, name, *path):
"""
Write out a C{setup.py} file to a location determined by
L{self.basedir} and L{path}. L{self.setupTemplate} is used to
generate its contents.
"""
outdir = self.basedir.descendant(path)
outdir.makedirs()
setup = outdir.child("setup.py")
setup.setContent(self.setupTemplate % (name,))
def writeEmptySetup(self, *path):
"""
Write out an empty C{setup.py} file to a location determined by
L{self.basedir} and L{path}.
"""
outdir = self.basedir.descendant(path)
outdir.makedirs()
outdir.child("setup.py").setContent("")
def assertExtensions(self, expected):
"""
Assert that the given names match the (sorted) names of discovered
extensions.
"""
extensions = dist.getExtensions()
names = [extension.name for extension in extensions]
self.assertEqual(sorted(names), expected)
def test_getExtensions(self):
"""
Files named I{setup.py} in I{twisted/topfiles} and I{twisted/*/topfiles}
are executed with L{execfile} in order to discover the extensions they
declare.
"""
self.writeSetup("twisted.transmutate", "topfiles")
self.writeSetup("twisted.tele.port", "tele", "topfiles")
self.assertExtensions(["twisted.tele.port", "twisted.transmutate"])
def test_getExtensionsTooDeep(self):
"""
Files named I{setup.py} in I{topfiles} directories are not considered if
they are too deep in the directory hierarchy.
"""
self.writeSetup("twisted.trans.mog.rify", "trans", "mog", "topfiles")
self.assertExtensions([])
def test_getExtensionsNotTopfiles(self):
"""
The folder in which I{setup.py} is discovered must be called I{topfiles}
otherwise it is ignored.
"""
self.writeSetup("twisted.metamorphosis", "notfiles")
self.assertExtensions([])
def test_getExtensionsNotSupportedOnJava(self):
"""
Extensions are not supported on Java-based platforms.
"""
self.addCleanup(setattr, sys, "platform", sys.platform)
sys.platform = "java"
self.writeSetup("twisted.sorcery", "topfiles")
self.assertExtensions([])
def test_getExtensionsExtensionsLocalIsOptional(self):
"""
It is acceptable for extensions to not define the C{extensions} local
variable.
"""
self.writeEmptySetup("twisted.necromancy", "topfiles")
self.assertExtensions([])