Skip to contentSkip to conversation

compressed import v3

PATCH In review Clark Williams 2 participants 5 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 compressed import v3

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Karl/Catalin, et al,

Attached is my latest stab at StGit importing patches from compressed files. This version doesn't try to differentiate by the file extension; it just tries to open gzip or bz2 files and if those fail it reverts to text. I'm not completely happy with it, but I've spent about as much time on it as I can afford to (for this week anyway).

Yeah, yeah, Karl, there are four new tests in t1800-import. Let me know if you think there should be more (more for compressed input that is; I'm not crazy enough to sign up to write more tests for *everything*). :)

Clark
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkhRlgIACgkQqA4JVb61b9d2ygCfTUPc5I9eXM4947VTrTZ+mO0H +vIAoJJACG94TdnyUIac73lB4UYCZVlG
=sa3t
-----END PGP SIGNATURE-----

Patch to allow import from compressed files (gzip and bzip2)

From: Clark Williams <[email protected]>

---

 stgit/commands/imprt.py |   42 ++++++++++++++++++++++++++++++++++++------
 t/t1800-import.sh       |   42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+), 6 deletions(-)
stgit/commands/imprt.py +36 -6
index 4a4b792..fc5cdce 100644
@@ -175,14 +175,49 @@ def __create_patch(filename, message, author_name, author_email,
                                  backup = False)
         out.done()
 
+def __mkpatchname(name, suffix):
+    if name.lower().endswith(suffix.lower()):
+        return name[:-len(suffix)]
+    return name
+
+def __gethandleandname(filename):
+    """return a file handle and a patch name derived from filename
+    """
+    # see if it's a gzip'ed patch
+    try:
+        import gzip
+        f = gzip.open(filename)
+        f.read(1)
+        f.seek(0)
+        return (f, __mkpatchname(filename, '.gz'))
+    except IOError, e:
+            pass
+    # see if it's a bzip2'ed patch
+    try:
+        import bz2
+        f = bz2.BZ2File(filename)
+        f.read(1)
+        f.seek(0)
+        return (f, __mkpatchname(filename, '.bz2'))
+    except IOError, e:
+            pass
+    # plain old file...
+    return (open(filename), filename)
+
 def __import_file(filename, options, patch = None):
     """Import a patch from a file or standard input
     """
+    pname = None
     if filename:
-        f = file(filename)
+        (f, pname) = __gethandleandname(filename)
     else:
         f = sys.stdin
 
+    if patch:
+        pname = patch
+    elif not pname:
+        pname = filename
+
     if options.mail:
         try:
             msg = email.message_from_file(f)
@@ -197,11 +232,6 @@ def __import_file(filename, options, patch = None):
     if filename:
         f.close()
 
-    if patch:
-        pname = patch
-    else:
-        pname = filename
-
     __create_patch(pname, message, author_name, author_email,
                    author_date, diff, options)
 
t/t1800-import.sh +42 -0

46 lines not on this page · raw message

#1 lore raw
Quoted 4 lines · Clark Williams

Attached is my latest stab at StGit importing patches from compressed files. This version doesn't try to differentiate by the file extension; it just tries to open gzip or bz2 files and if those fail it reverts to text.

Very good!

Quoted 2 lines

I'm not completely happy with it, but I've spent about as much time on it as I can afford to (for this week anyway).

Heh. I see an opportunity for condensing the code a bit in __gethandleandname(), and you intented the "pass"es funny, but I'll take care of it (or not, seeing as they're kind of minor nits -- in any case, I'll just take the patch off your hands and stop tormenting you about it).

Quoted 4 lines

Yeah, yeah, Karl, there are four new tests in t1800-import. Let me know if you think there should be more (more for compressed input that is; I'm not crazy enough to sign up to write more tests for *everything*). :)

No, this is exactly what I was aiming for.

Bleed 'em dry, but _no more_! ;-)

Signature · 2 lines
Karl Hasselström, [email protected]
      www.treskal.com/kalle
#2 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 9 lines · Karl Hasselström
Quoted 4 lines · Clark Williams

Attached is my latest stab at StGit importing patches from compressed files. This version doesn't try to differentiate by the file extension; it just tries to open gzip or bz2 files and if those fail it reverts to text.

Very good!

Quoted 9 lines
Quoted 2 lines

I'm not completely happy with it, but I've spent about as much time on it as I can afford to (for this week anyway).

Heh. I see an opportunity for condensing the code a bit in __gethandleandname(), and you intented the "pass"es funny, but I'll take care of it (or not, seeing as they're kind of minor nits -- in any case, I'll just take the patch off your hands and stop tormenting you about it).

Ugh, I missed that on the pass'es. I had some other logic in there, attempting to make sure that the IOError was in fact because the input was uncompressed, but I punted that and forgot to unindent the pass. I can't believe emacs didn't catch that for me :)

I'm interested to hear how you'd condense the code in __gethandleandname().

Clark

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkhSif0ACgkQqA4JVb61b9dA1gCeMVvcY4GobZLp+k7qskLVgozN vsAAmQFoTxf2xxohyzCnCrRQRs1Gdf+O
=lBEr
-----END PGP SIGNATURE-----

#3 lore raw
Quoted 2 lines · Clark Williams

I'm interested to hear how you'd condense the code in __gethandleandname().

Like this, for example:

import bz2, gzip
for copen, ext in [(gzip.open, '.gz'), (bz2.BZ2File, '.bz2')]:
try:
f = copen(filename)
f.read(1)
f.seek(0)
return (f, __mkpatchname(filename, ext))
except IOError, e:
pass

If you don't mind, I'll just fix that up directly in your patch. (I also took the liberty to rename the function to __get_handle_and_name, since that's more consistent with other functions in that file.)

Signature · 2 lines
Karl Hasselström, [email protected]
      www.treskal.com/kalle
#4 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 17 lines · Karl Hasselström
Quoted 2 lines · Clark Williams

I'm interested to hear how you'd condense the code in __gethandleandname().

Like this, for example:

import bz2, gzip
for copen, ext in [(gzip.open, '.gz'), (bz2.BZ2File, '.bz2')]:
try:
f = copen(filename)
f.read(1)
f.seek(0)
return (f, __mkpatchname(filename, ext))
except IOError, e:
pass

Quoted 4 lines

If you don't mind, I'll just fix that up directly in your patch. (I also took the liberty to rename the function to __get_handle_and_name, since that's more consistent with other functions in that file.)

Ah, that's very nice. Your changes work for me.

Clark
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iEYEARECAAYFAkhT6G8ACgkQqA4JVb61b9cCeQCfedD0BjOeRa3sbGvkRNe2BPMq 6roAoJE7klMbgymRCJ3B+pEjzgnTWO2l
=uZ+n
-----END PGP SIGNATURE-----