-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure.py
54 lines (40 loc) · 1.53 KB
/
configure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/python3
import sys
import subprocess
import json
import re
if len(sys.argv) != 4:
print('Usage:\nconfigure.py [encrypted file] [password] [output file] ')
sys.exit()
encrypted_file, password, output_file = sys.argv[1:]
print('Decrypting {}'.format(encrypted_file))
# Call gpg, decrypted file will be stored in completed_process.stdout
try:
command_output = subprocess.check_output(['gpg', '-d', '--batch', '--passphrase', password, encrypted_file])
except subprocess.CalledProcessError:
print(completed_process.stdout, '\n', completed_process.stderr, '\nDecryption failed, exiting')
sys.exit(1)
api_keys = json.loads(command_output.decode('utf8'))
print('Keys to fill: ' + ', '.join(api_keys.keys()))
# Create a dict with @key_name@ for keys
patterns_dict = dict()
for key_name in api_keys.keys(): patterns_dict['@{}@'.format(key_name)] = api_keys[key_name]
print('Opening file {}'.format(output_file))
with open(output_file, 'r') as f:
data = f.read()
any_matches = False
def replace(match):
global any_matches
any_matches = True
print('Filled in {}'.format(match.group(0)))
return patterns_dict[match.group(0)]
# Create regular expression which will match all dict keys
regex = re.compile('({})'.format('|'.join(map(re.escape, patterns_dict.keys()))))
# For each match, find corresponding value in dict
data = regex.sub(replace, data)
if not any_matches:
print('No patters matched. Check your file again.')
print('Saving')
with open(output_file, 'w') as f:
f.write(data)
print('Finished')