summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Mandereau <john.mandereau@gmail.com>2012-08-01 12:14:50 +0200
committerJohn Mandereau <john.mandereau@gmail.com>2012-08-15 16:22:08 +0200
commitc0a47b91cd930053074d42363047a77b889e05f7 (patch)
tree46559b49212aa41f4bf899efbaa59e3cde97ea7e
parent277bb30e28d026f359b6a4de36060ddcb9ecd93b (diff)
mass-link: handle filesystem crossing and support OS without link support
-rw-r--r--scripts/build/mass-link.py37
1 files changed, 31 insertions, 6 deletions
diff --git a/scripts/build/mass-link.py b/scripts/build/mass-link.py
index 33b8dac467..32c0c07048 100644
--- a/scripts/build/mass-link.py
+++ b/scripts/build/mass-link.py
@@ -5,13 +5,26 @@
#
# create hard or symbolic links to SOURCEDIR/FILES in DESTDIR
#
-# If --prepend-suffix is specified, link to foo.bar will be called fooSUFFIX.bar.
-# Shell wildcards expansion is performed on FILES.
+# If symbolic or hard links are not provided by the operating system,
+# copies will be made instead. However, if the operating system
+# support symbolic or hard links, then this program expects to
+# operate on a filesystem which supports them too.
+#
+# If --prepend-suffix is specified, link to foo.bar will be called
+# fooSUFFIX.bar. Shell wildcards expansion is performed on FILES.
+#
+# No check is performed on FILES type; in particular, if FILES
+# expansions contain a directory and hard links are requested,
+# this program may fail non-gracefully.
+#
+# Attempts to make hard links across different filesystems are
+# caught and replaced by copies.
import sys
import os
import glob
import getopt
+import shutil
optlist, args = getopt.getopt (sys.argv[1:], '', ['prepend-suffix='])
link_type, source_dir, dest_dir = args[0:3]
@@ -36,9 +49,15 @@ else:
insert_suffix = lambda p: p
if link_type == 'symbolic':
- link = os.symlink
+ if hasattr (os, 'symlink'):
+ link = os.symlink
+ else:
+ link = shutil.copy
elif link_type == 'hard':
- link = os.link
+ if hasattr (os, 'link'):
+ link = os.link
+ else:
+ link = shutil.copy
else:
sys.stderr.write(sys.argv[0] + ': ' + link_type + ": wrong argument, expected 'symbolic' or 'hard'\n")
sys.exit (1)
@@ -59,8 +78,14 @@ destdirs = set ([os.path.dirname (dest) for dest in destfiles])
def force_link (src,dest):
if os.path.exists (dest):
- os.system ('rm -f ' + dest)
- link (src, dest)
+ os.remove (dest)
+ try:
+ link (src, dest)
+ except OSError as e:
+ if e.errno == 18:
+ shutil.copy (src, dest)
+ else:
+ raise
os.utime (dest, None)
map (force_link, sourcefiles, destfiles)