]> git.donarmstrong.com Git - lilypond.git/blob - scripts/build/mass-link.py
Add '-dcrop' option to ps and svg backends
[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 symbolic or hard links are not provided by the operating system,
9 # copies will be made instead.  However, if the operating system
10 # support symbolic or hard links, then this program expects to
11 # operate on a filesystem which supports them too.
12 #
13 # If --prepend-suffix is specified, link to foo.bar will be called
14 # fooSUFFIX.bar.  Shell wildcards expansion is performed on FILES.
15 #
16 # No check is performed on FILES type; in particular, if FILES
17 # expansions contain a directory and hard links are requested,
18 # this program may fail non-gracefully.
19 #
20 # Attempts to make hard links across different filesystems are
21 # caught and replaced by copies.
22
23 import sys
24 import os
25 import glob
26 import getopt
27 import shutil
28
29 optlist, args = getopt.getopt (sys.argv[1:], '', ['prepend-suffix='])
30 link_type, source_dir, dest_dir = args[0:3]
31 files = args[3:]
32
33 source_dir = os.path.normpath (source_dir)
34 dest_dir = os.path.normpath (dest_dir)
35
36 prepended_suffix = ''
37 for x in optlist:
38     if x[0] == '--prepend-suffix':
39         prepended_suffix = x[1]
40
41 if prepended_suffix:
42     def insert_suffix (p):
43         l = p.split ('.')
44         if len (l) >= 2:
45             l[-2] += prepended_suffix
46             return '.'.join (l)
47         return p + prepended_suffix
48 else:
49     insert_suffix = lambda p: p
50
51 if link_type == 'symbolic':
52     if hasattr (os, 'symlink'):
53         link = os.symlink
54     else:
55         link = shutil.copy
56 elif link_type == 'hard':
57     if hasattr (os, 'link'):
58         link = os.link
59     else:
60         link = shutil.copy
61 else:
62     sys.stderr.write(sys.argv[0] + ': ' + link_type + ": wrong argument, expected 'symbolic' or 'hard'\n")
63     sys.exit (1)
64
65 sourcefiles = []
66 for pattern in files:
67     sourcefiles += (glob.glob (os.path.join (source_dir, pattern)))
68
69 def relative_path (f):
70     if source_dir == '.':
71         return f
72     return f[len (source_dir) + 1:]
73
74 destfiles = [os.path.join (dest_dir, insert_suffix (relative_path (f))) for f in sourcefiles]
75
76 destdirs = set ([os.path.dirname (dest) for dest in destfiles])
77 [os.makedirs (d) for d in destdirs if not os.path.exists (d)]
78
79 def force_link (src,dest):
80     if os.path.exists (dest):
81         os.remove (dest)
82     try:
83         link (src, dest)
84     except OSError, e: # can't use "as" because GUB has python 2.4.5.
85         if e.errno == 18:
86             shutil.copy (src, dest)
87         else:
88             raise
89     os.utime (dest, None)
90
91 map (force_link, sourcefiles, destfiles)