代码: 全选
#!/usr/bin/env python
#coding:utf-8
import sys
backupfile = 'backup'
def backup():
import os
cmd = 'dpkg --get-selections > %s'% backupfile
os.system( cmd)
def restore():
import os
if not os.path.exists( backupfile):
print '还没备份'
return
import tempfile
tmpf = tempfile.NamedTemporaryFile()
tmp = tmpf.name
cmd = 'dpkg --get-selections > %s; diff %s %s | awk \'{if ($1==">"){printf " "$2}}\' | awk \'{printf "sudo apt-get autoremove --purge "$0}\''% ( tmp, backupfile, tmp)#把现在有原来没有的删除,而现在没有原来有的不理采。
r = os.popen( cmd)
cmd = r.read()
cmd = clear_linux_image(cmd)
if cmd:
print cmd
os.system( cmd)
else:
print '没有比备份多安装的软件,不需要恢复。'
tmpf.close()
def clear_linux_image( txt):
import re
#print re.sub(r'linux-headers[^ ]+','','linux-headers-2.6.32-21 linux-headers-2.6.32-21-generic wdfk55 d5')#print wdfk45 d4
txt = re.sub(r'linux-headers[^ ]+','', txt)
txt = re.sub(r'linux-image[^ ]+','', txt)
if txt.strip()=='sudo apt-get autoremove --purge':
txt=''
return txt
def info():
print '备份与恢复'
print sys.argv[0],'restore'
print sys.argv[0],'backup'
import os, re
cmd = 'dpkg -l |awk \'{printf $2" "}\''
r = os.popen( cmd)
cmd = r.read()
r.close()
x = re.findall(r'linux-image-(\d[^ ]+)', cmd)
for i in x:
print '已安装:',i
n = 'uname -r'
r = os.popen( n)
d = r.read()
r.close()
print '正在使用:',d
if len(x)==1:
print '只有一个内核并正在使用。'
return
print '如果要删除不使用的内核:\nsudo apt-get autoremove --purge ',
for i in x:
if i != d[:-1]:
n = re.findall(r'([\d\.-]+)-',i)[0]
print ' '.join( re.findall(r'([^ ]+%s+[^ ]*)'%n, cmd)),
if __name__ == '__main__':
if len(sys.argv) > 1:
c = sys.argv[1]
if c == 'restore':
restore()
elif c == 'backup':
backup()
else:
info()
else:
info()
试过先备份,安装 kubuntu-desktop,然后恢复。所有kubuntu-desktop相关的193个软件都能删除。
代码: 全选
#!/usr/bin/env python
#coding:utf-8
import sys
backupfile = 'backup'
def backup():
import os
cmd = 'dpkg --get-selections > %s'% backupfile
os.system( cmd)
def restore():
import os
if not os.path.exists( backupfile):
print '还没备份'
return
import tempfile
tmpf = tempfile.NamedTemporaryFile()
tmp = tmpf.name
cmd = 'dpkg --get-selections > %s; diff %s %s | awk \'{if ($1==">"){printf " "$2}}\' | awk \'{printf "sudo apt-get autoremove --purge "$0}\''% ( tmp, backupfile, tmp)#把现在有原来没有的删除,而现在没有原来有的不理采。
r = os.popen( cmd)
cmd = r.read()
if cmd:
print cmd
os.system( cmd)
else:
print '没有比备份多安装的软件,不需要恢复。'
tmpf.close()
def info():
print '备份与恢复'
print sys.argv[0],'restore'
print sys.argv[0],'backup'
if __name__ == '__main__':
if len(sys.argv) > 1:
c = sys.argv[1]
if c == 'restore':
restore()
elif c == 'backup':
backup()
else:
info()
else:
info()