def check_escape_unescape(self, initial): escaped = escape.escape(initial) self.assertTrue(all(c in (escape.LEGAL_CHARS + '_') for c in escaped)) unescaped = escape.unescape(escaped) self.assertEqual(initial, unescaped)
def check_escape_unescape(self, initial): escaped = escape.escape(initial) for c in escaped: self.assertIn(c, escape.LEGAL_CHARS + '_') unescaped = escape.unescape(escaped) self.assertEqual(initial, unescaped)
def run(self, argv): # parse args, setup logging and prepare keyrings args = self.parser.parse_args(argv) self.setup_logging(args.verbose) inkr = CryptFileKeyring() outkr = CryptFileKeyring() outkr.aesmode = args.aesmode # prepare infile infile = args.infile if not infile: infile = inkr.file_path else: inkr.file_path = infile inkr.filename = os.path.basename(infile) if not os.path.exists(infile): self.errexit('%s not found' % infile) if not inkr._check_file(): self.errexit('Failed to parse %s' % infile) log.info('infile %s: %s', infile, inkr.scheme) # prepare outfile outfile = args.outfile if not outfile: outfile = infile + '.%d' % os.getpid() if os.path.exists(outfile): if os.path.samefile(infile, outfile): self.errexit('infile and outfile must NOT be the same file') # outfile exists: rename os.rename(outfile, outfile + '~') log.info('%s renamed to %s~', outfile, outfile) outkr.file_path = outfile outkr.filename = os.path.basename(outfile) log.info('outfile %s: %s', outfile, outkr.scheme) # unlock the infile keyring try: inkr.keyring_key except ValueError as e: self.errexit('Unlock %s: %s' % (infile, e)) # keep old password or request password for new keyring if args.keep: outkr._get_new_password = lambda: inkr.keyring_key else: outkr.keyring_key # process infile config = configparser.RawConfigParser() config.read(infile) for section in config.sections(): log.debug('process section: [%s]', section) if section != escape('keyring-setting'): for username in config.options(section): username = unescape(username) section = unescape(section) log.info('process: [%s] %s', section, username) password = inkr.get_password(section, username) if password: outkr.set_password(section, username, password) log.debug('[%s] %s: %s', section, username, password) else: log.error('invalid entry: [%s]%s', section, username) return 0