Пример #1
0
    def test_unzipping(self):
        self.makeZipFiles()

        zipstream.unzip('littlefiles.zip')
        self.failUnless(os.path.isfile('zipstreamdir/937'))
        self.cleanupUnzippedJunk()

        uziter=zipstream.unzipIter('littlefiles.zip')
        r=uziter.next()
        self.assertEqual(r, 999)
        for r in uziter: pass
        self.assertEqual(r, 0)

        # test that files don't get overwritten unless you tell it to
        # do so
        # overwrite zipstreamjunk with some different contents
        stuff('zipstreamjunk', 'stuff')
        zipstream.unzip('bigfile.zip')
        newmd5=newsum('zipstreamjunk')
        self.assertEqual(newmd5, 'c13d88cb4cb02003daedb8a84e5d272a')

        zipstream.unzip('bigfile.zip', overwrite=1)
        newmd5=newsum('zipstreamjunk')
        self.assertEqual(newmd5, junkmd5)

        self.cleanupUnzippedJunk()

        uziter=zipstream.unzipIterChunky('bigfile.zip', chunksize=500)
        r=uziter.next()
        # test that the number of chunks is in the right ballpark;
        # this could theoretically be any number but statistically it
        # should always be in this range
        approx = 35<r<45
        self.failUnless(approx)
        for r in uziter: pass
        self.assertEqual(r, 0)
        newmd5=newsum('zipstreamjunk')
        self.assertEqual(newmd5, junkmd5)

        self.cleanupUnzippedJunk()

        uziter=zipstream.unzipIterChunky('bigfile_deflated.zip',
                                         chunksize=972)
        r=uziter.next()
        approx = 23<r<27
        self.failUnless(approx)
        for r in uziter: pass
        self.assertEqual(r, 0)
        newmd5=newsum('zipstreamjunk')
        self.assertEqual(newmd5, junkmd5)
Пример #2
0
    def test_overwrite(self):
        """
        L{twisted.python.zipstream.unzip} and
        L{twisted.python.zipstream.unzipIter} shouldn't overwrite files unless
        the 'overwrite' flag is passed
        """
        testfile = self.unzipdir.child('0')
        zpfilename = self.makeZipFile(['OVERWRITTEN'])

        testfile.setContent('NOT OVERWRITTEN')
        zipstream.unzip(zpfilename, self.unzipdir.path)
        self.assertEquals(testfile.open().read(), 'NOT OVERWRITTEN')
        zipstream.unzip(zpfilename, self.unzipdir.path, overwrite=True)
        self.assertEquals(testfile.open().read(), 'OVERWRITTEN')

        testfile.setContent('NOT OVERWRITTEN')
        uziter = zipstream.unzipIter(zpfilename, self.unzipdir.path)
        uziter.next()
        self.assertEquals(testfile.open().read(), 'NOT OVERWRITTEN')
        uziter = zipstream.unzipIter(zpfilename, self.unzipdir.path,
                                     overwrite=True)
        uziter.next()
        self.assertEquals(testfile.open().read(), 'OVERWRITTEN')
Пример #3
0
    def test_unzipIter(self):
        """
        L{twisted.python.zipstream.unzipIter} should unzip a file for each
        iteration and yield the number of files left to unzip after that
        iteration
        """
        numfiles = 10
        contents = ['This is test file %d!' % i for i in range(numfiles)]
        zpfilename = self.makeZipFile(contents)
        uziter = zipstream.unzipIter(zpfilename, self.unzipdir.path)
        for i in range(numfiles):
            self.assertEqual(len(list(self.unzipdir.children())), i)
            self.assertEqual(uziter.next(), numfiles - i - 1)
        self.assertEqual(len(list(self.unzipdir.children())), numfiles)

        for child in self.unzipdir.children():
            num = int(child.basename())
            self.assertEqual(child.open().read(), contents[num])
Пример #4
0
def doItConsolicious(opt):
    # reclaim stdout/stderr from log
    sys.stdout = sys.__stdout__
    sys.stderr = sys.__stderr__
    if opt['zipfile']:
        print 'Unpacking documentation...'
        for n in zipstream.unzipIter(opt['zipfile'], opt['ziptargetdir']):
            if n % 100 == 0:
                print n,
            if n % 1000 == 0:
                print
        print 'Done unpacking.'
        
    if opt['compiledir']:
        print 'Compiling to pyc...'
        import compileall
        compileall.compile_dir(opt["compiledir"])
        print 'Done compiling.'
