Exemple #1
0
def copytree(src, dst, symlinks=False, ignore=None, dry_run=False):
    if not os.path.exists(dst):
        os.makedirs(dst)
        shutil.copystat(src, dst)
    lst = os.listdir(src)
    if ignore:
        excl = ignore(src, lst)
        lst = [x for x in lst if x not in excl]

    # do not copy lock files
    if '.conda_lock' in lst:
        lst.remove('.conda_lock')

    dst_lst = [os.path.join(dst, item) for item in lst]

    if not dry_run:
        for idx, item in enumerate(lst):
            s = os.path.join(src, item)
            d = dst_lst[idx]
            if symlinks and os.path.islink(s):
                if os.path.lexists(d):
                    os.remove(d)
                os.symlink(os.readlink(s), d)
                try:
                    st = os.lstat(s)
                    mode = stat.S_IMODE(st.st_mode)
                    os.lchmod(d, mode)
                except:
                    pass  # lchmod not available
            elif os.path.isdir(s):
                copytree(s, d, symlinks, ignore)
            else:
                _copy_with_shell_fallback(s, d)

    return dst_lst
Exemple #2
0
def copy_into(src, dst, timeout=90, symlinks=False, lock=None, locking=True, clobber=False):
    """Copy all the files and directories in src to the directory dst"""
    log = get_logger(__name__)
    if symlinks and islink(src):
        try:
            os.makedirs(os.path.dirname(dst))
        except OSError:
            pass
        if os.path.lexists(dst):
            os.remove(dst)
        src_base, dst_base = get_prefix_replacement_paths(src, dst)
        src_target = os.readlink(src)
        src_replaced = src_target.replace(src_base, dst_base)
        os.symlink(src_replaced, dst)
        try:
            st = os.lstat(src)
            mode = stat.S_IMODE(st.st_mode)
            os.lchmod(dst, mode)
        except:
            pass  # lchmod not available
    elif isdir(src):
        merge_tree(src, dst, symlinks, timeout=timeout, lock=lock, locking=locking, clobber=clobber)

    else:
        if isdir(dst):
            dst_fn = os.path.join(dst, os.path.basename(src))
        else:
            dst_fn = dst

        if os.path.isabs(src):
            src_folder = os.path.dirname(src)
        else:
            if os.path.sep in dst_fn:
                src_folder = os.path.dirname(dst_fn)
                if not os.path.isdir(src_folder):
                    os.makedirs(src_folder)
            else:
                src_folder = os.getcwd()

        if os.path.islink(src) and not os.path.exists(os.path.realpath(src)):
            log.warn('path %s is a broken symlink - ignoring copy', src)
            return

        if not lock:
            lock = get_lock(src_folder, timeout=timeout)
        locks = [lock] if locking else []
        with try_acquire_locks(locks, timeout):
            # if intermediate folders not not exist create them
            dst_folder = os.path.dirname(dst)
            if dst_folder and not os.path.exists(dst_folder):
                try:
                    os.makedirs(dst_folder)
                except OSError:
                    pass

            try:
                _copy_with_shell_fallback(src, dst_fn)
            except shutil.Error:
                log.debug("skipping %s - already exists in %s",
                            os.path.basename(src), dst)
Exemple #3
0
 def copy(src, dst, symlinks=False, ignore=None):
     """
     Copy a directory recursively.
     :param src: source directory
     :param dst: destination directory
     :param symlinks: follow symlinks ?
     :param ignore: ignore files list
     """
     if not os.path.exists(dst):
         os.makedirs(dst)
         shutil.copystat(src, dst)
     lst = os.listdir(src)
     if ignore:
         excl = ignore(src, lst)
         lst = [x for x in lst if x not in excl]
     for item in lst:
         s = os.path.join(src, item)
         d = os.path.join(dst, item)
         if symlinks and os.path.islink(s):
             if os.path.lexists(d):
                 os.remove(d)
             os.symlink(os.readlink(s), d)
             try:
                 st = os.lstat(s)
                 mode = stat.S_IMODE(st.st_mode)
                 os.lchmod(d, mode)
             except:
                 pass  # lchmod not available
         elif os.path.isdir(s):
             Directory.copy(s, d, symlinks, ignore)
         else:
             shutil.copy2(s, d)
Exemple #4
0
def _preserve_stats(entry_path: Path, stat: os.stat_result, xattrs: dict, mode):
    logger = logging.getLogger(__name__).getChild('_preserve_stats')
    new_mode = stat.st_mode & (os.st.S_IRWXU | os.st.S_IRWXG | os.st.S_IRWXO)
    new_uid = stat.st_uid
    new_gid = stat.st_gid
    new_utime = stat.st_atime_ns, stat.st_mtime_ns

    if mode is True or 'chmod' in mode:
        if hasattr(os, 'lchmod'):
            logger.debug(f'lchmod {new_mode} {entry_path}')
            os.lchmod(entry_path, new_mode)
        elif not entry_path.is_symlink():
            logger.debug(f'chmod {new_mode} {entry_path}')
            os.chmod(entry_path, new_mode)
        else:
            logger.warn(f'Current platform has no lchmod implemented: symlink {entry_path} will loose the perms')
    if mode is True or 'chown' in mode:
        logger.debug(f'lchown {new_uid}:{new_gid} {entry_path}')
        os.lchown(entry_path, new_uid, new_gid)
    if mode is True or 'utime' in mode:
        logger.debug(f'utime {new_utime} {entry_path}')
        os.utime(entry_path, ns=new_utime, follow_symlinks=False)
    if (mode is True or 'xattr' in mode) and xattrs is not None:
        for xattr_key, xattr_value in xattrs.items():
            logger.debug(f'xattr {xattr_key}:{xattr_value} {entry_path}')
            _setxattr(
                entry_path,
                xattr_key,
                xattr_value,
                symlink=False,
            )
    def copytree(self, src, dst, symlinks = False, ignore = None):
        """ 
        Copies the content for directory src into directory dst.
        The function overwrites existing files.
        """
        dst = str(dst)
        src = str(src)

        if not os.path.exists(dst):
            os.makedirs(dst)
            shutil.copystat(src, dst)
        lst = os.listdir(src)
        if ignore:
            excl = ignore(src, lst)
            lst = [x for x in lst if x not in excl]
        for item in lst:
            s = os.path.join(src, item)
            d = os.path.join(dst, item)
            if symlinks and os.path.islink(s):
                if os.path.lexists(d):
                    os.remove(d)
                os.symlink(os.readlink(s), d)
                try:
                    st = os.lstat(s)
                    mode = stat.S_IMODE(st.st_mode)
                    os.lchmod(d, mode)
                except:
                    pass # lchmod not available
            elif os.path.isdir(s):
                self.copytree(s, d, symlinks, ignore)
            else:
                shutil.copy2(s, d)
