473,509 Members | 3,095 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7401
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2499
by: jordi | last post by:
Hi, I'm starting to use Python embedded in a C program. I'm using Python to execute several scripts using as a variables information retrieved for several multithread "agents" written in C. ...
0
1830
by: Atul Kshirsagar | last post by:
I am embedding python in my C++ application. I am using Python *2.3.2* with a C++ extention DLL in multi-threaded environment. I am using SWIG-1.3.19 to generate C++ to Python interface. Now to...
23
2926
by: Robey Holderith | last post by:
Anyone know a good way to embed python within python? Now before you tell me that's silly, let me explain what I'd like to do. I'd like to allow user-defined scriptable objects. I'd like to...
4
2760
by: Alicia Haumann | last post by:
I accidentally sent this to webmaster@python.org, so this could be a duplicate if "webmaster" forwards it to this list. :{ Hi, there. Thanks for any help that can be offered. I've been...
1
3162
by: Craig Ringer | last post by:
Hi folks I'm a bit of a newbie here, though I've tried to appropriately research this issue before posting. I've found a lot of questions, a few answers that don't really answer quite what I'm...
1
1898
by: amit | last post by:
Hello, I am currently studying how to embedd python. I am developing a graphical C++ application. My goal is to embedd python script that will control some kind of animation. I have some...
1
1813
by: Tommy Nordgren | last post by:
I want to write an application that embeds and extends (at least) the Python and Perl interpreters. Now i want to find as much as possible about the Python tools used for extending and embedding...
0
1165
by: Fozzie | last post by:
Hi, I have a problem which is quite circular, and hopefully either someone has encountered something similar or has a reason why this will not work. We have a COM library providing...
0
1337
by: DevEng | last post by:
Hi all, I am new to Python and trying to embed it into a c/c++ application. I started with examples from the documentation pages and go to the Pure Embedding example...
0
7234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4730
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3216
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.