Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1157854
  • 博文数量: 241
  • 博客积分: 4385
  • 博客等级: 上校
  • 技术积分: 2383
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-07 23:13
文章分类

全部博文(241)

文章存档

2013年(1)

2012年(8)

2011年(62)

2010年(109)

2009年(61)

分类: Python/Ruby

2011-01-25 09:30:57

#!/usr/bin/env python

import os, os.path, sys

search_file = [ "control", "control.slp", "desktop.slp", "postinst" ]
search_path = "./"

args = len(sys.argv)
if args == 2:
    search_path = sys.argv[1]
else:
    print "Usage: %s [directory]" % sys.argv[0]
    
def add_deb(file):
    src_file = file
    des_file = "./add.deb.tmp"
    
    src_file_object = open(src_file, "r")
    des_file_object = open(des_file, "a")
    
    for each_line in src_file_object:
        if "com.samsung" in each_line and "deb" not in each_line:
            des_file_object.write(each_line.replace("com.samsung", "deb.com.samsung"))
        else:
            des_file_object.write(each_line)
            
    if "com.samsung" in file and "deb.com.samsung" not in file:
        mv_cmd = "mv"+" "+des_file+" "+src_file.replace("com.samsung", "deb.com.samsung")
        print mv_cmd    
        
        rm_cmd = "rm"+" "+file
        os.system(rm_cmd)
    else:
        mv_cmd = "mv"+" "+des_file+" "+src_file
    os.system(mv_cmd)
    
def find_files(path, search_files):
    result = []
    for i in os.walk(path):
        if "debian" in i[0]:
            for item in i[2]:
                for file in search_files:
                    if file in item and "swp" not in item:
                        if os.path.join(i[0], item) not in result:
                            result.append(os.path.join(i[0], item))
    return result

files = find_files(search_path, search_file)
for file in files:
    print file
    add_deb(file)


阅读(1844) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~