def exists(self, ofile, opts): ''' check if file already exists ''' # in case file already exists, skip if os.path.isfile(ofile) and opts.force is False: msg.errn('Already exists, skipping: %s' % ofile) ofile = None return ofile
def rename(self, before, after): ''' replace original file with generated file ''' try: self.dboxclient.file_move(before, after) return after except: msg.errn('Permission denied: cannot write file: %s' % after) return None
def remove(self, ofile): ''' delete local file ''' try: self.dboxclient.file_delete(ofile) return ofile except: msg.errn('Permission denied: cannot delete file: %s' % ofile) return None
def check_file_access(path): ''' Check the file access. ''' if os.access(path, os.R_OK) is False: msg.errn('Cannot read file: %s' % path) return 1 if os.access(path, os.W_OK) is False: msg.errn('Cannot write file: %s' % path) return 1 return 0
def write(self, ofile, data, ifile): ''' write data to local file ''' try: # get original file system status using stat(1) stat = os.stat(ifile) # write file to disk with open(ofile, 'wb') as ofp: ofp.write(data) # restore original file system status os.chmod(ofile, stat.st_mode) return ofile except: msg.errn('Error occured while writing file: %s' % ofile) return None
def test_msg_05_errn(self): ''' MSG-05 | error message ''' sys.argv = ['shayfara', '-e', 'test/dirtest/file0'] args = opts.getopts() message = 'test message' expected = 'ERROR: %s\n' % message ret = msg.errn(message, args) return self.assertEqual(ret, expected)
def write(self, ofile, data, ifile=None): ''' write data to local file ''' try: # write temporary local file with open(ofile, 'wb') as ofp: ofp.write(data) # send file to dropbox with open(ofile, 'rb') as ofp: self.dboxclient.put_file(ofile, ofp) # delete temporary local file os.remove(ofile) return ofile except dropbox.rest.ErrorResponse as err: msg.errn('Error writing file: %s: %s' % (ofile, err)) # delete temporary local file os.remove(ofile) return None
def load_files(args): ''' Load the specified files. ''' # init number of errors and file list nerrs = 0 files = [] for entry in args.FILES: # case argument is a file if os.path.isfile(entry): msg.infov('Loading file ' + entry, args) # add to file list and check file access files.append(entry) nerrs += check_file_access(entry) # case argument is a directory and not no-recursive flag elif os.path.isdir(entry): if args.no_recursive is not False: for wroot, wdirs, wfiles in os.walk(entry): for wfile in sorted(wfiles, key=str.lower): if wfile in ['.', '..']: continue wpath = os.path.join(wroot, wfile) msg.infov('Loading file ' + wpath, args) # add to file list and check file access files.append(wpath) nerrs += check_file_access(wpath) else: msg.infov('Skipping dir ' + entry, args) else: msg.errn('Skipping entry %s: No such file or directory' % entry) # print total number of loaded files if args.dry_run is False: msg.info('%d files loaded' % (len(files)), args) else: msg.info('%d files loaded (DRY RUN)' % (len(files)), args) # in case errors were found if nerrs: msg.errx('%d access errors found, cannot proceed' % (nerrs)) return files