robot文件夹下的__main__.py函数 是使用module运行时的入口函数:
1 import sys 2 3 # Allows running as a script. __name__ check needed with multiprocessing: 4 # https://github.com/robotframework/robotframework/issues/1137 5 if 'robot' not in sys.modules and __name__ == '__main__': 6 import pythonpathsetter 7 8 from robot import run_cli 9 10 11 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
1 import sys 2 import fnmatch 3 from os.path import abspath, dirname 4 5 ROBOTDIR = dirname(abspath(__file__)) 6 7 def add_path(path, end=False): 8 if not end: 9 remove_path(path) 10 sys.path.insert(0, path) 11 elif not any(fnmatch.fnmatch(p, path) for p in sys.path): 12 sys.path.append(path) 13 14 def remove_path(path): 15 sys.path = [p for p in sys.path if not fnmatch.fnmatch(p, path)] 16 17 18 # When, for example, robot/run.py is executed as a script, the directory 19 # containing the robot module is not added to sys.path automatically but 20 # the robot directory itself is. Former is added to allow importing 21 # the module and the latter removed to prevent accidentally importing 22 # internal modules directly. 23 add_path(dirname(ROBOTDIR)) #或者去pythonpathsetter.py的上级目录, 加到sys.path中, 供导入模块使用 24 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:SoftwareSoftwarePython2.7.11Libsite-packages obot>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
,,, ,,, ,,, ,,,