Skip to content

Commit 135c749

Browse files
committed
Merge pull request matplotlib#473 from efiring/jj_doc_make_py_v24_compat
pull request 440 plus a suggested comment
2 parents cfe40d3 + c04da27 commit 135c749

File tree

1 file changed

+102
-2
lines changed

1 file changed

+102
-2
lines changed

doc/make.py

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,106 @@
55
import shutil
66
import sys
77

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

51151
# Clean out PDF files from the _images directory
52152
for filename in glob.glob('build/html/_images/*.pdf'):

0 commit comments

Comments
 (0)