Python examples: backwards compatibility
authorBen McGinnes <ben@adversary.org>
Sat, 15 Sep 2018 02:10:05 +0000 (12:10 +1000)
committerBen McGinnes <ben@adversary.org>
Sat, 15 Sep 2018 02:10:05 +0000 (12:10 +1000)
* lang/python/examples/howto/groups.py: subprocess update
* lang/python/examples/howto/export-secret-keys.py: subprocess update

  Both of these try the nice and easy method of getting the subprocess
  output available in Python 3, but will fall back to the older Popen
  method if it doesn't work.  Essentially this is to be a little nicer
  to Python 2.7.15 (even though the examples are filled with warnings
  that py2 support is not guaranteed with the examples).

lang/python/examples/howto/export-secret-keys.py
lang/python/examples/howto/groups.py

index f0a791e..7203ded 100755 (executable)
@@ -84,7 +84,13 @@ else:
     if os.path.exists(os.environ["GNUPGHOME"]) is True:
         hd = os.environ["GNUPGHOME"]
     else:
-        hd = subprocess.getoutput(gpgconfcmd)
+        try:
+            hd = subprocess.getoutput(gpgconfcmd)
+        except:
+            process = subprocess.Popen(gpgconfcmd.split(),
+                                       stdout=subprocess.PIPE)
+            procom = process.communicate()
+            hd = procom[0].decode().strip()
     gpgfile = "{0}/{1}.gpg".format(hd, keyfile)
     ascfile = "{0}/{1}.asc".format(hd, keyfile)
 
index b8317b6..154961b 100644 (file)
@@ -37,7 +37,12 @@ if sys.platform == "win32":
 else:
     gpgconfcmd = "gpgconf --list-options gpg"
 
-lines = subprocess.getoutput(gpgconfcmd).splitlines()
+try:
+    lines = subprocess.getoutput(gpgconfcmd).splitlines()
+except:
+    process = subprocess.Popen(gpgconfcmd.split(), stdout=subprocess.PIPE)
+    procom = process.communicate()
+    lines = procom[0].decode().splitlines()
 
 for i in range(len(lines)):
     if lines[i].startswith("group") is True: