def createdir(self, directory): ''' create a directory, output error if failed and exit ''' try: self.dboxclient.file_create_folder(directory) except: msg.errx('Cannot create sub-dir: permission denied: %s' % directory)
def test_msg_04_errx(self): ''' MSG-04 | errorx exit message ''' sys.argv = ['shayfara', '-e', 'test/dirtest/file0'] args = opts.getopts() message = 'test message' expected = 1 with self.assertRaises(SystemExit) as cm: msg.errx(message, args) return self.assertEqual(cm.exception.code, expected)
def createdir(self, directory): ''' create a directory, output error if failed and exit ''' if not os.path.isdir(directory): try: msg.info('Creating directory: %s' % directory) os.makedirs(directory) except: msg.errx('Cannot create sub-dir: permission denied: %s' % directory)
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
def get_password(args): ''' Get the password. ''' # User specified it on the command line. Not safe but useful for testing # and for scripts. if args.password: return args.password # User specified the password in a file. It should be 0600. if args.password_file: password = None try: with open(args.password_file, 'r') as ifp: for line in ifp.readlines(): line = line.strip() if len(line) == 0: continue # skip blank lines if line[0] == '#': continue # skip comments password = line break except: msg.errx("unable to open password file: %s" % (args.password_file)) if password is None: msg.errx('Password was not found in file %s' % args.password_file) return password # if password not specify in command-line password = getpass.getpass('Password: '******'Re-enter password: '******'Passwords did not match!') return password
def createdir(self, directory): ''' create a directory, output error if failed and exit - return 'ofile' if write successful - exit using msg.errx() if failed ''' msg.errx('You should not be seing this message: %s' % directory)
def crypt(args): ''' Encrypt or decrypt the files. ''' # set mode, used by messages (msg.py) if args.decrypt is True: mode = 'decrypt' else: mode = 'encrypt' # init return code ret = 0 skipped = 0 # set cipher to use if args.cipher == 'simplecrypt': from shayfara.ciphers import scrypt cipher = scrypt # set output plug-in to use if args.plugin == 'local': from shayfara.plugins import local plugin = local.PluginLocal() elif args.plugin == 'dropbox': from shayfara.plugins import dbox if args.auth_token: plugin = dbox.PluginDropbox(args.auth_token) else: msg.errx('Authentification token required for dropbox plugin') # set database to use if args.database == 'plaintext': from shayfara.databases import shelve # get database output directory if not args.dest_dir: dbdir = path.dirname(path.abspath(args.FILES[0])) else: dbdir = args.dest_dir # create destination directory if it does not exist plugin.createdir(dbdir) # init the database db = shelve.DBShelve(dbdir) # get password password = utils.get_password(args) # expand all files from command line files = utils.load_files(args) # parse and process files for ifile in files: # get output file name # returns empty if file exists and --force not specified ofile = utils.get_output_file(ifile, args) # in case new directory is specified using -D|--dest-dir # update output file path if args.dest_dir: ofile = utils.updatedir(ofile, args.dest_dir, args.FILES[0]) # create directory in --force, skip if dry-run if args.force is True and args.dry_run is False: plugin.createdir(path.dirname(path.abspath(ofile))) # check if files exists, and force is not specified ofile = plugin.exists(ofile, args) # if output file name is empty, skip file if ofile is None: skipped += 1 continue # write action (encrypting/decrypting) if verbose msg.infov('%sing: %s' % (mode, ofile), args=args) if args.decrypt: # encrypt file using extension output = cipher.decrypt_file(password, ifile) else: # encrypt file using extension output = cipher.encrypt_file(password, ifile) # write output using proper plug-in # if out is empty, increment error if output: # skip if dry-run if args.dry_run is False: # write to file, increment skipped if error occurs if plugin.write(ofile, output, ifile): # store target file name and stat of original file in # database, if encryption succeeded. db.put(ofile, stat(ifile)) else: # in case write failed, increment skipped counter skipped += 1 continue else: skipped += 1 continue # in case --in-place, rename file to original (replace) if args.in_place and args.dest_dir is None: msg.infov('renaming : %s' % ifile, args) # skip if dry-run if args.dry_run is False: # if out is empty, increment error if plugin.rename(ofile, ifile): ret += 1 continue if args.dry_run is False: msg.info('%d files %sed' % (len(files) - skipped, mode), args=args) else: msg.info('%d files %sed (DRY RUN)' % (len(files) - skipped, mode), args=args) # write file database if args.database: db.close() return ret