Python shutil.copymode() Examples

The following are 30 code examples for showing how to use shutil.copymode(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module shutil , or try the search function .

Example 1
Project: flocker   Author: ClusterHQ   File: requirements.py    License: Apache License 2.0 6 votes vote down vote up
def requirements_from_infile(infile):
    outfile = infile.sibling(infile.basename()[:-len(".in")])
    with NamedTemporaryFile(
        prefix="{}.".format(outfile.basename()),
        suffix=".created-by-update-requirements-entrypoint",
        dir=os.path.dirname(outfile.parent().path),
        delete=False,
    ) as temporary_outfile:
        print "PROCESSING", infile
        check_call(
            ["docker", "run",
             "--rm",
             "--volume", "{}:/requirements.txt".format(infile.path),
             REQUIREMENTS_IMAGE],
            stdout=temporary_outfile
        )
        shutil.copymode(outfile.path, temporary_outfile.name)
        os.rename(temporary_outfile.name, outfile.path) 
Example 2
Project: paasta   Author: Yelp   File: fsm_cmd.py    License: Apache License 2.0 6 votes vote down vote up
def make_copyfile_symlink_aware():
    """The reasoning behind this monkeypatch is that cookiecutter doesn't
    respect symlinks at all, and at Yelp we use symlinks to reduce duplication
    in the soa configs. Maybe cookie-cutter will accept a symlink-aware PR?
    """
    orig_copyfile = shutil.copyfile
    orig_copymode = shutil.copymode

    def symlink_aware_copyfile(*args, **kwargs):
        kwargs.setdefault("follow_symlinks", False)
        orig_copyfile(*args, **kwargs)

    def symlink_aware_copymode(*args, **kwargs):
        kwargs.setdefault("follow_symlinks", False)
        orig_copymode(*args, **kwargs)

    shutil.copyfile = symlink_aware_copyfile
    shutil.copymode = symlink_aware_copymode
    try:
        yield
    finally:
        shutil.copyfile = orig_copyfile
        shutil.copymode = orig_copymode 
Example 3
Project: warriorframework   Author: warriorframework   File: file_Utils.py    License: Apache License 2.0 6 votes vote down vote up
def copymode(src, dst):
    """
    Copy the permission bits from src to dst. The file contents, owner, and
    group are unaffected. src and dst are path names given as strings.
    :Arguments:
        src - mode of the file to be copied
        dst - file on which mode has to be copied
    :Return:
        True/False - based on the success/failure of the operation
    """
    status = False
    try:
        shutil.copymode(src, dst)
        print_info("mode of src {} copied to dst {} successfully".
                   format(src, dst))
        status = True
    except Exception as e:
        print_error("copying file mode from {} to file {} raised exception {}".
                    format(src, dst, str(e)))
    return status 
Example 4
Project: toil   Author: DataBiosphere   File: absolute_imports.py    License: Apache License 2.0 6 votes vote down vote up
def main(root_path):
    for dir_path, dir_names, file_names in os.walk(root_path):
        for file_name in file_names:
            if file_name.endswith('.py') and file_name != 'setup.py':
                file_path = os.path.join(dir_path, file_name)
                with open(file_path) as file:
                    script = file.read()
                new_script = enable_absolute_imports(script, file_name)
                if new_script is not None:
                    temp_handle, temp_file_path = tempfile.mkstemp(prefix=file_name, dir=dir_path)
                    try:
                        with os.fdopen(temp_handle, 'w') as temp_file:
                            temp_file.write(new_script)
                    except:
                        os.unlink(temp_file_path)
                        raise
                    else:
                        shutil.copymode(file_path,temp_file_path)
                        os.rename(temp_file_path, file_path) 
Example 5
Project: dcos   Author: dcos   File: __init__.py    License: Apache License 2.0 6 votes vote down vote up
def stage_new_units(self, new_wants_dir):
        """Prepare new systemd units for activation.

        Unit files targeted by the symlinks in new_wants_dir are copied to a temporary location in the base systemd
        directory, and the symlinks are rewritten to target the intended final destination of the copied unit files.

        """
        for unit_name in self.unit_names(new_wants_dir):
            wants_symlink_path = os.path.join(new_wants_dir, unit_name)
            package_file_path = os.path.realpath(wants_symlink_path)
            systemd_file_path = os.path.join(self.__base_systemd, unit_name)
            tmp_systemd_file_path = systemd_file_path + self.new_unit_suffix

            # Copy the unit file to the systemd directory with a suffix added to the filename.
            # This file will be moved to systemd_file_path when the new package set is swapped in.
            shutil.copyfile(package_file_path, tmp_systemd_file_path)
            shutil.copymode(package_file_path, tmp_systemd_file_path)

            # Rewrite the symlink to point to the copied unit file's destination.
            # This symlink won't point to the correct file until the copied unit file is moved to its target location
            # during activate_new_unit_files().
            os.remove(wants_symlink_path)
            os.symlink(systemd_file_path, wants_symlink_path) 
Example 6
Project: conda-smithy   Author: conda-forge   File: feedstock_io.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def copy_file(src, dst):
    """
    Tried to copy utf-8 text files line-by-line to avoid
    getting CRLF characters added on Windows.

    If the file fails to be decoded with utf-8, we revert to a regular copy.
    """
    try:
        with io.open(src, "r", encoding="utf-8") as fh_src:
            with io.open(dst, "w", encoding="utf-8", newline="\n") as fh_dst:
                for line in fh_src:
                    fh_dst.write(line)
    except UnicodeDecodeError:
        # Leave any other files alone.
        shutil.copy(src, dst)

    shutil.copymode(src, dst)

    repo = get_repo(dst)
    if repo:
        repo.index.add([dst]) 
Example 7
Project: toonapilib   Author: costastf   File: patch.py    License: MIT License 5 votes vote down vote up
def write_hunks(self, srcname, tgtname, hunks):
    src = open(srcname, "rb")
    tgt = open(tgtname, "wb")

    debug("processing target file %s" % tgtname)

    tgt.writelines(self.patch_stream(src, hunks))

    tgt.close()
    src.close()
    # [ ] TODO: add test for permission copy
    shutil.copymode(srcname, tgtname)
    return True 
Example 8
Project: pi_video_looper   Author: adafruit   File: usb_drive_copymode.py    License: GNU General Public License v2.0 5 votes vote down vote up
def _load_config(self, config):
        self._mount_path = config.get('usb_drive', 'mount_path')
        self._readonly = config.getboolean('usb_drive', 'readonly')
        self._target_path = config.get('directory', 'path')
        self._copy_mode = config.get('copymode', 'mode')
        self._copyloader = config.getboolean('copymode', 'copyloader')
        self._password = config.get('copymode', 'password')

        #needs to be changed to a more generic approach to support other players
        self._extensions = '|'.join(config.get(self._config.get('video_looper', 'video_player'), 'extensions') \
                                 .translate(str.maketrans('','', ' \t\r\n.')) \
                                 .split(',')) 
Example 9
Project: pi_video_looper   Author: adafruit   File: usb_drive_copymode.py    License: GNU General Public License v2.0 5 votes vote down vote up
def copy_with_progress(self, src, dst, *, follow_symlinks=True):
        if os.path.isdir(dst):
            dst = os.path.join(dst, os.path.basename(src))

        # clear screen before copying
        self.clear_screen(False)

        self.copyfile(src, dst, follow_symlinks=follow_symlinks)
        # shutil.copymode(src, dst)
        return dst 
Example 10
Project: misp42splunk   Author: remg427   File: main.py    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
def write_file(self, new_text, filename, old_text, encoding):
        orig_filename = filename
        if self._output_dir:
            if filename.startswith(self._input_base_dir):
                filename = os.path.join(self._output_dir,
                                        filename[len(self._input_base_dir):])
            else:
                raise ValueError('filename %s does not start with the '
                                 'input_base_dir %s' % (
                                         filename, self._input_base_dir))
        if self._append_suffix:
            filename += self._append_suffix
        if orig_filename != filename:
            output_dir = os.path.dirname(filename)
            if not os.path.isdir(output_dir) and output_dir:
                os.makedirs(output_dir)
            self.log_message('Writing converted %s to %s.', orig_filename,
                             filename)
        if not self.nobackups:
            # Make backup
            backup = filename + ".bak"
            if os.path.lexists(backup):
                try:
                    os.remove(backup)
                except OSError as err:
                    self.log_message("Can't remove backup %s", backup)
            try:
                os.rename(filename, backup)
            except OSError as err:
                self.log_message("Can't rename %s to %s", filename, backup)
        # Actually write the new file
        write = super(StdoutRefactoringTool, self).write_file
        write(new_text, filename, old_text, encoding)
        if not self.nobackups:
            shutil.copymode(backup, filename)
        if orig_filename != filename:
            # Preserve the file mode in the new output directory.
            shutil.copymode(orig_filename, filename) 
Example 11
Project: py   Author: pytest-dev   File: local.py    License: MIT License 5 votes vote down vote up
def copy(self, target, mode=False, stat=False):
        """ copy path to target.

            If mode is True, will copy copy permission from path to target.
            If stat is True, copy permission, last modification
            time, last access time, and flags from path to target.
        """
        if self.check(file=1):
            if target.check(dir=1):
                target = target.join(self.basename)
            assert self!=target
            copychunked(self, target)
            if mode:
                copymode(self.strpath, target.strpath)
            if stat:
                copystat(self, target)
        else:
            def rec(p):
                return p.check(link=0)
            for x in self.visit(rec=rec):
                relpath = x.relto(self)
                newx = target.join(relpath)
                newx.dirpath().ensure(dir=1)
                if x.check(link=1):
                    newx.mksymlinkto(x.readlink())
                    continue
                elif x.check(file=1):
                    copychunked(x, newx)
                elif x.check(dir=1):
                    newx.ensure(dir=1)
                if mode:
                    copymode(x.strpath, newx.strpath)
                if stat:
                    copystat(x, newx) 
Example 12
Project: py   Author: pytest-dev   File: local.py    License: MIT License 5 votes vote down vote up
def copymode(src, dest):
    """ copy permission from src to dst. """
    import shutil
    shutil.copymode(src, dest) 
Example 13
Project: astroNN   Author: henrysky   File: patch_util.py    License: MIT License 5 votes vote down vote up
def write_hunks(self, srcname, tgtname, hunks):
        src = open(srcname, "rb")
        tgt = open(tgtname, "wb")

        debug(f"processing target file {tgtname}")

        tgt.writelines(self.patch_stream(src, hunks))

        tgt.close()
        src.close()
        shutil.copymode(srcname, tgtname)
        return True 
Example 14
Project: python-netsurv   Author: sofia-netsurv   File: local.py    License: MIT License 5 votes vote down vote up
def copy(self, target, mode=False, stat=False):
        """ copy path to target.

            If mode is True, will copy copy permission from path to target.
            If stat is True, copy permission, last modification
            time, last access time, and flags from path to target.
        """
        if self.check(file=1):
            if target.check(dir=1):
                target = target.join(self.basename)
            assert self!=target
            copychunked(self, target)
            if mode:
                copymode(self.strpath, target.strpath)
            if stat:
                copystat(self, target)
        else:
            def rec(p):
                return p.check(link=0)
            for x in self.visit(rec=rec):
                relpath = x.relto(self)
                newx = target.join(relpath)
                newx.dirpath().ensure(dir=1)
                if x.check(link=1):
                    newx.mksymlinkto(x.readlink())
                    continue
                elif x.check(file=1):
                    copychunked(x, newx)
                elif x.check(dir=1):
                    newx.ensure(dir=1)
                if mode:
                    copymode(x.strpath, newx.strpath)
                if stat:
                    copystat(x, newx) 
Example 15
Project: python-netsurv   Author: sofia-netsurv   File: local.py    License: MIT License 5 votes vote down vote up
def copymode(src, dest):
    """ copy permission from src to dst. """
    import shutil
    shutil.copymode(src, dest) 
Example 16
Project: python-netsurv   Author: sofia-netsurv   File: local.py    License: MIT License 5 votes vote down vote up
def copy(self, target, mode=False, stat=False):
        """ copy path to target.

            If mode is True, will copy copy permission from path to target.
            If stat is True, copy permission, last modification
            time, last access time, and flags from path to target.
        """
        if self.check(file=1):
            if target.check(dir=1):
                target = target.join(self.basename)
            assert self!=target
            copychunked(self, target)
            if mode:
                copymode(self.strpath, target.strpath)
            if stat:
                copystat(self, target)
        else:
            def rec(p):
                return p.check(link=0)
            for x in self.visit(rec=rec):
                relpath = x.relto(self)
                newx = target.join(relpath)
                newx.dirpath().ensure(dir=1)
                if x.check(link=1):
                    newx.mksymlinkto(x.readlink())
                    continue
                elif x.check(file=1):
                    copychunked(x, newx)
                elif x.check(dir=1):
                    newx.ensure(dir=1)
                if mode:
                    copymode(x.strpath, newx.strpath)
                if stat:
                    copystat(x, newx) 
Example 17
Project: python-netsurv   Author: sofia-netsurv   File: local.py    License: MIT License 5 votes vote down vote up
def copymode(src, dest):
    """ copy permission from src to dst. """
    import shutil
    shutil.copymode(src, dest) 
Example 18
Project: locationsharinglib   Author: costastf   File: patch.py    License: MIT License 5 votes vote down vote up
def write_hunks(self, srcname, tgtname, hunks):
    src = open(srcname, "rb")
    tgt = open(tgtname, "wb")

    debug("processing target file %s" % tgtname)

    tgt.writelines(self.patch_stream(src, hunks))

    tgt.close()
    src.close()
    # [ ] TODO: add test for permission copy
    shutil.copymode(srcname, tgtname)
    return True 
Example 19
Project: signac   Author: glotzerlab   File: syncutil.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _copy_p(self, src, dst):
        if not self.dry_run:
            shutil.copy(src, dst)
            shutil.copymode(src, dst) 
Example 20
Project: quiver   Author: ssorj   File: plano.py    License: Apache License 2.0 5 votes vote down vote up
def configure_file(input_file, output_file, **kwargs):
    notice("Configuring '{0}' for output '{1}'", input_file, output_file)

    content = read(input_file)

    for name, value in kwargs.items():
        content = content.replace("@{0}@".format(name), value)

    write(output_file, content)

    _shutil.copymode(input_file, output_file) 
Example 21
Project: pycopia   Author: kdart   File: PosixServer.py    License: Apache License 2.0 5 votes vote down vote up
def copymode(self, src, dst):
        return shutil.copymode(src, dst) 
Example 22
Project: pycopia   Author: kdart   File: WindowsServer.py    License: Apache License 2.0 5 votes vote down vote up
def copymode(self, src, dst):
        return shutil.copymode(src, dst) 
Example 23
Project: armi   Author: terrapower   File: __init__.py    License: Apache License 2.0 5 votes vote down vote up
def safeCopy(src, dst):
    """This copy overwrites ``shutil.copy`` and checks that copy operation is truly completed before continuing."""
    waitTime = 0.01  # 10 ms
    if os.path.isdir(dst):
        dst = os.path.join(dst, os.path.basename(src))
    srcSize = os.path.getsize(src)
    shutil.copyfile(src, dst)
    shutil.copymode(src, dst)
    while True:
        dstSize = os.path.getsize(dst)
        if srcSize == dstSize:
            break
        time.sleep(waitTime)
    runLog.extra("Copied {} -> {}".format(src, dst)) 
Example 24
Project: Fluid-Designer   Author: Microvellum   File: main.py    License: GNU General Public License v3.0 5 votes vote down vote up
def write_file(self, new_text, filename, old_text, encoding):
        orig_filename = filename
        if self._output_dir:
            if filename.startswith(self._input_base_dir):
                filename = os.path.join(self._output_dir,
                                        filename[len(self._input_base_dir):])
            else:
                raise ValueError('filename %s does not start with the '
                                 'input_base_dir %s' % (
                                         filename, self._input_base_dir))
        if self._append_suffix:
            filename += self._append_suffix
        if orig_filename != filename:
            output_dir = os.path.dirname(filename)
            if not os.path.isdir(output_dir):
                os.makedirs(output_dir)
            self.log_message('Writing converted %s to %s.', orig_filename,
                             filename)
        if not self.nobackups:
            # Make backup
            backup = filename + ".bak"
            if os.path.lexists(backup):
                try:
                    os.remove(backup)
                except OSError as err:
                    self.log_message("Can't remove backup %s", backup)
            try:
                os.rename(filename, backup)
            except OSError as err:
                self.log_message("Can't rename %s to %s", filename, backup)
        # Actually write the new file
        write = super(StdoutRefactoringTool, self).write_file
        write(new_text, filename, old_text, encoding)
        if not self.nobackups:
            shutil.copymode(backup, filename)
        if orig_filename != filename:
            # Preserve the file mode in the new output directory.
            shutil.copymode(orig_filename, filename) 
Example 25
Project: Fluid-Designer   Author: Microvellum   File: test_shutil.py    License: GNU General Public License v3.0 5 votes vote down vote up
def test_copymode_follow_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')
        dst_link = os.path.join(tmp_dir, 'quux')
        write_file(src, 'foo')
        write_file(dst, 'foo')
        os.symlink(src, src_link)
        os.symlink(dst, dst_link)
        os.chmod(src, stat.S_IRWXU|stat.S_IRWXG)
        # file to file
        os.chmod(dst, stat.S_IRWXO)
        self.assertNotEqual(os.stat(src).st_mode, os.stat(dst).st_mode)
        shutil.copymode(src, dst)
        self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode)
        # On Windows, os.chmod does not follow symlinks (issue #15411)
        if os.name != 'nt':
            # follow src link
            os.chmod(dst, stat.S_IRWXO)
            shutil.copymode(src_link, dst)
            self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode)
            # follow dst link
            os.chmod(dst, stat.S_IRWXO)
            shutil.copymode(src, dst_link)
            self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode)
            # follow both links
            os.chmod(dst, stat.S_IRWXO)
            shutil.copymode(src_link, dst_link)
            self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) 
Example 26
Project: Fluid-Designer   Author: Microvellum   File: test_shutil.py    License: GNU General Public License v3.0 5 votes vote down vote up
def test_copymode_symlink_to_symlink(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')
        dst_link = os.path.join(tmp_dir, 'quux')
        write_file(src, 'foo')
        write_file(dst, 'foo')
        os.symlink(src, src_link)
        os.symlink(dst, dst_link)
        os.chmod(src, stat.S_IRWXU|stat.S_IRWXG)
        os.chmod(dst, stat.S_IRWXU)
        os.lchmod(src_link, stat.S_IRWXO|stat.S_IRWXG)
        # link to link
        os.lchmod(dst_link, stat.S_IRWXO)
        shutil.copymode(src_link, dst_link, follow_symlinks=False)
        self.assertEqual(os.lstat(src_link).st_mode,
                         os.lstat(dst_link).st_mode)
        self.assertNotEqual(os.stat(src).st_mode, os.stat(dst).st_mode)
        # src link - use chmod
        os.lchmod(dst_link, stat.S_IRWXO)
        shutil.copymode(src_link, dst, follow_symlinks=False)
        self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode)
        # dst link - use chmod
        os.lchmod(dst_link, stat.S_IRWXO)
        shutil.copymode(src, dst_link, follow_symlinks=False)
        self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) 
Example 27
Project: Fluid-Designer   Author: Microvellum   File: test_shutil.py    License: GNU General Public License v3.0 5 votes vote down vote up
def test_copymode_symlink_to_symlink_wo_lchmod(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')
        dst_link = os.path.join(tmp_dir, 'quux')
        write_file(src, 'foo')
        write_file(dst, 'foo')
        os.symlink(src, src_link)
        os.symlink(dst, dst_link)
        shutil.copymode(src_link, dst_link, follow_symlinks=False)  # silent fail 
Example 28
Project: kf2-magicked-admin   Author: th3-z   File: patch.py    License: MIT License 5 votes vote down vote up
def write_hunks(self, srcname, tgtname, hunks):
    src = open(srcname, "rb")
    tgt = open(tgtname, "wb")

    debug("processing target file %s" % tgtname)

    tgt.writelines(self.patch_stream(src, hunks))

    tgt.close()
    src.close()
    # [ ] TODO: add test for permission copy
    shutil.copymode(srcname, tgtname)
    return True 
Example 29
Project: Imogen   Author: CedricGuillemet   File: main.py    License: MIT License 5 votes vote down vote up
def write_file(self, new_text, filename, old_text, encoding):
        orig_filename = filename
        if self._output_dir:
            if filename.startswith(self._input_base_dir):
                filename = os.path.join(self._output_dir,
                                        filename[len(self._input_base_dir):])
            else:
                raise ValueError('filename %s does not start with the '
                                 'input_base_dir %s' % (
                                         filename, self._input_base_dir))
        if self._append_suffix:
            filename += self._append_suffix
        if orig_filename != filename:
            output_dir = os.path.dirname(filename)
            if not os.path.isdir(output_dir) and output_dir:
                os.makedirs(output_dir)
            self.log_message('Writing converted %s to %s.', orig_filename,
                             filename)
        if not self.nobackups:
            # Make backup
            backup = filename + ".bak"
            if os.path.lexists(backup):
                try:
                    os.remove(backup)
                except OSError as err:
                    self.log_message("Can't remove backup %s", backup)
            try:
                os.rename(filename, backup)
            except OSError as err:
                self.log_message("Can't rename %s to %s", filename, backup)
        # Actually write the new file
        write = super(StdoutRefactoringTool, self).write_file
        write(new_text, filename, old_text, encoding)
        if not self.nobackups:
            shutil.copymode(backup, filename)
        if orig_filename != filename:
            # Preserve the file mode in the new output directory.
            shutil.copymode(orig_filename, filename) 
Example 30
Project: ironpython3   Author: IronLanguages   File: main.py    License: Apache License 2.0 5 votes vote down vote up
def write_file(self, new_text, filename, old_text, encoding):
        orig_filename = filename
        if self._output_dir:
            if filename.startswith(self._input_base_dir):
                filename = os.path.join(self._output_dir,
                                        filename[len(self._input_base_dir):])
            else:
                raise ValueError('filename %s does not start with the '
                                 'input_base_dir %s' % (
                                         filename, self._input_base_dir))
        if self._append_suffix:
            filename += self._append_suffix
        if orig_filename != filename:
            output_dir = os.path.dirname(filename)
            if not os.path.isdir(output_dir):
                os.makedirs(output_dir)
            self.log_message('Writing converted %s to %s.', orig_filename,
                             filename)
        if not self.nobackups:
            # Make backup
            backup = filename + ".bak"
            if os.path.lexists(backup):
                try:
                    os.remove(backup)
                except OSError as err:
                    self.log_message("Can't remove backup %s", backup)
            try:
                os.rename(filename, backup)
            except OSError as err:
                self.log_message("Can't rename %s to %s", filename, backup)
        # Actually write the new file
        write = super(StdoutRefactoringTool, self).write_file
        write(new_text, filename, old_text, encoding)
        if not self.nobackups:
            shutil.copymode(backup, filename)
        if orig_filename != filename:
            # Preserve the file mode in the new output directory.
            shutil.copymode(orig_filename, filename)