Add packets_from_bytes to help parse multi-packet streams
authorW. Trevor King <wking@tremily.us>
Thu, 19 Dec 2013 04:45:21 +0000 (20:45 -0800)
committerW. Trevor King <wking@tremily.us>
Fri, 20 Dec 2013 05:20:49 +0000 (21:20 -0800)
gpg-migrate.py

index 00e672c8a5fce7d0bc92743e3c281861508d330d..d227be71f8b03fd095236d8e92d7e346e80812c7 100755 (executable)
@@ -58,6 +58,14 @@ class PGPPacket (dict):
         pass
 
 
+def packets_from_bytes(data):
+    offset = 0
+    while offset < len(data):
+        packet = PGPPacket()
+        offset += packet.from_bytes(data=data[offset:])
+        yield packet
+
+
 def migrate(old_key, new_key):
     """Add the old key and sub-keys to the new key
 
@@ -67,16 +75,16 @@ def migrate(old_key, new_key):
     """
     old_key_export = _get_stdout(
         ['gpg', '--export', old_key])
-    old_key_packet = PGPPacket()
-    old_key_packet.from_bytes(data=old_key_export)
+    old_key_packets = list(
+        packets_from_bytes(data=old_key_export))
     old_key_secret_export = _get_stdout(
         ['gpg', '--export-secret-keys', old_key])
-    old_key_secret_packet = PGPPacket()
-    old_key_secret_packet.from_bytes(data=old_key_secret_export)
+    old_key_secret_packets = list(
+        packets_from_bytes(data=old_key_secret_export))
 
     import pprint
-    pprint.pprint(old_key_packet)
-    pprint.pprint(old_key_secret_packet)
+    pprint.pprint(old_key_packets)
+    pprint.pprint(old_key_secret_packets)
 
 
 if __name__ == '__main__':