blob: 35eb211be313189c3ca47ddcec740ed1178b16ac [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001#!/usr/bin/env python3
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002#
3# Allow copying of $1 to $2 but if files in $1 disappear during the copy operation,
4# don't error.
5# Also don't error if $1 disappears.
6#
7
8import sys
9import os
10import shutil
11
12def copytree(src, dst, symlinks=False, ignore=None):
13 """Based on shutil.copytree"""
14 names = os.listdir(src)
15 try:
16 os.makedirs(dst)
17 except OSError:
18 # Already exists
19 pass
20 errors = []
21 for name in names:
22 srcname = os.path.join(src, name)
23 dstname = os.path.join(dst, name)
24 try:
25 d = dstname
26 if os.path.isdir(dstname):
27 d = os.path.join(dstname, os.path.basename(srcname))
28 if os.path.exists(d):
29 continue
30 try:
31 os.link(srcname, dstname)
32 except OSError:
33 shutil.copy2(srcname, dstname)
34 # catch the Error from the recursive copytree so that we can
35 # continue with other files
Patrick Williamsc0f7c042017-02-23 20:41:17 -060036 except shutil.Error as err:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050037 errors.extend(err.args[0])
Patrick Williamsc0f7c042017-02-23 20:41:17 -060038 except EnvironmentError as why:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039 errors.append((srcname, dstname, str(why)))
40 try:
41 shutil.copystat(src, dst)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060042 except OSError as why:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050043 errors.extend((src, dst, str(why)))
44 if errors:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060045 raise shutil.Error(errors)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050046
47try:
48 copytree(sys.argv[1], sys.argv[2])
49except shutil.Error:
50 pass
51except OSError:
52 pass