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

Skip to content

Commit 57d53a9

Browse files
committed
- Optionally copy file times for copy/copytree (default on)
- Added touch(file) routine to tell the finder a files icon or something has changed (*finally* found out how to do this)
1 parent df34cf1 commit 57d53a9

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

Mac/Lib/macostools.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
from MACFS import *
1111
import MacOS
12+
import time
1213
try:
1314
openrf = MacOS.openrf
1415
except AttributeError:
@@ -52,7 +53,18 @@ def mkdirs(dst):
5253
mkdirs(head)
5354
os.mkdir(dst, 0777)
5455

55-
def copy(src, dst, createpath=0):
56+
def touched(dst):
57+
"""Tell the finder a file has changed"""
58+
file_fss = macfs.FSSpec(dst)
59+
vRefNum, dirID, name = file_fss.as_tuple()
60+
dir_fss = macfs.FSSpec((vRefNum, dirID, ''))
61+
crdate, moddate, bkdate = dir_fss.GetDates()
62+
now = time.time()
63+
if now == moddate:
64+
now = now + 1
65+
dir_fss.SetDates(crdate, now, bkdate)
66+
67+
def copy(src, dst, createpath=0, copydates=1):
5668
"""Copy a file, including finder info, resource fork, etc"""
5769
if createpath:
5870
mkdirs(os.path.split(dst)[0])
@@ -82,13 +94,17 @@ def copy(src, dst, createpath=0):
8294
df.Creator, df.Type = sf.Creator, sf.Type
8395
df.Flags = (sf.Flags & (kIsStationary|kNameLocked|kHasBundle|kIsInvisible|kIsAlias))
8496
dstfss.SetFInfo(df)
97+
if copydates:
98+
crdate, mddate, bkdate = srcfss.GetDates()
99+
dstfss.SetDates(crdate, mddate, bkdate)
100+
touched(dstfss)
85101

86-
def copytree(src, dst):
102+
def copytree(src, dst, copydates=1):
87103
"""Copy a complete file tree to a new destination"""
88104
if os.path.isdir(src):
89105
mkdirs(dst)
90106
files = os.listdir(src)
91107
for f in files:
92-
copytree(os.path.join(src, f), os.path.join(dst, f))
108+
copytree(os.path.join(src, f), os.path.join(dst, f), copydates)
93109
else:
94-
copy(src, dst, 1)
110+
copy(src, dst, 1, copydates)

0 commit comments

Comments
 (0)