· 7 years ago · Mar 27, 2018, 08:44 AM
1import argparse
2import os
3from filecmp import dircmp
4import subprocess
5
6def get_command_line_arguments():
7 parser = argparse.ArgumentParser()
8 parser.add_argument('-r', help='Directory containing directories for each batch job. Accepts both absolute and relative paths.', required = True, metavar='ROOT_DIRECTORY')
9 parser.add_argument('-p', help='Absolute path to public GPG key.', required = True, metavar='GPG_PUBLIC_KEY')
10 parser.add_argument('-s', help='Absolute path to secret GPG key.', required = True, metavar='GPG_SECRET_KEY')
11 parser.add_argument('-e', help='Passphrase for decryption with secret GPG key.', required = True, metavar='GPG_PASSPHRASE')
12 parser.add_argument('-n', help='Show files that are present only in one of two directories', action='store_true')
13 return parser.parse_args()
14
15def list_files(root_dir, passphrase, showUniqueFiles):
16 root_dir = os.path.join(os.path.abspath(root_dir), '')
17 print "Root directory is %s" % root_dir
18
19 print "Start decryption"
20 for subdir in os.listdir(root_dir):
21 subdir_abs_path = os.path.join(root_dir, subdir)
22 archive_dir = os.path.join(subdir_abs_path, "archive")
23 chase_sftp_dir = os.path.join(subdir_abs_path, "SFTP/Chase_SFTP")
24
25 decrypt_files(archive_dir, passphrase)
26 decrypt_files(chase_sftp_dir, passphrase)
27
28 print "Finish decryption\n"
29
30 for subdir in os.listdir(root_dir):
31 print "Current directory: %s" % subdir
32 subdir_abs_path = os.path.join(root_dir, subdir)
33 archive_dir = os.path.join(subdir_abs_path, "archive")
34 chase_sftp_dir = os.path.join(subdir_abs_path, "SFTP/Chase_SFTP")
35
36 dcmp = dircmp(archive_dir, chase_sftp_dir)
37 for name in dcmp.diff_files:
38 print "File '%s' has different content in '%s' and '%s'" % (name, dcmp.left,dcmp.right)
39
40 if showUniqueFiles:
41 for name in dcmp.left_only:
42 if not name.endswith(".pgp"):
43 print "File '%s' is present only in '%s'" % (name, dcmp.left)
44 for name in dcmp.right_only:
45 if not name.endswith(".pgp"):
46 print "File '%s' is present only in '%s'" % (name, dcmp.right)
47
48def import_gpg_keys(public_key, secret_key):
49 bash_import_public_key_command = "gpg -q --import %s" % public_key
50 bash_import_secret_key_command = "gpg -q --import %s" % secret_key
51
52 subprocess.call(bash_import_public_key_command, shell=True)
53 subprocess.call(bash_import_secret_key_command, shell=True)
54
55def decrypt_files(root_dir, passphrase):
56 if not os.path.isdir(root_dir):
57 print "Skipping file: '%s'" % root_dir
58 return
59
60 #passphrase='123'
61
62 encrypted_files = filter(lambda filename: not os.path.isdir(filename), os.listdir(root_dir))
63 encrypted_files = filter(lambda filename: filename.endswith(".pgp"), encrypted_files)
64 print "Files to decrypt: %s" % encrypted_files
65 for file in encrypted_files:
66 bash_decrypt_command = "gpg --quiet --yes --output %s --passphrase %s --decrypt %s" % (os.path.join(root_dir, file)[:-4], passphrase, os.path.join(root_dir, file))
67 subprocess.call(bash_decrypt_command, shell=True)
68
69args = get_command_line_arguments()
70root_dir = args.r
71showUniqueFiles = args.n
72passphrase = args.e
73
74import_gpg_keys(args.p, args.s)
75print "Start comparing contents of archive and SFTP directories for each job"
76list_files(root_dir, passphrase, showUniqueFiles)
77print "Finish comparing contents of archive and SFTP directories for each job"