/opt/stack/keystone/keystone/cmd/manage.py
OpenStack所有项目都是基于Python开发,并且都是标准的Python项目,通过setuptools工具管理项目,负责Python模块的安装和分发。想知道一个项目有哪些服务组成,最直接有效的办法就是找到入口函数(main函数)在哪里,只要是标准的基于setuptools管理的项目的所有入口函数都会在项目根目录的setup.cfg文件中定义,console_scripts就是所有服务组件的入口,比如nova(Mitaka版本)的setup.cfg的console_scripts如下:
[entry_points]
...
console_scripts =
进入到cd /opt/stack/keystone/keystone/cmd/ 这个目录,然后python命令进入python环境
python
import os
import sys
>>> os.getcwd() #获区当前目录
'/opt/stack/keystone/keystone/cmd'
>>> dir1 = '/opt/stack/keystone/keystone/cmd' #获区该目录下的文件列表
>>> os.listdir(dir1)
['__init__.py', 'status.py', 'bootstrap.py', 'cli.pyc', 'doctor', 'manage.py', '__init__.pyc', 'manage.pyc', 'cli.py', 'bootstrap.pyc']
>>> print os.path.abspath('manage.py') #注意括号中的文件名需要加上引号
/opt/stack/keystone/keystone/cmd/manage.py
>>> os.path.abspath('manage.py')
'/opt/stack/keystone/keystone/cmd/manage.py'
>>>
>>> possible_topdir = os.path.normpath(os.path.join(os.path.abspath('manage.py'),
... os.pardir,
... os.pardir,
... os.pardir))
>>> possible_topdir
'/opt/stack/keystone'
>>> os.pardir
'..'
>>>
>>> dir2 = os.path.join(possible_topdir, 'keystone','__init__.py')
>>> dir2
'/opt/stack/keystone/keystone/__init__.py'
>>>
>>> os.path.exists('/opt/stack/keystone/keystone/__init__.py')
True
>>>
>>> sys.path.insert(0, possible_topdir) #最终结果就是往sys.path里添加了/opt/stack/keystone这个路径,往列表里指定索引位置插入元素sys.path.insert(0, possible_topdir)
>>> sys.path #是一个列表
['', '/opt/stack/keystone', '/opt/stack/cinder', '/opt/stack/neutron', '/opt/stack/nova', '/opt/stack/placement', '/opt/stack/horizon', '/opt/stack/glance', '/opt/stack/tempest', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
>>>
>>> sys.argv # 返回一个空列表
['']
/opt/stack/keystone/keystone/cmd/cli.py 通过manage.py调用cli.py里的main函数
/usr/local/lib/python2.7/dist-packages 中可以找到oslo