dotfiles / fixemail.py

#!/usr/bin/env python
"""Changes E-Mail address.

Example::

    $ ./fixemail.py foo@bar.baz
"""
import os, sys, re, shutil
from install import walk_dotfiles, ROOTDIR

EMAIL_RE = re.compile(r'[\w\d\-.]+@[\w\d\-.]+\.\w{2,3}')
EXCLUDE = [r'^dot\.ssh/', r'^dot\.emacs\.d/lisp/']

def main():
    if len(sys.argv) <= 1:
        print __doc__
        exit(1)

    newemail = sys.argv[1]
    if not EMAIL_RE.match(newemail):
        raise ValueError('wrong email address: %s' % newemail)

    for e in walk_dotfiles():
        if any(re.search(pat, e) for pat in EXCLUDE):
            continue

        fname = os.path.join(ROOTDIR, e)

        if not os.access(fname, os.W_OK):
            continue

        f = open(fname, 'rb')
        try:
            destf = open(fname + '.new', 'wb')
            try:
                shutil.copymode(f.name, destf.name)
                for line in f:
                    destf.write(EMAIL_RE.sub(newemail, line))

                destf.flush()
                os.rename(destf.name, f.name)
            except Exception, e:
                os.unlink(destf.name)
                raise e
            finally:
                destf.close()
        finally:
            f.close()


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.