Mercurial > hg > config
annotate python/gpg_add.py @ 923:b25fbb9257ec default tip
Breeze GTK theme
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Thu, 14 Nov 2024 09:30:28 -0800 |
parents | a3ee050ae568 |
children |
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 |
848
a3ee050ae568
[documentation] reference links
Jeff Hammel <k0scist@gmail.com>
parents:
847
diff
changeset
|
7 # Reference: |
a3ee050ae568
[documentation] reference links
Jeff Hammel <k0scist@gmail.com>
parents:
847
diff
changeset
|
8 # - https://www.gnupg.org/gph/en/manual/x110.html |
a3ee050ae568
[documentation] reference links
Jeff Hammel <k0scist@gmail.com>
parents:
847
diff
changeset
|
9 # - https://www.gnupg.org/gph/en/manual/x56.html |
a3ee050ae568
[documentation] reference links
Jeff Hammel <k0scist@gmail.com>
parents:
847
diff
changeset
|
10 # - http://tuxlabs.com/?p=450 |
a3ee050ae568
[documentation] reference links
Jeff Hammel <k0scist@gmail.com>
parents:
847
diff
changeset
|
11 |
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
|
12 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
|
13 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
|
14 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
|
15 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
|
16 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
|
17 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
|
18 |
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 |
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 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
|
21 """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
|
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 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
|
24 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
|
25 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
|
26 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
|
27 |
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 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
|
29 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
|
30 |
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 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
|
32 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
|
33 # 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
|
34 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
|
35 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
|
36 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
|
37 |
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 # 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
|
39 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
|
40 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
|
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 # 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
|
43 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
|
44 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
|
45 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
|
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 # 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
|
48 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
|
49 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
|
50 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
|
51 |
6c918c07d0e3
[gpg] make a front-end to add a line to a gpg symmetrically encrypted file
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
52 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
|
53 main() |