1
mirror of https://github.com/rclone/rclone synced 2024-12-22 13:03:02 +01:00
rclone/bin/decrypt_names.py
Fabian Möller 1f5e23aedb scripts: make absolute paths consistent
Change absolute binary paths in scripts to /usr/bin/env or make them
relative.
This allows the scripts to be used on linux distributions
like NixOS, where binaries are not located in /usr/ or /bin/.
2018-01-26 09:39:05 +00:00

60 lines
1.7 KiB
Python
Executable File

#!/usr/bin/env python
"""
This is a tool to decrypt file names in rclone logs.
Pass two files in, the first should be a crypt mapping generated by
rclone ls --crypt-show-mapping remote:path
The second should be a log file that you want the paths decrypted in.
Note that if the crypt mappings file is large it can take some time to
run.
"""
import re
import sys
# Crypt line
match_crypt = re.compile(r'NOTICE: (.*?): Encrypts to "(.*?)"$')
def read_crypt_map(mapping_file):
"""
Read the crypt mapping file in, creating a dictionary of substitutions
"""
mapping = {}
with open(mapping_file) as fd:
for line in fd:
match = match_crypt.search(line)
if match:
plaintext, ciphertext = match.groups()
plaintexts = plaintext.split("/")
ciphertexts = ciphertext.split("/")
for plain, cipher in zip(plaintexts, ciphertexts):
mapping[cipher] = plain
return mapping
def map_log_file(crypt_map, log_file):
"""
Substitute the crypt_map in the log file.
This uses a straight forward O(N**2) algorithm. I tried using
regexps to speed it up but it made it slower!
"""
with open(log_file) as fd:
for line in fd:
for cipher, plain in crypt_map.iteritems():
line = line.replace(cipher, plain)
sys.stdout.write(line)
def main():
if len(sys.argv) < 3:
print "Syntax: %s <crypt-mapping-file> <log-file>" % sys.argv[0]
raise SystemExit(1)
mapping_file, log_file = sys.argv[1:]
crypt_map = read_crypt_map(mapping_file)
map_log_file(crypt_map, log_file)
if __name__ == "__main__":
main()