home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Mac / scripts / BuildApplication.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  3.1 KB  |  146 lines

  1. """Create a standalone application from a Python script.
  2.  
  3. This puts up a dialog asking for a Python source file ('TEXT').
  4. The output is a file with the same name but its ".py" suffix dropped.
  5. It is created by copying an applet template, all used shared libs and 
  6. then adding 'PYC ' resources containing compiled versions of all used 
  7. modules wirrten in Python and the main script itself, as __main__.
  8. """
  9.  
  10.  
  11. import sys
  12.  
  13. import string
  14. import os
  15. import macfs
  16. import MacOS
  17. import Res
  18. import Dlg
  19. import EasyDialogs
  20. import buildtools
  21.  
  22. # Hmmm...
  23. MACFREEZEPATH = os.path.join(sys.prefix, ":Mac:Tools:macfreeze")
  24. if MACFREEZEPATH not in sys.path:
  25.     sys.path.append(MACFREEZEPATH)
  26.  
  27. import macmodulefinder
  28. import macgen_bin
  29.  
  30. # dialog, items
  31. DLG_ID = 400
  32. OK_BUTTON = 1
  33. CANCEL_BUTTON = 2
  34. GENFAT_BUTTON = 4
  35. GENPPC_BUTTON = 5
  36. GEN68K_BUTTON = 6
  37.  
  38.  
  39. try:
  40.     Res.GetResource('DITL', DLG_ID)
  41. except Res.Error:
  42.     Res.OpenResFile("BuildApplication.rsrc")
  43. else:
  44.     pass # we're an applet
  45.  
  46.  
  47. def main():
  48.     try:
  49.         buildapplication()
  50.     except buildtools.BuildError, detail:
  51.         EasyDialogs.Message(detail)
  52.  
  53.  
  54. def buildapplication(debug = 0):
  55.     buildtools.DEBUG = debug
  56.     
  57.     # Ask for source text if not specified in sys.argv[1:]
  58.     
  59.     if not sys.argv[1:]:
  60.         srcfss, ok = macfs.PromptGetFile('Select Python source:', 'TEXT')
  61.         if not ok:
  62.             return
  63.         filename = srcfss.as_pathname()
  64.     else:
  65.         if sys.argv[2:]:
  66.             raise buildtools.BuildError, "please select one file at a time"
  67.         filename = sys.argv[1]
  68.     tp, tf = os.path.split(filename)
  69.     
  70.     # interact with user
  71.     architecture, ok = interact(tf)
  72.     if not ok:
  73.         return
  74.     if tf[-3:] == '.py':
  75.         tf = tf[:-3]
  76.         logfile = tf[:-3] + '.log'
  77.     else:
  78.         tf = tf + '.app'
  79.         logfile = tf + '.log'
  80.     
  81.     dstfss, ok = macfs.StandardPutFile('Save application as:', tf)
  82.     if not ok:
  83.         return
  84.     dstfilename = dstfss.as_pathname()
  85.     
  86.     macgen_bin.generate(filename, dstfilename, None, architecture, 1)
  87.  
  88.  
  89. class radio:
  90.     
  91.     def __init__(self, dlg, *items):
  92.         self.items = {}
  93.         for item in items:
  94.             tp, h, rect = dlg.GetDialogItem(item)
  95.             self.items[item] = h.as_Control()
  96.     
  97.     def set(self, setitem):
  98.         for item, ctl in self.items.items():
  99.             if item == setitem:
  100.                 ctl.SetControlValue(1)
  101.             else:
  102.                 ctl.SetControlValue(0)
  103.     
  104.     def get(self):
  105.         for item, ctl in self.items.items():
  106.             if ctl.GetControlValue():
  107.                 return item
  108.     
  109.     def hasitem(self, item):
  110.         return self.items.has_key(item)
  111.  
  112.  
  113. def interact(scriptname):
  114.     d = Dlg.GetNewDialog(DLG_ID, -1)
  115.     if not d:
  116.         print "Can't get DLOG resource with id =", DLG_ID
  117.         return
  118.     d.SetDialogDefaultItem(OK_BUTTON)
  119.     d.SetDialogCancelItem(CANCEL_BUTTON)
  120.     Dlg.ParamText(scriptname, "", "", "")
  121.     
  122.     radiogroup = radio(d, GENFAT_BUTTON, GENPPC_BUTTON, GEN68K_BUTTON)
  123.     radiogroup.set(GENFAT_BUTTON)
  124.     
  125.     gentype = 'fat'
  126.     while 1:
  127.         n = Dlg.ModalDialog(None)
  128.         if n == OK_BUTTON or n == CANCEL_BUTTON:
  129.             break
  130.         elif radiogroup.hasitem(n):
  131.             radiogroup.set(n)
  132.     genitem = radiogroup.get()
  133.     del radiogroup
  134.     del d
  135.     if genitem == GENFAT_BUTTON:
  136.         gentype = 'fat'
  137.     elif genitem == GENPPC_BUTTON:
  138.         gentype = 'pwpc'
  139.     elif genitem == GEN68K_BUTTON:
  140.         gentype = 'm68k'
  141.     return gentype, n == OK_BUTTON
  142.  
  143.  
  144. if __name__ == '__main__':
  145.     main()
  146.