Completed
Push — master ( f5d9f6...12103c )
by John
03:10
created

prep_pass()   A

Complexity

Conditions 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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