Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 423c798

Browse files
committed
copy() can now create destination path
1 parent f5101ee commit 423c798

1 file changed

Lines changed: 21 additions & 10 deletions

File tree

Mac/Lib/macostools.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,36 @@ def mkalias(src, dst):
3535
dstfinfo.Flags = dstfinfo.Flags|0x8000 # Alias flag
3636
dstfss.SetFInfo(dstfinfo)
3737

38-
def copy(src, dst):
38+
def mkdirs(dst):
39+
"""Make directories leading to 'dst' if they don't exist yet"""
40+
if dst == '' or os.path.exists(dst):
41+
return
42+
head, tail = os.path.split(dst)
43+
print 'XX', dst, '->', (head, tail)
44+
# XXXX Is this a bug in os.path.split?
45+
if not ':' in head:
46+
head = head + ':'
47+
mkdirs(head)
48+
os.mkdir(dst, 0777)
49+
50+
def copy(src, dst, createpath=0):
3951
"""Copy a file, including finder info, resource fork, etc"""
52+
if createpath:
53+
mkdirs(os.path.split(dst)[0])
4054
srcfss = macfs.FSSpec(src)
4155
dstfss = macfs.FSSpec(dst)
4256

43-
ifp = fopen(srcfss.as_pathname(), 'rb')
44-
ofp = fopen(dstfss.as_pathname(), 'wb')
57+
ifp = open(srcfss.as_pathname(), 'rb')
58+
ofp = open(dstfss.as_pathname(), 'wb')
4559
d = ifp.read(BUFSIZ)
4660
while d:
4761
ofp.write(d)
4862
d = ifp.read(BUFSIZ)
4963
ifp.close()
5064
ofp.close()
5165

52-
ifp = fopen(srcfss.as_pathname(), '*rb')
53-
ofp = fopen(dstfss.as_pathname(), '*wb')
66+
ifp = open(srcfss.as_pathname(), '*rb')
67+
ofp = open(dstfss.as_pathname(), '*wb')
5468
d = ifp.read(BUFSIZ)
5569
while d:
5670
ofp.write(d)
@@ -66,12 +80,9 @@ def copy(src, dst):
6680
def copytree(src, dst):
6781
"""Copy a complete file tree to a new destination"""
6882
if os.path.isdir(src):
69-
if not os.path.exists(dst):
70-
os.mkdir(dst)
71-
elif not os.path.isdir(dst):
72-
raise Error, 'Not a directory: '+dst
83+
mkdirs(dst)
7384
files = os.listdir(src)
7485
for f in files:
7586
copytree(os.path.join(src, f), os.path.join(dst, f))
7687
else:
77-
copy(src, dst)
88+
copy(src, dst, 1)

0 commit comments

Comments
 (0)