Exemple #6
0
def copytree(src, dst, symlinks=False, ignore=None, dry_run=False):
    if not os.path.exists(dst):
        os.makedirs(dst)
        shutil.copystat(src, dst)
    lst = os.listdir(src)
    if ignore:
        excl = ignore(src, lst)
        lst = [x for x in lst if x not in excl]

    # do not copy lock files
    if '.conda_lock' in lst:
        lst.remove('.conda_lock')

    dst_lst = [os.path.join(dst, item) for item in lst]

    if not dry_run:
        for idx, item in enumerate(lst):
            s = os.path.join(src, item)
            d = dst_lst[idx]
            if symlinks and os.path.islink(s):
                if os.path.lexists(d):
                    os.remove(d)
                os.symlink(os.readlink(s), d)
                try:
                    st = os.lstat(s)
                    mode = stat.S_IMODE(st.st_mode)
                    os.lchmod(d, mode)
                except:
                    pass  # lchmod not available
            elif os.path.isdir(s):
                copytree(s, d, symlinks, ignore)
            else:
                _copy_with_shell_fallback(s, d)

    return dst_lst
Exemple #7
0
def chmod(args):
    # os.chmod expects the mode to be in base 8 so we have to convert it here.
    try:
        os.lchmod(args[0], int(args[1], 8))
        return 0
    except:
        return 1
    def update(self, name, content, last_modified):
        # use '/' as name when updating the last_modified data for the dir
        if name == '/':
            name = ''

        filename = os.path.join(self.json_dir, name)
        dirname = os.path.dirname(filename)
        if not os.path.exists(dirname):
            os.makedirs(dirname)

        if content:
            log.debug('Writing new copy of %s to %s.' % (
                name, self.json_dir))
            tf = tempfile.NamedTemporaryFile(delete=False)
            tf.write(content.encode('utf8'))
            tf.close()

            # lchmod is available on BSD-based Unixes only.
            if hasattr(os, 'lchmod'):
                os.lchmod(tf.name, 0o644)
            else:
                os.chmod(tf.name, 0o644)

            shutil.move(tf.name, filename)
            lm_fn = self.last_modified_file_name(name)
        else:
            # in this case `name` should be either empty string or "regions/"
            lm_fn = os.path.join(filename, self.last_modified_dir_file_name)

        with open(lm_fn, 'w') as lm_fo:
            lm_fo.write(last_modified)
Exemple #9
0
 def test_copytree_symlinks(self):
     tmp_dir = self.mkdtemp()
     src_dir = os.path.join(tmp_dir, 'src')
     dst_dir = os.path.join(tmp_dir, 'dst')
     sub_dir = os.path.join(src_dir, 'sub')
     os.mkdir(src_dir)
     os.mkdir(sub_dir)
     write_file((src_dir, 'file.txt'), 'foo')
     src_link = os.path.join(sub_dir, 'link')
     dst_link = os.path.join(dst_dir, 'sub/link')
     os.symlink(os.path.join(src_dir, 'file.txt'),
                src_link)
     if hasattr(os, 'lchmod'):
         os.lchmod(src_link, stat.S_IRWXU | stat.S_IRWXO)
     if hasattr(os, 'lchflags') and hasattr(stat, 'UF_NODUMP'):
         os.lchflags(src_link, stat.UF_NODUMP)
     src_stat = os.lstat(src_link)
     shutil.copytree(src_dir, dst_dir, symlinks=True)
     self.assertTrue(os.path.islink(os.path.join(dst_dir, 'sub', 'link')))
     self.assertEqual(os.readlink(os.path.join(dst_dir, 'sub', 'link')),
                      os.path.join(src_dir, 'file.txt'))
     dst_stat = os.lstat(dst_link)
     if hasattr(os, 'lchmod'):
         self.assertEqual(dst_stat.st_mode, src_stat.st_mode)
     if hasattr(os, 'lchflags'):
         self.assertEqual(dst_stat.st_flags, src_stat.st_flags)
Exemple #10
0
def copytree(src, dst, symlinks = False, ignore = None):
    if not os.path.exists(dst):
        os.makedirs(dst)
        shutil.copystat(src, dst)
    
    lst = os.listdir(src)
    if ignore:
        excludes = ignore(src, lst)
        lst = [item for item in lst if item not in excludes]
    
    for item in lst:
        s = os.path.join(src, item)
        d = os.path.join(dst, item)
        if symlinks and os.path.islink(s):
            if os.path.lexists(d):
                os.remove(d)
            os.symlink(os.readlink(s), d)
            try:
                st = os.lstat(s)
                mode = stat.S_IMODE(st.st_mode)
                os.lchmod(d, mode)
            except:
                # lchmod not available
                pass
        elif os.path.isdir(s):
            copytree(s, d, symlinks, ignore)
        else:
            shutil.copy2(s, d)
def MergeTree(src, dst, symlinks = False):
	if not os.path.exists(dst):
		os.makedirs(dst)
		shutil.copystat(src, dst)
	lst = os.listdir(src)
	excl = IgnoreFiles(src, lst)
	lst = [x for x in lst if x not in excl]
	for item in lst:
		s = os.path.join(src, item)
		d = os.path.join(dst, item)
		if symlinks and os.path.islink(s):
			if os.path.lexists(d):
				os.remove(d)
			os.symlink(os.readlink(s), d)
			try:
				st = os.lstat(s)
				mode = stat.S_IMODE(st.st_mode)
				os.lchmod(d, mode)
			except:
				pass # lchmod not available
		elif os.path.isdir(s):
			MergeTree(s, d, symlinks)
		else:
			if os.path.exists(d):
				print "## Overwriting file %s with %s" % (d, s)
			shutil.copy2(s, d)
Exemple #12
0
 def test_copy2_symlinks(self):
     tmp_dir = self.mkdtemp()
     src = os.path.join(tmp_dir, 'foo')
     dst = os.path.join(tmp_dir, 'bar')
     src_link = os.path.join(tmp_dir, 'baz')
     write_file(src, 'foo')
     os.symlink(src, src_link)
     if hasattr(os, 'lchmod'):
         os.lchmod(src_link, stat.S_IRWXU | stat.S_IRWXO)
     if hasattr(os, 'lchflags') and hasattr(stat, 'UF_NODUMP'):
         os.lchflags(src_link, stat.UF_NODUMP)
     src_stat = os.stat(src)
     src_link_stat = os.lstat(src_link)
     # follow
     shutil.copy2(src_link, dst, symlinks=False)
     self.assertFalse(os.path.islink(dst))
     self.assertEqual(read_file(src), read_file(dst))
     os.remove(dst)
     # don't follow
     shutil.copy2(src_link, dst, symlinks=True)
     self.assertTrue(os.path.islink(dst))
     self.assertEqual(os.readlink(dst), os.readlink(src_link))
     dst_stat = os.lstat(dst)
     if hasattr(os, 'lutimes'):
         for attr in 'st_atime', 'st_mtime':
             # The modification times may be truncated in the new file.
             self.assertLessEqual(getattr(src_link_stat, attr),
                                  getattr(dst_stat, attr) + 1)
     if hasattr(os, 'lchmod'):
         self.assertEqual(src_link_stat.st_mode, dst_stat.st_mode)
         self.assertNotEqual(src_stat.st_mode, dst_stat.st_mode)
     if hasattr(os, 'lchflags') and hasattr(src_link_stat, 'st_flags'):
         self.assertEqual(src_link_stat.st_flags, dst_stat.st_flags)
