Blame lang/python/examples/howto/revoke-userid.py

Packit Service 0ef63b
#!/usr/bin/env python3
Packit Service 0ef63b
# -*- coding: utf-8 -*-
Packit Service 0ef63b
Packit Service 0ef63b
from __future__ import absolute_import, division, unicode_literals
Packit Service 0ef63b
Packit Service 0ef63b
# Copyright (C) 2018 Ben McGinnes <ben@gnupg.org>
Packit Service 0ef63b
#
Packit Service 0ef63b
# This program is free software; you can redistribute it and/or modify it under
Packit Service 0ef63b
# the terms of the GNU General Public License as published by the Free Software
Packit Service 0ef63b
# Foundation; either version 2 of the License, or (at your option) any later
Packit Service 0ef63b
# version.
Packit Service 0ef63b
#
Packit Service 0ef63b
# This program is free software; you can redistribute it and/or modify it under
Packit Service 0ef63b
# the terms of the GNU Lesser General Public License as published by the Free
Packit Service 0ef63b
# Software Foundation; either version 2.1 of the License, or (at your option)
Packit Service 0ef63b
# any later version.
Packit Service 0ef63b
#
Packit Service 0ef63b
# This program is distributed in the hope that it will be useful, but WITHOUT
Packit Service 0ef63b
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
Packit Service 0ef63b
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License and the GNU
Packit Service 0ef63b
# Lesser General Public License for more details.
Packit Service 0ef63b
#
Packit Service 0ef63b
# You should have received a copy of the GNU General Public License and the GNU
Packit Service 0ef63b
# Lesser General Public along with this program; if not, see
Packit Service 0ef63b
# <https://www.gnu.org/licenses/>.
Packit Service 0ef63b
Packit Service 0ef63b
import gpg
Packit Service 0ef63b
import os.path
Packit Service 0ef63b
Packit Service 0ef63b
print("""
Packit Service 0ef63b
This script revokes a user ID on an existing key.
Packit Service 0ef63b
Packit Service 0ef63b
The gpg-agent and pinentry are invoked to enter the passphrase.
Packit Service 0ef63b
""")
Packit Service 0ef63b
Packit Service 0ef63b
c = gpg.Context()
Packit Service 0ef63b
Packit Service 0ef63b
homedir = input("Enter the GPG configuration directory path (optional): ")
Packit Service 0ef63b
fpr0 = input("Enter the fingerprint of the key to modify: ")
Packit Service 0ef63b
uid_name = input("Enter the name of the user ID: ")
Packit Service 0ef63b
uid_email = input("Enter the email address of the user ID: ")
Packit Service 0ef63b
uid_cmnt = input("Enter a comment to include (optional): ")
Packit Service 0ef63b
Packit Service 0ef63b
if homedir.startswith("~"):
Packit Service 0ef63b
    if os.path.exists(os.path.expanduser(homedir)) is True:
Packit Service 0ef63b
        c.home_dir = os.path.expanduser(homedir)
Packit Service 0ef63b
    else:
Packit Service 0ef63b
        pass
Packit Service 0ef63b
elif os.path.exists(homedir) is True:
Packit Service 0ef63b
    c.home_dir = homedir
Packit Service 0ef63b
else:
Packit Service 0ef63b
    pass
Packit Service 0ef63b
Packit Service 0ef63b
fpr = "".join(fpr0.split())
Packit Service 0ef63b
Packit Service 0ef63b
if uid_cmnt:
Packit Service 0ef63b
    userid = "{0} ({1}) <{2}>".format(uid_name, uid_cmnt, uid_email)
Packit Service 0ef63b
else:
Packit Service 0ef63b
    userid = "{0} <{2}>".format(uid_name, uid_email)
Packit Service 0ef63b
Packit Service 0ef63b
key = c.get_key(fpr, secret=True)
Packit Service 0ef63b
c.key_revoke_uid(key, userid)