]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/mass-link.py
17412e5559f6aa7b34a0a17c5e1561703aaa3bff
[lilypond.git] / buildscripts / mass-link.py
1 #!@PYTHON@
2 # mass-link.py
3
4 # USAGE:  mass-link.py  [--prepend-suffix SUFFIX]   symbolic | hard   SOURCEDIR DESTDIR FILES
5 #
6 # create hard or symbolic links to SOURCEDIR/FILES in DESTDIR
7 #
8 # If --prepend-suffix is specified, link to foo.bar will be called fooSUFFIX.bar.
9 # Shell wildcards expansion is performed on FILES.
10
11 import sys
12 import os
13 import glob
14 import getopt
15
16 print "mass-link.py"
17
18 optlist, args = getopt.getopt (sys.argv[1:], '', ['prepend-suffix='])
19 link_type, source_dir, dest_dir = args[0:3]
20 files = args[3:]
21
22 source_dir = os.path.normpath (source_dir)
23 dest_dir = os.path.normpath (dest_dir)
24
25 prepended_suffix = ''
26 for x in optlist:
27     if x[0] == '--prepend-suffix':
28         prepended_suffix = x[1]
29
30 if prepended_suffix:
31     def insert_suffix (p):
32         l = p.split ('.')
33         if len (l) >= 2:
34             l[-2] += prepended_suffix
35             return '.'.join (l)
36         return p + prepended_suffix
37 else:
38     insert_suffix = lambda p: p
39
40 if link_type == 'symbolic':
41     link = os.symlink
42 elif link_type == 'hard':
43     link = os.link
44 else:
45     sys.stderr.write(sys.argv[0] + ': ' + link_type + ": wrong argument, expected 'symbolic' or 'hard'\n")
46     sys.exit (1)
47
48 sourcefiles = []
49 for pattern in files:
50     sourcefiles += (glob.glob (os.path.join (source_dir, pattern)))
51
52 def relative_path (f):
53     if source_dir == '.':
54         return f
55     return f[len (source_dir) + 1:]
56
57 destfiles = [os.path.join (dest_dir, insert_suffix (relative_path (f))) for f in sourcefiles]
58
59 destdirs = set ([os.path.dirname (dest) for dest in destfiles])
60 [os.makedirs (d) for d in destdirs if not os.path.exists (d)]
61
62 def force_link (src,dest):
63     if os.path.exists (dest):
64         os.system ('rm -f ' + dest)
65     link (src, dest)
66
67 map (force_link, sourcefiles, destfiles)