home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pippy-0.6beta-src.tar.gz / pippy-0.6beta-src.tar / pippy-0.6beta-src / src / Palm / Tools / palm_pdb.py
Text File  |  2000-12-21  |  3KB  |  125 lines

  1. """This module contains utilities for writing compiled python to
  2. to a palm database."""
  3.  
  4.  
  5. import marshal
  6. import os
  7. import imp
  8. import tempfile
  9. import struct
  10.  
  11. DEFAULT_CID = 'PyMd'
  12. RESOURCE_BASE = 0x3e8
  13. RESOURCE_INCR = 10
  14.  
  15.  
  16. class frozenRecord:
  17.     def __init__(self, name, code, size):
  18.         self.name = name
  19.         self.code = code
  20.         self.size = size
  21.  
  22.  
  23. def build_struct(module_filename, name=None):
  24.     """Build the following structure:
  25.  
  26.     typedef struct _frozen_record {
  27.         int size;
  28.         char *name;
  29.         unsigned char *code;
  30.     } frozen_record;
  31.  
  32.     """
  33.     fh = open(module_filename,'rb')
  34.     
  35.     if imp.get_magic() != fh.read(4):
  36.         raise IOError, "File " + module_filename + " is not a valid pyc file"
  37.  
  38.     fh.seek(0)
  39.     code = fh.read()
  40.     code = code[8:]  # skip first 8 bytes
  41.     length = len(code)
  42.     n, ext = os.path.splitext(os.path.basename(module_filename))
  43.     if not name:
  44.         name = n
  45.     
  46.     return frozenRecord( name, code, length )
  47.  
  48.  
  49. def convert_file(module_filename, name=None, baseAddress=RESOURCE_BASE):
  50.     """Convert a prc file to the desired palm database representation."""
  51.     global creatorID
  52.     print "converting", module_filename
  53.  
  54.     s = build_struct(module_filename, name)
  55.  
  56.     localfile, ext = os.path.splitext(os.path.basename(module_filename))
  57.     localfile = creatorID + ("0000%x" % baseAddress)[-4:] + '.bin'
  58.     fh = open(localfile, 'wb')
  59.     fh.write(struct.pack(">i", s.size) )
  60.     fh.write(s.name)
  61.     fh.write('\0')
  62.     fh.write(s.code)
  63.     fh.close()
  64.  
  65. def compiled_filename(filename):
  66.     n, ext = os.path.splitext(filename)
  67.     if ext == '.py':
  68.         print "compiling", filename
  69.         from py_compile import compile
  70.         compile(filename)
  71.         filename = filename + 'c'
  72.     return filename
  73.  
  74.  
  75. def run(module_filenames, main=None, baseAddress=RESOURCE_BASE):
  76.     import types
  77.     if type(module_filenames) not in (types.TupleType, types.ListType):
  78.         module_filenames = [module_filenames]
  79.  
  80.     if main:
  81.         cname = compiled_filename(module_filenames[0])
  82.         convert_file(cname, '__main__', baseAddress)
  83.         del module_filenames[0]
  84.         baseAddress = baseAddress + RESOURCE_INCR
  85.     
  86.     for name in module_filenames:
  87.         cname = compiled_filename(name)
  88.         convert_file(cname, baseAddress=baseAddress)
  89.         baseAddress = baseAddress + RESOURCE_INCR
  90.         
  91.  
  92. if __name__ == '__main__':
  93.     ## Need to add option for obtaining the name of the module
  94.     ## - particularly for __main__ - maybe just add a flag --main?
  95.     ##
  96.     import sys
  97.     import getopt
  98.     optlist, files = getopt.getopt(sys.argv[1:], 'x',
  99.                                   ['main', 'cid=', 'base_address='])
  100.  
  101.     creatorID = DEFAULT_CID
  102.     main = None
  103.     baseAddress = RESOURCE_BASE
  104.     for opt, val in optlist:
  105.         if opt == '--main':
  106.             main = '__main__'
  107.         if opt == '--cid':
  108.             creatorID = val
  109.             if len(val) > 4:
  110.                 raise ValueError, "Creator ID must be <= 4 bytes"
  111.         if opt == '--base_address':
  112.             baseAddress = val
  113.  
  114.     run(files, main, baseAddress)
  115.     
  116.     
  117. # NOTES:
  118.  
  119. #  create a pdb database:
  120. #
  121. #  the following only works for .prc files!!!  pdb files have a
  122. #  different format
  123. #  build-prc -t dATA pymods2.pdb 'Python Modules-PyMd' PyMd *bin
  124. #
  125.