- name = input_string("What is your name? (Max Mustermann) ")
- email = input_string("What is your email address? (max@example.de) ")
- motto = input_string("What is your motto phrase, if any? (Schlüssel für 2014) ")
+ with tempfile.NamedTemporaryFile () as tmp:
+ batch_key = dedented ("""
+ %ask-passphrase
+ Key-Type: DSA
+ Key-Length: 2048
+ Subkey-Type: ELG-E
+ Subkey-Length: 2048
+ Name-Real: {}
+ Name-Email: {}
+ Expire-Date: 0
+ """).format (name, email)
+
+ if comment != "":
+ batch_key += "\nName-Comment: {}\n".format (comment)
+
+ tmp.write (batch_key)
+ tmp.flush ()
+
+ batch_env = dict(os.environ)
+ del batch_env["DISPLAY"]
+
+ gnupg_process = subprocess.Popen (["gpg2", "--homedir", gnupg_home, "--batch", "--gen-key", tmp.name],
+ env = batch_env)
+ gnupg_process.wait ()
+
+ if gnupg_process.returncode != 0:
+ raise Exception ("Couldn't create GnuPG key.")
+
+
+def ensure_directories (path, mode = 0o777):
+ try:
+ os.makedirs (path, mode)
+ except OSError as exception:
+ if exception.errno != errno.EEXIST:
+ raise