def make_temp_dir(parent_dir, itime): # make a subdirectory of 'parent_dir' using the time of day (seconds) # and the PID of the current process # returns the new directory path # the permissions are set so that group and other have no read, write, exe pid = str(os.getpid()) dt = '_'.join(time.ctime(itime).split()) sd = os.path.join(parent_dir, 'filecopy_' + dt + '_p' + pid) print3('mkdir ' + os.uname()[1] + ':' + sd) os.mkdir(sd) perms.apply(sd, 'u=rwx', 'g=', 'o=') return sd
def local_copy(readL, destdir, fperms=[], dperms=[], group=None): """ Copy source files/directories from current machine to a destination directory on the current machine. The 'readL' comes from glob_paths() and is a list of [dir,base1,base2], where base1 and base2 are basenames. """ tmpd = make_temp_dir(destdir, time.time()) for dirn, basn, dstn in readL: rf = os.path.join(dirn, basn) wf = os.path.join(tmpd, basn) if os.path.islink(rf): rf1 = os.readlink(rf) print3('ln -s', rf1, wf) os.symlink(rf1, wf) elif os.path.isdir(rf): print3('cp -r', rf, wf) shutil.copytree(rf, wf, symlinks=True) perms.apply(wf, *dperms) if group: perms.apply(wf, group) apply_permissions(wf, os.listdir(wf), fperms, dperms, group) else: print3('cp -p', rf, wf) shutil.copy2(rf, wf) if fperms: perms.apply(wf, *fperms) if group: perms.apply(wf, group) swap_paths(readL, tmpd, destdir) print3('rm -r ' + tmpd) shutil.rmtree(tmpd)
def apply_permissions(directory, fileL, fperms, dperms, group): # for basn in fileL: wf = os.path.join(directory, basn) if os.path.islink(wf): pass elif os.path.isdir(wf): perms.apply(wf, *dperms) if group: perms.apply(wf, group) apply_permissions(wf, os.listdir(wf), fperms, dperms, group) else: if fperms: perms.apply(wf, *fperms) if group: perms.apply(wf, group)
def file_perms(fname, permissions, remote=None): """ Sets file permissions on file 'fname' given a python list of (string) specifications. If 'remote' is not None, it must be a connected RemotePython instance, which is used to perform the operations on the remote machine. The permissions are only changed if the user of this process and the user of the file are the same. """ if remote == None: if perms.get_user_name(fname) == perms.get_user_name(): if type(permissions) == type(''): perms.apply(fname, permissions) else: # assume 'permissions' is a tuple or list perms.apply(fname, *permissions) else: if remote.perms.get_user_name(fname) == remote.perms.get_user_name(): if type(permissions) == type(''): remote.perms.apply(fname, permissions) else: # assume 'permissions' is a tuple or list remote.perms.apply(fname, *permissions)
def open_log_file_for_write(logpath, jobfile, group): "" date = time.strftime("%a_%b_%d_%Y_%H:%M:%S_%Z") joblogdir = make_root_log_name(logpath, jobfile) + date if group: # do not mask out rx for group os.umask(os.umask(0o000) & 0o727) os.mkdir(joblogdir) if group: perms.apply(joblogdir, group, 'g+rX') os.chdir(joblogdir) fp = open('log.txt', 'w') if group: perms.apply('log.txt', group, 'g+r') return joblogdir, fp