def test_os_listdir(self): root = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'testdata') self.assertEqual(modulegraph.os_listdir('/etc/'), os.listdir('/etc')) self.assertRaises(IOError, modulegraph.os_listdir, '/etc/hosts/foobar') self.assertRaises(IOError, modulegraph.os_listdir, os.path.join(root, 'test.egg', 'bar')) self.assertEqual(list(sorted(modulegraph.os_listdir(os.path.join(root, 'test.egg', 'foo')))), [ 'bar', 'bar.txt', 'baz.txt' ])
def test_os_listdir(self): root = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'testdata') self.assertEqual(modulegraph.os_listdir('/etc/'), os.listdir('/etc')) self.assertRaises(IOError, modulegraph.os_listdir, '/etc/hosts/foobar') self.assertRaises(IOError, modulegraph.os_listdir, os.path.join(root, 'test.egg', 'bar')) self.assertEqual( list( sorted( modulegraph.os_listdir( os.path.join(root, 'test.egg', 'foo')))), ['bar', 'bar.txt', 'baz.txt'])
def is_python_package(path): """Returns whether `path` is a python package (has a __init__.py(c|o) file). """ if os_path_isdir(path): for p in os_listdir(path): if p.startswith('__init__.') and p[8:] in {'.py', '.pyc', '.pyo'}: return True return False
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.file_util import copy_file 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 os_path_isdir(src): raise DistutilsFileError, \ "cannot copy tree '%s': not a directory" % src try: names = os_listdir(src) except os.error, (errno, errstr): if dry_run: names = [] else: raise DistutilsFileError, \ "error listing files in '%s': %s" % (src, errstr)
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'. """ from distutils.dir_util import mkpath from distutils.file_util import copy_file 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 os_path_isdir(src): raise DistutilsFileError, \ "cannot copy tree '%s': not a directory" % src try: names = os_listdir(src) except os.error, (errno, errstr): if dry_run: names = [] else: raise DistutilsFileError, \ "error listing files in '%s': %s" % (src, errstr)
def copy_package_data(self, package, target_dir): """ Copy any package data in a python package into the target_dir. This is a bit of a hack, it would be better to identify python eggs and copy those in whole. """ exts = [i[0] for i in imp.get_suffixes()] exts.append('.py') exts.append('.pyc') exts.append('.pyo') def datafilter(item): for e in exts: if item.endswith(e): return False return True target_dir = os.path.join(target_dir, *(package.identifier.split('.'))) for dname in package.packagepath: filenames = filter(datafilter, os_listdir(dname)) for fname in filenames: if fname in ('.svn', 'CVS'): # Scrub revision manager junk continue pth = os.path.join(dname, fname) # Check if we have found a package, exclude those if os_path_isdir(pth): for p in os_listdir(pth): if p.startswith('__init__.') and p[8:] in exts: break else: copy_tree(pth, os.path.join(target_dir, fname)) continue else: copy_file(pth, os.path.join(target_dir, fname))
def copy_package_data(self, package, target_dir): """ Copy any package data in a python package into the target_dir. This is a bit of a hack, it would be better to identify python eggs and copy those in whole. """ exts = [ i[0] for i in imp.get_suffixes() ] exts.append('.py') exts.append('.pyc') exts.append('.pyo') def datafilter(item): for e in exts: if item.endswith(e): return False return True target_dir = os.path.join(target_dir, *(package.identifier.split('.'))) for dname in package.packagepath: filenames = filter(datafilter, os_listdir(dname)) for fname in filenames: if fname in ('.svn', 'CVS'): # Scrub revision manager junk continue pth = os.path.join(dname, fname) # Check if we have found a package, exclude those if os_path_isdir(pth): for p in os_listdir(pth): if p.startswith('__init__.') and p[8:] in exts: break else: copy_tree(pth, os.path.join(target_dir, fname)) continue else: copy_file(pth, os.path.join(target_dir, fname))
def copy_resource(source, destination, dry_run=0): """ Copy a resource file into the application bundle """ if os.path.isdir(source): # XXX: This is wrong, need to call ourselves recursively if not dry_run: if not os.path.exists(destination): os.mkdir(destination) for fn in os_listdir(source): copy_resource(os.path.join(source, fn), os.path.join(destination, fn), dry_run=dry_run) else: copy_file_data(source, destination, dry_run=dry_run)
def copy_resource(source, destination, dry_run=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): # XXX: This is wrong, need to call ourselves recursively if not dry_run: if not os.path.exists(destination): os.mkdir(destination) for fn in os_listdir(source): copy_resource(os.path.join(source, fn), os.path.join(destination, fn), dry_run=dry_run) else: copy_file(source, destination, dry_run=dry_run)
def copy_package_data(package, target_dir): """ Copy any package data in a python package into the target_dir. This is a bit of a hack, it would be better to identify python eggs and copy those in whole. """ exts = [i[0] for i in imp.get_suffixes()] exts.append(".py") exts.append(".pyc") exts.append(".pyo") def datafilter(item): for e in exts: if item.endswith(e): return False return True target_dir = os.path.join(target_dir, *(package.identifier.split("."))) for dname in package.packagepath: filenames = list(filter(datafilter, os_listdir(dname))) for fname in filenames: if fname in SCMDIRS: # Scrub revision manager junk continue if fname in ("__pycache__",): # Ignore PEP 3147 bytecode cache continue pth = os.path.join(dname, fname) # Check if we have found a package, exclude those if is_python_package(pth): continue copydest = op.join(target_dir, fname) if op.isdir(pth): copy_tree(pth, copydest) else: copy_file(pth, copydest)
def copy_package_data(package, target_dir): """ Copy any package data in a python package into the target_dir. This is a bit of a hack, it would be better to identify python eggs and copy those in whole. """ exts = [i[0] for i in imp.get_suffixes()] exts.append('.py') exts.append('.pyc') exts.append('.pyo') def datafilter(item): for e in exts: if item.endswith(e): return False return True target_dir = os.path.join(target_dir, *(package.identifier.split('.'))) for dname in package.packagepath: filenames = list(filter(datafilter, os_listdir(dname))) for fname in filenames: if fname in SCMDIRS: # Scrub revision manager junk continue if fname in ('__pycache__', ): # Ignore PEP 3147 bytecode cache continue pth = os.path.join(dname, fname) # Check if we have found a package, exclude those if is_python_package(pth): continue copydest = op.join(target_dir, fname) if op.isdir(pth): copy_tree(pth, copydest) else: copy_file(pth, copydest)
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), repr(src) assert isinstance(dst, str), repr(dst) from distutils.dir_util import mkpath from distutils.file_util import copy_file from distutils.dep_util import newer from distutils.errors import DistutilsFileError from distutils import log if condition is None: condition = skipscm if not dry_run and not os_path_isdir(src): raise DistutilsFileError("cannot copy tree '%s': not a directory" % src) try: names = os_listdir(src) except os.error as xxx_todo_changeme: (errno, errstr) = xxx_todo_changeme.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 os_path_islink(src_name): link_dest = os_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 os_path_isdir(src_name): 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