|
5 | 5 | import shutil |
6 | 6 | import sys |
7 | 7 |
|
| 8 | +# ignore_patterns and copytree funtions are copies of what is included |
| 9 | +# in shutil.copytree of python v2.6 and later. |
| 10 | + |
| 11 | +try: |
| 12 | + WindowsError |
| 13 | +except NameError: |
| 14 | + WindowsError = None |
| 15 | + |
| 16 | +def ignore_patterns(*patterns): |
| 17 | + """Function that can be used as copytree() ignore parameter. |
| 18 | +
|
| 19 | + Patterns is a sequence of glob-style patterns |
| 20 | + that are used to exclude files""" |
| 21 | + import fnmatch |
| 22 | + def _ignore_patterns(path, names): |
| 23 | + ignored_names = [] |
| 24 | + for pattern in patterns: |
| 25 | + ignored_names.extend(fnmatch.filter(names, pattern)) |
| 26 | + return set(ignored_names) |
| 27 | + return _ignore_patterns |
| 28 | + |
| 29 | +def copytree(src, dst, symlinks=False, ignore=None): |
| 30 | + """Recursively copy a directory tree using copy2(). |
| 31 | +
|
| 32 | + The destination directory must not already exist. |
| 33 | + If exception(s) occur, an Error is raised with a list of reasons. |
| 34 | +
|
| 35 | + If the optional symlinks flag is true, symbolic links in the |
| 36 | + source tree result in symbolic links in the destination tree; if |
| 37 | + it is false, the contents of the files pointed to by symbolic |
| 38 | + links are copied. |
| 39 | +
|
| 40 | + The optional ignore argument is a callable. If given, it |
| 41 | + is called with the `src` parameter, which is the directory |
| 42 | + being visited by copytree(), and `names` which is the list of |
| 43 | + `src` contents, as returned by os.listdir(): |
| 44 | +
|
| 45 | + callable(src, names) -> ignored_names |
| 46 | +
|
| 47 | + Since copytree() is called recursively, the callable will be |
| 48 | + called once for each directory that is copied. It returns a |
| 49 | + list of names relative to the `src` directory that should |
| 50 | + not be copied. |
| 51 | +
|
| 52 | + XXX Consider this example code rather than the ultimate tool. |
| 53 | +
|
| 54 | + """ |
| 55 | + from shutil import copy2, Error, copystat |
| 56 | + names = os.listdir(src) |
| 57 | + if ignore is not None: |
| 58 | + ignored_names = ignore(src, names) |
| 59 | + else: |
| 60 | + ignored_names = set() |
| 61 | + |
| 62 | + os.makedirs(dst) |
| 63 | + errors = [] |
| 64 | + for name in names: |
| 65 | + if name in ignored_names: |
| 66 | + continue |
| 67 | + srcname = os.path.join(src, name) |
| 68 | + dstname = os.path.join(dst, name) |
| 69 | + try: |
| 70 | + if symlinks and os.path.islink(srcname): |
| 71 | + linkto = os.readlink(srcname) |
| 72 | + os.symlink(linkto, dstname) |
| 73 | + elif os.path.isdir(srcname): |
| 74 | + copytree(srcname, dstname, symlinks, ignore) |
| 75 | + else: |
| 76 | + # Will raise a SpecialFileError for unsupported file types |
| 77 | + copy2(srcname, dstname) |
| 78 | + # catch the Error from the recursive copytree so that we can |
| 79 | + # continue with other files |
| 80 | + except Error, err: |
| 81 | + errors.extend(err.args[0]) |
| 82 | + except EnvironmentError, why: |
| 83 | + errors.append((srcname, dstname, str(why))) |
| 84 | + try: |
| 85 | + copystat(src, dst) |
| 86 | + except OSError, why: |
| 87 | + if WindowsError is not None and isinstance(why, WindowsError): |
| 88 | + # Copying file access times may fail on Windows |
| 89 | + pass |
| 90 | + else: |
| 91 | + errors.extend((src, dst, str(why))) |
| 92 | + if errors: |
| 93 | + raise Error, errors |
| 94 | + |
| 95 | + |
8 | 96 | def copy_if_out_of_date(original, derived): |
9 | 97 | if (not os.path.exists(derived) or |
10 | 98 | os.stat(derived).st_mtime < os.stat(original).st_mtime): |
@@ -44,9 +132,9 @@ def html(): |
44 | 132 | figures_dest_path = 'build/html/pyplots' |
45 | 133 | if os.path.exists(figures_dest_path): |
46 | 134 | shutil.rmtree(figures_dest_path) |
47 | | - shutil.copytree( |
| 135 | + copytree( |
48 | 136 | 'pyplots', figures_dest_path, |
49 | | - ignore=shutil.ignore_patterns("*.pyc")) |
| 137 | + ignore=ignore_patterns("*.pyc")) |
50 | 138 |
|
51 | 139 | # Clean out PDF files from the _images directory |
52 | 140 | for filename in glob.glob('build/html/_images/*.pdf'): |
|
0 commit comments