#
#
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Module that adds directories needed by Robot to sys.path when imported."""
import sys
import os
import fnmatch
def norm_path(path):
path = os.path.normpath(path)
# If windows or cyqwin (os.path.normcase not supported in jython)
if os.sep == '\\' or sys.platform.count('cygwin') > 0:
path = path.lower()
return path
def add_path(path, to_beginning=False):
if not path:
return
path = norm_path(path)
if path not in sys.path and os.path.exists(path):
if to_beginning:
sys.path.insert(0, path)
else:
sys.path.append(path)
def remove_path(path):
path = norm_path(path)
while path in sys.path:
sys.path.remove(path)
sys.path = [ norm_path(p) for p in sys.path ]
ROBOTDIR = norm_path(os.path.dirname(os.path.abspath(__file__)))
print ROBOTDIR
PARENTDIR = os.path.dirname(ROBOTDIR)
add_path(os.path.join(ROBOTDIR, 'libraries'), to_beginning=True)
add_path(PARENTDIR, to_beginning=True)
# Handles egg installations
if fnmatch.fnmatchcase(os.path.basename(PARENTDIR), 'robotframework-*.egg'):
add_path(os.path.dirname(PARENTDIR), to_beginning=True)
# Remove ROBOTDIR dir to disallow importing robot internal modules directly
remove_path(ROBOTDIR)
# Elements from PYTHONPATH. By default it is not processed in Jython and in
# Python valid non-absolute paths may be ignored.
PYPATH = os.environ.get('PYTHONPATH')
if PYPATH:
for path in PYPATH.split(os.pathsep):
add_path(path)
del path
# Current dir (it seems to be in Jython by default so let's be consistent)
add_path('.')
del norm_path, add_path, remove_path, ROBOTDIR, PARENTDIR, PYPATH
文件路径:C:\Python26\Lib\site-packages\robot\pythonpathsetter.py
函数norm_path:用于将windows或者cyqwin的目录转换为小写。如果系统分隔符号os.sep为"\",则为windows。如果sys.platform有cygwin,则为cygwin,后面那种虚拟机不建议使用。
函数add_path:添加搜索路径,默认添加至末尾。
函数remove_path:删除搜索路径。通过while,有多个PATH变量也可以删除
PARENTDIR = os.path.dirname(ROBOTDIR)计算出上已成目录,也算一点技巧。
如果是用egg方式安装,把egg文件的目录也加入搜索路径。注意调用了fnmatch,fnmatch
模块使用模式来匹配文件名。
remove_path(ROBOTDIR)是为了防止直接导入robot模块。
PYTHONPATH一般不会使用,这里也加到搜索路径。
添加当前目录
删除相关的函数和变量。
阅读(20759) | 评论(0) | 转发(0) |