Exemple #13
0
def copytree(_src,
             _dst,
             symlinks=False,
             ignore=None,
             copy_function=shutil.copy2):
    if not os.path.exists(_dst):
        os.makedirs(_dst)
        shutil.copystat(_src, _dst)
    lst = os.listdir(_src)
    if ignore:
        excl = ignore(_src, lst)
        lst = [x for x in lst if x not in excl]
    for item in lst:
        s = os.path.join(_src, item)
        d = os.path.join(_dst, item)
        if symlinks and os.path.islink(s):
            if os.path.lexists(d):
                os.remove(d)
            os.symlink(os.readlink(s), d)
            try:
                st = os.lstat(s)
                mode = stat.S_IMODE(st.st_mode)
                os.lchmod(d, mode)
            except:
                pass  # lchmod not available
        elif os.path.isdir(s):
            copytree(s, d, symlinks, ignore, copy_function=copy_function)
        else:
            copy_function(s, d)
Exemple #14
0
def copytree(src, dst, symlinks = False, ignore = None):
    """
    Custom version of copy, which actually merges.
    See: http://stackoverflow.com/a/22331852/1104116
    :param src:
    :param dst:
    :param symlinks:
    :param ignore:
    :return:
    """
    if not os.path.exists(dst):
        os.makedirs(dst)
        shutil.copystat(src, dst)
    lst = os.listdir(src)
    if ignore:
        excl = ignore(src, lst)
        lst = [x for x in lst if x not in excl]
    for item in lst:
        s = os.path.join(src, item)
        d = os.path.join(dst, item)
        if symlinks and os.path.islink(s):
            if os.path.lexists(d):
                os.remove(d)
            os.symlink(os.readlink(s), d)
            try:
                st = os.lstat(s)
                mode = stat.S_IMODE(st.st_mode)
                os.lchmod(d, mode)
            except:
                pass # lchmod not available
        elif os.path.isdir(s):
            copytree(s, d, symlinks, ignore)
        else:
            shutil.copy2(s, d)
Exemple #15
0
Fichier : sh.py Projet : mk-fg/fgc
def cp_meta(src, dst, attrs=False, dereference=True, skip_ts=None):
	'Copy mode or full attrs (atime, mtime and ownership) from src to dst'
	chown, st, utime_set = (os.chown, os.stat, os.utime)\
		if dereference else (os.lchown, os.lstat, os_ext.lutimes)
	st = st(src) if isinstance(src, types.StringTypes) else src
	mode = stat.S_IMODE(st.st_mode)
	src_acl = None
	if attrs and acl and isinstance(src, (file, bytes, int)): # just not a stat result
		src_acl = set(acl.get(src, effective=False))
		if not acl.is_mode(src_acl):
			src_acl_eff = set(acl.get(src, effective=True))
			if src_acl != src_acl_eff: # apply full acl, chmod, then apply effective acl
				acl.apply(src_acl, dst)
				src_acl = src_acl_eff
		else:
			acl.unset(dst)
			src_acl = None
	if dereference: os.chmod(dst, mode)
	else:
		try: os.lchmod(dst, mode)
		except AttributeError: # linux does not support symlink modes
			if not os.path.islink(dst): os.chmod(dst, mode)
	if attrs:
		if src_acl: acl.apply(src_acl, dst)
		chown(dst, st.st_uid, st.st_gid)
	if (attrs if skip_ts is None else not skip_ts):
		if dereference and islink(dst): dst = os.readlink(dst)
		utime_set(dst, (st.st_atime, st.st_mtime))
	return st
Exemple #16
0
def copytree(src, dst, symlinks=False, ignore=None, rename=None):
    """
    Copy a tree of files and dirs and merge into existing dir if needed
    Optionally rename items with names in the `rename` dict
    Source: http://stackoverflow.com/a/22331852
    """
    if not os.path.exists(dst):
        os.makedirs(dst)
        shutil.copystat(src, dst)
    lst = os.listdir(src)
    if ignore:
        excl = ignore(src, lst)
        lst = [x for x in lst if x not in excl]
    for item in lst:
        s = os.path.join(src, item)
        d = os.path.join(dst, item)
        if rename and item in rename:
            d = os.path.join(dst, rename[item])
        if symlinks and os.path.islink(s):
            if os.path.lexists(d):
                os.remove(d)
            os.symlink(os.readlink(s), d)
            try:
                st = os.lstat(s)
                mode = os.stat.S_IMODE(st.st_mode)
                os.lchmod(d, mode)
            except:
                pass # lchmod not available
        elif os.path.isdir(s):
            copytree(s, d, symlinks, ignore, rename)
        else:
            shutil.copy2(s, d)
Exemple #17
0
def set_read_only(path, read_only):
  """Sets or resets the write bit on a file or directory.

  Zaps out access to 'group' and 'others'.
  """
  assert isinstance(path, unicode), path
  assert isinstance(read_only, bool), read_only
  mode = os.lstat(path).st_mode
  # TODO(maruel): Stop removing GO bits.
  if read_only:
    mode = mode & 0500
  else:
    mode = mode | 0200
  if hasattr(os, 'lchmod'):
    os.lchmod(path, mode)  # pylint: disable=E1101
  else:
    if stat.S_ISLNK(mode):
      # Skip symlink without lchmod() support.
      logging.debug(
          'Can\'t change %sw bit on symlink %s',
          '-' if read_only else '+', path)
      return

    # TODO(maruel): Implement proper DACL modification on Windows.
    os.chmod(path, mode)
Exemple #18
0
def copytree(src, dst, follow_symlinks=False, ignore=None, overwrite=False):
    """Like shutil.copytree but merge directories."""
    if not os.path.exists(dst):
        os.makedirs(dst)
        shutil.copystat(src, dst)

    lst = os.listdir(src)

    if ignore:
        excl = ignore(src, lst)
        lst = [x for x in lst if x not in excl]

    for item in lst:
        s = os.path.join(src, item)
        d = os.path.join(dst, item)
        if os.path.islink(s) and follow_symlinks:
            if os.path.lexists(d):
                os.remove(d)
            os.symlink(os.readlink(s), d)
            try:
                st = os.lstat(s)
                mode = stat.S_IMODE(st.st_mode)
                os.lchmod(d, mode)
            except:  # noqa
                pass  # lchmod not available
        elif os.path.isdir(s):
            copytree(s, d, follow_symlinks, ignore, overwrite)
        elif os.path.isfile(s):
            copyfile(src, dst, overwrite)
Exemple #19
0
def copytree(src, dst, symlinks=False, ignore=None):
    Logger.__call__().get_logger().info("SRC=" + src)
    Logger.__call__().get_logger().info("DST=" + dst)
    if not os.path.exists(dst):
        os.makedirs(dst)
        shutil.copystat(src, dst)
    lst = os.listdir(src)
    if ignore:
        excl = ignore(src, lst)
        lst = [x for x in lst if x not in excl]
    for item in lst:
        s = os.path.join(src, item)
        d = os.path.join(dst, item)
        if symlinks and os.path.islink(s):
            if os.path.lexists(d):
                os.remove(d)
            os.symlink(os.readlink(s), d)
            try:
                st = os.lstat(s)
                mode = stat.S_IMODE(st.st_mode)
                os.lchmod(d, mode)
            except:
                pass  # lchmod not available
        elif os.path.isdir(s):
            copytree(s, d, symlinks, ignore)
        else:
            try:
                shutil.copy2(s, d)
            except IOError as e:
                Logger.__call__().get_logger().info("Unable to copy file. " +
                                                    e.filename)
