4 # USAGE: mass-link.py [--prepend-suffix SUFFIX] symbolic | hard SOURCEDIR DESTDIR FILES
6 # create hard or symbolic links to SOURCEDIR/FILES in DESTDIR
8 # If --prepend-suffix is specified, link to foo.bar will be called fooSUFFIX.bar.
9 # Shell wildcards expansion is performed on FILES.
16 optlist, args = getopt.getopt (sys.argv[1:], '', ['prepend-suffix='])
17 link_type, source_dir, dest_dir = args[0:3]
20 source_dir = os.path.normpath (source_dir)
21 dest_dir = os.path.normpath (dest_dir)
25 if x[0] == '--prepend-suffix':
26 prepended_suffix = x[1]
29 def insert_suffix (p):
32 l[-2] += prepended_suffix
34 return p + prepended_suffix
36 insert_suffix = lambda p: p
38 if link_type == 'symbolic':
40 elif link_type == 'hard':
43 sys.stderr.write(sys.argv[0] + ': ' + link_type + ": wrong argument, expected 'symbolic' or 'hard'\n")
48 sourcefiles += (glob.glob (os.path.join (source_dir, pattern)))
50 def relative_path (f):
53 return f[len (source_dir) + 1:]
55 destfiles = [os.path.join (dest_dir, insert_suffix (relative_path (f))) for f in sourcefiles]
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)]
60 def force_link (src,dest):
61 if os.path.exists (dest):
62 os.system ('rm -f ' + dest)
65 map (force_link, sourcefiles, destfiles)