From f706b87bb094b3c11f1e717ee3d46d464b77557f Mon Sep 17 00:00:00 2001 From: Rob Date: Tue, 25 Aug 2009 12:40:52 +0200 Subject: [PATCH] Refactoring to clean up decisions about argument types, centralized processing of --file-source --- signduterre.py | 61 ++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/signduterre.py b/signduterre.py index 9a504b3..e55019f 100755 --- a/signduterre.py +++ b/signduterre.py @@ -703,23 +703,40 @@ current_outfile = sys.stdout; current_private = sys.stderr; # Determine which kind of argument you have -def arg_is_env (argument): - return re.match(r'\$\{?[^\}]+\}?$', argument); - def arg_is_shell_command (argument): - return argument.startswith('$(') and argument.endswith(')'); + return_value = False; + if argument.startswith('$(') and argument.endswith(')'): + return_value = argument[2:-1]; + return return_value; +def arg_is_env (argument): + return_value = False; + if arg_is_shell_command(argument): return return_value; + match = re.match(r'\$\{?[^\}]+\}?$', argument); + if match != None: + return_value = match.group(1); + return return_value; + def arg_is_python_script (argument): - return argument.startswith('@(') and argument.endswith(')'); + return_value = False; + if argument.startswith('@(') and argument.endswith(')'): + return_value = argument[2:-1]; + return return_value; def arg_is_hidden (argument): - return argument.startswith('[') and argument.endswith(']'); + return_value = False; + if argument.startswith('[') and argument.endswith(']'): + return_value = argument[1:-1]; + return return_value; def arg_is_tunnel (argument): return argument.startswith('ssh://'); def arg_is_stat (argument): - return argument.startswith('?'); + return_value = False; + if argument.startswith('?'): + return_value = argument[1:]; + return return_value; def arg_is_stdin (argument): return argument == '-'; @@ -1254,6 +1271,19 @@ for my_passphrase in passphrase_list: filehash.update(bytes(prefix, encoding='ascii')); # Remove [] filename = org_filename.strip('[').rstrip(']'); + # Preprocessing filename to include file sources + # Insert remote ssh:// if necessary + if my_filesource: + if arg_is_tunnel(my_filesource) and not arg_is_tunnel(filename): + if arg_is_env(filename) : + match = re.search(r"(?i)ssh://([^/]+)", my_filesource); + if match != None: filename = "${ssh://"+match.group(1)+"/"+arg_is_env(filename)+"}"; + elif arg_is_shell_command(filename): + match = re.search(r"(?i)ssh://([^/]+)", my_filesource); + if match != None: filename = "$(ssh://"+match.group(1)+"/"+arg_is_shell_command(filename)+")"; + elif arg_is_plain_file(filename): + filename = my_filesource+filename; + # Use python @() constructs if arg_is_python_script(filename): if not execute: @@ -1298,10 +1328,6 @@ for my_passphrase in passphrase_list: current_var = not_allowed_chars.sub(" ", match.group(1)); if print_verbose: print("# echo $"+ current_var, file=current_outfile); - # Insert remote ssh:// if necessary - if my_filesource and current_var.find('://') == -1 : - match = re.search(r"(?i)ssh://([^/]+)", my_filesource); - if match != None: current_var = "ssh://"+match.group(1)+"/"+current_var; # Redirect env query to other system if current_var.startswith("ssh://"): match = re.search(r"(?i)(ssh://[^/]+/)(.*)$", current_var); @@ -1322,10 +1348,6 @@ for my_passphrase in passphrase_list: exit(0); # Clean up command current_command = not_allowed_chars.sub(" ", match.group(1)); - # Insert remote ssh:// if necessary - if my_filesource and current_command.find('://') == -1 : - match = re.search(r"(?i)ssh://([^/]+)", my_filesource); - if match != None: current_command = "ssh://"+match.group(1)+"/"+current_command; # Expand remote ssh:// current_host = None; current_executable = None; @@ -1422,10 +1444,7 @@ for my_passphrase in passphrase_list: print("ERROR: No bytes read from STDIN. Processing aborted.", file=sys.stderr); quit(1); else: - current_filename = filename; - if my_filesource and arg_is_plain_file(filename) : - current_filename = my_filesource+current_filename; - with open_infile(current_filename, 'rb') as file: + with open_infile(filename, 'rb') as file: for b in file: if type(b).__name__ == 'str': b = bytes(b, encoding='utf8'); @@ -1434,7 +1453,7 @@ for my_passphrase in passphrase_list: print(str(b)); current_digest = filehash.hexdigest(); - print_name = filename; + print_name = org_filename; if my_quiet or arg_is_hidden(org_filename): file_argnum += 1; print_name = '['+str(file_argnum)+']'; @@ -1450,7 +1469,7 @@ for my_passphrase in passphrase_list: if not (my_quiet and total_only) and not (my_allsalts and snum > 1): print(current_hash_line, file=current_outfile); elif not (my_quiet or my_allsalts): - if check_hashes[print_name] == (len(current_digest)*'-'): + if check_hashes[print_name] == (len(current_digest)*'-'): # Suppress redundant output of empty, ----, lines if snum <= 1 and pnum <= 1: print(check_hashes[print_name]+" *"+print_name, file=current_outfile); -- 2.11.4.GIT