Ejemplo n.º 1
0
def _copy_file(source, destination, preserve_mode=False, preserve_times=False, update=False, dry_run=0):
    log.info("copying file %s -> %s", source, destination)
    with zipio.open(source, 'rb') as fp_in:
        if not dry_run:
            if os.path.exists(destination):
                os.unlink(destination)

            with open(destination, 'wb') as fp_out:
                data = fp_in.read()
                fp_out.write(data)

            if preserve_mode:
                mode = None
                if hasattr(zipio, 'getmode'):
                    mode = zipio.getmode(source)

                elif os.path.isfile(source):
                    mode = os.stat(source).st_mode

                if mode is not None:
                    os.chmod(destination, mode)

            if preserve_times:
                mtime = zipio.getmtime(source)
                os.utime(destination, (mtime, mtime))
Ejemplo n.º 2
0
def _copy_file(source,
               destination,
               preserve_mode=False,
               preserve_times=False,
               update=False,
               dry_run=0):
    log.info("copying file %s -> %s", source, destination)
    with zipio.open(source, 'rb') as fp_in:
        if not dry_run:
            if os.path.exists(destination):
                os.unlink(destination)

            with open(destination, 'wb') as fp_out:
                data = fp_in.read()
                fp_out.write(data)

            if preserve_mode:
                mode = None
                if hasattr(zipio, 'getmode'):
                    mode = zipio.getmode(source)

                elif os.path.isfile(source):
                    mode = os.stat(source).st_mode

                if mode is not None:
                    os.chmod(destination, mode)

            if preserve_times:
                mtime = zipio.getmtime(source)
                os.utime(destination, (mtime, mtime))
Ejemplo n.º 3
0
def _copy_file(
    source,
    destination,
    preserve_mode=False,
    preserve_times=False,
    update=False,
    dry_run=0,
):
    log.info("copying file %s -> %s", source, destination)
    with zipio.open(source, "rb") as fp_in:
        if not dry_run:
            if os.path.isdir(destination):
                destination = os.path.join(destination, os.path.basename(source))
            if os.path.exists(destination):
                os.unlink(destination)

            with open(destination, "wb") as fp_out:
                data = fp_in.read()
                fp_out.write(data)

            if preserve_mode:
                mode = None
                if hasattr(zipio, "getmode"):
                    mode = zipio.getmode(source)

                elif os.path.isfile(source):
                    mode = stat.S_IMODE(os.stat(source).st_mode)

                if mode is not None:
                    os.chmod(destination, mode)

            if preserve_times:
                mtime = zipio.getmtime(source)
                os.utime(destination, (mtime, mtime))
Ejemplo n.º 4
0
    def test_open(self):
        # 1. Regular file
        fp = zipio.open(os.path.join(TESTDATA, 'test.txt'), 'r')
        data = fp.read()
        fp.close()
        self.assertEqual(data, 'This is test.txt\n')

        if sys.version_info[0] == 3:
            fp = zipio.open(os.path.join(TESTDATA, 'test.txt'), 'rb')
            data = fp.read()
            fp.close()
            self.assertEqual(data, b'This is test.txt\n')

        # 2. File inside zipfile
        fp = zipio.open(os.path.join(TESTDATA, 'zipped.egg', 'test.txt'), 'r')
        data = fp.read()
        fp.close()
        self.assertEqual(data, 'Zipped up test.txt\n')

        if sys.version_info[0] == 3:
            fp = zipio.open(os.path.join(TESTDATA, 'zipped.egg', 'test.txt'), 'rb')
            data = fp.read()
            fp.close()
            self.assertEqual(data, b'Zipped up test.txt\n')

        # 3. EXC: Directory inside zipfile
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'zipped.egg', 'subdir'))
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'zipped.egg', 'subdir2'))
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'zipped.egg', 'subdir2/subdir'))
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'zipped.egg', 'subdir3'))
        # TODO: Add subdir4/file.txt, without directory entry
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'zipped.egg', 'subdir4'))

        # 4. EXC: No such file in zipfile
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'zipped.egg', 'no-such-file'))
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'zipped.egg', 'subdir/no-such-file'))

        # 5. EXC: No such regular file
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'no-such-file.txt'))

        # 6. EXC: Open r/w
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'test.txt'), 'w')
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'test.txt'), 'a')
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'test.txt'), 'r+')
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'test.txt'), 'w+')
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'test.txt'), 'a+')
    def test_open(self):
        # 1. Regular file
        fp = zipio.open(os.path.join(TESTDATA, 'test.txt'), 'r')
        data = fp.read()
        fp.close()
        self.assertEqual(data, 'This is test.txt\n')

        if sys.version_info[0] == 3:
            fp = zipio.open(os.path.join(TESTDATA, 'test.txt'), 'rb')
            data = fp.read()
            fp.close()
            self.assertEqual(data, b'This is test.txt\n')

        # 2. File inside zipfile
        fp = zipio.open(os.path.join(TESTDATA, 'zipped.egg', 'test.txt'), 'r')
        data = fp.read()
        fp.close()
        self.assertEqual(data, 'Zipped up test.txt\n')

        if sys.version_info[0] == 3:
            fp = zipio.open(os.path.join(TESTDATA, 'zipped.egg', 'test.txt'), 'rb')
            data = fp.read()
            fp.close()
            self.assertEqual(data, b'Zipped up test.txt\n')

        # 3. EXC: Directory inside zipfile
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'zipped.egg', 'subdir'))
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'zipped.egg', 'subdir2'))
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'zipped.egg', 'subdir2/subdir'))
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'zipped.egg', 'subdir3'))
        # TODO: Add subdir4/file.txt, without directory entry
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'zipped.egg', 'subdir4'))

        # 4. EXC: No such file in zipfile
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'zipped.egg', 'no-such-file'))
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'zipped.egg', 'subdir/no-such-file'))

        # 5. EXC: No such regular file
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'no-such-file.txt'))

        # 6. EXC: Open r/w
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'test.txt'), 'w')
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'test.txt'), 'a')
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'test.txt'), 'r+')
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'test.txt'), 'w+')
        self.assertRaises(IOError, zipio.open, os.path.join(TESTDATA, 'test.txt'), 'a+')
Ejemplo n.º 6
0
    def test_contextlib(self):
        # 1. Regular file
        with zipio.open(os.path.join(TESTDATA, 'test.txt'), 'r') as fp:
            data = fp.read()
        try:
            fp.read()
            self.fail("file not closed")
        except (ValueError, IOError):
            pass

        self.assertEqual(data, 'This is test.txt\n')

        if sys.version_info[0] == 3:
            with zipio.open(os.path.join(TESTDATA, 'test.txt'), 'rb') as fp:
                data = fp.read()
            try:
                fp.read()
                self.fail("file not closed")
            except (ValueError, IOError):
                pass

            self.assertEqual(data, b'This is test.txt\n')

        # 2. File inside zipfile
        with zipio.open(os.path.join(TESTDATA, 'zipped.egg', 'test.txt'),
                        'r') as fp:
            data = fp.read()
        try:
            fp.read()
            self.fail("file not closed")
        except (ValueError, IOError):
            pass
        self.assertEqual(data, 'Zipped up test.txt\n')

        if sys.version_info[0] == 3:
            with zipio.open(os.path.join(TESTDATA, 'zipped.egg', 'test.txt'),
                            'rb') as fp:
                data = fp.read()
            try:
                fp.read()
                self.fail("file not closed")
            except (IOError, ValueError):
                pass
            self.assertEqual(data, b'Zipped up test.txt\n')
