]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/mass-link.py
947cd8ccef5a47990a4796a4d6b0c62d1be3619c
[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 = map (lambda f: os.path.join (dest_dir, insert_suffix (relative_path (f))), sourcefiles)
58
59 def force_link (src,dest):
60     if os.path.exists (dest):
61         os.system ('rm -f ' + dest)
62     link (src, dest)
63
64 map (force_link, sourcefiles, destfiles)