~pythonregexp2.7/python/issue2636-11

« back to all changes in this revision

Viewing changes to Doc/library/shutil.rst

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-21 17:53:26 UTC
  • mfrom: (39025.1.14 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080921175326-92vaej2hc3yuecxb
Merged in changes from the core Regexp branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
   can't copy all file metadata.
23
23
   
24
24
   On POSIX platforms, this means that file owner and group are lost as well
25
 
   as ACLs.  On MacOS, the resource fork and other metadata are not used.
 
25
   as ACLs.  On Mac OS, the resource fork and other metadata are not used.
26
26
   This means that resources will be lost and file type and creator codes will
27
27
   not be correct. On Windows, file owners, ACLs and alternate data streams
28
28
   are not copied.
78
78
   Unix command :program:`cp -p`.
79
79
 
80
80
 
81
 
.. function:: copytree(src, dst[, symlinks])
 
81
.. function:: ignore_patterns(\*patterns)
 
82
 
 
83
   This factory function creates a function that can be used as a callable for
 
84
   :func:`copytree`\'s *ignore* argument, ignoring files and directories that
 
85
   match one of the glob-style *patterns* provided.  See the example below.
 
86
 
 
87
   .. versionadded:: 2.6
 
88
 
 
89
 
 
90
.. function:: copytree(src, dst[, symlinks=False[, ignore=None]])
82
91
 
83
92
   Recursively copy an entire directory tree rooted at *src*.  The destination
84
 
   directory, named by *dst*, must not already exist; it will be created as well as
85
 
   missing parent directories. Permissions and times of directories are copied with
86
 
   :func:`copystat`, individual files are copied using :func:`copy2`.   If
87
 
   *symlinks* is true, symbolic links in the source tree are represented as
88
 
   symbolic links in the new tree; if false or omitted, the contents of the linked
89
 
   files are copied to the new tree.  If exception(s) occur, an :exc:`Error` is
90
 
   raised with a list of reasons.
91
 
 
92
 
   The source code for this should be considered an example rather than a tool.
 
93
   directory, named by *dst*, must not already exist; it will be created as well
 
94
   as missing parent directories.  Permissions and times of directories are
 
95
   copied with :func:`copystat`, individual files are copied using
 
96
   :func:`copy2`.
 
97
 
 
98
   If *symlinks* is true, symbolic links in the source tree are represented as
 
99
   symbolic links in the new tree; if false or omitted, the contents of the
 
100
   linked files are copied to the new tree.
 
101
 
 
102
   If *ignore* is given, it must be a callable that will receive as its
 
103
   arguments the directory being visited by :func:`copytree`, and a list of its
 
104
   contents, as returned by :func:`os.listdir`.  Since :func:`copytree` is
 
105
   called recursively, the *ignore* callable will be called once for each
 
106
   directory that is copied.  The callable must return a sequence of directory
 
107
   and file names relative to the current directory (i.e. a subset of the items
 
108
   in its second argument); these names will then be ignored in the copy
 
109
   process.  :func:`ignore_patterns` can be used to create such a callable that
 
110
   ignores names based on glob-style patterns.
 
111
 
 
112
   If exception(s) occur, an :exc:`Error` is raised with a list of reasons.
 
113
 
 
114
   The source code for this should be considered an example rather than the
 
115
   ultimate tool.
93
116
 
94
117
   .. versionchanged:: 2.3
95
118
      :exc:`Error` is raised if any exceptions occur during copying, rather than
99
122
      Create intermediate directories needed to create *dst*, rather than raising an
100
123
      error. Copy permissions and times of directories using :func:`copystat`.
101
124
 
 
125
   .. versionchanged:: 2.6
 
126
      Added the *ignore* argument to be able to influence what is being copied. 
 
127
 
102
128
 
103
129
.. function:: rmtree(path[, ignore_errors[, onerror]])
104
130
 
152
178
above, with the docstring omitted.  It demonstrates many of the other functions
153
179
provided by this module. ::
154
180
 
155
 
   def copytree(src, dst, symlinks=False):
 
181
   def copytree(src, dst, symlinks=False, ignore=None):
156
182
       names = os.listdir(src)
 
183
       if ignore is not None:
 
184
           ignored_names = ignore(src, names)
 
185
       else:
 
186
           ignored_names = set()
 
187
 
157
188
       os.makedirs(dst)
158
189
       errors = []
159
190
       for name in names:
 
191
           if name in ignored_names:   
 
192
               continue
160
193
           srcname = os.path.join(src, name)
161
194
           dstname = os.path.join(dst, name)
162
195
           try:
164
197
                   linkto = os.readlink(srcname)
165
198
                   os.symlink(linkto, dstname)
166
199
               elif os.path.isdir(srcname):
167
 
                   copytree(srcname, dstname, symlinks)
 
200
                   copytree(srcname, dstname, symlinks, ignore)
168
201
               else:
169
202
                   copy2(srcname, dstname)
170
203
               # XXX What about devices, sockets etc.?
183
216
           errors.extend((src, dst, str(why)))
184
217
       if errors:
185
218
           raise Error, errors
 
219
 
 
220
Another example that uses the :func:`ignore_patterns` helper::
 
221
 
 
222
   from shutil import copytree, ignore_patterns
 
223
   
 
224
   copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))
 
225
 
 
226
This will copy everything except ``.pyc`` files and files or directories whose
 
227
name starts with ``tmp``.
 
228
 
 
229
Another example that uses the *ignore* argument to add a logging call::
 
230
 
 
231
   from shutil import copytree
 
232
   import logging
 
233
   
 
234
   def _logpath(path, names):
 
235
       logging.info('Working in %s' % path)
 
236
       return []   # nothing will be ignored
 
237
 
 
238
   copytree(source, destination, ignore=_logpath)
 
239