Ejemplo n.º 7
0
    def test_contextlib(self):
        # 1. Regular file
        with zipio.open(os.path.join(TESTDATA, 'test.txt'), 'r') as fp:
            data = fp.read()
        try:
            fp.read()
            self.fail("file not closed")
        except (ValueError, IOError):
            pass

        self.assertEqual(data, 'This is test.txt\n')

        if sys.version_info[0] == 3:
            with zipio.open(os.path.join(TESTDATA, 'test.txt'), 'rb') as fp:
                data = fp.read()
            try:
                fp.read()
                self.fail("file not closed")
            except (ValueError, IOError):
                pass

            self.assertEqual(data, b'This is test.txt\n')

        # 2. File inside zipfile
        with zipio.open(os.path.join(TESTDATA, 'zipped.egg', 'test.txt'), 'r') as fp:
            data = fp.read()
        try:
            fp.read()
            self.fail("file not closed")
        except (ValueError, IOError):
            pass
        self.assertEqual(data, 'Zipped up test.txt\n')

        if sys.version_info[0] == 3:
            with zipio.open(os.path.join(TESTDATA, 'zipped.egg', 'test.txt'), 'rb') as fp:
                data = fp.read()
            try:
                fp.read()
                self.fail("file not closed")
            except (IOError, ValueError):
                pass
            self.assertEqual(data, b'Zipped up test.txt\n')
Ejemplo n.º 8
0
def _copy_file(source, destination, preserve_mode=False, preserve_times=False, update=False, dry_run=0):
    log.info("copying file %s -> %s", source, destination)
    with zipio.open(source, 'rb') as fp_in:
        if not dry_run:
            with open(destination, 'wb') as fp_out:
                data = fp_in.read()
                fp_out.write(data)

            if preserve_mode:
                # XXX: copy current mode
                pass

            if preserve_times:
                # XXX: copy current times
                pass
Ejemplo n.º 9
0
def _copy_file(source,
               destination,
               preserve_mode=False,
               preserve_times=False,
               update=False,
               dry_run=0):
    log.info("copying file %s -> %s", source, destination)
    with zipio.open(source, 'rb') as fp_in:
        if not dry_run:
            with open(destination, 'wb') as fp_out:
                data = fp_in.read()
                fp_out.write(data)

            if preserve_mode:
                # XXX: copy current mode
                pass

            if preserve_times:
                # XXX: copy current times
                pass
