xref: /openbmc/openbmc/poky/scripts/cp-noerror (revision 92b42cb3)
1#!/usr/bin/env python3
2#
3# Copyright OpenEmbedded Contributors
4#
5# SPDX-License-Identifier: GPL-2.0-only
6#
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
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
40        except shutil.Error as err:
41            errors.extend(err.args[0])
42        except EnvironmentError as why:
43            errors.append((srcname, dstname, str(why)))
44    try:
45        shutil.copystat(src, dst)
46    except OSError as why:
47        errors.extend((src, dst, str(why)))
48    if errors:
49        raise shutil.Error(errors)
50
51try:
52    copytree(sys.argv[1], sys.argv[2])
53except shutil.Error:
54   pass
55except OSError:
56   pass
57