示例#1
0
    def test_listdir(self):
        # 1. Regular directory
        self.assertEqual(set(os.listdir(os.path.join(TESTDATA, 'subdir'))),
                         set(['file1.txt', 'file2.txt']))

        # 2. Zipfile with files in directory
        self.assertEqual(
            set(zipio.listdir(os.path.join(TESTDATA, 'zipped.egg'))),
            set(['test.txt', 'subdir', 'subdir2', 'subdir3', 'subdir4']))

        # 3. Zipfile with files in subdirectory
        self.assertEqual(
            set(zipio.listdir(os.path.join(TESTDATA, 'zipped.egg', 'subdir'))),
            set(['file1.txt', 'file2.txt']))
        self.assertEqual(
            set(zipio.listdir(os.path.join(TESTDATA, 'zipped.egg',
                                           'subdir2'))), set(['subdir']))
        self.assertEqual(
            set(
                zipio.listdir(
                    os.path.join(TESTDATA, 'zipped.egg', 'subdir4',
                                 'subdir6'))), set(['mydir']))

        # 4. Zipfile with entry for directory, no files
        self.assertEqual(
            set(zipio.listdir(os.path.join(TESTDATA, 'zipped.egg',
                                           'subdir3'))), set([]))

        # 5. EXC: Zipfile without directory
        self.assertRaises(IOError, zipio.listdir,
                          os.path.join(TESTDATA, 'zipped.egg', 'subdir10'))

        # 6. EXC: Regular directory doesn't exist
        self.assertRaises(IOError, zipio.listdir,
                          os.path.join(TESTDATA, 'subdir10'))
示例#2
0
文件: util.py 项目: HanTester/test
def copy_resource(source, destination, dry_run=0, symlink=0):
    """
    Copy a resource file into the application bundle
    """
    import py2app.converters as conv

    converter = find_converter(source)
    if converter is not None:
        converter(source, destination, dry_run=dry_run)
        return

    if os.path.isdir(source):
        if not dry_run:
            if not os.path.exists(destination):
                os.mkdir(destination)
        for fn in zipio.listdir(source):
            copy_resource(os.path.join(source, fn),
                          os.path.join(destination, fn),
                          dry_run=dry_run,
                          symlink=symlink)

    else:
        if symlink:
            if not dry_run:
                if os.path.exists(destination):
                    os.unlink(destination)
                os.symlink(os.path.abspath(source), destination)

        else:
            copy_file(source, destination, dry_run=dry_run, preserve_mode=True)
示例#3
0
def os_listdir(path):
    """
    Deprecated name
    """
    warnings.warn("Use zipio.listdir instead of os_listdir",
            DeprecationWarning)
    return zipio.listdir(path)
示例#4
0
def copy_resource(source, destination, dry_run=0, symlink=0):
    """
    Copy a resource file into the application bundle
    """
    import py2app.converters as conv

    converter = find_converter(source)
    if converter is not None:
        converter(source, destination, dry_run=dry_run)
        return

    if os.path.isdir(source):
        if not dry_run:
            if not os.path.exists(destination):
                os.mkdir(destination)
        for fn in zipio.listdir(source):
            copy_resource(os.path.join(source, fn), 
                    os.path.join(destination, fn), dry_run=dry_run, symlink=symlink)

    else:
        if symlink:
            if not dry_run:
                if os.path.exists(destination):
                    os.unlink(destination)
                os.symlink(os.path.abspath(source), destination)

        else:
            copy_file(source, destination, dry_run=dry_run)
示例#5
0
def os_listdir(path):
    """
    Deprecated name
    """
    warnings.warn("Use zipio.listdir instead of os_listdir",
            DeprecationWarning) 
    return zipio.listdir(path)