Exemple #20
0
            def copytreex(srce, dst, symlinks=False, ignore=None):

                if not os.path.exists(dst):
                    os.makedirs(dst)
                    shutil.copystat(srce, dst)
                lst = os.listdir(srce)
                if ignore:
                    excl = ignore(srce, lst)
                    lst = [x for x in lst if x not in excl]
                for item in lst:

                    s = os.path.join(srce, item)
                    d = os.path.join(dst, item)
                    if symlinks and os.path.islink(s):
                        if os.path.lexists(d):
                            os.remove(d)
                        os.symlink(os.readlink(s), d)
                        try:
                            st = os.lstat(s)
                            mode = stat.S_IMODE(st.st_mode)
                            os.lchmod(d, mode)
                        except:
                            pass  # lchmod not available
                    elif os.path.isdir(s):
                        copytreex(s, d, symlinks, ignore)
                    else:
                        shutil.copy2(s, d)

                        print("Operation complete")

                copytreex(source, destination)
                self.finish_popup("Copy Operation")
Exemple #21
0
    def copytree(src, dst, symlinks = False, ignore = None):
        """ Recursively copy a directory tree from src to dst.

        Taken from http://stackoverflow.com/a/22331852/1120125.

        Needed because distutils.dir_util.copy_tree will only copy a given
        directory one time.  Which is annoying!

        """
        if not os.path.exists(dst):
            os.makedirs(dst)
            shutil.copystat(src, dst)
        lst = os.listdir(src)
        if ignore:
            excl = ignore(src, lst)
            lst = [x for x in lst if x not in excl]
        for item in lst:
            s = os.path.join(src, item)
            d = os.path.join(dst, item)
            if symlinks and os.path.islink(s):
                if os.path.lexists(d):
                    os.remove(d)
                os.symlink(os.readlink(s), d)
                try:
                    st = os.lstat(s)
                    mode = stat.S_IMODE(st.st_mode)
                    os.lchmod(d, mode)
                except:
                    pass # lchmod not available
            elif os.path.isdir(s):
                Utils.copytree(s, d, symlinks, ignore)
            else:
                shutil.copy2(s, d)
Exemple #22
0
def set_read_only(path, read_only):
  """Sets or resets the write bit on a file or directory.

  Zaps out access to 'group' and 'others'.
  """
  assert isinstance(path, unicode), path
  assert isinstance(read_only, bool), read_only
  mode = os.lstat(path).st_mode
  # TODO(maruel): Stop removing GO bits.
  if read_only:
    mode = mode & 0500
  else:
    mode = mode | 0200
  if hasattr(os, 'lchmod'):
    os.lchmod(path, mode)  # pylint: disable=E1101
  else:
    if stat.S_ISLNK(mode):
      # Skip symlink without lchmod() support.
      logging.debug(
          'Can\'t change %sw bit on symlink %s',
          '-' if read_only else '+', path)
      return

    # TODO(maruel): Implement proper DACL modification on Windows.
    os.chmod(path, mode)
 def copytree(cls, src, dst, symlinks=False, ignore=None):
     """
     Work around the fact that shututil.copytree will raise when a directory already exists...
     """
     if not os.path.exists(dst):
         os.makedirs(dst)
         shutil.copystat(src, dst)
     lst = os.listdir(src)
     if ignore:
         excl = ignore(src, lst)
         lst = [x for x in lst if x not in excl]
     for item in lst:
         s = os.path.join(src, item)
         d = os.path.join(dst, item)
         if symlinks and os.path.islink(s):
             if os.path.lexists(d):
                 os.remove(d)
             os.symlink(os.readlink(s), d)
             try:
                 st = os.lstat(s)
                 mode = stat.S_IMODE(st.st_mode)
                 os.lchmod(d, mode)
             except:
                 pass  # lchmod not available
         elif os.path.isdir(s):
             cls.copytree(s, d, symlinks, ignore)
         else:
             shutil.copy2(s, d)
Exemple #24
0
def LChmod(path, mode):
    error = None
    try:
        os.lchmod(path, mode)
    except OSError, error:
        print("Exception  changing mode of file " + path + " Error Code: " + str(error.errno) + " Error: " + error.message + error.strerror, file=sys.stderr)
        LG().Log('ERROR', "Exception  changing mode of file " + path + " Error Code: " + str(error.errno) + " Error: " + error.message + error.strerror)
Exemple #25
0
def mycopytree(src, dst, symlinks=False, ignore=None):
    if not os.path.exists(dst):
        os.makedirs(dst)
        shutil.copystat(src, dst)
    lst = os.listdir(src)

    if ignore:
        excl = ignore(src, lst)
        lst = [x for x in lst if x not in excl]
    for item in lst:
        s = os.path.join(src, item)
        d = os.path.join(dst, item)
        if symlinks and os.path.islink(s):
            if os.path.lexists(d):
                os.remove(d)
            os.symlink(os.readlink(s), d)
            try:
                st = os.lstat(s)
                mode = stat.S_IMODE(st.st_mode)
                os.lchmod(d, mode)
            except:
                pass  # lchmod not available
        elif os.path.isdir(s):
            mycopytree(s, d, symlinks, ignore)
        else:
            #avoid chmod issues with copy2
            #shutil.copy2(s, d)
            shutil.copyfile(s, d)
Exemple #26
0
def copytree(src, dst, symlinks=False, ignore=None):
    r''' Recursively copy directory tree, fixing shutil.copytree
         from https://stackoverflow.com/questions/1868714
    '''
    if not os.path.exists(dst):
        os.makedirs(dst)
        shutil.copystat(src, dst)
    lst = os.listdir(src)
    if ignore:
        excl = ignore(src, lst)
        lst = [x for x in lst if x not in excl]
    for item in lst:
        s = os.path.join(src, item)
        d = os.path.join(dst, item)
        if symlinks and os.path.islink(s):
            if os.path.lexists(d):
                os.remove(d)
            os.symlink(os.readlink(s), d)
            try:
                st = os.lstat(s)
                mode = stat.S_IMODE(st.st_mode)
                os.lchmod(d, mode)
            except OSError:
                pass  # lchmod not available
        elif os.path.isdir(s):
            copytree(s, d, symlinks, ignore)
        else:
            shutil.copy2(s, d)
Exemple #27
0
def copytree_with_merge(src, dst, symlinks=False, ignore=None):
    # type: (Text, Text, bool, Callable[..., Any]) -> None
    if not os.path.exists(dst):
        os.makedirs(dst)
        shutil.copystat(src, dst)
    lst = os.listdir(src)
    if ignore:
        excl = ignore(src, lst)
        lst = [x for x in lst if x not in excl]
    for item in lst:
        spath = os.path.join(src, item)
        dpath = os.path.join(dst, item)
        if symlinks and os.path.islink(spath):
            if os.path.lexists(dpath):
                os.remove(dpath)
            os.symlink(os.readlink(spath), dpath)
            try:
                s_stat = os.lstat(spath)
                mode = stat.S_IMODE(s_stat.st_mode)
                os.lchmod(dpath, mode)
            except:
                pass  # lchmod not available, only available on unix
        elif os.path.isdir(spath):
            copytree_with_merge(spath, dpath, symlinks, ignore)
        else:
            shutil.copy2(spath, dpath)
