#!/usr/bin/env python3 import sys, os, socket, fnmatch SRC=os.path.expanduser("~/dotfiles/") DST=os.path.expanduser("~/") try: HOSTNAME=socket.gethostbyaddr(socket.gethostname())[0] except: HOSTNAME="unknown" def linkfile(prefix, srcfile, dstfile): srcpath = os.path.join(prefix, srcfile) dstpath = os.path.join(DST, dstfile) if os.path.exists(dstpath): if os.path.islink(dstpath): print(dstpath, "already exists as a link; removing it") os.unlink(dstpath) else: ok = input(dstpath + " already exists; remove it? (y/n)") if (ok.startswith("y")): print("removing", dstpath) os.unlink(dstpath) else: print("ignoring", dstpath) return print("linking in", srcpath, "as", dstpath) os.symlink(os.path.join(prefix, srcfile), os.path.join(DST, dstfile)) def linkasdotfile(prefix, name): linkfile(prefix, name, "."+name) def ignore(prefix, filename): pass def linkcontents(prefix, name): contents = os.listdir(os.path.join(prefix, name)) if not os.path.exists(os.path.join(DST, "."+name)): print("making directory", (os.path.join(DST, "."+name))) os.mkdir(os.path.join(DST, "."+name)) for x in contents: if x.startswith("."): continue # ignore .svn, etc if x.startswith("#") or x.endswith("~"): continue # ignore editor cruft linkasdotfile(prefix, os.path.join(name,x)) SPECIAL = {"local" : ignore, "install-dotfiles" : ignore, "ssh" : linkcontents, "subversion" : linkcontents, "elisp" : (lambda p,x : linkfile(p, os.path.join(x,"emacs.el"), ".emacs")) } LOCAL = {"csail" : "*.csail.mit.edu", "lcs" : "*.lcs.mit.edu", "midnight-anchovy" : "18.141.0.*", "clamshell" : "clamshell.ambulatoryclam.net", "none" : "unknown"} def traversedir(dirname): for x in os.listdir(dirname): if x.startswith("."): continue # ignore .svn, etc if x.startswith("#") or x.endswith("~"): continue # ignore editor cruft if x in SPECIAL: SPECIAL[x](dirname, x) else: linkasdotfile(dirname, x) traversedir(SRC) localdir = None try: localdir = file(os.path.expanduser("~/.dotfiles-local")).readline() print("Saved local dir:", localdir) except: pass if len(sys.argv) == 2: localdir = sys.argv[1] print("Using (and saving) specified local dir:", localdir) localdirfile = file(os.path.expanduser("~/.dotfiles-local"), "w") localdirfile.write(localdir) if not localdir: print("I think my hostname is", HOSTNAME) for d in os.listdir(os.path.join(SRC, "local")): if d.startswith("."): continue # ignore .svn, etc if d not in LOCAL: print("didn't find a LOCAL entry for", d) continue if fnmatch.fnmatch(HOSTNAME, LOCAL[d]): print("Guessing local dir as:", d) localdir = d break if localdir: print("Installing local files for", localdir) traversedir(os.path.join(SRC, "local", localdir))