New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'safe_backup()' to cement2.tools #118

Closed
derks opened this Issue Jun 22, 2012 · 0 comments

Comments

Projects
None yet
1 participant
@derks
Member

derks commented Jun 22, 2012

    def _backup(self, path):
        """
        Rename a file or directory safely without overwriting an existing
        backup of the same name.
        """
        count = -1
        new_path = None
        while True:
            if os.path.exists(path):
                if count == -1:
                    new_path = "%s.bak" % (path)
                else:
                    new_path = "%s.bak.%s" % (path, count)
                if os.path.exists(new_path):
                    count += 1
                    continue
                else:
                    if keep_original:
                        if os.path.isfile(path):
                            shutil.copy(path, new_path)
                        elif os.path.isdir(path):
                            shutil.copytree(path, new_path)
                    else:
                        shutil.move(path, new_path)
                    break
            else:
                break
        return new_path

@ghost ghost assigned derks Jun 22, 2012

derks pushed a commit that referenced this issue Jun 29, 2012

@derks derks closed this Jun 29, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment