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 | # |
| 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 | |
| 8 | import sys |
| 9 | import os |
| 10 | import shutil |
| 11 | |
| 12 | def 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 36 | except shutil.Error as err: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 37 | errors.extend(err.args[0]) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 38 | except EnvironmentError as why: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 39 | errors.append((srcname, dstname, str(why))) |
| 40 | try: |
| 41 | shutil.copystat(src, dst) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 42 | except OSError as why: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 43 | errors.extend((src, dst, str(why))) |
| 44 | if errors: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 45 | raise shutil.Error(errors) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 46 | |
| 47 | try: |
| 48 | copytree(sys.argv[1], sys.argv[2]) |
| 49 | except shutil.Error: |
| 50 | pass |
| 51 | except OSError: |
| 52 | pass |