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