This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
copymodes
executable file
·72 lines (54 loc) · 1.68 KB
/
copymodes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
import sys
import os.path
import shutil
import subprocess
import re
re_ls_acl = re.compile(r'^ *\d+: *')
def usage():
print >>sys.stderr, "Usage: copymodes srcroot dstroot"
def pathslash(path):
if not path.endswith("/"):
return path + "/"
else:
return path
def copymeta(src, dst):
# copy mode, flags, and time stamps
shutil.copystat(src, dst)
# copy uid and gid
s = os.stat(src)
try:
os.chown(dst, s.st_uid, s.st_gid)
except OSError as e:
print >>sys.stderr, "chown: %s" % e.strerror
# copy ACLs
ls = subprocess.Popen(("ls", "-led", src), stdout=subprocess.PIPE)
(out, err) = ls.communicate()
if ls.returncode != 0:
print >>sys.stderr, "ls -led: failed with returncode %d" % e.returncode
acls = [re_ls_acl.sub("", l) for l in out.splitlines(True)[1:]]
if acls:
chmod = subprocess.Popen(("chmod", "-E", dst), stdin=subprocess.PIPE)
chmod.communicate("".join(acls))
def main(argv):
if len(argv) != 3:
usage()
return 1
srcroot = pathslash(argv[1])
dstroot = pathslash(argv[2])
if not os.path.isdir(srcroot):
print >>sys.stderr, "%s: not a directory" % srcroot
return 1
if not os.path.isdir(dstroot):
print >>sys.stderr, "%s: not a directory" % dstroot
return 1
for (dirpath, dirnames, filenames) in os.walk(dstroot):
dst = pathslash(dirpath)
tail = dst[len(dstroot):]
src = srcroot + tail
if os.path.isdir(src):
print src
copymeta(src, dst)
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))