Python twisted.python.filepath.FilePath() Examples

The following are 30 code examples of twisted.python.filepath.FilePath(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module twisted.python.filepath , or try the search function .
Example #1
Source File: test_webclient.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_downloadPageDeprecated(self):
        """
        L{client.downloadPage} is deprecated.
        """
        port = reactor.listenTCP(
            0, server.Site(Data(b'', 'text/plain')), interface="127.0.0.1")
        portno = port.getHost().port
        self.addCleanup(port.stopListening)
        url = networkString("http://127.0.0.1:%d" % (portno,))

        path = FilePath(self.mktemp())
        d = client.downloadPage(url, path.path)

        warningInfo = self.flushWarnings([self.test_downloadPageDeprecated])
        self.assertEqual(len(warningInfo), 1)
        self.assertEqual(warningInfo[0]['category'], DeprecationWarning)
        self.assertEqual(
            warningInfo[0]['message'],
            "twisted.web.client.downloadPage was deprecated in "
            "Twisted 16.7.0; please use https://pypi.org/project/treq/ or twisted.web.client.Agent instead")

        return d.addErrback(lambda _: None) 
Example #2
Source File: test_script.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_render(self):
        """
        L{ResourceScriptDirectory.getChild} returns a resource which renders a
        response with the HTTP 200 status code and the content of the rpy's
        C{request} global.
        """
        tmp = FilePath(self.mktemp())
        tmp.makedirs()
        tmp.child("test.rpy").setContent(b"""
from twisted.web.resource import Resource
class TestResource(Resource):
    isLeaf = True
    def render_GET(self, request):
        return b'ok'
resource = TestResource()""")
        resource = ResourceScriptDirectory(tmp._asBytesPath())
        request = DummyRequest([b''])
        child = resource.getChild(b"test.rpy", request)
        d = _render(child, request)
        def cbRendered(ignored):
            self.assertEqual(b"".join(request.written), b"ok")
        d.addCallback(cbRendered)
        return d 
Example #3
Source File: test_script.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_renderException(self):
        """
        L{ResourceScriptDirectory.getChild} returns a resource which renders a
        response with the HTTP 200 status code and the content of the rpy's
        C{request} global.
        """
        tmp = FilePath(self.mktemp())
        tmp.makedirs()
        child = tmp.child("test.epy")
        child.setContent(b'raise Exception("nooo")')
        resource = PythonScript(child._asBytesPath(), None)
        request = DummyRequest([b''])
        d = _render(resource, request)
        def cbRendered(ignored):
            self.assertIn(b"nooo", b"".join(request.written))
        d.addCallback(cbRendered)
        return d 
Example #4
Source File: test_update.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def setUp(self):

        self.srcdir = FilePath(self.mktemp())
        self.srcdir.makedirs()

        packagedir = self.srcdir.child('inctestpkg')
        packagedir.makedirs()

        packagedir.child('__init__.py').setContent(b"""
from incremental import Version
introduced_in = Version('inctestpkg', 'NEXT', 0, 0).short()
next_released_version = "inctestpkg NEXT"
""")
        self.getcwd = lambda: self.srcdir.path
        self.packagedir = packagedir

        class Date(object):
            year = 2016
            month = 8

        self.date = Date() 
Example #5
Source File: util.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def _removeSafely(path):
    """
    Safely remove a path, recursively.

    If C{path} does not contain a node named C{_trial_marker}, a
    L{_NoTrialMarker} exception is raised and the path is not removed.
    """
    if not path.child(b'_trial_marker').exists():
        raise _NoTrialMarker(
            '%r is not a trial temporary path, refusing to remove it'
            % (path,))
    try:
        path.remove()
    except OSError as e:
        print ("could not remove %r, caught OSError [Errno %s]: %s"
               % (path, e.errno, e.strerror))
        try:
            newPath = FilePath(b'_trial_temp_old' +
                               str(randrange(10000000)).encode("utf-8"))
            path.moveTo(newPath)
        except OSError as e:
            print ("could not rename path, caught OSError [Errno %s]: %s"
                   % (e.errno,e.strerror))
            raise 
Example #6
Source File: update.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def _findPath(path, package):

    cwd = FilePath(path)

    src_dir = cwd.child("src").child(package.lower())
    current_dir = cwd.child(package.lower())

    if src_dir.isdir():
        return src_dir
    elif current_dir.isdir():
        return current_dir
    else:
        raise ValueError(("Can't find under `./src` or `./`. Check the "
                          "package name is right (note that we expect your "
                          "package name to be lower cased), or pass it using "
                          "'--path'.")) 
Example #7
Source File: test_version.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def setUp(self):
        """
        Create a temporary directory with a package structure in it.
        """
        self.entry = FilePath(mkdtemp())
        self.addCleanup(self.entry.remove)

        self.preTestModules = sys.modules.copy()
        sys.path.append(self.entry.path)
        pkg = self.entry.child("incremental_test_package")
        pkg.makedirs()
        pkg.child("__init__.py").setContent(
            b"from incremental import Version\n"
            b"version = Version('incremental_test_package', 1, 0, 0)\n")
        self.svnEntries = pkg.child(".svn")
        self.svnEntries.makedirs() 
Example #8
Source File: test_runner.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_runnerDebuggerDefaultsToPdb(self):
        """
        Trial uses pdb if no debugger is specified by `--debugger`
        """
        self.parseOptions(['--debug', 'twisted.trial.test.sample'])
        pdbrcFile = FilePath("pdbrc")
        pdbrcFile.touch()

        self.runcall_called = False
        def runcall(pdb, suite, result):
            self.runcall_called = True
        self.patch(pdb.Pdb, "runcall", runcall)

        self.runSampleSuite(self.getRunner())

        self.assertTrue(self.runcall_called) 
Example #9
Source File: test_discover.py    From automat with MIT License 6 votes vote down vote up
def setUp(self):
        super(FindMachinesIntegrationTests, self).setUp()
        from .._discover import findMachines

        self.findMachines = findMachines

        packageDir = self.FilePath(self.pathDir).child("test_package")
        packageDir.makedirs()
        self.pythonPath = self.PythonPath([self.pathDir])
        self.writeSourceInto(self.SOURCE, packageDir.path, '__init__.py')

        subPackageDir = packageDir.child('subpackage')
        subPackageDir.makedirs()
        subPackageDir.child('__init__.py').touch()

        self.makeModule(self.SOURCE, subPackageDir.path, 'module.py')

        self.packageDict = self.loadModuleAsDict(
            self.pythonPath['test_package'])
        self.moduleDict = self.loadModuleAsDict(
            self.pythonPath['test_package']['subpackage']['module']) 
Example #10
Source File: test_tap.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def _pathOption(self):
        """
        Helper for the I{--path} tests which creates a directory and creates
        an L{Options} object which uses that directory as its static
        filesystem root.

        @return: A two-tuple of a L{FilePath} referring to the directory and
            the value associated with the C{'root'} key in the L{Options}
            instance after parsing a I{--path} option.
        """
        path = FilePath(self.mktemp())
        path.makedirs()
        options = Options()
        options.parseOptions(['--path', path.path])
        root = options['root']
        return path, root 
Example #11
Source File: test_tap.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_pathServer(self):
        """
        The I{--path} option to L{makeService} causes it to return a service
        which will listen on the server address given by the I{--port} option.
        """
        path = FilePath(self.mktemp())
        path.makedirs()
        port = self.mktemp()
        options = Options()
        options.parseOptions(['--port', 'unix:' + port, '--path', path.path])
        service = makeService(options)
        service.startService()
        self.addCleanup(service.stopService)
        self.assertIsInstance(service.services[0].factory.resource, File)
        self.assertEqual(service.services[0].factory.resource.path, path.path)
        self.assertTrue(os.path.exists(port))
        self.assertTrue(stat.S_ISSOCK(os.stat(port).st_mode)) 
Example #12
Source File: test_static.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_notFound(self):
        """
        If a request is made which encounters a L{File} before a final segment
        which does not correspond to any file in the path the L{File} was
        created with, a not found response is sent.
        """
        base = FilePath(self.mktemp())
        base.makedirs()
        file = static.File(base.path)

        request = DummyRequest([b'foobar'])
        child = resource.getChildForRequest(file, request)

        d = self._render(child, request)
        def cbRendered(ignored):
            self.assertEqual(request.responseCode, 404)
        d.addCallback(cbRendered)
        return d 
Example #13
Source File: test_static.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_securityViolationNotFound(self):
        """
        If a request is made which encounters a L{File} before a final segment
        which cannot be looked up in the filesystem due to security
        considerations, a not found response is sent.
        """
        base = FilePath(self.mktemp())
        base.makedirs()
        file = static.File(base.path)

        request = DummyRequest([b'..'])
        child = resource.getChildForRequest(file, request)

        d = self._render(child, request)
        def cbRendered(ignored):
            self.assertEqual(request.responseCode, 404)
        d.addCallback(cbRendered)
        return d 
Example #14
Source File: test_static.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_forbiddenResource(self):
        """
        If the file in the filesystem which would satisfy a request cannot be
        read, L{File.render} sets the HTTP response code to I{FORBIDDEN}.
        """
        base = FilePath(self.mktemp())
        base.setContent(b'')
        # Make sure we can delete the file later.
        self.addCleanup(base.chmod, 0o700)

        # Get rid of our own read permission.
        base.chmod(0)

        file = static.File(base.path)
        request = DummyRequest([b''])
        d = self._render(file, request)
        def cbRendered(ignored):
            self.assertEqual(request.responseCode, 403)
        d.addCallback(cbRendered)
        return d 
Example #15
Source File: endpoints.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def _privateCertFromPaths(certificatePath, keyPath):
    """
    Parse a certificate path and key path, either or both of which might be
    L{None}, into a certificate object.

    @param certificatePath: the certificate path
    @type certificatePath: L{bytes} or L{unicode} or L{None}

    @param keyPath: the private key path
    @type keyPath: L{bytes} or L{unicode} or L{None}

    @return: a L{PrivateCertificate} or L{None}
    """
    if certificatePath is None:
        return None
    certBytes = FilePath(certificatePath).getContent()
    if keyPath is None:
        return PrivateCertificate.loadPEM(certBytes)
    else:
        return PrivateCertificate.fromCertificateAndKeyPair(
            Certificate.loadPEM(certBytes),
            KeyPair.load(FilePath(keyPath).getContent(), 1)
        ) 
Example #16
Source File: test_static.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_indexNames(self):
        """
        If a request is made which encounters a L{File} before a final empty
        segment, a file in the L{File} instance's C{indexNames} list which
        exists in the path the L{File} was created with is served as the
        response to the request.
        """
        base = FilePath(self.mktemp())
        base.makedirs()
        base.child("foo.bar").setContent(b"baz")
        file = static.File(base.path)
        file.indexNames = [b'foo.bar']

        request = DummyRequest([b''])
        child = resource.getChildForRequest(file, request)

        d = self._render(child, request)
        def cbRendered(ignored):
            self.assertEqual(b''.join(request.written), b'baz')
            self.assertEqual(
                request.responseHeaders.getRawHeaders(b'content-length')[0],
                b'3')
        d.addCallback(cbRendered)
        return d 
Example #17
Source File: test_static.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_staticFile(self):
        """
        If a request is made which encounters a L{File} before a final segment
        which names a file in the path the L{File} was created with, that file
        is served as the response to the request.
        """
        base = FilePath(self.mktemp())
        base.makedirs()
        base.child("foo.bar").setContent(b"baz")
        file = static.File(base.path)

        request = DummyRequest([b'foo.bar'])
        child = resource.getChildForRequest(file, request)

        d = self._render(child, request)
        def cbRendered(ignored):
            self.assertEqual(b''.join(request.written), b'baz')
            self.assertEqual(
                request.responseHeaders.getRawHeaders(b'content-length')[0],
                b'3')
        d.addCallback(cbRendered)
        return d 
Example #18
Source File: test_static.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_ignoredExtensionsIgnored(self):
        """
        A request for the I{base} child of a L{File} succeeds with a resource
        for the I{base<extension>} file in the path the L{File} was created
        with if such a file exists and the L{File} has been configured to
        ignore the I{<extension>} extension.
        """
        base = FilePath(self.mktemp())
        base.makedirs()
        base.child('foo.bar').setContent(b'baz')
        base.child('foo.quux').setContent(b'foobar')
        file = static.File(base.path, ignoredExts=(b".bar",))

        request = DummyRequest([b"foo"])
        child = resource.getChildForRequest(file, request)

        d = self._render(child, request)
        def cbRendered(ignored):
            self.assertEqual(b''.join(request.written), b'baz')
        d.addCallback(cbRendered)
        return d 
Example #19
Source File: test_static.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_directoryWithoutTrailingSlashRedirects(self):
        """
        A request for a path which is a directory but does not have a trailing
        slash will be redirected to a URL which does have a slash by L{File}.
        """
        base = FilePath(self.mktemp())
        base.makedirs()
        base.child('folder').makedirs()
        file = static.File(base.path)

        request = DummyRequest([b"folder"])
        request.uri = b"http://dummy/folder#baz?foo=bar"
        child = resource.getChildForRequest(file, request)

        self.successResultOf(self._render(child, request))
        self.assertEqual(request.responseCode, FOUND)
        self.assertEqual(request.responseHeaders.getRawHeaders(b"location"),
                         [b"http://dummy/folder/#baz?foo=bar"]) 
Example #20
Source File: test_static.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def setUp(self):
        """
        Create a temporary file with a fixed payload of 64 bytes.  Create a
        resource for that file and create a request which will be for that
        resource.  Each test can set a different range header to test different
        aspects of the implementation.
        """
        path = FilePath(self.mktemp())
        # This is just a jumble of random stuff.  It's supposed to be a good
        # set of data for this test, particularly in order to avoid
        # accidentally seeing the right result by having a byte sequence
        # repeated at different locations or by having byte values which are
        # somehow correlated with their position in the string.
        self.payload = (b'\xf8u\xf3E\x8c7\xce\x00\x9e\xb6a0y0S\xf0\xef\xac\xb7'
                        b'\xbe\xb5\x17M\x1e\x136k{\x1e\xbe\x0c\x07\x07\t\xd0'
                        b'\xbckY\xf5I\x0b\xb8\x88oZ\x1d\x85b\x1a\xcdk\xf2\x1d'
                        b'&\xfd%\xdd\x82q/A\x10Y\x8b')
        path.setContent(self.payload)
        self.file = path.open()
        self.resource = static.File(self.file.name)
        self.resource.isLeaf = 1
        self.request = DummyRequest([b''])
        self.request.uri = self.file.name
        self.catcher = []
        log.addObserver(self.catcher.append) 
Example #21
Source File: test_static.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_renderFiles(self):
        """
        L{static.DirectoryLister} is able to list all the files inside a
        directory.
        """
        path = FilePath(self.mktemp())
        path.makedirs()
        path.child('file1').setContent(b"content1")
        path.child('file2').setContent(b"content2" * 1000)

        lister = static.DirectoryLister(path.path)
        data = lister.render(self._request(b'foo'))
        body = b"""<tr class="odd">
    <td><a href="file1">file1</a></td>
    <td>8B</td>
    <td>[text/html]</td>
    <td></td>
</tr>
<tr class="even">
    <td><a href="file2">file2</a></td>
    <td>7K</td>
    <td>[text/html]</td>
    <td></td>
</tr>"""
        self.assertIn(body, data) 
Example #22
Source File: test_static.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_renderFiltered(self):
        """
        L{static.DirectoryLister} takes an optional C{dirs} argument that
        filter out the list of directories and files printed.
        """
        path = FilePath(self.mktemp())
        path.makedirs()
        path.child('dir1').makedirs()
        path.child('dir2').makedirs()
        path.child('dir3').makedirs()
        lister = static.DirectoryLister(path.path, dirs=["dir1", "dir3"])
        data = lister.render(self._request(b'foo'))
        body = b"""<tr class="odd">
    <td><a href="dir1/">dir1/</a></td>
    <td></td>
    <td>[Directory]</td>
    <td></td>
</tr>
<tr class="even">
    <td><a href="dir3/">dir3/</a></td>
    <td></td>
    <td>[Directory]</td>
    <td></td>
</tr>"""
        self.assertIn(body, data) 
Example #23
Source File: test_static.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_brokenSymlink(self):
        """
        If on the file in the listing points to a broken symlink, it should not
        be returned by L{static.DirectoryLister._getFilesAndDirectories}.
        """
        path = FilePath(self.mktemp())
        path.makedirs()
        file1 = path.child('file1')
        file1.setContent(b"file1")
        file1.linkTo(path.child("file2"))
        file1.remove()

        lister = static.DirectoryLister(path.path)
        directory = os.listdir(path.path)
        directory.sort()
        dirs, files = lister._getFilesAndDirectories(directory)
        self.assertEqual(dirs, [])
        self.assertEqual(files, []) 
Example #24
Source File: test_template.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_load(self):
        """
        Verify that the loader returns a tag with the correct children.
        """
        loader = self.loaderFactory()
        tag, = loader.load()

        warnings = self.flushWarnings(offendingFunctions=[self.loaderFactory])
        if self.deprecatedUse:
            self.assertEqual(len(warnings), 1)
            self.assertEqual(warnings[0]['category'], DeprecationWarning)
            self.assertEqual(
                warnings[0]['message'],
                "Passing filenames or file objects to XMLFile is "
                "deprecated since Twisted 12.1.  Pass a FilePath instead.")
        else:
            self.assertEqual(len(warnings), 0)

        self.assertEqual(tag.tagName, 'p')
        self.assertEqual(tag.children, [u'Hello, world.']) 
Example #25
Source File: test_webclient.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_downloadPageBrokenDownload(self):
        """
        If the connection is closed before the number of bytes indicated by
        I{Content-Length} have been received, the L{Deferred} returned by
        L{downloadPage} fails with L{PartialDownloadError}.
        """
        # test what happens when download gets disconnected in the middle
        path = FilePath(self.mktemp())
        d = client.downloadPage(self.getURL("broken"), path.path)
        d = self.assertFailure(d, client.PartialDownloadError)

        def checkResponse(response):
            """
            The HTTP status code from the server is propagated through the
            C{PartialDownloadError}.
            """
            self.assertEqual(response.status, b"200")
            self.assertEqual(response.message, b"OK")
            return response
        d.addCallback(checkResponse)

        def cbFailed(ignored):
            self.assertEqual(path.getContent(), b"abc")
        d.addCallback(cbFailed)
        return d 
Example #26
Source File: test_webclient.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_downloadCookies(self):
        """
        The C{cookies} dict passed to the L{client.HTTPDownloader}
        initializer is used to populate the I{Cookie} header included in the
        request sent to the server.
        """
        output = self.mktemp()
        factory = client._makeGetterFactory(
            self.getURL('cookiemirror'),
            client.HTTPDownloader,
            fileOrName=output,
            cookies={b'foo': b'bar'})
        def cbFinished(ignored):
            self.assertEqual(
                FilePath(output).getContent(),
                b"[('foo', 'bar')]")
        factory.deferred.addCallback(cbFinished)
        return factory.deferred 
Example #27
Source File: test_warning.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_missingSource(self):
        """
        Warnings emitted by a function the source code of which is not
        available can still be flushed.
        """
        package = FilePath(self.mktemp().encode('utf-8')).child(b'twisted_private_helper')
        package.makedirs()
        package.child(b'__init__.py').setContent(b'')
        package.child(b'missingsourcefile.py').setContent(b'''
import warnings
def foo():
    warnings.warn("oh no")
''')
        pathEntry = package.parent().path.decode('utf-8')
        sys.path.insert(0, pathEntry)
        self.addCleanup(sys.path.remove, pathEntry)
        from twisted_private_helper import missingsourcefile
        self.addCleanup(sys.modules.pop, 'twisted_private_helper')
        self.addCleanup(sys.modules.pop, missingsourcefile.__name__)
        package.child(b'missingsourcefile.py').remove()

        missingsourcefile.foo()
        self.assertEqual(len(self.flushWarnings([missingsourcefile.foo])), 1) 
Example #28
Source File: test_irc.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_resumeWhenFileDoesNotExist(self):
        """
        If given a resumeOffset to resume writing to a file that does not
        exist, L{DccFileReceive} will raise L{OSError}.
        """
        fp = FilePath(self.mktemp())

        error = self.assertRaises(
            OSError,
            self.makeConnectedDccFileReceive, fp.path, resumeOffset=1)

        self.assertEqual(errno.ENOENT, error.errno) 
Example #29
Source File: test_irc.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_setOverwrite(self):
        """
        When local file already exists it can be overwritten using the
        L{DccFileReceive.set_overwrite} method.
        """
        fp = FilePath(self.mktemp())
        fp.setContent(b'I love contributing to Twisted!')
        protocol = self.makeConnectedDccFileReceive(fp.path, overwrite=True)

        self.allDataReceivedForProtocol(protocol, b'Twisted rocks!')

        self.assertEqual(fp.getContent(), b'Twisted rocks!') 
Example #30
Source File: test_irc.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_resumeFromResumeOffset(self):
        """
        If given a resumeOffset argument, L{DccFileReceive} will attempt to
        resume from that number of bytes if the file exists.
        """
        fp = FilePath(self.mktemp())
        fp.setContent(b'Twisted is awesome!')
        protocol = self.makeConnectedDccFileReceive(fp.path, resumeOffset=11)

        self.allDataReceivedForProtocol(protocol, b'amazing!')

        self.assertEqual(fp.getContent(), b'Twisted is amazing!')