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