Connecting Tech Pros Worldwide Help | Site Map

Embedding a binary file in a python script

mrstephengross
Guest
 
Posts: n/a
#1: Feb 15 '06
I want to find a way to embed a tar file *in* my python script, and
then use the tarfile module to extract it. That is, instead of
distributing two files (extractor.py and archive.tar) I want to be able
to distribute *one* file (extractor-with-embedded-archive.py). Is there
a way to do this?

Thanks,
--Steve (mrstephengross@hotmail.com)

Rene Pijlman
Guest
 
Posts: n/a
#2: Feb 15 '06

re: Embedding a binary file in a python script


mrstephengross:[color=blue]
>I want to find a way to embed a tar file *in* my python script, and
>then use the tarfile module to extract it. That is, instead of
>distributing two files (extractor.py and archive.tar) I want to be able
>to distribute *one* file (extractor-with-embedded-archive.py). Is there
>a way to do this?[/color]

I guess you can uuencode your tar file, and put the result in a multiline
string. See module 'uu'.

--
René Pijlman
mrstephengross
Guest
 
Posts: n/a
#3: Feb 15 '06

re: Embedding a binary file in a python script


Ok, this is a neat idea... The uu module deals with files though, not
strings. Is there a way in python to make a string act like a file
handle?

Example:

my_string = "uu-encoded-stuf......"
my_out_file_handle = ?? # What should this variable look like?
import uu
uu.decode(my_string, my_out_file_handle)
import tarfile
tarfile.open(my_out_file_handle)

Thanks again,
--Steve (mrstephengross@hotmail.com)

Fredrik Lundh
Guest
 
Posts: n/a
#4: Feb 15 '06

re: Embedding a binary file in a python script


"mrstephengross" wrote:
[color=blue]
> I want to find a way to embed a tar file *in* my python script, and
> then use the tarfile module to extract it. That is, instead of
> distributing two files (extractor.py and archive.tar) I want to be able
> to distribute *one* file (extractor-with-embedded-archive.py). Is there
> a way to do this?[/color]

I'm not sure I understand why you think you need to embed a tarfile
(why not just embed the actual files?), but here's an outline:

$ echo hello >hello.txt
$ echo world >world.txt
$ tar cvfz archive.tar hello.txt world.txt
hello.txt
world.txt
$ ls -l archive.tar
-rw-r--r-- 1 effbot effbot 151 2006-02-15 17:52 archive.tar

$ python[color=blue][color=green][color=darkred]
>>> import base64, sys
>>> base64.encode(open("archive.tar", "rb"), sys.stdout)[/color][/color][/color]
H4sIAPlc80MAA+3TwQqDMAzG8Z73FH2CkWqbPs/GFA+FguvYHn8qQ3aaJ3XC/3cJJJeQ8HVNSvlc
XsWsRwbq/VhdDPJdP9Q4qaPXoFIPfVdVTo2VFXeaPe7l0ltrmra95h9XWJo fVDf+/7T3FtjLM/fp
9lf5D1P+fST/W5j+T/4BAAAAAAAAAAAAAAAO6w1uw+KBACgAAA==[color=blue][color=green][color=darkred]
>>> ^D[/color][/color][/color]

$ python[color=blue][color=green][color=darkred]
>>> import base64, cStringIO, tarfile
>>> f = cStringIO.StringIO(base64.decodestring("""[/color][/color][/color]
.... H4sIAPlc80MAA+3TwQqDMAzG8Z73FH2CkWqbPs/GFA+FguvYHn8qQ3aaJ3XC/3cJJJeQ8HVNSvlc
.... XsWsRwbq/VhdDPJdP9Q4qaPXoFIPfVdVTo2VFXeaPe7l0ltrmra95h9XWJo fVDf+/7T3FtjLM/fp
.... 9lf5D1P+fST/W5j+T/4BAAAAAAAAAAAAAAAO6w1uw+KBACgAAA==
.... """))[color=blue][color=green][color=darkred]
>>> f[/color][/color][/color]
<cStringIO.StringI object at 0xb7de85f0>[color=blue][color=green][color=darkred]
>>> tar = tarfile.TarFile.gzopen("dummy", fileobj=f)
>>> tar.list()[/color][/color][/color]
-rw-r--r-- effbot/effbot 6 2006-02-15 17:51:36 hello.txt
-rw-r--r-- effbot/effbot 6 2006-02-15 17:51:41 world.txt[color=blue][color=green][color=darkred]
>>> tar.extractfile("hello.txt").read()[/color][/color][/color]
'hello\n'

hope this helps!

</F>



mrstephengross
Guest
 
Posts: n/a
#5: Feb 15 '06

re: Embedding a binary file in a python script


Ok, this looks really cool, but can you explain a little more
step-by-step what's going on? In the end, I need to have a single
python script that (1) contains the archive and (2) can extract that
archive. The example you've given is interesting, but it's not clear to
me how to create the actual python script to do it all.

Thanks again,
--Steve (mrstephengross@hotmail.com)

Diez B. Roggisch
Guest
 
Posts: n/a
#6: Feb 15 '06

re: Embedding a binary file in a python script


mrstephengross wrote:
[color=blue]
> Ok, this is a neat idea... The uu module deals with files though, not
> strings. Is there a way in python to make a string act like a file
> handle?[/color]

(c)?StringIO

Diez
Rene Pijlman
Guest
 
Posts: n/a
#7: Feb 15 '06

re: Embedding a binary file in a python script


mrstephengross:[color=blue]
>Ok, this looks really cool, but can you explain a little more
>step-by-step what's going on?[/color]

What happened to "Hey thanks, I'll look into that" :-(

--
René Pijlman
mrstephengross
Guest
 
Posts: n/a
#8: Feb 15 '06

re: Embedding a binary file in a python script


Good point.... I appreciate all the help, and apologize if I came
across badly. I'm definitely willing to put in the work to understand
all this, it's just that it's a lot of new modules for me and I'm a bit
overwhelmed. Sorry if I seemed impatient...

Florent Manens
Guest
 
Posts: n/a
#9: Feb 21 '06

re: Embedding a binary file in a python script


Hi,

Le 15-02-2006, mrstephengross <mrstephengross@hotmail.com> a écrit*:[color=blue]
> I want to find a way to embed a tar file *in* my python script, and
> then use the tarfile module to extract it. That is, instead of
> distributing two files (extractor.py and archive.tar) I want to be able
> to distribute *one* file (extractor-with-embedded-archive.py). Is there
> a way to do this?[/color]

It's exactly the goal of my script here (sorry for the blog post):
http://grossac.org/index.php/2006/01...ee-avec-py2exe
and here :
http://grossac.org/index.php/2006/02...on-et-remarque
It's in French, I can help if you need.

Take a look at Fredrik Lundh answer, it is very similar.

code sample :

In [6]:f = file("text.txt", "rb")
In [7]:buff = f.read()
In [8]:print "data = %s%s%s" % ('"""', buff.encode("zlib").encode("base64"), '"""')
data = """eJxzys/Lyi8tUgQADecDAQ==
"""

You get a compressed, base64 encoded string, then add data = """..."""
to your source code.

In [9]:data = """eJxzys/Lyi8tUgQADecDAQ==
.9.:"""
In [10]:data.decode("base64").decode("zlib")
Out[10]:'Bonjour!'

If you want to create a .exe with files included, take a look at PyInstaller.

Hope this helps.

--
Florent Manens
manens@grossac.org
http://grossac.org
Closed Thread