示例#6
0
    def test_listdir(self):
        # 1. Regular directory
        self.assertEqual(set(os.listdir(os.path.join(TESTDATA, 'subdir'))), set(['file1.txt', 'file2.txt']))

        # 2. Zipfile with files in directory
        self.assertEqual(set(zipio.listdir(os.path.join(TESTDATA, 'zipped.egg'))), set([
            'test.txt', 'subdir', 'subdir2', 'subdir3', 'subdir4']))

        # 3. Zipfile with files in subdirectory
        self.assertEqual(set(zipio.listdir(os.path.join(TESTDATA, 'zipped.egg', 'subdir'))), set(['file1.txt', 'file2.txt']))
        self.assertEqual(set(zipio.listdir(os.path.join(TESTDATA, 'zipped.egg', 'subdir2'))), set(['subdir']))
        self.assertEqual(set(zipio.listdir(os.path.join(TESTDATA, 'zipped.egg', 'subdir4', 'subdir6'))), set(['mydir']))

        # 4. Zipfile with entry for directory, no files
        self.assertEqual(set(zipio.listdir(os.path.join(TESTDATA, 'zipped.egg', 'subdir3'))), set([]))

        # 5. EXC: Zipfile without directory
        self.assertRaises(IOError, zipio.listdir, os.path.join(TESTDATA, 'zipped.egg', 'subdir10'))

        # 6. EXC: Regular directory doesn't exist
        self.assertRaises(IOError, zipio.listdir, os.path.join(TESTDATA, 'subdir10'))
示例#7
0
 def find_all_submodules(self, m):
     if not m.packagepath:
         return
     # 'suffixes' used to be a list hardcoded to [".py", ".pyc", ".pyo"].
     # But we must also collect Python extension modules - although
     # we cannot separate normal dlls from Python extensions.
     suffixes = [triple[0] for triple in imp.get_suffixes()]
     for path in m.packagepath:
         try:
             names = zipio.listdir(path)
         except (os.error, IOError):
             self.msg(2, "can't list directory", path)
             continue
         for (path, mode, typ) in ifilter(None, imap(moduleInfoForPath, names)):
             if path != "__init__":
                 yield path
示例#8
0
 def find_all_submodules(self, m):
     if not m.packagepath:
         return
     # 'suffixes' used to be a list hardcoded to [".py", ".pyc", ".pyo"].
     # But we must also collect Python extension modules - although
     # we cannot separate normal dlls from Python extensions.
     suffixes = [triple[0] for triple in imp.get_suffixes()]
     for path in m.packagepath:
         try:
             names = zipio.listdir(path)
         except (os.error, IOError):
             self.msg(2, "can't list directory", path)
             continue
         for info in (moduleInfoForPath(p) for p in names):
             if info is None: continue
             if info[0] != '__init__':
                 yield info[0]
示例#9
0
def copy_resource(source, destination, dry_run=0, symlink=0):
    """
    Copy a resource file into the application bundle
    """
    if hasattr(source, "getvalue"):
        if not dry_run:
            contents = source.getvalue()

            if isinstance(contents, bytes):
                mode = "wb"
            else:
                mode = "w"

            if os.path.exists(destination):
                os.unlink(destination)
            with open(destination, mode) as fp:
                fp.write(contents)
        return

    converter = find_converter(source)
    if converter is not None:
        converter(source, destination, dry_run=dry_run)
        return

    if os.path.isdir(source):
        if not dry_run:
            if not os.path.exists(destination):
                os.mkdir(destination)
        for fn in zipio.listdir(source):
            copy_resource(
                os.path.join(source, fn),
                os.path.join(destination, fn),
                dry_run=dry_run,
                symlink=symlink,
            )

    else:
        if symlink:
            if not dry_run:
                make_symlink(os.path.abspath(source), destination)

        else:
            copy_file(source, destination, dry_run=dry_run, preserve_mode=True)
