1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
"""Use GPG to sign all files in a directory.""" |
3
|
|
|
|
4
|
|
|
import getpass # invisible passwords (cf. sudo) |
5
|
|
|
import os # path operations |
6
|
|
|
import sys # load arguments |
7
|
|
|
|
8
|
|
|
from bbarchivist import argutils # arguments |
9
|
|
|
from bbarchivist import gpgutils # main program |
10
|
|
|
from bbarchivist import utilities # bool parsing |
11
|
|
|
|
12
|
|
|
__author__ = "Thurask" |
13
|
|
|
__license__ = "WTFPL v2" |
14
|
|
|
__copyright__ = "2015-2018 Thurask" |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
def prep_key(key): |
18
|
|
|
""" |
19
|
|
|
Prepare key. |
20
|
|
|
|
21
|
|
|
:param key: Key to use. 8-character hexadecimal, with or without 0x. |
22
|
|
|
:type key: str |
23
|
|
|
""" |
24
|
|
|
if key is None: |
25
|
|
|
key = input("PGP KEY (0x12345678): ") |
26
|
|
|
return key |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
def prep_pass(key, password): |
30
|
|
|
""" |
31
|
|
|
Prepare password. |
32
|
|
|
|
33
|
|
|
:param key: Key to use. 8-character hexadecimal, with or without 0x. |
34
|
|
|
:type key: str |
35
|
|
|
|
36
|
|
|
:param password: Passphrase for given key. |
37
|
|
|
:type password: str |
38
|
|
|
""" |
39
|
|
|
if password is None: |
40
|
|
|
password = getpass.getpass(prompt="PGP PASSPHRASE: ") |
41
|
|
|
write = utilities.i2b("SAVE PASSPHRASE (Y/N)?: ") |
42
|
|
|
password2 = password if write else None |
43
|
|
|
gpgutils.gpg_config_writer(key, password2) |
44
|
|
|
return password |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
def prep_key_pass(): |
48
|
|
|
""" |
49
|
|
|
Prepare key and password. |
50
|
|
|
""" |
51
|
|
|
key, password = gpgutils.gpg_config_loader() |
52
|
|
|
if key is None or password is None: |
53
|
|
|
key = prep_key(key) |
54
|
|
|
password = prep_pass(key, password) |
55
|
|
|
return key, password |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
def gpgrunner_main(): |
59
|
|
|
""" |
60
|
|
|
Parse arguments from argparse/questionnaire. |
61
|
|
|
|
62
|
|
|
Invoke :func:`bbarchivist.gpgutils.gpgrunner` with those arguments. |
63
|
|
|
""" |
64
|
|
|
parser = argutils.default_parser("bb-gpgrunner", "GPG-sign files") |
65
|
|
|
parser.add_argument( |
66
|
|
|
"folder", |
67
|
|
|
help="Working directory, default is local", |
68
|
|
|
nargs="?", |
69
|
|
|
default=None) |
70
|
|
|
parser.add_argument( |
71
|
|
|
"-s", |
72
|
|
|
"--selective", |
73
|
|
|
dest="selective", |
74
|
|
|
help="Filter out files generated by this package", |
75
|
|
|
default=False, |
76
|
|
|
action="store_true") |
77
|
|
|
parser.set_defaults() |
78
|
|
|
args = parser.parse_args(sys.argv[1:]) |
79
|
|
|
args.folder = utilities.dirhandler(args.folder, os.getcwd()) |
80
|
|
|
workfolder = args.folder |
81
|
|
|
key, password = prep_key_pass() |
82
|
|
|
print(" ") |
83
|
|
|
gpgutils.gpgrunner(workfolder, key, password, args.selective) |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
if __name__ == "__main__": |
87
|
|
|
gpgrunner_main() |
88
|
|
|
|