2014年1月8日 星期三

Winpe加入Python

最近想在winpe上加入一些script的程式,如Python、Perl等,上網找了一資料,實驗可以成功使用,順道整理一下使用心得。
使用Winpe4.0來加入Python,步驟如下。

[準備軟體]
Winpe 4.0 9200版ADK
Python 2.7.2

[製作含有Python 的Winpe 4.0 Image]
Step 1. 先在一台機器上安裝Python 2.7.2,安裝時請選擇"Install just for me" (僅供我自己使用)。

Step 2. Mount Winpe 4.0 Image
Dism.exe /mount-wim /wimfile:C:\winpe_x86\media\sources\boot.wim /index:1 /mountdir:C:\winpe_x86\mount 
Step 3. 加入剛安裝好的Python 2.7.2的整個資料夾Python27到mount資料夾中。
copy C:\Python27\ C:\winpe_x86\mount 
Step 4. 加入以下command到winpe_x86\mount\Windows\System32下的start.cmd檔案中。
assoc .py=PythonFile
ftype PythonFile=X:\Python27\python.exe %%1 %%*

使用assoc指令,相當於對Registry進行編輯,assoc .py=PythonFile執行後可以Registry的HKEY_CLASS_ROOT中找到.py的設定,。

使用assoc指令,相當於對Registry HKEY_CLASSES_ROOT進行編輯,ftype PythonFile=X:\Python27\python.exe %%1 %%*執行後可以Registry的HKEY_CLASSES_ROOT\PythonFile\shell\open\command中找到PythonFile的設定。

ftype使用說明
ftype:顯示或修改用在副檔名關聯的檔案型態。
指令詳解:ftype [fileType[=[openCommandString]]]
在開啟命令字串中,%0 或 %1 會被取代為經由檔案關聯所啟動的檔案名稱。
%* 代表所有參數而 %2 代表第一個參數、%3 代表第二個,以此類推。
%~n 代表在含第 n 個後的其餘所有參數,其中 n 可以從 2 到 9。例如:
ASSOC .pl=PythonFile
ftype PythonFile=X:\Python27\python.exe %1 %*
[注意]上述ftype PythonFile=X:\Python27\python.exe %1 %*,後面接的%1、%*是在手動下command時使用,如果是要寫成Batch file就必須加入保留字元"%",會變成%%1、%%*。
手動下:
ftype PythonFile=X:\Python27\python.exe %1 %*
Batch下:
ftype PythonFile=X:\Python27\python.exe %%1 %%*
Step 5. 再加入環境變數設定
set PATH=X:\Python27;%PATH%
set PATHEXT=.py;%PATHEXT%

Step 6. Unmount 含有Python的Winpe 4.0 Image。
Dism.exe /unmount-wim /mountdir:C:\winpe_x86\mount /commit
將Boot.wim放至USB中,開機即可使用。

最後寫一簡單的Python程式驗證。
# -*- coding: utf-8 -*-

import os

if __name__ == "__main__":
    argv = os.sys.argv
    argc = len(argv)

    a = int(argv[1])
    b = int(argv[2])

    print "a + b = " + str(a+b)

沒有留言:

張貼留言