Commit c86c8d6b authored by Jack Jansen's avatar Jack Jansen

copy() can now create destination path

parent 8908c4ac
...@@ -35,13 +35,27 @@ def mkalias(src, dst): ...@@ -35,13 +35,27 @@ def mkalias(src, dst):
dstfinfo.Flags = dstfinfo.Flags|0x8000 # Alias flag dstfinfo.Flags = dstfinfo.Flags|0x8000 # Alias flag
dstfss.SetFInfo(dstfinfo) dstfss.SetFInfo(dstfinfo)
def copy(src, dst): def mkdirs(dst):
"""Make directories leading to 'dst' if they don't exist yet"""
if dst == '' or os.path.exists(dst):
return
head, tail = os.path.split(dst)
print 'XX', dst, '->', (head, tail)
# XXXX Is this a bug in os.path.split?
if not ':' in head:
head = head + ':'
mkdirs(head)
os.mkdir(dst, 0777)
def copy(src, dst, createpath=0):
"""Copy a file, including finder info, resource fork, etc""" """Copy a file, including finder info, resource fork, etc"""
if createpath:
mkdirs(os.path.split(dst)[0])
srcfss = macfs.FSSpec(src) srcfss = macfs.FSSpec(src)
dstfss = macfs.FSSpec(dst) dstfss = macfs.FSSpec(dst)
ifp = fopen(srcfss.as_pathname(), 'rb') ifp = open(srcfss.as_pathname(), 'rb')
ofp = fopen(dstfss.as_pathname(), 'wb') ofp = open(dstfss.as_pathname(), 'wb')
d = ifp.read(BUFSIZ) d = ifp.read(BUFSIZ)
while d: while d:
ofp.write(d) ofp.write(d)
...@@ -49,8 +63,8 @@ def copy(src, dst): ...@@ -49,8 +63,8 @@ def copy(src, dst):
ifp.close() ifp.close()
ofp.close() ofp.close()
ifp = fopen(srcfss.as_pathname(), '*rb') ifp = open(srcfss.as_pathname(), '*rb')
ofp = fopen(dstfss.as_pathname(), '*wb') ofp = open(dstfss.as_pathname(), '*wb')
d = ifp.read(BUFSIZ) d = ifp.read(BUFSIZ)
while d: while d:
ofp.write(d) ofp.write(d)
...@@ -66,12 +80,9 @@ def copy(src, dst): ...@@ -66,12 +80,9 @@ def copy(src, dst):
def copytree(src, dst): def copytree(src, dst):
"""Copy a complete file tree to a new destination""" """Copy a complete file tree to a new destination"""
if os.path.isdir(src): if os.path.isdir(src):
if not os.path.exists(dst): mkdirs(dst)
os.mkdir(dst)
elif not os.path.isdir(dst):
raise Error, 'Not a directory: '+dst
files = os.listdir(src) files = os.listdir(src)
for f in files: for f in files:
copytree(os.path.join(src, f), os.path.join(dst, f)) copytree(os.path.join(src, f), os.path.join(dst, f))
else: else:
copy(src, dst) copy(src, dst, 1)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment