-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_home_links.py
More file actions
58 lines (48 loc) · 1.76 KB
/
create_home_links.py
File metadata and controls
58 lines (48 loc) · 1.76 KB
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
import os
import sys
import glob
import shutil
import stat
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
home = os.path.expanduser('~')
present_directory = os.getcwd()
myShellScripts = os.environ.get('MY_SHELL_SCRIPTS', present_directory)
# Remove all .sh files, mySetup.txt, and library dir from home
def safe_unlink(path):
try:
if os.path.islink(path) or os.path.isfile(path):
os.unlink(path)
elif os.path.isdir(path):
shutil.rmtree(path)
except Exception:
pass
for pattern in ['*.sh', 'mySetup.txt']:
for f in glob.glob(os.path.join(home, pattern)):
safe_unlink(f)
safe_unlink(os.path.join(home, 'library'))
# Remove backup files (*.*~) from myShellScripts
def remove_backups(directory):
for f in glob.glob(os.path.join(directory, '*.*~')):
os.remove(f)
if os.path.isdir(myShellScripts):
remove_backups(myShellScripts)
# Make all .sh files in myShellScripts executable
for f in glob.glob(os.path.join(myShellScripts, '*.sh')):
st = os.stat(f)
os.chmod(f, st.st_mode | stat.S_IEXEC)
# Create symlinks for all .sh files, mySetup.txt, and library dir in home
def symlink_force(target, link_name):
try:
if os.path.lexists(link_name):
os.unlink(link_name)
except Exception:
pass
os.symlink(target, link_name)
for f in glob.glob(os.path.join(myShellScripts, '*.sh')):
symlink_force(f, os.path.join(home, os.path.basename(f)))
mySetup = os.path.join(myShellScripts, 'mySetup.txt')
if os.path.exists(mySetup):
symlink_force(mySetup, os.path.join(home, 'mySetup.txt'))
library_dir = os.path.join(myShellScripts, 'library')
if os.path.exists(library_dir):
symlink_force(library_dir, os.path.join(home, 'library'))