-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinkify
executable file
·85 lines (67 loc) · 2.17 KB
/
linkify
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python
from __future__ import with_statement
import errno
import optparse
import os
TO_IGNORE = [
'.',
'.git',
'.gitignore',
'linkify',
]
def make_private(files):
for filename in files:
if os.path.isdir(filename):
os.chmod(filename, 0700)
else:
os.chmod(filename, 0600)
def linkify(files):
for filename in files:
if filename not in TO_IGNORE:
target = os.path.join('..', '.' + filename)
source = os.path.join('.dotfiles', filename)
try:
os.unlink(target)
except OSError, e:
if e.errno == errno.ENOENT:
pass
elif e.errno == errno.EISDIR:
print("%s is a directory. Ignoring." % target)
else:
print errno.errorcode[e.errno]
finally:
os.symlink(source, target)
def add_hook():
with open('.git/hooks/post-commit', 'w') as hook:
hook.write('#!/bin/sh\ngit push\n')
os.chmod('.git/hooks/post-commit', 0755)
def parse_cli():
parser = optparse.OptionParser()
parser.add_option('-H',
'--install-hook',
help='Install git hook for automatic push.',
action='store_true',
default=False)
parser.add_option('-n',
'--no-linkify',
help='Disable creation of symlinks',
action='store_true',
default=False)
parser.add_option('-P',
'--no-fix-perms',
help='Disable fix of permission for private files.',
action='store_true',
default=False)
return parser.parse_args()
if __name__ == '__main__':
opts, files = parse_cli()
# Change to the directory where the dotfiles are
os.chdir(os.path.join(os.environ['HOME'], '.dotfiles'))
if not files:
files = os.listdir('.')
if not opts.no_linkify:
linkify(files)
if not opts.no_fix_perms:
make_private(files)
if opts.install_hook:
add_hook()