#! /usr/bin/python # Copyright 2003 Aaron Forrest # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # script to copy all .php files in the directory to .phps # to make the source available # import modules import re, os, sys, getopt # ***** FUNCTION TO PRINT USAGE INFORMATION ***** def usage(): print """ phptophps Copyright (c) 2003 Aaron Forrest phptophps is a python script that will copy all files ending in the .php suffix to .phps so you can share the source of your php files with the public. SYNTAX ./phptophps [options] [-t=target] [file|directory] OPTIONS -d --delete delete all phps files -h --help print this help file -r --recursive perform operation recursively through directory tree -t --target specify target directory of .phps files OTHER For bugreports send email to aaron@ultramoderne.net """ # ***** FUNCTION TO DELETE .phps FILES ***** def deletephps(files): for f in files: try: result = re.search("\w\.phps\Z", f) if result != None: os.remove(f) print f + ".....deleted" except os.error: print f + ": no such file or directory" sys.exit(1) # ***** FUNCTION TO RECURSE THROUGH FILETREE WITH SELECTED SUBFUNCTION ***** def recurse(func, args): def getFileList(filename_list, directory, files): for f in files: filename_list.append(os.path.join(directory, f)) files = [] for arg in args: os.path.walk(arg, getFileList, files) func = "%s(%s)" % (func, files) eval(func) sys.exit() # ***** FUNCTION TO CREATE .phps FILES FROM .php FILES ***** def makephps(files): global target, working_dir keep_path = 0 try: for fnm in files: fnm = os.path.abspath(fnm) result = re.search("\w\.php\Z", fnm) if result != None: if target == working_dir or keep_path == 1 : target = os.path.dirname(fnm) keep_path = 1 target_file = os.path.basename(fnm) command = "cp %s %s/%ss" % (fnm, target, target_file) print fnm, ret = os.system(command) if ret == 0: print ".....success!" else: print ".....copy failed" except os.error: print "operation failed" sys.exit(1) def main(): global working_dir working_dir = os.getcwd() # set default target for copied files to current directory global target target = working_dir recurse_flag = None delete = None # parse through and validate command line options and arguments try: opts, args = getopt.getopt(sys.argv[1:], "dhrt:", ["delete", "help", "recursive", "target="]) except getopt.error: # print usage info and exit: usage() sys.exit(2) # set action flags according to command line options # excepting help, in which case print usage file and exit for o, a in opts: if o in ("-t", "--target"): target = os.path.abspath(a) if o in ("-r", "--recursive"): recurse_flag = 1 if o in ("-d", "--delete"): delete = 1 if o in ("-h", "--help"): usage() sys.exit() # if there are no command line args, perform ops on current directory if not args: args = [working_dir] # check recursion first, follow other functions from there if recurse_flag: if delete: recurse("deletephps", args) else: recurse("makephps", args) # then check if user has chosen to delete phps files elif delete: files = [] for arg in args: if os.path.isdir(arg): file_list = os.listdir(arg) for i in file_list: files.append( os.path.join( working_dir, arg, i ) ) elif os.path.isfile(arg): files.append(arg) else: print arg, ": no such file or directory" sys.exit(1) if len(files) > 0: deletephps(files) sys.exit() else: files = [] for arg in args: if os.path.isdir(arg): file_list = os.listdir(arg) for i in file_list: files.append( os.path.join( working_dir, arg, i ) ) elif os.path.isfile(arg): files.append(arg) else: print arg, ": no such file or directory (main)" sys.exit(1) if len(files) > 0: makephps(files) sys.exit() # for fnm in files: # result = re.search("\w\.php\Z", fnm) # if result != None: # command = "cp %s %ss" % (fnm, fnm) # print fnm, # ret = os.system(command) # if ret == 0: # print ".....success!" # else: # print ".....copy failed" if __name__ == "__main__": main()