@@ -53,6 +53,14 @@ from random import sample
53
53
MIN_VERSIONS = {
54
54
'datalad' : '0.7'
55
55
}
56
+
57
+ # Some variables for reuse
58
+ import stat
59
+
60
+ ALL_CAN_WRITE = (stat .S_IWUSR | stat .S_IWGRP | stat .S_IWOTH )
61
+ ALL_CAN_READ = (stat .S_IRUSR | stat .S_IRGRP | stat .S_IROTH )
62
+ assert ALL_CAN_READ >> 1 == ALL_CAN_WRITE # Assumption in the code
63
+
56
64
PY3 = sys .version_info [0 ] >= 3
57
65
58
66
import logging
@@ -305,6 +313,35 @@ def find_files(regex, topdir=curdir, exclude=None, exclude_vcs=True, dirs=False)
305
313
find_files .__doc__ %= (_VCS_REGEX ,)
306
314
307
315
316
+ def set_readonly (path , read_only = True ):
317
+ """Make file read only or writeable while preserving "access levels"
318
+
319
+ So if file was not readable by "others" it should remain not readable
320
+ by others
321
+
322
+ Parameters
323
+ ----------
324
+ path : str
325
+ read_only : bool, optional
326
+ If True (default) - would make it read-only. If False, would make it
327
+ writeable for levels where it is readable
328
+ """
329
+ # get current permissions
330
+ perms = stat .S_IMODE (os .lstat (path ).st_mode )
331
+ # set new permissions
332
+ if read_only :
333
+ new_perms = perms & (~ ALL_CAN_WRITE )
334
+ else :
335
+ # need to set only for those which had read bit set
336
+ # read bit is <<1 away from write bit
337
+ whocanread = perms & ALL_CAN_READ
338
+ thosecanwrite = whocanread >> 1
339
+ new_perms = perms | thosecanwrite
340
+ # apply and return those target permissions
341
+ os .chmod (path , new_perms )
342
+ return new_perms
343
+
344
+
308
345
def group_dicoms_into_seqinfos (
309
346
files , file_filter = None , dcmfilter = None , grouping = 'studyUID'
310
347
):
0 commit comments