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