dberube4

IPy2Exe

Dec 9th, 2012
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.34 KB | None | 0 0
  1. import clr,os
  2.  
  3. clr.AddReference("System.Windows.Forms")
  4. clr.AddReference("System.Drawing")
  5.  
  6. from System import *
  7. from System.Windows.Forms import *
  8. from System.Drawing import *
  9.  
  10. class IPY2EXE(Form):
  11.  
  12.     def __init__(self):
  13.         self.initUI()
  14.        
  15.        
  16.     def initUI(self):
  17.        
  18.         def initWidgets():
  19.            
  20.             #Label 1
  21.             self.label1 = Label()
  22.             self.label1.AutoSize = True
  23.             self.label1.Location = Point(9, 13)
  24.             self.label1.Name = 'label1'
  25.             self.label1.Size = Size(89, 13)
  26.             self.label1.TabIndex = 2
  27.             self.label1.Text = 'IronPython script:'
  28.             self.Controls.Add(self.label1)
  29.  
  30.             # label2
  31.             self.label2 = Label()
  32.             self.label2.AutoSize = True
  33.             self.label2.Location = Point(13, 55)
  34.             self.label2.Name = 'label2'
  35.             self.label2.Size = Size(41, 13)
  36.             self.label2.TabIndex = 7
  37.             self.label2.Text = 'Target:'
  38.             self.Controls.Add(self.label2)
  39.            
  40.             # CompileBtn
  41.             self.CompileBtn = Button()
  42.             self.CompileBtn.Location = Point(291, 80)
  43.             self.CompileBtn.Name = 'CompileBtn'
  44.             self.CompileBtn.Size = Size(87, 36)
  45.             self.CompileBtn.TabIndex = 6
  46.             self.CompileBtn.Text = 'Compile'
  47.             self.CompileBtn.UseVisualStyleBackColor = True
  48.             self.CompileBtn.Click += self.compile
  49.             self.Controls.Add(self.CompileBtn)
  50.            
  51.             # BrowseScriptBtn
  52.             self.BrowseScriptBtn = Button()
  53.             self.BrowseScriptBtn.Location = Point(303, 26)
  54.             self.BrowseScriptBtn.Name = 'BrowseScriptBtn'
  55.             self.BrowseScriptBtn.Size = Size(75, 28)
  56.             self.BrowseScriptBtn.TabIndex = 0
  57.             self.BrowseScriptBtn.Text = 'Browse'
  58.             self.BrowseScriptBtn.UseVisualStyleBackColor = True
  59.             self.BrowseScriptBtn.Click += self.BrowseScriptBtnClick
  60.             self.Controls.Add(self.BrowseScriptBtn)
  61.            
  62.             # ScriptPathTxtBx
  63.             self.ScriptPathTxtBx = TextBox()
  64.             self.ScriptPathTxtBx.Location = Point(12, 31)
  65.             self.ScriptPathTxtBx.Name = 'ScriptPathTxtBx'
  66.             self.ScriptPathTxtBx.Size = Size(285, 20)
  67.             self.ScriptPathTxtBx.TabIndex = 1
  68.             self.Controls.Add(self.ScriptPathTxtBx)
  69.            
  70.             # listBox1
  71.             self.listBox1 = ListBox()
  72.             self.listBox1.FormattingEnabled = True
  73.             self.listBox1.Items.AddRange(Array[Object](('dll', 'exe', 'winexe', )))
  74.             self.listBox1.Location = Point(16, 73)
  75.             self.listBox1.Name = 'listBox1'
  76.             self.listBox1.Size = Size(120, 43)
  77.             self.listBox1.TabIndex = 8
  78.             self.Controls.Add(self.listBox1)
  79.            
  80.             # checkBox1
  81.             self.checkBox1 = CheckBox()
  82.             self.checkBox1.AutoSize = True
  83.             self.checkBox1.Location = Point(142, 95)
  84.             self.checkBox1.Name = 'checkBox1'
  85.             self.checkBox1.Size = Size(128, 17)
  86.             self.checkBox1.TabIndex = 9
  87.             self.checkBox1.Text = 'Standalone (not dll)'
  88.             self.checkBox1.UseVisualStyleBackColor = True
  89.             self.Controls.Add(self.checkBox1)
  90.        
  91.         self.Text = 'IPY2EXE'
  92.         self.Name = 'IPY2EXE'
  93.         self.ClientSize = Size(390, 124)
  94.         self.ResumeLayout(False)
  95.         self.PerformLayout()
  96.         initWidgets()
  97.    
  98.     def BrowseScriptBtnClick(self, Object, EventArg):
  99.         dialog = OpenFileDialog()
  100.         dialog.Title = 'Open .py'
  101.         dialog.Filter = 'Py Files(*.py)|*.py'
  102.        
  103.         if dialog.ShowDialog() != 0:
  104.             self.ScriptPathTxtBx.Text = dialog.FileName
  105.            
  106.     def compile(self, Object, EventArg):
  107.        
  108.         try:
  109.            
  110.             if self.listBox1.SelectedItem == None:
  111.                 MessageBox.Show('Please select a target', 'Error', MessageBoxButtons.OK,MessageBoxIcon.Error)
  112.                 return False
  113.             target = self.listBox1.SelectedItem.ToString()
  114.            
  115.             if self.checkBox1.Checked:standAlone = '/embed /standalone'
  116.             else: standAlone = ''
  117.            
  118.             if self.ScriptPathTxtBx.Text == '':
  119.                 MessageBox.Show('Please select a script', 'Error', MessageBoxButtons.OK,MessageBoxIcon.Error)
  120.                 return False
  121.            
  122.             scriptPath = self.ScriptPathTxtBx.Text
  123.            
  124.             dialog = SaveFileDialog()
  125.             dialog.Title = 'Compile'
  126.             dialog.Filter = 'Windows executable(*.exe)|*.exe'
  127.            
  128.             if dialog.ShowDialog() != 0:
  129.                 output = dialog.FileName
  130.             else:
  131.                 return False
  132.            
  133.             pycArg = 'ipy.exe Tools\Scripts\pyc.py /target:{0} {1} /out:{2} /main:{3}'.format(target,standAlone,output,scriptPath)
  134.             os.system(pycArg)
  135.             MessageBox.Show('Script Compiled','IPY2EXE',MessageBoxButtons.OK,MessageBoxIcon.Information)
  136.        
  137.         except AttributeError:
  138.             MessageBox.Show('Is this program located in the Ironpython folder and\nhas been started as administrator?', 'Unknow Error', MessageBoxButtons.OK,MessageBoxIcon.Error)
  139.  
  140.  
  141. form = IPY2EXE()
  142. Application.Run(form)
Advertisement
Add Comment
Please, Sign In to add comment