Skip to content

Instantly share code, notes, and snippets.

@feltnerm
Created April 19, 2011 05:48
Show Gist options
  • Save feltnerm/926877 to your computer and use it in GitHub Desktop.
Save feltnerm/926877 to your computer and use it in GitHub Desktop.
Backup dotfiles to github
#!/usr/bin/env python
"""
backup_dotfiles.py
Copies dotfiles over (.zshrc, .screenrc, etc.) to a backup dir
and syncs them with github.
"""
import argparse
from datetime import datetime
import os
import os.path
import shutil
import subprocess
import sys
def push_git():
cwd = os.path.join(os.environ['HOME'], 'lib/dotfiles')
timestamp = datetime.now().strftime('%m-%d-%y %H:%M.%S')
cmd = "cd %s && git add . && git commit -am '%s' && git push" % (cwd, timestamp)
print cmd
cmd = cmd.split(' ')
p = subprocess.Popen(cmd)
def copy(src, dst):
srcfd = file(src)
with open(dst, 'wb') as dstfd:
shutil.copyfileobj(srcfd, dstfd)
pass
def copy_r(src, dst):
dst = os.path.join(dst, os.path.basename(src).lstrip('.').rstrip('/'))
args = ['cp', '-R', src, dst]
p = subprocess.Popen(args)
def copy_dotfiles(file = None, dir = None):
if file:
copy(file, dir)
else:
dotfiles = ['.bash_profile', '.bashrc', '.emacs', '.gitconfig'
, '.gitignore', '.gvrimrc', '.pystartup', '.screenrc'
, '.vimrc', '.zprofile', '.zshenv', '.zshlogin', '.zshrc']
dotdirs = ['.vim', '.zsh']
others = []
#others = ['lib']
home = os.environ['HOME']
#dir = '/User/mark/lib/dotfiles
# Copy files
for f in os.listdir(home):
if f in dotfiles:
src = os.path.join(home, f)
dst = os.path.join(dir, f.lstrip('.'))
print 'copy(%s, %s)' % (src, dst)
copy(src, dst)
# Copy dirs
for d in os.listdir(home):
if d in dotdirs:
src = os.path.join(home, d)
dst = dir
print 'copy_r(%s, %s)' % (src, dst)
copy_r(src, dst)
# Copy other
for o in os.listdir(home):
if o in others:
p = os.path.join(home, o)
if os.path.isdir(p):
s = os.path.join(home, o)
d = os.path.join(dir, o.lstrip('.'))
print 'copy_r(%s, %s)' % (s, d)
copy_r(s, d)
elif os.path.isfile(p):
s = os.path.join(home, o)
d = os.path.join(dir, o.lstrip('.'))
print 'copy(%s, %s)' % (s, d)
copy(s, d)
else:
print o
def proc_args(argv):
if argv is None:
argv = sys.argv[1:]
p = argparse.ArgumentParser()
p.add_argument('-f', '--file', default = None
, help = 'Define a specific file to backup')
p.add_argument('-d', '--dir'
, default = os.path.join(os.environ['HOME'], 'lib/dotfiles')
, help = 'Define a directory to backup to.')
args = p.parse_args(argv)
return {"dir":args.dir, "file": args.file}
def main(argv = None):
settings = proc_args(argv)
copy_dotfiles(settings["file"], settings["dir"])
push_git()
if __name__ == "__main__":
status = main()
sys.exit(status)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment