Hi,

I have a small app which consist of a few .py files. Is there any way to distribute it in jar like fashion as a single file I can just run python on. I obviously look for platform independent solution.

Thx in advance, A.

"""
Author:  Shane Geiger <[EMAIL PROTECTED]>


Wed Mar 14 21:55:58 CDT 2007

Hi,

I have a small app which consist of a few .py files. Is there any way to
distribute it in jar like fashion as a single file I can just run python
on. I obviously look for platform independent solution.

------

Here's an answer to your question.


To use this script put it in the same directory as your .py files.
See the glob.glob function below if you want to specify a directory if you want to change that.
(I tried to keep this example simple.)


Use the 'pack_mode()' and 'unpack_mode()' function calls at the bottom of the script to
control it.

Note: I have not tested this on Windows.

"""


import base64
import string, os
import zipfile
import glob

def readfile(f): return open(f, "r").read()
def writefile(f, data, perms=750): open(f, "w").write(data) and os.chmod(f, perms)

zip_file_name = 'python_modules.zip'


def write_test_modules():
   ## some test modules
   srgexample="""
print "This was base64 encoded."
   """
   srgexample2="""
print "This, too, was base64 encoded, and it was imported from a zip."
   """
   writefile('srgexample.py',srgexample)
   writefile('srgexample2.py',srgexample2)

def test_mode_cleanup():
   os.remove('srgexample.py')
   os.remove('srgexample2.py')
   os.remove(zip_file_name)


def pack_mode():
   """
   PACKING MODE:
   (you could automate this procedure if it is important to you)
   Zip the source files into one .zip file for easier handling.
   Base64 encode the zip file to avoid problems of including the file.
   Include the base64 representation of the zip file in a docstring, a
      step that you could automate (but I haven't).
   """

   write_test_modules()
# open the zip file for writing, and write stuff to it
   import zipfile
   import glob, os
   # open the zip file for writing, and write stuff to it
   file = zipfile.ZipFile(zip_file_name, "w")

#    for name in glob.glob("*.py"):
for name in [x for x in glob.glob("*.py") if x != __file__]: # dont' try to zip up this file
       file.write(name, os.path.basename(name), zipfile.ZIP_DEFLATED)
   file.close()
   ## Here's how to watch what gets added
   ## open the file again, to see what's in it
   #file = zipfile.ZipFile(zip_file_name, "r")
   #for info in file.infolist():
# print info.filename, info.date_time, info.file_size, info.compress_size

   contents = open(zip_file_name, "r").read()
   file_contents_in_base64 = base64.encodestring(contents)
print "Assign the following base64 encoded string to the 'python_modules_zip_base64' "
   print "  variable (after which you can use unpack mode)."
print file_contents_in_base64
   # THEN MANUALLY ADD THAT to your main file.
   # You could automatically edit the currently running file like this:

   test_mode_cleanup()


def unpack_mode():
   """
   UNPACKING MODE:
   """
   python_modules_zip_base64="""
UEsDBBQAAAAIABq8bjYpPJvJJgAAACYAAAANAAAAc3JnZXhhbXBsZS5weeMqKMrMK1FQCsnILFYo
TyxWSEosTjUzUUjNS85PSU3RU+JSAAIAUEsDBBQAAAAIABq8bjZ5v8x4SAAAAEwAAAAOAAAAc3Jn
ZXhhbXBsZTIucHkdyTEKgDAMBdC9p/h0Dk7iSXqBaCJmaFPagODpFd/6Uh/WArlcNgnhTrh5Yuep
2wpth4sKgZvA4i+r3Ueo4BxewXisLznh8wJQSwECFAMUAAAACAAavG42KTybySYAAAAmAAAADQAA
AAAAAAAAAAAApIEAAAAAc3JnZXhhbXBsZS5weVBLAQIUAxQAAAAIABq8bjZ5v8x4SAAAAEwAAAAO
AAAAAAAAAAAAAACkgVEAAABzcmdleGFtcGxlMi5weVBLBQYAAAAAAgACAHcAAADFAAAAAAA=
"""
   zip_file_contents = base64.decodestring(python_modules_zip_base64)
def writefile(f, data, perms=750): open(f, "w").write(data) and os.chmod(f, perms)
   writefile(zip_file_name,zip_file_contents)

   # open the file, to see what's in it
   #file = zipfile.ZipFile("test.zip", "r")
   #for info in file.infolist():
# print info.filename, info.date_time, info.file_size, info.compress_size

   import sys
   sys.path.insert(0, zip_file_name)  # Add .zip file to front of path
   import srgexample
   import srgexample2

   ### you could even remove the file after using it
   os.remove(zip_file_name)

pack_mode()
#unpack_mode()





--
Shane Geiger
IT Director
National Council on Economic Education
[EMAIL PROTECTED]  |  402-438-8958  |  http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy

begin:vcard
fn:Shane Geiger
n:Geiger;Shane
org:National Council on Economic Education (NCEE)
adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States
email;internet:[EMAIL PROTECTED]
title:IT Director
tel;work:402-438-8958
x-mozilla-html:FALSE
url:http://www.ncee.net
version:2.1
end:vcard

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to