blob: ab617c5d35a941d952a9615bd71e624ee0cd325d [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001#!/usr/bin/env python3
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002#
Brad Bishopc342db32019-05-15 21:57:59 -04003# SPDX-License-Identifier: GPL-2.0-only
4#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005# Allow copying of $1 to $2 but if files in $1 disappear during the copy operation,
6# don't error.
7# Also don't error if $1 disappears.
8#
9
10import sys
11import os
12import shutil
13
14def copytree(src, dst, symlinks=False, ignore=None):
15 """Based on shutil.copytree"""
16 names = os.listdir(src)
17 try:
18 os.makedirs(dst)
19 except OSError:
20 # Already exists
21 pass
22 errors = []
23 for name in names:
24 srcname = os.path.join(src, name)
25 dstname = os.path.join(dst, name)
26 try:
27 d = dstname
28 if os.path.isdir(dstname):
29 d = os.path.join(dstname, os.path.basename(srcname))
30 if os.path.exists(d):
31 continue
32 try:
33 os.link(srcname, dstname)
34 except OSError:
35 shutil.copy2(srcname, dstname)
36 # catch the Error from the recursive copytree so that we can
37 # continue with other files
Patrick Williamsc0f7c042017-02-23 20:41:17 -060038 except shutil.Error as err:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039 errors.extend(err.args[0])
Patrick Williamsc0f7c042017-02-23 20:41:17 -060040 except EnvironmentError as why:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050041 errors.append((srcname, dstname, str(why)))
42 try:
43 shutil.copystat(src, dst)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060044 except OSError as why:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050045 errors.extend((src, dst, str(why)))
46 if errors:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060047 raise shutil.Error(errors)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050048
49try:
50 copytree(sys.argv[1], sys.argv[2])
51except shutil.Error:
52 pass
53except OSError:
54 pass