由来
由于缺乏某python程序直接外部的API调用的支持,于是打算直接使用该python工具内部的api,通过几步设置好环境以后,该API可以调用并且成功。但是,发现调用应用的目标是错误的,在服务器端调用该api不能达到处理的目的,这个api应该在目标板端执行,也就是通过板子命令行执行。
解决之道
1. 板子缺乏相应的python支持,但好在是Ubuntu的系统,安装该python工具需要的包后,将该工具包源码下载,并安装到板子系统中,取得了这个包的API支持。
2. Python的-c参数可以直接在命令行中执行一个python命令
$ python-c'import foo; print foo.hello()'
3. 注意服务器送入板子端的指令应该是raw指令,即不会自动进行变量代换比如"必须写为\"
cmd = ("python -c 'import linaro_image_tools.media_create.boards"
" as lmc_boards; board_type = \"%s\"; rootfs_uuid = \"\";"
" boot_partition = \"/dev/disk/by-label/testboot\";"
" boot_disk = \"/mnt/boot\"; chroot_dir = \"/mnt/root\";"
" boot_device_or_file = \"/dev/null\"; is_live = False;"
" is_lowmem = False; consoles = \"\";"
" board = lmc_boards.board_configs[board_type]();"
" board.set_metadata([\"/mnt/root/tmp/%s\"]);"
" board.populate_boot(chroot_dir, rootfs_uuid, boot_partition, "
"boot_disk, boot_device_or_file, is_live, is_lowmem, consoles)'"
% (board_type, hwpack_name))
参考文档
http://stackoverflow.com/questions/3987041/python-run-function-from-the-command-line
延伸阅读
该插件要求在每一个module下都加上一段语句,该module中的函数才能被调用,见Usage
"""
PythonCall.py
PythonCall is a shortcut that allows a CLI command with arguments to call
any function within a Python module with only two lines of plumbing code.
Usage
=====
* Add code below to bottom of Python module.
if __name__ == "__main__":
import sys, PythonCall
PythonCall.PythonCall(sys.argv).execute()
* From command line or desktop shortcut, build a command of form:
Example: C:\Python25\python.exe C:\Dev\PyUtils\src\TextUtils.py xclip wrap 64
Notes
=====
* Called functions must expect args to be strings and do their own conversions.
* No argument checking or error-checking.
* In case of exception, PythonCall sends message to stderr.
* I often use this with text I pass in via the clipboard, which requires
code to read and write the clipboard (not included here).
Tested with Windows; should work on other platforms.
Jack Trainor 2008
"""
import os, os.path
import sys
import types
class PythonCall(object):
def __init__(self, sysArgs):
try:
self.function = None
self.args = []
self.modulePath = sysArgs[0]
self.moduleDir, tail = os.path.split(self.modulePath)
self.moduleName, ext = os.path.splitext(tail)
__import__(self.moduleName)
self.module = sys.modules[self.moduleName]
if len(sysArgs) > 1:
self.functionName = sysArgs[1]
self.function = self.module.__dict__[self.functionName]
self.args = sysArgs[2:]
except Exception, e:
sys.stderr.write("%s %s\n" % ("PythonCall#__init__", e))
def execute(self):
try:
if self.function:
self.function(*self.args)
except Exception, e:
sys.stderr.write("%s %s\n" % ("PythonCall#execute", e))
#####################################################
# Test Examples - REMOVE
#
# Normally the function calls are in a module other than PythonCall.
# These are only examples.
#
# Example 1: C:\Python25\python.exe C:\Dev\PyUtils\src\PythonCall.py double 14
# Example 2: C:\Python25\python.exe C:\Dev\PyUtils\src\PythonCall.py capitalize "Four score and seven years ago..."
#####################################################
def double(x):
x = int(x)
print 2 * x
def capitalize(s):
print s.upper()
if __name__ == "__main__":
import sys, PythonCall
PythonCall.PythonCall(sys.argv).execute()
#####################################################
# End - REMOVE
#####################################################
-EOF