Belle II Software light-2406-ragdoll
sym_link.py
1#!/usr/bin/env python
2
3
10
11import os
12import errno
13from SCons.Script import GetOption
14from SCons.Builder import Builder
15
16# define relpath for python < 2.6
17if not hasattr(os.path, 'relpath'):
18
19 def relpath(path, start=os.path.curdir):
20 """Return a relative version of a path"""
21
22 if not path:
23 raise ValueError('no path specified')
24
25 start_list = os.path.abspath(start).split(os.path.sep)
26 path_list = os.path.abspath(path).split(os.path.sep)
27
28 # Work out how much of the filepath is shared by start and path.
29 i = len(os.path.commonprefix([start_list, path_list]))
30
31 rel_list = [os.path.pardir] * (len(start_list) - i) + path_list[i:]
32 if not rel_list:
33 return os.path.curdir
34 return os.path.join(*rel_list)
35
36 os.path.relpath = relpath
37
38
39def create_symlink(target, source, env):
40 """Create a symbolic link from source to target"""
41
42 source = os.path.relpath(source, os.path.dirname(target))
43 try:
44 os.symlink(source, target)
45 except OSError as e:
46 # target might exist if source was moved!
47 if e.errno == errno.EEXIST:
48 os.remove(target)
49 os.symlink(source, target)
50 else:
51 raise e
52 return 0
53
54
55copy_file = Builder(action='cp -pf $SOURCE $TARGET')
56copy_file.action.cmdstr = '${COPYCOMSTR}'
57
58
59def generate(env):
60 if not GetOption('no-symlink'):
61 env['INSTALL'] = create_symlink
62 env['BUILDERS']['Copy'] = copy_file
63
64
65def exists(env):
66 return True