示例#10
0
文件: util.py 项目: HanTester/test
def copy_tree(src,
              dst,
              preserve_mode=1,
              preserve_times=1,
              preserve_symlinks=0,
              update=0,
              verbose=0,
              dry_run=0,
              condition=None):
    """
    Copy an entire directory tree 'src' to a new location 'dst'.  Both
    'src' and 'dst' must be directory names.  If 'src' is not a
    directory, raise DistutilsFileError.  If 'dst' does not exist, it is
    created with 'mkpath()'.  The end result of the copy is that every
    file in 'src' is copied to 'dst', and directories under 'src' are
    recursively copied to 'dst'.  Return the list of files that were
    copied or might have been copied, using their output name.  The
    return value is unaffected by 'update' or 'dry_run': it is simply
    the list of all files under 'src', with the names changed to be
    under 'dst'.

    'preserve_mode' and 'preserve_times' are the same as for
    'copy_file'; note that they only apply to regular files, not to
    directories.  If 'preserve_symlinks' is true, symlinks will be
    copied as symlinks (on platforms that support them!); otherwise
    (the default), the destination of the symlink will be copied.
    'update' and 'verbose' are the same as for 'copy_file'.
    """
    assert isinstance(src, (str, unicode)), repr(src)
    assert isinstance(dst, (str, unicode)), repr(dst)

    from distutils.dir_util import mkpath
    from distutils.dep_util import newer
    from distutils.errors import DistutilsFileError
    from distutils import log

    src = fsencoding(src)
    dst = fsencoding(dst)

    if condition is None:
        condition = skipscm

    if not dry_run and not zipio.isdir(src):
        raise DistutilsFileError("cannot copy tree '%s': not a directory" %
                                 src)
    try:
        names = zipio.listdir(src)
    except os.error as exc:
        (errno, errstr) = exc.args
        if dry_run:
            names = []
        else:
            raise DistutilsFileError("error listing files in '%s': %s" %
                                     (src, errstr))

    if not dry_run:
        mkpath(dst)

    outputs = []

    for n in names:
        src_name = os.path.join(src, n)
        dst_name = os.path.join(dst, n)
        if (condition is not None) and (not condition(src_name)):
            continue

        if preserve_symlinks and zipio.islink(src_name):
            link_dest = zipio.readlink(src_name)
            log.info("linking %s -> %s", dst_name, link_dest)
            if not dry_run:
                if update and not newer(src, dst_name):
                    pass
                else:
                    if os.path.islink(dst_name):
                        os.remove(dst_name)
                    os.symlink(link_dest, dst_name)
            outputs.append(dst_name)

        elif zipio.isdir(src_name) and not os.path.isfile(src_name):
            # ^^^ this odd tests ensures that resource files that
            # happen to be a zipfile won't get extracted.
            # XXX: need API in zipio to clean up this code
            outputs.extend(
                copy_tree(src_name,
                          dst_name,
                          preserve_mode,
                          preserve_times,
                          preserve_symlinks,
                          update,
                          dry_run=dry_run,
                          condition=condition))
        else:
            copy_file(src_name,
                      dst_name,
                      preserve_mode,
                      preserve_times,
                      update,
                      dry_run=dry_run)
            outputs.append(dst_name)

    return outputs
示例#11
0
def copy_tree(src, dst,
        preserve_mode=1,
        preserve_times=1,
        preserve_symlinks=0,
        update=0,
        verbose=0,
        dry_run=0,
        condition=None):

    """
    Copy an entire directory tree 'src' to a new location 'dst'.  Both
    'src' and 'dst' must be directory names.  If 'src' is not a
    directory, raise DistutilsFileError.  If 'dst' does not exist, it is
    created with 'mkpath()'.  The end result of the copy is that every
    file in 'src' is copied to 'dst', and directories under 'src' are
    recursively copied to 'dst'.  Return the list of files that were
    copied or might have been copied, using their output name.  The
    return value is unaffected by 'update' or 'dry_run': it is simply
    the list of all files under 'src', with the names changed to be
    under 'dst'.

    'preserve_mode' and 'preserve_times' are the same as for
    'copy_file'; note that they only apply to regular files, not to
    directories.  If 'preserve_symlinks' is true, symlinks will be
    copied as symlinks (on platforms that support them!); otherwise
    (the default), the destination of the symlink will be copied.
    'update' and 'verbose' are the same as for 'copy_file'.
    """
    assert isinstance(src, (str, unicode)), repr(src)
    assert isinstance(dst, (str, unicode)), repr(dst)


    from distutils.dir_util import mkpath
    from distutils.dep_util import newer
    from distutils.errors import DistutilsFileError
    from distutils import log

    src = fsencoding(src)
    dst = fsencoding(dst)

    if condition is None:
        condition = skipscm

    if not dry_run and not zipio.isdir(src):
        raise DistutilsFileError(
              "cannot copy tree '%s': not a directory" % src)
    try:
        names = zipio.listdir(src)
    except os.error as exc:
        (errno, errstr) = exc.args
        if dry_run:
            names = []
        else:
            raise DistutilsFileError(
                  "error listing files in '%s': %s" % (src, errstr))

    if not dry_run:
        mkpath(dst)

    outputs = []

    for n in names:
        src_name = os.path.join(src, n)
        dst_name = os.path.join(dst, n)
        if (condition is not None) and (not condition(src_name)):
            continue

        if preserve_symlinks and zipio.islink(src_name):
            link_dest = zipio.readlink(src_name)
            log.info("linking %s -> %s", dst_name, link_dest)
            if not dry_run:
                if update and not newer(src, dst_name):
                    pass
                else:
                    if os.path.islink(dst_name):
                        os.remove(dst_name)
                    os.symlink(link_dest, dst_name)
            outputs.append(dst_name)

        elif zipio.isdir(src_name) and not os.path.isfile(src_name):
            # ^^^ this odd tests ensures that resource files that
            # happen to be a zipfile won't get extracted.
            # XXX: need API in zipio to clean up this code
            outputs.extend(
                copy_tree(src_name, dst_name, preserve_mode,
                          preserve_times, preserve_symlinks, update,
                          dry_run=dry_run, condition=condition))
        else:
            copy_file(src_name, dst_name, preserve_mode,
                      preserve_times, update, dry_run=dry_run)
            outputs.append(dst_name)

    return outputs
