2013/03/20

使用 py2exe 製作 wxPython 執行檔

製作 wxPython 執行檔的方法很多,像 pyinstaller, cx-freeze, shedlin,網路上有人做了個比較表可以參考。本篇主要給了一個 py2exe 的例子。

所有的程式放在 github

環境
  • Windows XP
  • Visual Studio 2008 Express
  • Python 2.7
  • wxPython 2.8
  • py2exe 0.6.8.win32-py2.7
Hello World in wxPython

首先,先準備好要編成執行檔的 wxPython 源碼,存成 main.py

import wx

class MainWindow(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(200,100))
        self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
        self.CreateStatusBar() # A Statusbar in the bottom of the window

        # Setting up the menu.
        filemenu= wx.Menu()

        # wx.ID_ABOUT and wx.ID_EXIT are standard IDs provided by wxWidgets.
        filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
        filemenu.AppendSeparator()
        filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")

        # Creating the menubar.
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
        self.SetMenuBar(menuBar) # Adding the MenuBar to the Frame content.
        self.Show(True)

app = wx.App(False)
frame = MainWindow(None, "Sample editor")
app.MainLoop()

py2exe setup.py

複製 Visual studio libraries 到 python DLLs 資料匣
copy C:\Program Files\Microsoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT\*.dll C:\Python27\DLLs
準備 py2exe 佈署設定程式 setup.py 如下

#!/usr/bin/python

from distutils.core import setup
import py2exe
from glob import glob
import sys

#sys.path.append('ftpserver')

manifest = """
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1"
manifestVersion="1.0">
<assemblyIdentity
    version="0.64.1.0"
    processorArchitecture="x86"
    name="Controls"
    type="win32"
/>
<description>Your Application</description>
<dependency>
    <dependentAssembly>
        <assemblyIdentity
            type="win32"
            name="Microsoft.Windows.Common-Controls"
            version="6.0.0.0"
            processorArchitecture="X86"
            publicKeyToken="6595b64144ccf1df"
            language="*"
        />
    </dependentAssembly>
</dependency>
</assembly>
"""

"""
installs manifest and icon into the .exe
but icon is still needed as we open it
for the window icon (not just the .exe)
changelog and logo are included in dist
      data_files=["yourapplication.ico"]
            "other_resources": [(24,1,manifest)]
    data_files=data_files,
"""
data_files = [("Microsoft.VC90.CRT", glob(r'C:\Program Files\Microsoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT\*.*'))]
includes = []
excludes = ['_gtkagg', '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger',
            'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl',
            'Tkconstants', 'Tkinter']
packages = []
dll_excludes = ['libgdk-win32-2.0-0.dll', 'libgobject-2.0-0.dll', 'tcl84.dll',
                'tk84.dll']

setup(
    windows = [
        {
            "script": "main.py",
        }
    ],
    options = {"py2exe": {"compressed": 2,
                          "optimize": 2,
                          "includes": includes,
                          "excludes": excludes,
                          "packages": packages,
                          "dll_excludes": dll_excludes,
                          "bundle_files": 3,
                          "dist_dir": "dist",
                          "xref": False,
                          "skip_archive": False,
                          "ascii": False,
                          "custom_boot_script": '',
                         }
              },
) 

打開命令提示字元下

cd \path\to\hello
python setup.py py2exe

在資料匣 dist 便可發現執行檔

2013/03/18

近況


記錄一下近況


這陣子結束了 1 年多在 NAS 產業的工作,該公司剛成立我便進去了,離開的原因有很多,除了我認為公司要上市的時間會比我想像的很還久,另外也是 C 社的魅力,以及我希望能做一些開源的專案。

A 公司

在 A 公司的那段時間,我主要的工作是把 Boxee 放在 NAS 上執行。為了達到最佳的使用者經驗,做了不少貢獻,
  1. a window manager which is based on evilwm/xcompmgr
  2. a media player which is based on mplayer
  3. SGX 545 OpenGL and VAAPI porting
  4. a server, written in Python, to broadcast Avahi packet and communicate with Remote Controller and Boxee
  5. a Android App to control Boxee remotely. 
除了 Boxee 外,另一個專案關於串流的部分還沒來得及看到成果便離開。

 C 社

在加入 C 社前思考了很多,最大的動力是想說來到這裡可以有更多的時間從事自己有興趣的專案,也希望在這幾個月內,能夠多寫一些東西或將目前在做的電視串流的專案能夠開源。

2012/08/24

VIM: 使用 pyclewn 遠端除錯 C++ 程式

[pyclewn] 是一套可以用 vim 當做 front-end,gdb or pdb 當做 back-end 的 debugger。相較於同類型的玩具可以參考 clewn、vimgdb、GdbFromVim。之前我是用 cgdb 或是 clewn 這套,今天上官網上看到他推薦 pyclewn 便抓來試試,才發現就易用性來說它真的很不錯。

因為目前我的環境都需要用 remote debugger,所以就順道帶過。

安裝 pyclewn


請先在官網上抓 pyclewn*.tar.gz 後,解壓縮,再用

sudo python setup.py install

安裝即可,如要將 vim plugin 安裝到特定資料匣可在 vimdir 的環境變數

sudo vimdir=/home/dorowu/src/gdb/pyclewn-vim/ python setup.py install

跑跑跑...


1. 在 target 上使用 gdbserver 執行 ./Boxee 並在 port 1234 等待 gdb 連線

# gdbserver 0.0.0.0:1234 ./Boxee 
Process ./Boxee created; pid = 9787
Listening on port 1234

2. 在 host 上的 gvim 裡 gvim

:Pyclewn                           " 開啟 pyclewn
:C target remote 172.16.5.8:1234   " 連線到 gdbserver,172.16.5.8 為 target ip
:C symbol-file ./Boxee             " 載入 symbol file
:C b main                          " 在 main 函式設斷點
:C continue                        " 讓程式繼續執行

各位可以發現若要執行 gdb command 可直接用 prefix C 後接 gdb command 即可。

3. pyclewn 提供了一些快速鍵,要使用這些快速鍵時請先 :Cmapkyes。取消快速鍵為 :Cunmapkeys

CTRL-Z  send an interrupt to gdb and the program it is running (unix
                only)
        B       info breakpoints
        L       info locals
        A       info args
        S       step
        CTRL-N  next: next source line, skipping all function calls
        F       finish
        R       run
        Q       quit
        C       continue
        W       where
        X       foldvar
        CTRL-U  up: go up one frame
        CTRL-D  down: go down one frame

cursor position: ~
        CTRL-B  set a breakpoint on the line where the cursor is located
        CTRL-E  clear all breakpoints on the line where the cursor is located

mouse pointer position: ~
        CTRL-P  print the value of the variable defined by the mouse pointer
                position
        CTRL-X  print the value that is referenced by the address whose
                value is that of the variable defined by the mouse pointer
                position

4. 關閉 pyclewn 請用 :nbclose
5. 其它功能可參考 [官方文件]

Screenshot


下圖顯示了 pyclewn 的一些功能,像是

1. 滑鼠指到的變數會查詢內容
2. 你會有一個 gdb 視窗 (但是你不能直接編輯,請用 :Cgdbcommand)
3. 你的 QuickFix 會列出所有的中斷點