1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4from __future__ import absolute_import, division, unicode_literals 5 6import gpg 7import os.path 8import sys 9 10# Copyright (C) 2018 Ben McGinnes <ben@gnupg.org> 11# 12# This program is free software; you can redistribute it and/or modify it under 13# the terms of the GNU General Public License as published by the Free Software 14# Foundation; either version 2 of the License, or (at your option) any later 15# version. 16# 17# This program is free software; you can redistribute it and/or modify it under 18# the terms of the GNU Lesser General Public License as published by the Free 19# Software Foundation; either version 2.1 of the License, or (at your option) 20# any later version. 21# 22# This program is distributed in the hope that it will be useful, but WITHOUT 23# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 24# FOR A PARTICULAR PURPOSE. See the GNU General Public License and the GNU 25# Lesser General Public License for more details. 26# 27# You should have received a copy of the GNU General Public License and the GNU 28# Lesser General Public along with this program; if not, see 29# <https://www.gnu.org/licenses/>. 30 31print(""" 32This script exports one or more public keys in minimised form. 33""") 34 35c = gpg.Context(armor=True) 36 37if len(sys.argv) >= 4: 38 keyfile = sys.argv[1] 39 logrus = sys.argv[2] 40 homedir = sys.argv[3] 41elif len(sys.argv) == 3: 42 keyfile = sys.argv[1] 43 logrus = sys.argv[2] 44 homedir = input("Enter the GPG configuration directory path (optional): ") 45elif len(sys.argv) == 2: 46 keyfile = sys.argv[1] 47 logrus = input("Enter the UID matching the key(s) to export: ") 48 homedir = input("Enter the GPG configuration directory path (optional): ") 49else: 50 keyfile = input("Enter the path and filename to save the key(s) to: ") 51 logrus = input("Enter the UID matching the key(s) to export: ") 52 homedir = input("Enter the GPG configuration directory path (optional): ") 53 54if len(homedir) == 0: 55 homedir = None 56elif homedir.startswith("~"): 57 userdir = os.path.expanduser(homedir) 58 if os.path.exists(userdir) is True: 59 homedir = os.path.realpath(userdir) 60 else: 61 homedir = None 62else: 63 homedir = os.path.realpath(homedir) 64 65if homedir is not None and os.path.exists(homedir) is False: 66 homedir = None 67elif homedir is not None and os.path.exists(homedir) is True: 68 if os.path.isdir(homedir) is False: 69 homedir = None 70 else: 71 pass 72 73if homedir is not None: 74 c.home_dir = homedir 75else: 76 pass 77 78try: 79 result = c.key_export_minimal(pattern=logrus) 80except: 81 result = c.key_export_minimal(pattern=None) 82 83if result is not None: 84 with open(keyfile, "wb") as f: 85 f.write(result) 86else: 87 pass 88