玩蛇网提供最新Python编程技术信息以及Python资源下载!

python中apt简单使用工具

python 培训

python中apt简单使用工具,这是将apt-get、apt-cache合并在一起使用的方法。
apt-tool -s xxxx :显示查找内容并进行安装交互
apt-tool -i xxxx :安装所选项
apt-tool -s -q xxxx :只显示查找内容

#!/usr/bin/env python
#-*-: coding: utf-8 -*-

import optparse
import subprocess
import sys
import locale
import os

class Apt(object):
	def __init__(self, opts):
		self.options = opts

	def apt_install(self, opts, listone=""):
		soft = opts.install if listone == "" else listone
		os.system("apt-get install %s" % soft)

	def apt_search(self, opts):
		list_buf = []
		cout = 0
		ret = subprocess.Popen("apt-cache search %s | awk -F' ' '{print $1}'" % opts.search, shell=True,
					stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
		if ret == 0:
			print "commad error !"
			sys.exit(125)
		output = ret.stdout.readlines()
		print '-'*31
		for out in output:
			print "%d\t%s" % (cout, out.strip())
			list_buf.append(out.strip())
			cout += 1
		print '-'*10 + "DONE!" + '-'*10
		if opts.quiet:			
			sys.exit(0)
		else:
			try:
				num = input("Intput software number : ")
			except KeyboardInterrupt:
				print "\tUser press Ctrl+C ,Exit\n"
				sys.exit(125)

			if num > len(list_buf) and num < 0:
				print "number is not right !"
				sys.exit(125)
			else:
				self.apt_install(opts, listone=list_buf[num])

	def run(self):
		if self.options.install:
			self.apt_install(self.options)
		elif self.options.search:
			self.apt_search(self.options)

def parse_cmdline():#www.iplaypython.com
	p = optparse.OptionParser(description="easy apt tool", prog="apt-tool", version="1.0", 
					usage="%prog [options] dest")
	p.add_option("-i", "--install", action="store", dest="install", help="software name", type="string")
	p.add_option("-s", "--search", action="store", dest="search", help="search software", type="string")
	p.add_option("-q", "--quiet", action="store_true", dest="quiet", help="don't install")
	
	(opts, args) = p.parse_args()
	
	if not opts.install and not opts.search:
		print sys.argv[0] + "[-i|-s]" + "software name"
	return opts, args

def main():
	locale.setlocale(locale.LC_ALL, '')
	opts, args = parse_cmdline()
	apt = Apt(opts)
	apt.run()

if __name__ == '__main__':
	main()

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/other/o2201.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-06-15 14:16 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部