xref: /openbmc/openbmc/poky/scripts/cp-noerror (revision eb8dc403)
1#!/usr/bin/env python3
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
8import sys
9import os
10import shutil
11
12def 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
36        except shutil.Error as err:
37            errors.extend(err.args[0])
38        except EnvironmentError as why:
39            errors.append((srcname, dstname, str(why)))
40    try:
41        shutil.copystat(src, dst)
42    except OSError as why:
43        errors.extend((src, dst, str(why)))
44    if errors:
45        raise shutil.Error(errors)
46
47try:
48    copytree(sys.argv[1], sys.argv[2])
49except shutil.Error:
50   pass
51except OSError:
52   pass
53