Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
shutil.py
Go to the documentation of this file.
1 """Utility functions for copying files and directory trees.
2 
3 XXX The functions here don't copy the resource fork or other metadata on Mac.
4 
5 """
6 
7 import os
8 import sys
9 import stat
10 
11 __all__ = ["copyfileobj","copyfile","copymode","copystat","copy","copy2",
12  "copytree","rmtree"]
13 
14 def copyfileobj(fsrc, fdst, length=16*1024):
15  """copy data from file-like object fsrc to file-like object fdst"""
16  while 1:
17  buf = fsrc.read(length)
18  if not buf:
19  break
20  fdst.write(buf)
21 
22 
23 def copyfile(src, dst):
24  """Copy data from src to dst"""
25  fsrc = None
26  fdst = None
27  try:
28  fsrc = open(src, 'rb')
29  fdst = open(dst, 'wb')
30  copyfileobj(fsrc, fdst)
31  finally:
32  if fdst:
33  fdst.close()
34  if fsrc:
35  fsrc.close()
36 
37 def copymode(src, dst):
38  """Copy mode bits from src to dst"""
39  if hasattr(os, 'chmod'):
40  st = os.stat(src)
41  mode = stat.S_IMODE(st[stat.ST_MODE])
42  os.chmod(dst, mode)
43 
44 def copystat(src, dst):
45  """Copy all stat info (mode bits, atime and mtime) from src to dst"""
46  st = os.stat(src)
47  mode = stat.S_IMODE(st[stat.ST_MODE])
48  if hasattr(os, 'utime'):
49  os.utime(dst, (st[stat.ST_ATIME], st[stat.ST_MTIME]))
50  if hasattr(os, 'chmod'):
51  os.chmod(dst, mode)
52 
53 
54 def copy(src, dst):
55  """Copy data and mode bits ("cp src dst").
56 
57  The destination may be a directory.
58 
59  """
60  if os.path.isdir(dst):
61  dst = os.path.join(dst, os.path.basename(src))
62  copyfile(src, dst)
63  copymode(src, dst)
64 
65 def copy2(src, dst):
66  """Copy data and all stat info ("cp -p src dst").
67 
68  The destination may be a directory.
69 
70  """
71  if os.path.isdir(dst):
72  dst = os.path.join(dst, os.path.basename(src))
73  copyfile(src, dst)
74  copystat(src, dst)
75 
76 
77 def copytree(src, dst, symlinks=0):
78  """Recursively copy a directory tree using copy2().
79 
80  The destination directory must not already exist.
81  Error are reported to standard output.
82 
83  If the optional symlinks flag is true, symbolic links in the
84  source tree result in symbolic links in the destination tree; if
85  it is false, the contents of the files pointed to by symbolic
86  links are copied.
87 
88  XXX Consider this example code rather than the ultimate tool.
89 
90  """
91  names = os.listdir(src)
92  os.mkdir(dst)
93  for name in names:
94  srcname = os.path.join(src, name)
95  dstname = os.path.join(dst, name)
96  try:
97  if symlinks and os.path.islink(srcname):
98  linkto = os.readlink(srcname)
99  os.symlink(linkto, dstname)
100  elif os.path.isdir(srcname):
101  copytree(srcname, dstname, symlinks)
102  else:
103  copy2(srcname, dstname)
104  # XXX What about devices, sockets etc.?
105  except (IOError, os.error), why:
106  print "Can't copy %s to %s: %s" % (`srcname`, `dstname`, str(why))
107 
108 def rmtree(path, ignore_errors=0, onerror=None):
109  """Recursively delete a directory tree.
110 
111  If ignore_errors is set, errors are ignored; otherwise, if
112  onerror is set, it is called to handle the error; otherwise, an
113  exception is raised.
114 
115  """
116  cmdtuples = []
117  _build_cmdtuple(path, cmdtuples)
118  for cmd in cmdtuples:
119  try:
120  apply(cmd[0], (cmd[1],))
121  except:
122  exc = sys.exc_info()
123  if ignore_errors:
124  pass
125  elif onerror:
126  onerror(cmd[0], cmd[1], exc)
127  else:
128  raise exc[0], (exc[1][0], exc[1][1] + ' removing '+cmd[1])
129 
130 # Helper for rmtree()
131 def _build_cmdtuple(path, cmdtuples):
132  for f in os.listdir(path):
133  real_f = os.path.join(path,f)
134  if os.path.isdir(real_f) and not os.path.islink(real_f):
135  _build_cmdtuple(real_f, cmdtuples)
136  else:
137  cmdtuples.append((os.remove, real_f))
138  cmdtuples.append((os.rmdir, path))