annotate python/gpg_add.py @ 847:6c918c07d0e3

[gpg] make a front-end to add a line to a gpg symmetrically encrypted file
author Jeff Hammel <k0scist@gmail.com>
date Tue, 17 Oct 2017 11:59:09 -0700
parents
children a3ee050ae568
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
847
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 add a line to a gpg file; requires `gpg` on your path
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 """
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 import argparse
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import os
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import shutil
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import subprocess
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 import sys
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 import tempfile
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 def main(args=sys.argv[1:]):
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 """CLI"""
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 parser = argparse.ArgumentParser(description=__doc__)
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 parser.add_argument('file', help="path to GPG file")
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 parser.add_argument('line', help='line to add + encrypt')
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 options = parser.parse_args(args)
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 if not os.path.isfile(options.file):
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 parser.error("Not a file: '{}'".format(options.file))
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 tmpdir = tempfile.mkdtemp()
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 try:
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 # decrypt the file
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 outfile = os.path.join(tmpdir, options.file + '.decrypted')
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 subprocess.check_call(['gpg', '--output', outfile, '--decrypt', options.file])
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 assert os.path.exists(outfile)
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 # read lines
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 with open(outfile) as f:
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 lines = f.read().strip().splitlines()
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 # write the file
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 lines.append(options.line)
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 with open(outfile, 'w') as f:
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 f.write('\n'.join(lines))
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 # encrypt
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 subprocess.check_call(['gpg', '--output', options.file, '--symmetric', outfile])
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 finally:
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 shutil.rmtree(tmpdir, ignore_errors=True)
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 if __name__ == '__main__':
6c918c07d0e3 [gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48 main()