Skip to content

Commit 783e0d2

Browse files
committed
Ensure correct permissions for copied files, even if an error happens during copying
1 parent c54bc38 commit 783e0d2

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

nbgrader/exchange/default/exchange.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -77,31 +77,36 @@ def do_copy(self, src, dest, log=None):
7777
specified by the options coursedir.ignore, coursedir.include
7878
and coursedir.max_file_size.
7979
"""
80-
shutil.copytree(src, dest,
81-
ignore=ignore_patterns(exclude=self.coursedir.ignore,
82-
include=self.coursedir.include,
83-
max_file_size=self.coursedir.max_file_size,
84-
log=self.log))
85-
# copytree copies access mode too - so we must add go+rw back to it if
86-
# we are in groupshared.
87-
if self.coursedir.groupshared:
88-
for dirname, _, filenames in os.walk(dest):
89-
# dirs become ug+rwx
90-
st_mode = os.stat(dirname).st_mode
91-
if st_mode & 0o2770 != 0o2770:
92-
try:
93-
os.chmod(dirname, (st_mode|0o2770) & 0o2777)
94-
except PermissionError:
95-
self.log.warning("Could not update permissions of %s to make it groupshared", dirname)
96-
97-
for filename in filenames:
98-
filename = os.path.join(dirname, filename)
99-
st_mode = os.stat(filename).st_mode
100-
if st_mode & 0o660 != 0o660:
80+
try:
81+
shutil.copytree(src, dest,
82+
ignore=ignore_patterns(exclude=self.coursedir.ignore,
83+
include=self.coursedir.include,
84+
max_file_size=self.coursedir.max_file_size,
85+
log=self.log))
86+
except OSError as err:
87+
raise err
88+
# Set permissions for copied files, even if some failed to copy
89+
finally:
90+
# copytree copies access mode too - so we must add go+rw back to it if
91+
# we are in groupshared.
92+
if self.coursedir.groupshared:
93+
for dirname, _, filenames in os.walk(dest):
94+
# dirs become ug+rwx
95+
st_mode = os.stat(dirname).st_mode
96+
if st_mode & 0o2770 != 0o2770:
10197
try:
102-
os.chmod(filename, (st_mode|0o660) & 0o777)
98+
os.chmod(dirname, (st_mode|0o2770) & 0o2777)
10399
except PermissionError:
104-
self.log.warning("Could not update permissions of %s to make it groupshared", filename)
100+
self.log.warning("Could not update permissions of %s to make it groupshared", dirname)
101+
102+
for filename in filenames:
103+
filename = os.path.join(dirname, filename)
104+
st_mode = os.stat(filename).st_mode
105+
if st_mode & 0o660 != 0o660:
106+
try:
107+
os.chmod(filename, (st_mode|0o660) & 0o777)
108+
except PermissionError:
109+
self.log.warning("Could not update permissions of %s to make it groupshared", filename)
105110

106111
def start(self):
107112
if sys.platform == 'win32':

0 commit comments

Comments
 (0)