Config files / copy_files.py

#!/usr/bin/env python3

import os.path
import shutil
import argparse


def getDestinationName(path):
    """Returns the name of the backuped file from the path to the original name.
    Examples:
    getDestinationName("/etc/rc.conf") -> "rc.conf"
    getDestinationName("~/.vimrc") -> "vimrc"
    """
    
    path = getAbsolutePath(path)

    name = ""
    # Is it a directory ?   
    if os.path.isdir(path):
        name = path.split("/")[-1]+"/"
    else:
        name = os.path.basename(path)

    # Hidden file
    if name[0] is ".":
        name = name[1:]

    return name    

def getAbsolutePath(path):
    """Expands a path relative to user home directory

    Example : getAbsolutePath("~/.vimrc") -> /home/antoine/.vimrc"""
    path = os.path.expanduser(path)
    path = os.path.abspath(path)
    return path


def backupFile(path):
    """Copies a file to the backup directory."""
    shutil.copy(getAbsolutePath(path), getDestinationName(path))


def revertFile(path):
    """Copies a file from the backup directory."""
    shutil.copy(getDestinationName(path), getAbsolutePath(path))



def main():
    fileList = []
    # Files to backup

    fileList.append("/etc/fstab")
    fileList.append("/home/antoine/.bashrc")
    fileList.append("/etc/mkinitcpio.conf")
    fileList.append("/home/antoine/.vimrc")
    fileList.append("/home/antoine/.hgrc") # Mercurial configuration file
    fileList.append("/etc/pacman.conf") # Pacman configuration file
    fileList.append("/etc/locale.gen")
    fileList.append("/etc/locale.conf")
    fileList.append("/etc/vconsole.conf")
    fileList.append("/etc/sysctl.conf")
    fileList.append("/boot/syslinux/syslinux.cfg") # Syslinux config file
    fileList.append("/boot/syslinux/splash.png") # Background image for syslinux
    fileList.append("/etc/out.icc") # Display calibration

    parser = argparse.ArgumentParser("Backups or reverts configuration files.")
    parser.add_argument("--revert", dest="action", action="store_const", const=1, default=0,
                        help="Reverts all files (default : backup)")

    args = parser.parse_args()
    if args.action == 0: # Backup files
        for f in fileList:
            print("Copying "+getDestinationName(f)+"...")
            backupFile(f)

    else:
        for f in fileList:
            print("Reverting "+getDestinationName(f)+"...")
            revertFile(f)

if __name__=="__main__":
    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.