-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.py
71 lines (60 loc) · 2.66 KB
/
setup.py
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
#!/usr/bin/env python3
import sys
from os import path, linesep
from subprocess import check_call
dotfilesPath = path.abspath(path.dirname(path.dirname(sys.argv[0]))) + '/'
def getSymlinkAndTarget(line):
result = []
for pathName in line.split('->'):
pathName = path.expandvars(path.expanduser(pathName.strip()))
if (path.isabs(pathName) is False and pathName is not ''):
pathName = dotfilesPath + pathName
result.append(pathName)
return (result[0], result[1])
def createSymlink(symlink, target, backupSymlinks):
if (path.islink(symlink) is True or path.exists(symlink) is True):
backupSymlink = '%s.bak' % symlink
backupSuffix = 1
while (True):
if (path.exists(backupSymlink) is True):
backupSymlink = '%s.bak%d' % (symlink, backupSuffix)
backupSuffix += 1
else:
break
cmdLine = 'mv %s %s' % (symlink, backupSymlink)
check_call(cmdLine, shell=True)
backupSymlinks.append((symlink, backupSymlink))
else:
cmdLine = 'mkdir -p %s' % path.dirname(symlink)
check_call(cmdLine, shell=True)
target = path.relpath(target, path.dirname(symlink))
cmdLine = 'ln -s %s %s' % (target, symlink)
check_call(cmdLine, shell=True)
def summary(skipedSymlinks, backupSymlinks):
print('\nAll Done!')
if (len(skipedSymlinks) is not 0):
print('Below symbolic links are not created (Skiped by you):')
print(linesep.join(skipedSymlinks))
if (len(backupSymlinks) is not 0):
print('\nBelow symbolic links are created and original files are backup:')
for (symlink, backupSymlink) in backupSymlinks:
print('%s backup to %s' % (symlink, backupSymlink))
choice = input('Would you like to delete the backup file? (y/n) [n]: ').strip()
if ((choice is not '') and (choice[0] is 'y' or choice[0] is 'Y')):
check_call('rm -rf %s' % backupSymlink, shell=True)
with open(dotfilesPath + 'symbolics') as f:
skipedSymlinks = []
backupSymlinks = []
for line in f.readlines():
if (line.strip() is ''):
continue
symlink, target = getSymlinkAndTarget(line)
# if target is None then read abs path from user
if (target is ''):
target = input('Please input the target that "%s" should be linked to,\n'
'leave blank to skip the creation of this symbolic link: ' % symlink).strip()
if (target is ''):
skipedSymlinks.append(symlink)
continue
createSymlink(symlink, target, backupSymlinks)
summary(skipedSymlinks, backupSymlinks)