Exemple #28
0
def overwriteCopy(src, dest, symlinks=False, ignore=None):
    if not os.path.exists(dest):
        os.makedirs(dest)
        shutil.copystat(src, dest)
    sub_list = os.listdir(src)
    if ignore:
        excl = ignore(src, sub_list)
        sub_list = [x for x in sub_list if x not in excl]
    for i_sub in sub_list:
        s_path = os.path.join(src, i_sub)
        d_path = os.path.join(dest, i_sub)
        if symlinks and os.path.islink(s_path):
            if os.path.lexists(d_path):
                os.remove(d_path)
            os.symlink(os.readlink(s_path), d_path)
            try:
                s_path_s = os.lstat(s_path)
                s_path_mode = stat.S_IMODE(s_path_s.st_mode)
                os.lchmod(d_path, s_path_mode)
            except Exception:
                pass
        elif os.path.isdir(s_path):
            overwriteCopy(s_path, d_path, symlinks, ignore)
        else:
            shutil.copy2(s_path, d_path)
Exemple #29
0
def copytree(src, dst, symlinks=False, ignore=None):
    """like shutil.copytree() but ignores existing files
    https://stackoverflow.com/a/22331852/1239986
    """
    if not os.path.exists(dst):
        os.makedirs(dst)
        shutil.copystat(src, dst)
    lst = os.listdir(src)
    if ignore:
        excl = ignore(src, lst)
        lst = [x for x in lst if x not in excl]
    for item in lst:
        s = os.path.join(src, item)
        d = os.path.join(dst, item)
        if symlinks and os.path.islink(s):
            if os.path.lexists(d):
                os.remove(d)
            os.symlink(os.readlink(s), d)
            try:
                st = os.lstat(s)
                mode = stat.S_IMODE(st.st_mode)
                os.lchmod(d, mode)
            except OSError:
                pass  # lchmod not available
        elif os.path.isdir(s):
            copytree(s, d, symlinks, ignore)
        else:
            shutil.copy2(s, d)
Exemple #30
0
 def copytree(src, dst, symlinks=False, ignore=None):
     # ozxyqk: https://stackoverflow.com/questions/22588225/how-do-you-merge-two-directories-or-move-with-replace-from-the-windows-command
     if not os.path.exists(dst):
         mkdirs(dst)
         # os.makedirs(dst)
         shutil.copystat(src, dst)
     lst = os.listdir(src)
     if ignore:
         excl = ignore(src, lst)
         lst = [x for x in lst if x not in excl]
     for item in lst:
         s = os.path.join(src, item)
         d = os.path.join(dst, item)
         if symlinks and os.path.islink(s):
             if os.path.exists(d):
                 os.remove(d)
             os.symlink(os.readlink(s), d)
             try:
                 st = os.lstat(s)
                 mode = stat.S_IMODE(st.st_mode)
                 os.lchmod(d, mode)
             except:
                 pass  # lchmod not available
         elif os.path.isdir(s):
             copytree(s, d, symlinks, ignore)
         else:
             shutil.copy(s, d)
Exemple #31
0
def copytree(_src, _dst, symlinks=False, ignore=None, copy_function=shutil.copy2):
    if not os.path.exists(_dst):
        os.makedirs(_dst)
        shutil.copystat(_src, _dst)
    lst = os.listdir(_src)
    if ignore:
        excl = ignore(_src, lst)
        lst = [x for x in lst if x not in excl]
    for item in lst:
        s = os.path.join(_src, item)
        d = os.path.join(_dst, item)
        if symlinks and os.path.islink(s):
            if os.path.lexists(d):
                os.remove(d)
            os.symlink(os.readlink(s), d)
            try:
                st = os.lstat(s)
                mode = stat.S_IMODE(st.st_mode)
                os.lchmod(d, mode)
            except:
                pass  # lchmod not available
        elif os.path.isdir(s):
            copytree(s, d, symlinks, ignore, copy_function=copy_function)
        else:
            copy_function(s, d)
Exemple #32
0
def copytree(src, dst, symlinks=False, ignore=None):
    """Copies a tree and overwrites."""

    if not os.path.exists(dst):
        os.makedirs(dst)
        shutil.copystat(src, dst)
    lst = os.listdir(src)

    if ignore:
        excl = ignore(src, lst)
        lst = [x for x in lst if x not in excl]

    for item in lst:
        s = os.path.join(src, item)
        d = os.path.join(dst, item)

        if symlinks and os.path.islink(s):
            if os.path.lexists(d):
                os.remove(d)
            os.symlink(os.readlink(s), d)
            try:
                st = os.lstat(s)
                mode = stat.S_IMODE(st.st_mode)
                os.lchmod(d, mode)
            except Exception:
                pass  # lchmod not available
        elif os.path.isdir(s):
            copytree(s, d, symlinks, ignore)
        else:
            shutil.copy2(s, d)
Exemple #33
0
def chmod(target,mode):
    """Change mode of file or directory

    This a wrapper for the os.chmod function, with the
    addition that it doesn't follow symbolic links.

    For symbolic links it attempts to use the os.lchmod
    function instead, as this operates on the link
    itself and not the link target. If os.lchmod is not
    available then links are ignored.

    Arguments:
      target: file or directory to apply new mode to
      mode: a valid mode specifier e.g. 0775 or 0664

    """
    logging.debug("Changing mode of %s to %s" % (target,mode))
    try:
        if os.path.islink(target):
            # Try to use lchmod to operate on the link
            try:
                os.lchmod(target,mode)
            except AttributeError,ex:
                # lchmod is not available on all systems
                # If not then just ignore
                logging.debug("os.lchmod not available? Exception: %s" % ex)
        else:
Exemple #34
0
def copytree(src, dst, symlinks=False, ignore=None):
    """
    This is a contributed re-implementation of 'copytree' that
    should work with the exact same behavior on multiple platforms.

    """

    if not os.path.exists(dst):
        os.makedirs(dst)
        shutil.copystat(src, dst)
    lst = os.listdir(src)

    if ignore:
        excl = ignore(src, lst)
        lst = [x for x in lst if x not in excl]

    for item in lst:
        s = os.path.join(src, item)
        d = os.path.join(dst, item)

        if symlinks and os.path.islink(s):
            if os.path.lexists(d):
                os.remove(d)
            os.symlink(os.readlink(s), d)
            try:
                st = os.lstat(s)
                mode = stat.S_IMODE(st.st_mode)
                os.lchmod(d, mode)
            except:
                pass  # lchmod not available
        elif os.path.isdir(s):
            copytree(s, d, symlinks, ignore)
        else:
            shutil.copy2(s, d)
def LChmod(path, mode):
    error = None
    try:
        os.lchmod(path, mode)
    except OSError, error:
        print("Exception  changing mode of file " + path + " Error Code: " + str(error.errno) + " Error: " + error.message + error.strerror, file=sys.stderr)
        LG().Log('ERROR', "Exception  changing mode of file " + path + " Error Code: " + str(error.errno) + " Error: " + error.message + error.strerror)
Exemple #36
0
def LChmod(path, mode):
    error = None
    try:
        os.lchmod(path, mode)
    except OSError, error:
        Print("Exception  changing mode of file " + path  + " Error: " + str(error), file=sys.stderr)
        LG().Log('ERROR', "Exception changing mode of file " + path  + " Error: " + str(error))
Exemple #37
0
def copytree_with_merge(src, dst, symlinks=False, ignore=None):
    # type: (Text, Text, bool, Callable[..., Any]) -> None
    if not os.path.exists(dst):
        os.makedirs(dst)
        shutil.copystat(src, dst)
    lst = os.listdir(src)
    if ignore:
        excl = ignore(src, lst)
        lst = [x for x in lst if x not in excl]
    for item in lst:
        spath = os.path.join(src, item)
        dpath = os.path.join(dst, item)
        if symlinks and os.path.islink(spath):
            if os.path.lexists(dpath):
                os.remove(dpath)
            os.symlink(os.readlink(spath), dpath)
            try:
                s_stat = os.lstat(spath)
                mode = stat.S_IMODE(s_stat.st_mode)
                os.lchmod(dpath, mode)
            except:
                pass  # lchmod not available, only available on unix
        elif os.path.isdir(spath):
            copytree_with_merge(spath, dpath, symlinks, ignore)
        else:
            shutil.copy2(spath, dpath)
def MergeTree(src, dst, symlinks=False):
    if not os.path.exists(dst):
        os.makedirs(dst)
        shutil.copystat(src, dst)
    lst = os.listdir(src)
    excl = IgnoreFiles(src, lst)
    lst = [x for x in lst if x not in excl]
    for item in lst:
        s = os.path.join(src, item)
        d = os.path.join(dst, item)
        if symlinks and os.path.islink(s):
            if os.path.lexists(d):
                os.remove(d)
            os.symlink(os.readlink(s), d)
            try:
                st = os.lstat(s)
                mode = stat.S_IMODE(st.st_mode)
                os.lchmod(d, mode)
            except:
                pass  # lchmod not available
        elif os.path.isdir(s):
            MergeTree(s, d, symlinks)
        else:
            if os.path.exists(d):
                print "## Overwriting file %s with %s" % (d, s)
            shutil.copy2(s, d)
Exemple #39
0
def mycopytree(src, dst, symlinks = False, ignore = None):
	if not os.path.exists(dst):
		os.makedirs(dst)
		shutil.copystat(src, dst)
  	lst = os.listdir(src)

  	if ignore:
		excl = ignore(src, lst)
		lst = [x for x in lst if x not in excl]
	for item in lst:
		s = os.path.join(src, item)
		d = os.path.join(dst, item)
		if symlinks and os.path.islink(s):
			if os.path.lexists(d):
				os.remove(d)
			os.symlink(os.readlink(s), d)
			try:
				st = os.lstat(s)
				mode = stat.S_IMODE(st.st_mode)
				os.lchmod(d, mode)
			except:
				pass # lchmod not available
		elif os.path.isdir(s):
			mycopytree(s, d, symlinks, ignore)
		else:
			#avoid chmod issues with copy2
			#shutil.copy2(s, d)
			shutil.copyfile(s, d)
Exemple #40
0
def copytree(src, dst, symlinks=False, ignore=None):
    if not dst.exists():
        dst.mkdir()
        shutil.copystat(str(src), str(dst))
    lst = [l.name for l in src.iterdir()]
    if ignore:
        excl = ignore(str(src), lst)
        lst = [x for x in lst if x not in excl]
    for item in lst:
        s = src / item
        d = dst / item
        if symlinks and s.is_symlink():
            if os.path.lexists(d):
                d.unlink()
            os.symlink(os.readlink(s), d)
            try:
                st = os.lstat(s)
                mode = stat.S_IMODE(st.st_mode)
                os.lchmod(d, mode)
            except BaseException:
                pass  # lchmod not available
        elif s.is_dir():
            copytree(s, d, symlinks, ignore)
        else:
            shutil.copy2(s, d)
Exemple #41
0
    def changeLinkPermissions(self, path, permissions):
        # permissions uses an object in 4 types: owner, group, others, special
        # owner,group,others each has 3 types, read,write,executable, contained in an array
        # special uses setuid,setgid,sticky
        #
        # In order to turn values on or off, just use true or false values.
        #
        # Permissions can alternatively be a numeric mode to chmod too.

        keys = {
            'owner': {'read': 'S_IRUSR', 'write': 'S_IWUSR', 'executable': 'S_IXUSR'},
            'group': {'read': 'S_IRGRP', 'write': 'S_IWGRP', 'executable': 'S_IXGRP'},
            'others': {'read': 'S_IROTH', 'write': 'S_IWOTH', 'executable': 'S_IXOTH'},
            'special': {'setuid': 'S_ISUID', 'setgid': 'S_ISGID', 'sticky': 'S_ISVTX'}
        }

        try:
            if isinstance(permissions, int):
                os.lchmod(path, permissions)
            else:
                bitnum = os.lstat(path).st_mode
                for section in permissions:
                    for key in permissions[section]:
                        try:
                            if permissions[section][key] is True:
                                bitnum = bitnum | stat.__dict__[keys[section][key]]
                            elif permissions[section][key] is False:
                                bitnum = bitnum & ~stat.__dict__[keys[section][key]]
                        except KeyError:
                            pass
                os.lchmod(path, bitnum)
            return True
        except OSError as (t, e):
            qDebug("FileSystem.changeLinkPermissions - %s: '%s'" % (e, path))
            return False
Exemple #42
0
def copyTree(src, dst, ignore_exts = () ,symlinks = False, ignore = None):
    try:
        if not os.path.exists(dst):
            os.makedirs(dst)
            shutil.copystat(src, dst)
        lst = os.listdir(src)
        if ignore:
            excl = ignore(src, lst)
            lst = [x for x in lst if x not in excl]
        for item in lst:
            if os.path.splitext(item)[1] in ignore_exts:
                continue
            s = os.path.join(src, item)
            d = os.path.join(dst, item)
            if symlinks and os.path.islink(s):
                if os.path.lexists(d):
                    os.remove(d)
                os.symlink(os.readlink(s), d)
                try:
                    st = os.lstat(s)
                    mode = stat.S_IMODE(st.st_mode)
                    os.lchmod(d, mode)
                except:
                    pass # lchmod not available
            elif os.path.isdir(s):
                copyTree(s, d, ignore_exts, symlinks, ignore)
            else:
                shutil.copy2(s, d)
        return True
    except Exception as e:
        log.fdbg('copy directory from (%s) to (%s) failed: %s' % (src,dst, str(e)),'E')
    
    return False
Exemple #43
0
    def customCopytree(self, src, dst, symlinks=False, ignore=None):
        customClearScreen()
        print("Currently Copying - \n\n{}\n\nto\n\n{}".format(src, dst))
        print("\n\nfiles left to copy = {}".format(self.countCopy))
        self.countCopy += -1

        if not os.path.exists(dst):
            os.makedirs(dst)
        shutil.copystat(src, dst)
        lst = os.listdir(src)
        if ignore:
            excl = ignore(src, lst)
            lst = [x for x in lst if x not in excl]
        for item in lst:
            s = os.path.join(src, item)
            d = os.path.join(dst, item)
            if symlinks and os.path.islink(s):
                if os.path.lexists(d):
                    os.remove(d)
                os.symlink(os.readlink(s), d)
                try:
                    st = os.lstat(s)
                    mode = stat.S_IMODE(st.st_mode)
                    os.lchmod(d, mode)
                except:
                    pass  # lchmod not available
            elif os.path.isdir(s):
                self.customCopytree(s, d, symlinks, ignore)
            else:
                try:
                    shutil.copy2(s, d)
                except Exception as e:
                    self.exceptionList.append(str(e))
Exemple #44
0
def test_os_lchmod(os_chmod_fixture):
    path, mode, expected_exception = os_chmod_fixture
    if expected_exception:
        with pytest.raises(expected_exception):
            os.lchmod(path, mode)
    else:
        os.lchmod(path, mode)
Exemple #45
0
def copytree(src, dst, symlinks=False, ignore=None):
    """
    copytree that works even if folder already exists
    """
    # http://stackoverflow.com/questions/1868714/how-do-i-copy-an-entire-directory-of-files-into-an-existing-directory-using-pyth
    if not exists(dst):
        os.makedirs(dst)
        shutil.copystat(src, dst)
    lst = os.listdir(src)
    if ignore:
        excl = ignore(src, lst)
        lst = [x for x in lst if x not in excl]
    for item in lst:
        s = join(src, item)
        d = join(dst, item)
        if symlinks and islink(s):
            if lexists(d):
                os.remove(d)
            os.symlink(os.readlink(s), d)
            try:
                st = os.lstat(s)
                mode = stat.S_IMODE(st.st_mode)
                os.lchmod(d, mode)
            except:
                pass  # lchmod not available
        elif isdir(s):
            copytree(s, d, symlinks, ignore)
        else:
            shutil.copy2(s, d)
Exemple #46
0
    def install_post_receive_hook(self, **kw):
        r"""
        >>> remote = LocalGit(bare=True)
        >>> remote.preserve()
        >>> hook = remote.install_post_receive_hook()
        >>> cont = open(hook).read()
        >>> open(hook, 'a').write('set -e\n' + cont + 'echo done > ' + shell.quote(remote/'done'))
        >>> local = _nonempty_repo()
        >>> local.check_call(['remote', 'add', 'origin', remote])
        >>> local.check_call(['push', 'origin', 'master:foo'], cwd=local)
        >>> assert (remote/'done').exists
        """
        hookpath = self.dot_git / "hooks" / "post-receive"
        hook = open(hookpath, "w")

        buildbot_src = Path(__file__).folder.folder / "external" / "buildbot"
        script = buildbot_src / "master" / "contrib" / "git_buildbot.py"
        kw["repository"] = kw.get("repository", self)

        cmd = [script, "-v"]
        for k, v in kw.items():
            cmd += ["--" + k, v]

        hook.write(shell.list2cmdline(cmd) + " || exit 1\n")
        hook.close()
        os.lchmod(hookpath, 0777)
        return hookpath
Exemple #47
0
def copytree(src, dst, symlinks=False, ignore=None):
    """
    This is a contributed re-implementation of 'copytree' that
    should work with the exact same behavior on multiple platforms.

    """

    if not os.path.exists(dst):
        os.makedirs(dst)
        shutil.copystat(src, dst)
    lst = os.listdir(src)

    if ignore:
        excl = ignore(src, lst)
        lst = [x for x in lst if x not in excl]

    for item in lst:
        s = os.path.join(src, item)
        d = os.path.join(dst, item)

        if symlinks and os.path.islink(s): # pragma: no cover
            if os.path.lexists(d):
                os.remove(d)
            os.symlink(os.readlink(s), d)
            try:
                st = os.lstat(s)
                mode = stat.S_IMODE(st.st_mode)
                os.lchmod(d, mode)
            except:
                pass  # lchmod not available
        elif os.path.isdir(s):
            copytree(s, d, symlinks, ignore)
        else:
            shutil.copy2(s, d)
    def update(self, name, content, last_modified):
        # use '/' as name when updating the last_modified data for the dir
        if name == '/':
            name = ''

        filename = os.path.join(self.json_dir, name)
        dirname = os.path.dirname(filename)
        if not os.path.exists(dirname):
            os.makedirs(dirname)

        if content:
            log.debug('Writing new copy of %s to %s.' % (
                name, self.json_dir))
            tf = tempfile.NamedTemporaryFile(delete=False)
            tf.write(content.encode('utf8'))
            tf.close()

            # lchmod is available on BSD-based Unixes only.
            if hasattr(os, 'lchmod'):
                os.lchmod(tf.name, 0o644)
            else:
                os.chmod(tf.name, 0o644)

            shutil.move(tf.name, filename)
            lm_fn = self.last_modified_file_name(name)
        else:
            # in this case `name` should be either empty string or "regions/"
            lm_fn = os.path.join(filename, self.last_modified_dir_file_name)

        with open(lm_fn, 'w') as lm_fo:
            lm_fo.write(last_modified)
Exemple #49
0
def copytree(src, dst, symlinks=False, ignore=None):
    """Copies a tree and overwrites."""

    if not os.path.exists(dst):
        os.makedirs(dst)
        shutil.copystat(src, dst)
    lst = os.listdir(src)

    if ignore:
        excl = ignore(src, lst)
        lst = [x for x in lst if x not in excl]

    for item in lst:
        s = os.path.join(src, item)
        d = os.path.join(dst, item)

        if symlinks and os.path.islink(s):
            if os.path.lexists(d):
                os.remove(d)
            os.symlink(os.readlink(s), d)
            try:
                st = os.lstat(s)
                mode = stat.S_IMODE(st.st_mode)
                os.lchmod(d, mode)
            except Exception:
                pass  # lchmod not available
        elif os.path.isdir(s):
            copytree(s, d, symlinks, ignore)
        else:
            shutil.copy2(s, d)
    def bundle(self, app_path):
        self.check(app_path)

        contents = os.path.join(app_path, 'Contents', )
        macos = os.path.join(contents, 'MacOS')
        resources = os.path.join(contents, 'Resources')
        info_plist = os.path.join(contents, 'Info.plist')

        exe_path = os.path.join(macos, self.script_name)

        info = self._build_info()

        print 'Creating app directories...'
        os.makedirs(macos)
        os.mkdir(resources)
        print 'Copying executable...'
        shutil.copyfile(self.script_path, exe_path)
        print 'Setting executable attributes...'
        os.lchmod(exe_path, 0755)

        if self.icon is not None:
            print 'Copying icon...'
            icon_dst = os.path.join(resources, self.icon_name)
            shutil.copy(self.icon, icon_dst)

        print 'Creating Info.plist...'
        plistlib.writePlist(info, info_plist)
