468,505 Members | 1,561 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,505 developers. It's quick & easy.

Embedding a binary file in a python script

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 (mr************@hotmail.com)

Feb 15 '06 #1
8 6158
mrstephengross:
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?


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

--
René Pijlman
Feb 15 '06 #2
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 (mr************@hotmail.com)

Feb 15 '06 #3
"mrstephengross" wrote:
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?


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
import base64, sys
base64.encode(open("archive.tar", "rb"), sys.stdout) H4sIAPlc80MAA+3TwQqDMAzG8Z73FH2CkWqbPs/GFA+FguvYHn8qQ3aaJ3XC/3cJJJeQ8HVNSvlc
XsWsRwbq/VhdDPJdP9Q4qaPXoFIPfVdVTo2VFXeaPe7l0ltrmra95h9XWJo fVDf+/7T3FtjLM/fp
9lf5D1P+fST/W5j+T/4BAAAAAAAAAAAAAAAO6w1uw+KBACgAAA== ^D
$ python import base64, cStringIO, tarfile
f = cStringIO.StringIO(base64.decodestring(""" .... H4sIAPlc80MAA+3TwQqDMAzG8Z73FH2CkWqbPs/GFA+FguvYHn8qQ3aaJ3XC/3cJJJeQ8HVNSvlc
.... XsWsRwbq/VhdDPJdP9Q4qaPXoFIPfVdVTo2VFXeaPe7l0ltrmra95h9XWJo fVDf+/7T3FtjLM/fp
.... 9lf5D1P+fST/W5j+T/4BAAAAAAAAAAAAAAAO6w1uw+KBACgAAA==
.... """)) f <cStringIO.StringI object at 0xb7de85f0> tar = tarfile.TarFile.gzopen("dummy", fileobj=f)
tar.list() -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 tar.extractfile("hello.txt").read()

'hello\n'

hope this helps!

</F>

Feb 15 '06 #4
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 (mr************@hotmail.com)

Feb 15 '06 #5
mrstephengross wrote:
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?


(c)?StringIO

Diez
Feb 15 '06 #6
mrstephengross:
Ok, this looks really cool, but can you explain a little more
step-by-step what's going on?


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

--
René Pijlman
Feb 15 '06 #7
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...

Feb 15 '06 #8
Hi,

Le 15-02-2006, mrstephengross <mr************@hotmail.com> a écrit*:
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?


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
ma****@grossac.org
http://grossac.org
Feb 21 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Robey Holderith | last post: by
4 posts views Thread by Alicia Haumann | last post: by
1 post views Thread by amit | last post: by
1 post views Thread by Tommy Nordgren | last post: by
reply views Thread by DevEng | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.