示例#12
0
def copy_tree(
    src,
    dst,
    preserve_mode=1,
    preserve_times=1,
    preserve_symlinks=0,
    update=0,
    verbose=0,
    dry_run=0,
    condition=None,
    progress=None,
):
    """
    Copy an entire directory tree 'src' to a new location 'dst'.  Both
    'src' and 'dst' must be directory names.  If 'src' is not a
    directory, raise DistutilsFileError.  If 'dst' does not exist, it is
    created with 'mkpath()'.  The end result of the copy is that every
    file in 'src' is copied to 'dst', and directories under 'src' are
    recursively copied to 'dst'.  Return the list of files that were
    copied or might have been copied, using their output name.  The
    return value is unaffected by 'update' or 'dry_run': it is simply
    the list of all files under 'src', with the names changed to be
    under 'dst'.

    'preserve_mode' and 'preserve_times' are the same as for
    'copy_file'; note that they only apply to regular files, not to
    directories.  If 'preserve_symlinks' is true, symlinks will be
    copied as symlinks (on platforms that support them!); otherwise
    (the default), the destination of the symlink will be copied.
    'update' and 'verbose' are the same as for 'copy_file'.
    """
    assert isinstance(src, str), repr(src)
    assert isinstance(dst, str), repr(dst)

    from distutils.dep_util import newer
    from distutils.errors import DistutilsFileError

    if condition is None:
        condition = skipscm

    if not dry_run and not zipio.isdir(src):
        raise DistutilsFileError("cannot copy tree '%s': not a directory" %
                                 src)
    try:
        names = zipio.listdir(src)
    except os.error as exc:
        (errno, errstr) = exc.args
        if dry_run:
            names = []
        else:
            raise DistutilsFileError(
                f"error listing files in '{src}': {errstr}")

    if not dry_run and not os.path.exists(dst):
        if progress is not None:
            progress.trace(f"creating {dst}")
        os.makedirs(dst, 0o777)

    outputs = []

    for n in names:
        src_name = os.path.join(src, n)
        dst_name = os.path.join(dst, n)
        if (condition is not None) and (not condition(src_name)):
            continue

        # Note: using zipio's internal _locate function throws an IOError on
        # dead symlinks, so handle it here.
        if os.path.islink(src_name) and not os.path.exists(
                os.path.join(src, os.readlink(src_name))):
            continue

        if preserve_symlinks and zipio.islink(src_name):
            link_dest = zipio.readlink(src_name)
            if progress is not None:
                progress.trace(f"linking {dst_name} -> {link_dest}")
            if not dry_run:
                if update and not newer(src, dst_name):
                    pass
                else:
                    make_symlink(link_dest, dst_name)
            outputs.append(dst_name)

        elif zipio.isdir(src_name) and not os.path.isfile(src_name):
            # ^^^ this odd tests ensures that resource files that
            # happen to be a zipfile won't get extracted.
            outputs.extend(
                copy_tree(
                    src_name,
                    dst_name,
                    preserve_mode,
                    preserve_times,
                    preserve_symlinks,
                    update,
                    dry_run=dry_run,
                    condition=condition,
                    progress=progress,
                ))
        else:
            copy_file(
                src_name,
                dst_name,
                preserve_mode,
                preserve_times,
                update,
                dry_run=dry_run,
                progress=progress,
            )
            outputs.append(dst_name)

    return outputs