]> git.donarmstrong.com Git - lilypond.git/blob - scripts/build/mass-link.py
Change all occurences of "echo -n" to "printf" for portability
[lilypond.git] / scripts / build / 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 optlist, args = getopt.getopt (sys.argv[1:], '', ['prepend-suffix='])
17 link_type, source_dir, dest_dir = args[0:3]
18 files = args[3:]
19
20 source_dir = os.path.normpath (source_dir)
21 dest_dir = os.path.normpath (dest_dir)
22
23 prepended_suffix = ''
24 for x in optlist:
25     if x[0] == '--prepend-suffix':
26         prepended_suffix = x[1]
27
28 if prepended_suffix:
29     def insert_suffix (p):
30         l = p.split ('.')
31         if len (l) >= 2:
32             l[-2] += prepended_suffix
33             return '.'.join (l)
34         return p + prepended_suffix
35 else:
36     insert_suffix = lambda p: p
37
38 if link_type == 'symbolic':
39     link = os.symlink
40 elif link_type == 'hard':
41     link = os.link
42 else:
43     sys.stderr.write(sys.argv[0] + ': ' + link_type + ": wrong argument, expected 'symbolic' or 'hard'\n")
44     sys.exit (1)
45
46 sourcefiles = []
47 for pattern in files:
48     sourcefiles += (glob.glob (os.path.join (source_dir, pattern)))
49
50 def relative_path (f):
51     if source_dir == '.':
52         return f
53     return f[len (source_dir) + 1:]
54
55 destfiles = [os.path.join (dest_dir, insert_suffix (relative_path (f))) for f in sourcefiles]
56
57 destdirs = set ([os.path.dirname (dest) for dest in destfiles])
58 [os.makedirs (d) for d in destdirs if not os.path.exists (d)]
59
60 def force_link (src,dest):
61     if os.path.exists (dest):
62         os.system ('rm -f ' + dest)
63     link (src, dest)
64     os.utime (dest, None)
65
66 map (force_link, sourcefiles, destfiles)