]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/mass-link.py
Merge branch 'master' into topic/master-translation
[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-wildcard expansion is performed on FILES.
10
11 print "mass_link.py"
12
13 import sys
14 import os
15 import glob
16 import getopt
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 prepended_suffix = ''
23 for x in optlist:
24     if x[0] == '--prepend-suffix':
25         prepended_suffix = x[1]
26
27 if prepended_suffix:
28     def insert_suffix (p):
29         l = p.split ('.')
30         if len (l) >= 2:
31             l[-2] += prepended_suffix
32             return '.'.join (l)
33         return p + prepended_suffix
34 else:
35     insert_suffix = lambda p: p
36
37 if link_type == 'symbolic':
38     link = os.symlink
39 elif link_type == 'hard':
40     link = os.link
41 else:
42     sys.stderr.write(sys.argv[0] + ': ' + link_type + ": wrong argument, expected 'symbolic' or 'hard'\n")
43     sys.exit (1)
44
45 sourcefiles = []
46 for pattern in files:
47     sourcefiles += (glob.glob (os.path.join (source_dir, pattern)))
48
49 destfiles = map (lambda f: os.path.join (dest_dir, insert_suffix (os.path.basename (f))), sourcefiles)
50
51 def force_link (src,dest):
52     if os.path.exists (dest):
53         os.system ('rm -rf ' + dest)
54     link (src, dest)
55
56 map (force_link, sourcefiles, destfiles)