担心系统又崩掉,把自己做的脚本打包备份

#!/usr/bin/env python

import os,sys,fcntl,termios

dirlstfile = "/home/lzf/.dirlst"
dirlst = file(dirlstfile, "r+").read().split("/n")[:-1]
commonbase = ["/home/lzf/work/oms001/", "/home/lzf/work/borqs002/", "/home/lzf/work/borqs001/", "/home/lzf/work/merge*/", "/home/lzf/work/google/"]
updated = 0

def torelate(path):
    for item in commonbase:
        if path.find(item)>=0:
            return path[len(item):]

    return path

def toabspath(path):
    if path[0] != '/':
        pwd = os.getcwd()
        apppath = commonbase[0]
        for item in commonbase[1:]:
            if pwd == item[:len(pwd)]:
                apppath=item
                break
            if pwd[-2]=='*' and pwd[:len(item)-2]==item[:-2]:
                apppath=item
                break
        return "%s%s"%(apppath,path)
    return path

def listdir(dirlst, filter):
    startc = 'a'
    dircnt = 0
    for item in dirlst:
        if item!='' and filter in item:
            print startc,item
            dircnt+=1
        startc=chr(ord(startc)+1)
    if dircnt == 0 :
        print "use 'c +' to add dir cache"
        return
    ret = raw_input("select:")
    if ret == '' :return
    seldir(dirlst, ret[0])

def shellcmd(cmd):   
    for c in cmd:fcntl.ioctl(0, termios.TIOCSTI,c)

def seldir(dirlst, c):
    shellcmd( "cd %s/n"%toabspath(dirlst[ord(c)-ord('a')]))

if len(sys.argv) > 1:
    if sys.argv[1] == '-':
        if len(sys.argv) == 2:
            if os.getcwd() in dirlst:
                dirlst.remove(os.getcwd())
        else:
            startc = 'a'
            for item in dirlst:
                if starc in sys.argv[2]: dirbak.remove(item)
                startc=chr(ord(startc)+1)

    elif sys.argv[1] == '+':
        if os.getcwd() not in dirlst:
            dirlst.append(torelate(os.getcwd()))
            for item in dirlst:
                if item=="":
                    item=os.getcwd()
                    dirlst=dirlst[:-1]
                    break
            updated = 1
    elif '1' <= sys.argv[1][0] <= '9' and len(sys.argv[1]) == 1:
        shellcmd("cd %s/n"%('../'*(ord(sys.argv[1])-ord('0'))))
    elif len(sys.argv[1]) == 1:
        seldir(dirlst, sys.argv[1])
    else:
        listdir(dirlst, sys.argv[1])

else:
    listdir(dirlst, "")

if updated == 1:
    fl = file(dirlstfile, "w")
    for item in dirlst:
        fl.write("%s/n"%item)

 

你可能感兴趣的:(担心系统又崩掉,把自己做的脚本打包备份)