github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/bin/decrypt_names.py (about)

     1  #!/usr/bin/env python3
     2  """
     3  This is a tool to decrypt file names in rclone logs.
     4  
     5  Pass two files in, the first should be a crypt mapping generated by
     6  
     7  rclone ls --crypt-show-mapping remote:path
     8  
     9  The second should be a log file that you want the paths decrypted in.
    10  
    11  Note that if the crypt mappings file is large it can take some time to
    12  run.
    13  """
    14  
    15  import re
    16  import sys
    17  
    18  # Crypt line
    19  match_crypt = re.compile(r'NOTICE: (.*?): Encrypts to "(.*?)"$')
    20  
    21  def read_crypt_map(mapping_file):
    22      """
    23      Read the crypt mapping file in, creating a dictionary of substitutions
    24      """
    25      mapping = {}
    26      with open(mapping_file) as fd:
    27          for line in fd:
    28              match = match_crypt.search(line)
    29              if match:
    30                  plaintext, ciphertext = match.groups()
    31                  plaintexts = plaintext.split("/")
    32                  ciphertexts = ciphertext.split("/")
    33                  for plain, cipher in zip(plaintexts, ciphertexts):
    34                      mapping[cipher] = plain
    35      return mapping
    36  
    37  def map_log_file(crypt_map, log_file):
    38      """
    39      Substitute the crypt_map in the log file.
    40  
    41      This uses a straight forward O(N**2) algorithm.  I tried using
    42      regexps to speed it up but it made it slower!
    43      """
    44      with open(log_file) as fd:
    45          for line in fd:
    46              for cipher, plain in crypt_map.items():
    47                  line = line.replace(cipher, plain)
    48              sys.stdout.write(line)
    49  
    50  def main():
    51      if len(sys.argv) < 3:
    52          print("Syntax: %s <crypt-mapping-file> <log-file>" % sys.argv[0])
    53          raise SystemExit(1)
    54      mapping_file, log_file = sys.argv[1:]
    55      crypt_map = read_crypt_map(mapping_file)
    56      map_log_file(crypt_map, log_file)
    57  
    58  if __name__ == "__main__":
    59      main()