Exemple #51
0
 def restore_attrs(self, path, item, symlink=False, fd=None):
     xattrs = item.get(b'xattrs')
     if xattrs:
             for k, v in xattrs.items():
                 try:
                     xattr.setxattr(fd or path, k, v)
                 except OSError as e:
                     if e.errno != errno.ENOTSUP:
                         raise
     uid = gid = None
     if not self.numeric_owner:
         uid = user2uid(item[b'user'])
         gid = group2gid(item[b'group'])
     uid = uid or item[b'uid']
     gid = gid or item[b'gid']
     # This code is a bit of a mess due to os specific differences
     try:
         if fd:
             os.fchown(fd, uid, gid)
         else:
             os.lchown(path, uid, gid)
     except OSError:
         pass
     if fd:
         os.fchmod(fd, item[b'mode'])
     elif not symlink:
         os.chmod(path, item[b'mode'])
     elif has_lchmod:  # Not available on Linux
         os.lchmod(path, item[b'mode'])
     if fd and utime_supports_fd:  # Python >= 3.3
         os.utime(fd, None, ns=(item[b'mtime'], item[b'mtime']))
     elif utime_supports_fd:  # Python >= 3.3
         os.utime(path, None, ns=(item[b'mtime'], item[b'mtime']), follow_symlinks=False)
     elif not symlink:
         os.utime(path, (item[b'mtime'] / 10**9, item[b'mtime'] / 10**9))
Exemple #52
0
def chmod(target, mode):
    """Change mode of file or directory

    This a wrapper for the os.chmod function, with the
    addition that it doesn't follow symbolic links.

    For symbolic links it attempts to use the os.lchmod
    function instead, as this operates on the link
    itself and not the link target. If os.lchmod is not
    available then links are ignored.

    Arguments:
      target: file or directory to apply new mode to
      mode: a valid mode specifier e.g. 0775 or 0664

    """
    try:
        if os.path.islink(target):
            # Try to use lchmod to operate on the link
            try:
                os.lchmod(target, mode)
            except AttributeError as ex:
                # lchmod is not available on all systems
                # If not then just ignore
                logging.debug("os.lchmod not available? Exception: %s" % ex)
        else:
            # Use os.chmod for everything else
            os.chmod(target, mode)
    except OSError as ex:
        logging.warning("Failed to change permissions on %s to %s: %s" %
                        (target, mode, ex))
def overwriteCopy(src, dest, symlinks=False, ignore=None):
    if not os.path.exists(dest):
        os.makedirs(dest)
        shutil.copystat(src, dest)
    sub_list = os.listdir(src)
    if ignore:
        excl = ignore(src, sub_list)
        sub_list = [x for x in sub_list if x not in excl]
    for i_sub in sub_list:
        s_path = os.path.join(src, i_sub)
        d_path = os.path.join(dest, i_sub)
        if symlinks and os.path.islink(s_path):
            if os.path.lexists(d_path):
                os.remove(d_path)
            os.symlink(os.readlink(s_path), d_path)
            try:
                s_path_s = os.lstat(s_path)
                s_path_mode = stat.S_IMODE(s_path_s.st_mode)
                os.lchmod(d_path, s_path_mode)
            except Exception:
                pass
        elif os.path.isdir(s_path):
            overwriteCopy(s_path, d_path, symlinks, ignore)
        else:
            shutil.copy2(s_path, d_path)
Exemple #54
0
def copytree(src, dst, symlinks=False, ignore=None):
    """
    copytree that works even if folder already exists
    """
    # http://stackoverflow.com/questions/1868714/how-do-i-copy-an-entire-directory-of-files-into-an-existing-directory-using-pyth
    if not os.path.exists(dst):
        os.makedirs(dst)
        shutil.copystat(src, dst)
    lst = os.listdir(src)
    if ignore:
        excl = ignore(src, lst)
        lst = [x for x in lst if x not in excl]
    for item in lst:
        s = os.path.join(src, item)
        d = os.path.join(dst, item)
        if symlinks and os.path.islink(s):
            if os.path.lexists(d):
                os.remove(d)
            os.symlink(os.readlink(s), d)
            try:
                st = os.lstat(s)
                mode = stat.S_IMODE(st.st_mode)
                os.lchmod(d, mode)
            except:
                pass  # lchmod not available
        elif os.path.isdir(s):
            copytree(s, d, symlinks, ignore)
        else:
            shutil.copy2(s, d)
Exemple #55
0
def copytree(src: str, dst: str, symlinks: bool = True):
    """Custom copytree implementation that works with existing directories.

    This implementation has its roots in [1] and works around the problem that shutil.copytree
    before Python 3.8 does not work with existing directories. The alternatively recommended
    distutils.dir_util.copy_tree function supports this usecase but failed when when symlinks are
    enabled. Moreover, it is apparently not considered a public function [2].

    [1] https://stackoverflow.com/a/22331852
    [2] https://bugs.python.org/issue10948#msg337892
    """
    if not os.path.exists(dst):
        os.makedirs(dst)
        shutil.copystat(src, dst)
    lst = os.listdir(src)
    for item in lst:
        s = os.path.join(src, item)
        d = os.path.join(dst, item)
        if symlinks and os.path.islink(s):
            if os.path.lexists(d):
                os.remove(d)
            os.symlink(os.readlink(s), d)
            try:
                st = os.lstat(s)
                mode = stat.S_IMODE(st.st_mode)
                os.lchmod(d, mode)
            except:
                pass  # lchmod not available
        elif os.path.isdir(s):
            copytree(s, d, symlinks)
        else:
            # Work around the fact that copy2 fails when the destination is not writeable.
            if os.path.exists(d) and not os.access(d, os.W_OK):
                os.chmod(d, stat.S_IWRITE)
            shutil.copy2(s, d)
Exemple #56
0
	def _remove_readonly(fn, path, excinfo):
		# Handle read-only files and directories
		if fn is os.rmdir:
			os.chmod(path, stat.S_IWRITE)
			os.rmdir(path)
		elif fn is os.remove:
			os.lchmod(path, stat.S_IWRITE)
			os.remove(path)
Exemple #57
0
 def chmod(self, mode, follow_symlinks=True, **kwargs):
     if follow_symlinks:
         return os.chmod(self, mode, **kwargs)
     else:
         if PY33:
             os.chmod(self, mode, follow_symlinks=follow_symlinks, **kwargs)
         else:
             os.lchmod(self, mode, **kwargs)
Exemple #58
0
def lchmod(path, mode):
    """If path is not a symlink, do a regular chmod. If path is a symlink
    and os.lchmod is available, do os.lchmod. If path is a symlink and
    os.lchmod is not available, do nothing."""
    if os.path.islink(path):
        if hasattr(os, 'lchmod'):
            os.lchmod(path, mode)
    else:
        os.chmod(path, mode)
Exemple #59
0
Fichier : sh.py Projet : mk-fg/fgc
def chmod(path, bits, dereference=True, merge=False):
	if merge:
		bits = stat.S_IMODE(( os.stat
			if dereference else os.lstat )(path).st_mode) | bits
	if dereference: os.chmod(path, bits)
	else:
		try: os.lchmod(path, bits)
		except AttributeError: # linux does not support symlink modes
			if not os.path.islink(path): os.chmod(path, bits)