In order do avoid the need for temporary files, add a new xpak_mem() function that...
authorZac Medico <zmedico@gentoo.org>
Sun, 19 Feb 2006 20:19:41 +0000 (20:19 -0000)
committerZac Medico <zmedico@gentoo.org>
Sun, 19 Feb 2006 20:19:41 +0000 (20:19 -0000)
svn path=/main/trunk/; revision=2747

pym/xpak.py

index 5cb2d61ab7af813320700808570d6071fecbac0c..56c2f4b07a9ca94c4d0470911499e38ec4c9e1a9 100644 (file)
@@ -66,34 +66,39 @@ def xpak(rootdir,outfile=None):
 
        addtolist(mylist,"")
        mylist.sort()
+       mydata = {}
+       for x in mylist:
+               a = open(x, "r")
+               mydata[x] = a.read()
+               a.close()
+       os.chdir(origdir)
 
-       #Our list index has been created
-       
+       xpak_segment = xpak_mem(mydata)
+       if outfile:
+               outf = open(outfile, "w")
+               outf.write(xpak_segment)
+               outf.close()
+       else:
+               return xpak_segment
+
+def xpak_mem(mydata):
+       """Create an xpack segement from a map object."""
        indexglob=""
        indexpos=0
        dataglob=""
        datapos=0
-       for x in mylist:
-               a=open(x,"r")
-               newglob=a.read()
-               a.close()
+       for x, newglob in mydata.iteritems():
                mydatasize=len(newglob)
                indexglob=indexglob+encodeint(len(x))+x+encodeint(datapos)+encodeint(mydatasize)
                indexpos=indexpos+4+len(x)+4+4
                dataglob=dataglob+newglob
                datapos=datapos+mydatasize
-       os.chdir(origdir)
-       if outfile:
-               outf=open(outfile,"w")
-               outf.write("XPAKPACK"+encodeint(len(indexglob))+encodeint(len(dataglob)))
-               outf.write(indexglob)
-               outf.write(dataglob)
-               outf.write("XPAKSTOP")
-               outf.close()
-       else:
-               myret="XPAKPACK"+encodeint(len(indexglob))+encodeint(len(dataglob))
-               myret=myret+indexglob+dataglob+"XPAKSTOP"
-               return myret
+       return "XPAKPACK" \
+       + encodeint(len(indexglob)) \
+       + encodeint(len(dataglob)) \
+       + indexglob \
+       + dataglob \
+       + "XPAKSTOP"
 
 def xsplit(infile):
        """(infile) -- Splits the infile into two files.