robot文件夹下的__main__.py函数 是使用module运行时的入口函数:
import sys # Allows running as a script. __name__ check needed with multiprocessing:
# https://github.com/robotframework/robotframework/issues/1137
if 'robot' not in sys.modules and __name__ == '__main__':
import pythonpathsetter from robot import run_cli run_cli(sys.argv[1:])
STEP 1.
if 'robot' not in sys.modules and __name__ == '__main__': [explain]: 如果robot module包含在sys.modules 中则import pythonpathsetter 模块
STEP 2.
import pythonpathsetter
[explain]: 这个模块的朱啊哟作用是保证用户在引用时以module的形式进行引用,
不能直接引用内部脚本 pythonpathsetter.py
import sys
import fnmatch
from os.path import abspath, dirname ROBOTDIR = dirname(abspath(__file__)) def add_path(path, end=False):
if not end:
remove_path(path)
sys.path.insert(0, path)
elif not any(fnmatch.fnmatch(p, path) for p in sys.path):
sys.path.append(path) def remove_path(path):
sys.path = [p for p in sys.path if not fnmatch.fnmatch(p, path)] # When, for example, robot/run.py is executed as a script, the directory
# containing the robot module is not added to sys.path automatically but
# the robot directory itself is. Former is added to allow importing
# the module and the latter removed to prevent accidentally importing
# internal modules directly.
add_path(dirname(ROBOTDIR)) #或者去pythonpathsetter.py的上级目录, 加到sys.path中, 供导入模块使用
remove_path(ROBOTDIR) #从sys.path 中删除掉pythonpathsetter.py文件所在的目录, 防止内部脚本被直接应用
[ps]:
abspath 返回文件所在的绝对路径
dirname 返回上一级路径
abspath("monitor.log")
'E:\\Software\\Software\\Python2.7.11\\Lib\\site-packages\\robot\\monitor.log'
ap = abspath("monitor.log")
dp = dirname(ap)
dp
'E:\\Software\\Software\\Python2.7.11\\Lib\\site-packages\\robot'
STEP 3.
from robot import run_cli run_cli(sys.argv[1:])
从模块中导入run_cli函数供采集运行module时传入的参数
E:\Software\Software\Python2.7.11\Lib\site-packages\robot>python -m robot --help
*** cli_arguments *** ['--help']
Robot Framework -- A generic test automation framework Version: 3.0 (Python 2.7.11 on win32) Usage: robot [options] data_sources
or: python -m robot [options] data_sources
or: python path/to/robot [options] data_sources
or: java -jar robotframework.jar [options] data_sources
,,, ,,, ,,, ,,,