Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 2 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 3 | # Copyright OpenEmbedded Contributors |
| 4 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 5 | # SPDX-License-Identifier: GPL-2.0-only |
| 6 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | # 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 | |
| 12 | import sys |
| 13 | import os |
| 14 | import shutil |
| 15 | |
| 16 | def 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 40 | except shutil.Error as err: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 41 | errors.extend(err.args[0]) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 42 | except EnvironmentError as why: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 43 | errors.append((srcname, dstname, str(why))) |
| 44 | try: |
| 45 | shutil.copystat(src, dst) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 46 | except OSError as why: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 47 | errors.extend((src, dst, str(why))) |
| 48 | if errors: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 49 | raise shutil.Error(errors) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 50 | |
| 51 | try: |
| 52 | copytree(sys.argv[1], sys.argv[2]) |
| 53 | except shutil.Error: |
| 54 | pass |
| 55 | except OSError: |
| 56 | pass |