Ejemplo n.º 10
0
def byte_compile(py_files,
                 optimize=0,
                 force=0,
                 target_dir=None,
                 verbose=1,
                 dry_run=0,
                 direct=None):

    if direct is None:
        direct = (__debug__ and optimize == 0)

    # "Indirect" byte-compilation: write a temporary script and then
    # run it with the appropriate flags.
    if not direct:
        from tempfile import mktemp
        from distutils.util import execute, spawn
        script_name = mktemp(".py")
        if verbose:
            print("writing byte-compilation script '%s'" % script_name)
        if not dry_run:
            with open(script_name, "w") as script:
                script.write("""
from py2app.util import byte_compile
from modulegraph.modulegraph import *
files = [
""")

                for f in py_files:
                    script.write(repr(f) + ",\n")
                script.write("]\n")
                script.write("""
byte_compile(files, optimize=%r, force=%r,
             target_dir=%r,
             verbose=%r, dry_run=0,
             direct=1)
""" % (optimize, force, target_dir, verbose))

        # Ensure that py2app is on PYTHONPATH, this ensures that
        # py2app.util can be found even when we're running from
        # an .egg that was downloaded by setuptools
        import py2app
        pp = os.path.dirname(os.path.dirname(py2app.__file__))
        if 'PYTHONPATH' in os.environ:
            pp = '%s:%s' % (pp, os.environ['PYTHONPATH'])

        cmd = [
            '/usr/bin/env',
            'PYTHONPATH=%s' % (pp, ), sys.executable, script_name
        ]
        if optimize == 1:
            cmd.insert(3, "-O")
        elif optimize == 2:
            cmd.insert(3, "-OO")
        spawn(cmd, verbose=verbose, dry_run=dry_run)
        execute(os.remove, (script_name, ),
                "removing %s" % script_name,
                verbose=verbose,
                dry_run=dry_run)

    else:
        from py_compile import compile
        from distutils.dir_util import mkpath

        for mod in py_files:
            # Terminology from the py_compile module:
            #   cfile - byte-compiled file
            #   dfile - purported source filename (same as 'file' by default)
            if mod.filename == mod.identifier:
                cfile = os.path.basename(mod.filename)
                dfile = cfile + (__debug__ and 'c' or 'o')
            else:
                cfile = mod.identifier.replace('.', os.sep)

                if mod.packagepath:
                    dfile = cfile + os.sep + '__init__.py' + (__debug__ and 'c'
                                                              or 'o')
                else:
                    dfile = cfile + '.py' + (__debug__ and 'c' or 'o')
            if target_dir:
                cfile = os.path.join(target_dir, dfile)

            if force or newer(mod.filename, cfile):
                if verbose:
                    print("byte-compiling %s to %s" % (mod.filename, dfile))

                if not dry_run:
                    mkpath(os.path.dirname(cfile))
                    suffix = os.path.splitext(mod.filename)[1]

                    if suffix in ('.py', '.pyw'):
                        fn = cfile + '.py'

                        with zipio.open(mod.filename, 'rb') as fp_in:
                            with open(fn, 'wb') as fp_out:
                                fp_out.write(fp_in.read())

                        compile(fn, cfile, dfile)
                        os.unlink(fn)

                    elif suffix in PY_SUFFIXES:
                        # Minor problem: This will happily copy a file
                        # <mod>.pyo to <mod>.pyc or <mod>.pyc to
                        # <mod>.pyo, but it does seem to work.
                        copy_file(mod.filename, cfile, preserve_times=True)

                    else:
                        raise RuntimeError \
                              ("Don't know how to handle %r" % mod.filename)
            else:
                if verbose:
                    print("skipping byte-compilation of %s to %s" %
                          (mod.filename, dfile))
Ejemplo n.º 11
0
def byte_compile(py_files, optimize=0, force=0,
                 target_dir=None, verbose=1, dry_run=0,
                 direct=None):

    if direct is None:
        direct = (__debug__ and optimize == 0)

    # "Indirect" byte-compilation: write a temporary script and then
    # run it with the appropriate flags.
    if not direct:
        from tempfile import mktemp
        from distutils.util import execute, spawn
        script_name = mktemp(".py")
        if verbose:
            print("writing byte-compilation script '%s'" % script_name)
        if not dry_run:
            with open(script_name, "w") as script:
                script.write("""
from py2app.util import byte_compile
from modulegraph.modulegraph import *
files = [
""")

                for f in py_files:
                    script.write(repr(f) + ",\n")
                script.write("]\n")
                script.write("""
byte_compile(files, optimize=%r, force=%r,
             target_dir=%r,
             verbose=%r, dry_run=0,
             direct=1)
""" % (optimize, force, target_dir, verbose))

        # Ensure that py2app is on PYTHONPATH, this ensures that
        # py2app.util can be found even when we're running from
        # an .egg that was downloaded by setuptools
        import py2app
        pp = os.path.dirname(os.path.dirname(py2app.__file__))
        if 'PYTHONPATH' in os.environ:
            pp = '%s:%s'%(pp, os.environ['PYTHONPATH'])

        cmd = ['/usr/bin/env', 'PYTHONPATH=%s'%(pp,), sys.executable, script_name]
        if optimize == 1:
            cmd.insert(3, "-O")
        elif optimize == 2:
            cmd.insert(3, "-OO")
        spawn(cmd, verbose=verbose, dry_run=dry_run)
        execute(os.remove, (script_name,), "removing %s" % script_name,
                verbose=verbose, dry_run=dry_run)


    else:
        from py_compile import compile
        from distutils.dir_util import mkpath

        for mod in py_files:
            # Terminology from the py_compile module:
            #   cfile - byte-compiled file
            #   dfile - purported source filename (same as 'file' by default)
            if mod.filename == mod.identifier:
                cfile = os.path.basename(mod.filename)
                dfile = cfile + (__debug__ and 'c' or 'o')
            else:
                cfile = mod.identifier.replace('.', os.sep)

                if mod.packagepath:
                    dfile = cfile + os.sep + '__init__.py' + (__debug__ and 'c' or 'o')
                else:
                    dfile = cfile + '.py' + (__debug__ and 'c' or 'o')
            if target_dir:
                cfile = os.path.join(target_dir, dfile)

            if force or newer(mod.filename, cfile):
                if verbose:
                    print("byte-compiling %s to %s" % (mod.filename, dfile))
                    
                if not dry_run:
                    mkpath(os.path.dirname(cfile))
                    suffix = os.path.splitext(mod.filename)[1]

                    if suffix in ('.py', '.pyw'):
                        fn = cfile + '.py'

                        with zipio.open(mod.filename, 'rb') as fp_in:
                            with open(fn, 'wb') as fp_out:
                                fp_out.write(fp_in.read())

                        compile(fn, cfile, dfile)
                        os.unlink(fn)

                    elif suffix in PY_SUFFIXES:
                        # Minor problem: This will happily copy a file
                        # <mod>.pyo to <mod>.pyc or <mod>.pyc to
                        # <mod>.pyo, but it does seem to work.
                        copy_file(mod.filename, cfile)

                    else:
                        raise RuntimeError \
                              ("Don't know how to handle %r" % mod.filename)
            else:
                if verbose:
                    print("skipping byte-compilation of %s to %s" % 
                          (mod.filename, dfile))
