linux yum 脚本实现

yum 位于linux /usr/bin/yum

yum命令是python脚本进行编写的(python 2.6)

#!/usr/bin/python2.6                                                                                                                                  
import sys 
try:
    import yum 
except ImportError:
    print >> sys.stderr, """
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:
 
   %s
 
Please install a package which provides this module, or
verify that the module is installed correctly.
 
It's possible that the above module doesn't match the
current version of Python, which is:
%s
 
If you cannot solve this problem yourself, please go to 
the yum faq at:
  http://yum.baseurl.org/wiki/Faq
  
""" % (sys.exc_value, sys.version)
    sys.exit(1)
 
sys.path.insert(0, '/usr/share/yum-cli')
try:
    import yummain
    yummain.user_main(sys.argv[1:], exit_code=True)
except KeyboardInterrupt, e:
    print >> sys.stderr, "

Exiting on user cancel."
    sys.exit(1)

yum脚本会先import一些基础的包

sys,sys是python的基础库,非第三方,自带库,支持基本系统相关

yum,为实际调用库,yum脚本会先try的时候import,如果导入不成功会让你下载moudle

我们可以打印看一下这个yum库都有那些东西:

[root@sevck_linux ~]# /usr/bin/python2.6
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import yum
>>> print dir(yum)
['ArchStorage', 'BOOLEAN_STATES', 'ConfigParser', 'ConfigPreProcessor', 'DBVERSION', 'Errors', 'LETTERFLAGS', 'ListPackageSack', 'PATTERNS_INDEXED_MAX', 'PATTERNS_MAX', 'PLUG_OPT_BOOL', 'PLUG_OPT_FLOAT', 'PLUG_OPT_INT', 'PLUG_OPT_STRING', 'PLUG_OPT_WHERE_ALL', 'PLUG_OPT_WHERE_MAIN', 'PLUG_OPT_WHERE_REPO', 'PO_CONFIG', 'PO_DIR', 'PO_DOC', 'PO_FILE', 'PO_GHOST', 'PO_INSTALLEDPKG', 'PO_LOCALPKG', 'PO_REMOTEPKG', 'P_', 'ParsingError', 'REPO_PROBLEM_COMPS', 'REPO_PROBLEM_METADATA', 'REPO_PROBLEM_OTHER', 'REPO_PROBLEM_PACKAGE', 'REPO_PROBLEM_REPOMD', 'RPMTransaction', 'RPM_CHECKSUM_TYPES', 'RPM_TO_SQLITE', 'RepoStorage', 'SYMBOLFLAGS', 'SimpleCliCallBack', 'StringIO', 'TR_DEPENDS', 'TR_DEPENDSON', 'TR_OBSOLETEDBY', 'TR_OBSOLETES', 'TR_UPDATEDBY', 'TR_UPDATES', 'TS_AVAILABLE', 'TS_ERASE', 'TS_FAILED', 'TS_INSTALL', 'TS_INSTALL_STATES', 'TS_OBSOLETED', 'TS_OBSOLETING', 'TS_REMOVE_STATES', 'TS_TRUEINSTALL', 'TS_UPDATE', 'TS_UPDATED', 'TX_BLACK', 'TX_GREY', 'TX_WHITE', 'URLGrabError', 'URLGrabber', 'YUM_PID_FILE', 'YumAvailablePackage', 'YumBase', 'YumInstalledPackage', 'YumLocalPackage', 'YumUrlPackage', '_', '_YumCostExclude', '_YumPreBaseConf', '_YumPreRepoConf', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__', '__version_info__', '_rpm_ver_atleast', 'archDifference', 'callbacks', 'canCoinstall', 'compareEVR', 'comparePoEVR', 'comps', 'config', 'constants', 'default_grabber', 'depsolve', 'errno', 'exception2msg', 'fnmatch', 'format_number', 'glob', 'history', 'i18n', 'isMultiLibArch', 'logging', 'logginglevels', 'metalink', 'misc', 'num', 'operator', 'os', 'packageSack', 'packages', 'packagesNewestByName', 'packagesNewestByNameArch', 'parsePackages', 'parser', 'pgpmsg', 'pkgtag_db', 'plugins', 're', 'repoMDObject', 'repos', 'rpm', 'rpmUtils', 'rpmsack', 'rpmtrans', 'sqlitesack', 'sqlutils', 'string', 'tempfile', 'time', 'to_str', 'to_unicode', 'transactioninfo', 'types', 'urlgrabber', 'varReplace', 'warnings', 'weakref', 'yum', 'yumRepo']
>>> exit()

然后就是执行,

(sys.exc_value, sys.version)

然后就是导入yummain

我用2.6尝试手工import,但是结果失败,不知道其他人有没有和我一样。同样希望解答

[root@sevck_linux ~]# /usr/bin/python2.6
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import yummain
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named yummain
>>> 
原文地址:https://www.cnblogs.com/sevck/p/6929488.html