import fileinput import os import platform import re import shutil import sys import tempfile from distutils.sysconfig import get_python_lib ## RewriteWalker performs a os.path.walk of base_dir class RewriteWalker: def __init__(self, base_dir, file_re, find_str, replace_str): self.base_dir = base_dir self.file_filter = re.compile(file_re) self.find_str = find_str self.replace_str = replace_str def walk_files(self): for root, directories, filenames in os.walk(self.base_dir): for filename in filenames: if (self.file_filter.match(filename)): filepath = os.path.join(root, filename) self.find_lines(filepath) #rewrite(filepath) def find_lines(self, srcfile): line_count = 0; src = open(srcfile, 'r') for line in src: line_count += 1 if self.find_str in line: print srcfile print str(line_count) + ": " + line def rewrite(srcfile, searchPattern, replaceStr): dstfile = srcfile + '.bak' #Checks to see if the file has been backed up if not then create a backup of the file. if not os.path.isfile(dstfile): shutil.copy(srcfile,dstfile) #creates a temporary file in the same directory that the "executable" was launched from tmpfilename = str(os.getpid()) tfile = open(tmpfilename,'w') src = open(srcfile,'r') #loops through the entire file changing text for line in src: if searchPattern in line: tfile.write(line.replace(self.find_str,self.replace_str)) else: tfile.write(line) tfile.close() src.close() #overwrites the srcfile and deletes the temp file shutil.copy2(tmpfilename,srcfile) os.unlink(tmpfilename) class AGUpdater: def __init__(self): # define list of root paths and populate with those in python_lib self.root_path_list = [] ag_python_lib_folder_list = ["AccessGrid", "AccessGrid3"] python_lib_dir = get_python_lib() for folder_name in ag_python_lib_folder_list: folder_path = os.path.join(python_lib_dir, folder_name) if os.path.exists(folder_path): self.root_path_list.append(folder_path) # define map of substitutions that can be applied [globally] self.subs_map = {} ##self.subs_map['bugzilla.mcs.anl.gov'] = 'bugzilla.accessgrid.org' self.subs_map['mcs.anl.gov'] = 'accessgrid.org' ##self.subs_map['140.221.6.95'] = 'www.accessgrid.org' ##self.subs_map['140.221.8.33'] = 'vv3.accessgrid.org' # define list of filename_re that identify which files will be modified # please note that this uses regular expressions not just simple expansions self.filename_re_list = [] self.filename_re_list.append('.*\.py$') self.filename_re_list.append('.*\.txt$') # iterate throught root paths, filename_res and substitutions and create all combinations def generate_walkers(self): self.walkers=[] for root_folder in self.root_path_list: for filename_re in self.filename_re_list: for subs_key, subs_val in self.subs_map.iteritems(): walker = RewriteWalker(root_folder, filename_re, subs_key, subs_val) self.walkers.append(walker) def perform_update(self): for walker in self.walkers: walker.walk_files() class WindowsAGUpdater(AGUpdater): def __init__(self): AGUpdater.__init__(self) class WindowsXPAGUpdater(WindowsAGUpdater): pass class WindowsXP_32AGUpdater(WindowsXPAGUpdater): pass class WindowsXP_64AGUpdater(WindowsXPAGUpdater): pass class Windows7AGUpdater(WindowsAGUpdater): def __init__(self): WindowsAGUpdater.__init__(self) class Windows7_32AGUpdater(Windows7AGUpdater): def __init__(self): Windows7AGUpdater.__init__(self) root_path_list.append("C:\\Program Files\\AGTk3\\") class Windows7_64AGUpdater(Windows7AGUpdater): def __init__(self): Windows7AGUpdater.__init__(self) root_path_list.append("C:\\Program Files (x86)\\AGTk3\\") class PosixAGUpdater(AGUpdater): def __init__(self): AGUpdater.__init__(self) class LinuxAGUpdater(PosixAGUpdater): def __init__(self): PosixAGUpdater.__init__(self) class RedHatLinuxAGUpdater(LinuxAGUpdater): def __init__(self): LinuxAGUpdater.__init__(self) class RHELLinuxAGUpdater(RedHatLinuxAGUpdater): def __init__(self): RedHatLinuxAGUpdater.__init__(self) self.root_path_list.append('/usr/share/doc/AccessGrid-3.2/') self.filename_re_list.append('^Install\.LINUX') self.filename_re_list.append('^README') self.filename_re_list.append('^VERSION') class DebianLinuxAGUpdater(LinuxAGUpdater): def __init__(self): LinuxAGUpdater.__init__(self) class UbuntuLinuxAGUpdater(DebianLinuxAGUpdater): def __init__(self): DebianLinuxAGUpdater.__init__(self) class OSXAGUpdater(PosixAGUpdater): def __init__(self): PosixAGUpdater.__init__(self) if __name__ == "__main__": platform_str = platform.platform() updater = AGUpdater() if "Windows" in platform_str: if "7" in platform_str: if "64" in platform_str: updater = Windows7_64AGUpdater() elif "Linux" in platform_str: if "redhat" in platform_str: if "6.2" in platform_str: updater = RHELLinuxAGUpdater() updater.generate_walkers() updater.perform_update()