-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsymlinks_windows_ext.py
166 lines (147 loc) · 5.83 KB
/
symlinks_windows_ext.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""
symlinks.py
Creates a set of symbolic links in the default sketchbook directory for
all Arduino libraries in this repository. The purpose is to make it easy to
install a set of Arduino libraries.
symlinks.py usage:
python symlinks.py --install # Creates or refreshes links to the libraries
python symlinks.py --remove # Removes links to the libraries
Authors:
Will Dickson will@iorodeo.com
Peter Polidoro polidorop@janelia.hhmi.org
Modified by:
Jonathan Grizou jonathan.grizou@glasgow.ac.uk
Graham Keenan 1105045k@student.gla.ac.uk
"""
import sys
import os
import argparse
import platform
USERDIR = os.path.expanduser('~')
if platform.system() in ['Linux', 'Darwin']:
LIBDIR = os.path.join(USERDIR, 'sketchbook', 'libraries')
elif platform.system() == 'Windows':
LIBDIR = os.path.join(USERDIR, 'Documents', 'Arduino', 'libraries')
def symlink_ms(source, link_name):
""" Symlinks for windows """
import ctypes
csl = ctypes.windll.kernel32.CreateSymbolicLinkW
csl.argtypes = (ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_uint32)
csl.restype = ctypes.c_ubyte
flags = 1 if os.path.isdir(source) else 0
try:
if csl(link_name, source.replace('/', '\\'), flags) == 0:
raise ctypes.WinError()
except WindowsError as e:
print(f"Symbolic link creation failed. The error is: {e}")
except:
pass
def create_symlinks_windows():
if not os.path.isdir(LIBDIR):
print('Libraries directory does not exist - creating...')
os.makedirs(LIBDIR)
src_paths, dst_paths = get_paths()
for src, dst in zip(src_paths, dst_paths):
if os.path.exists(dst):
if not isLink(dst):
print('{0} exists and in not a symbolic link - not overwriting'.format(dst))
continue
else:
print('Unlinking {0}'.format(dst))
os.unlink(dst)
print('Creating new symbolic link {0}'.format(dst))
symlink_ms(src, dst)
def create_symlinks_unix():
# Create library directory if it doesn't exist
if not os.path.isdir(LIBDIR):
print('libraries directory does not exist - creating')
os.makedirs(LIBDIR)
# Create symbolic links
src_paths, dst_paths = get_paths()
for src, dst in zip(src_paths, dst_paths):
if os.path.exists(dst):
if not os.path.islink(dst):
print('{0} exists and in not a symbolic link - not overwriting'.format(dst))
continue
else:
print('unlinking {0}'.format(dst))
os.unlink(dst)
# Create symbolic link
print('creating new symbolic link {0}'.format(dst))
os.symlink(src, dst)
def remove_symlinks():
if not os.path.isdir(LIBDIR):
return
# Remove symbolic links
src_paths, dst_paths = get_paths()
for dst in dst_paths:
if os.path.islink(dst):
print('removing symbolic link {0}'.format(dst))
os.unlink(dst)
def get_paths():
"""
Get source and destination paths for symbolic links
"""
curdir = os.path.abspath(os.path.curdir)
dir_list = os.listdir(curdir)
src_paths = []
dst_paths = []
for item in dir_list:
if os.path.isdir(item):
if (item == '.hg') or (item == '.git'):
continue
src = os.path.join(curdir, item)
dst = os.path.join(LIBDIR, item)
src_paths.append(src)
dst_paths.append(dst)
return src_paths, dst_paths
# For Windows only
def isLink(path):
if os.path.exists(path):
if os.path.isdir(path):
FILE_ATTRIBUTE_REPARSE_POINT = 0x4000
if sys.version_info[0] < 3:
attributes = ctypes.windll.kernel32.GetFileAttributesW(unicode(path))
else:
attributes = ctypes.windll.kernel32.GetFileAttributesW(path)
return (attributes & FILE_ATTRIBUTE_REPARSE_POINT) > 0
else:
command = ['dir', path]
try:
with open(os.devnull, 'w') as NULL_FILE:
o0 = check_output(command, stderr=NULL_FILE, shell=True)
except CalledProcessError as e:
print(e.check_output)
return False
o1 = [s.strip() for s in o0.split('\n')]
if len(o1) < 6:
return False
else:
return 'SYMLINK' in o1[5]
else:
return False
# -----------------------------------------------------------------------------
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Arduino Libraries Symlinks')
parser.add_argument('-i', '--install',
help='Install all of the Arduino libraries in this repository to the default sketchbook directory as a set of symbolic links.',
action='store_true')
parser.add_argument('-r', '--remove',
help='Remove all of the Arduino library symbolic links from the default sketchbook directory.',
action='store_true')
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
if args.install:
if platform.system() in ['Linux', 'Darwin']:
create_symlinks_unix()
elif platform.system() == 'Windows':
import ctypes
if not ctypes.windll.shell32.IsUserAnAdmin():
# Re-run with admin privileges
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__+" -i", None, 1)
else:
create_symlinks_windows()
elif args.remove:
remove_symlinks()