[同步][Python]同步兩臺Linux PC上的安裝軟件

原文地址:http://nourlcn.ownlinux.net/2011/10/sync-soft-on-two-machines.html


寫了個腳本,可以同步兩臺linux pc上安裝的軟件。

需求:我最常用的兩臺pc裝的都使ubuntu11.04, 一臺在實驗室,一臺在公司,都處於內網中,互相不能訪問。兩臺機器都有開發的需要,因此最好能安裝相同的軟件環境。

因此寫了個腳本,通過dpkg -l > file,將file通過ubuntu one雲存儲服務同步,運行這個python腳本,設置好file的路徑,可以安裝remote機器上得deb包,卸載local機器上多餘的deb包。

工具很簡單,一看就明白,可能有bug,歡迎提出。


代碼託管在 https://github.com/Nourl/tools/blob/master/sync_soft.py
本博客訂閱地址:http://feeds.feedburner.com/nourlcn


 

#!/usr/bin/env python
#encode:utf-8

import re
import os

def get_soft_list(fobject):
    list = []
    i = 0
    r = re.compile('[a-z0-9+-\.]+')
    

    for line in fobject:
	    if i < 1 and line[:2] == "ii":
		    #line = list(line)
		    #print type(line) #str
		    #print len(line)
		    print line
            i += 1
            m = r.match(line[4:])
            if m:
                #print m.group()
                list.append(m.group())
    #print list,len(list)
    return list

def install_soft(local,remote):
    for x in remote:
        if x in local:
            pass
        else:
            print x,"is not installed\n"
            cmd = "aptitude install " + x 
            os.system(cmd)
            
def remove_soft(local,remote):
    for x in local:
        if x in remote:
            pass
        else:
            print x," will be removed\n"
            cmd = "aptitude remove " + x 
            os.system(cmd)
    os.system('aptitude autoremove')
    os.system('aptitude autoclean')
    



if __name__ == "__main__":
    fin = file('/home/nourl/install.soft','r')
    remote_list = get_soft_list(fin)
    fin.close()    
    
    #print local_list
    install = raw_input("install remote machine soft?")
    if install:
        #print len(soft_list)
        os.popen('dpkg -l > tmp_local_soft')
        flocal = file('tmp_local_soft','r')
        local_list = get_soft_list(flocal)
        flocal.close()
        install_soft(local_list, remote_list)
        
#    remove = raw_input("remove local machine soft?")
#    if remove:
#        #print len(soft_list)
#        os.popen('dpkg -l > tmp_local_soft')
#        flocal = file('tmp_local_soft','r')
#        local_list = get_soft_list(flocal)
#        flocal.close()
#        remove_soft(local_list, remote_list)
#        
#    if install or remove:
#        os.system('rm tmp_local_soft')
    if install:
        os.system('rm tmp_local_soft')
        
    print "Done!\n Soft on your system is the same as remote machine~!\n"

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章