Ejemplo n.º 12
0
def byte_compile(
    py_files,
    force=0,
    target_dir=None,
    progress=None,
    dry_run=0,
    optimize=-1,
):
    if progress is not None:
        task_id = progress.add_task("Byte compiling", len(py_files))
    for mod in py_files:
        # Terminology from the py_compile module:
        #   cfile - byte-compiled file
        #   dfile - purported source filename (same as 'file' by default)
        if mod.filename == mod.identifier:
            cfile = os.path.basename(mod.filename)
            dfile = cfile + (__debug__ and "c" or "o")
        else:
            cfile = mod.identifier.replace(".", os.sep)

            if mod.packagepath:
                dfile = cfile + os.sep + "__init__.pyc"
            else:
                dfile = cfile + ".pyc"
        if target_dir:
            cfile = os.path.join(target_dir, dfile)

        if force or newer(mod.filename, cfile):
            if progress is not None:
                progress.trace(f"byte-compiling {mod.filename} to {dfile}")

            if not dry_run:
                if not os.path.exists(os.path.dirname(cfile)):
                    if progress is not None:
                        progress.trace(f"create {os.path.dirname(cfile)}")
                    os.makedirs(os.path.dirname(cfile), 0o777)
                suffix = os.path.splitext(mod.filename)[1]

                if suffix in (".py", ".pyw"):
                    fn = cfile + ".py"

                    with zipio.open(mod.filename, "rb") as fp_in:
                        with open(fn, "wb") as fp_out:
                            fp_out.write(fp_in.read())

                    compile(fn, cfile, dfile, optimize=optimize)
                    os.unlink(fn)

                elif suffix in PY_SUFFIXES:
                    # Minor problem: This will happily copy a file
                    # <mod>.pyo to <mod>.pyc or <mod>.pyc to
                    # <mod>.pyo, but it does seem to work.
                    copy_file(mod.filename, cfile, preserve_times=True)

                else:
                    raise RuntimeError("Don't know how to handle %r" %
                                       mod.filename)
        else:
            if progress is not None:
                progress.info(
                    f"skipping byte-compilation of {mod.filename} to {dfile}")

        if progress is not None:
            progress.step_task(task_id)

    if progress is not None:
        progress._progress.stop_task(task_id)