Пример #5
0
def doItConsolicious(opt):
    # reclaim stdout/stderr from log
    sys.stdout = sys.__stdout__
    sys.stderr = sys.__stderr__
    if opt['zipfile']:
        print 'Unpacking documentation...'
        for n in zipstream.unzipIter(opt['zipfile'], opt['ziptargetdir']):
            if n % 100 == 0:
                print n,
            if n % 1000 == 0:
                print
        print 'Done unpacking.'

    if opt['compiledir']:
        print 'Compiling to pyc...'
        import compileall
        compileall.compile_dir(opt["compiledir"])
        print 'Done compiling.'
Пример #6
0
    def test_unzipIter(self):
        """
        L{twisted.python.zipstream.unzipIter} should unzip a file for each
        iteration and yield the number of files left to unzip after that
        iteration
        """
        numfiles = 10
        contents = ['This is test file %d!' % i for i in range(numfiles)]
        zpfilename = self.makeZipFile(contents)
        uziter = zipstream.unzipIter(zpfilename, self.unzipdir.path)
        for i in range(numfiles):
            self.assertEquals(len(list(self.unzipdir.children())), i)
            self.assertEquals(uziter.next(), numfiles - i - 1)
        self.assertEquals(len(list(self.unzipdir.children())), numfiles)

        for child in self.unzipdir.children():
            num = int(child.basename())
            self.assertEquals(child.open().read(), contents[num])
Пример #7
0
    def test_unzipIterDeprecated(self):
        """
        Use of C{twisted.python.zipstream.unzipIter} will emit a
        deprecated warning.
        """
        zpfilename = self.makeZipFile('foo')

        self.assertEqual(len(self.flushWarnings()), 0)

        for f in zipstream.unzipIter(zpfilename, self.unzipdir.path):
            pass

        warnings = self.flushWarnings()
        self.assertEqual(len(warnings), 1)
        self.assertEqual(warnings[0]['category'], DeprecationWarning)
        self.assertEqual(
            warnings[0]['message'],
            "zipstream.unzipIter is deprecated since Twisted 11.0.0 for "
            "security reasons.  Use Python's zipfile instead.")
Пример #8
0
    def test_unzipIterDeprecated(self):
        """
        Use of C{twisted.python.zipstream.unzipIter} will emit a
        deprecated warning.
        """
        zpfilename = self.makeZipFile('foo')

        self.assertEqual(len(self.flushWarnings()), 0)

        for f in zipstream.unzipIter(zpfilename, self.unzipdir.path):
            pass

        warnings = self.flushWarnings()
        self.assertEqual(len(warnings), 1)
        self.assertEqual(warnings[0]['category'], DeprecationWarning)
        self.assertEqual(
            warnings[0]['message'],
            "zipstream.unzipIter is deprecated since Twisted 11.0.0 for "
            "security reasons.  Use Python's zipfile instead.")
Пример #9
0
 def test_unzipping(self):
     self.makeZipFiles()
     zipstream.unzip('littlefiles.zip')
     self.failUnless(os.path.isfile('zipstreamdir/937'))
     self.cleanupUnzippedJunk()
     uziter=zipstream.unzipIter('littlefiles.zip')
     r=uziter.next()
     self.assertEqual(r, 999)
     for r in uziter: pass
     self.assertEqual(r, 0)
     stuff('zipstreamjunk', 'stuff')
     zipstream.unzip('bigfile.zip')
     newmd5=newsum('zipstreamjunk')
     self.assertEqual(newmd5, 'c13d88cb4cb02003daedb8a84e5d272a')
     zipstream.unzip('bigfile.zip', overwrite=1)
     newmd5=newsum('zipstreamjunk')
     self.assertEqual(newmd5, junkmd5)
     self.cleanupUnzippedJunk()
     uziter=zipstream.unzipIterChunky('bigfile.zip', chunksize=500)
     r=uziter.next()
     approx = 35<r<45
     self.failUnless(approx)
     for r in uziter: pass
     self.assertEqual(r, 0)
     newmd5=newsum('zipstreamjunk')
     self.assertEqual(newmd5, junkmd5)
     self.cleanupUnzippedJunk()
     uziter=zipstream.unzipIterChunky('bigfile_deflated.zip',
                                      chunksize=972)
     r=uziter.next()
     approx = 23<r<27
     self.failUnless(approx)
     for r in uziter: pass
     self.assertEqual(r, 0)
     newmd5=newsum('zipstreamjunk')
     self.assertEqual(newmd5, junkmd5)