commit/galaxy-central: guerler: grep/select tool: fixed tab-delimiter matching for GNU version of grep
1 new commit in galaxy-central: https://bitbucket.org/galaxy/galaxy-central/commits/4ed80ec3f309/ Changeset: 4ed80ec3f309 User: guerler Date: 2013-04-02 21:18:22 Summary: grep/select tool: fixed tab-delimiter matching for GNU version of grep Affected #: 1 file diff -r 715dc4f4c8e218f50d08eb81dc1b3ef0453860a5 -r 4ed80ec3f309f5c8e1c9e735666a2b9b682ff388 tools/filters/grep.py --- a/tools/filters/grep.py +++ b/tools/filters/grep.py @@ -17,6 +17,7 @@ import re import string import commands +from subprocess import Popen, PIPE from tempfile import NamedTemporaryFile # This function is exceedingly useful, perhaps package for reuse? @@ -34,89 +35,100 @@ args = sys.argv[1:] try: - opts = getopts(args) + opts = getopts(args) except IndexError: - print "Usage:" - print " -i Input file" - print " -o Output file" - print " -pattern RegEx pattern" - print " -v true or false (Invert match)" - return 0 + print "Usage:" + print " -i Input file" + print " -o Output file" + print " -pattern RegEx pattern" + print " -v true or false (Invert match)" + return 0 outputfile = opts.get("-o") if outputfile == None: - print "No output file specified." - return -1 + print "No output file specified." + return -1 inputfile = opts.get("-i") if inputfile == None: - print "No input file specified." - return -2 + print "No input file specified." + return -2 invert = opts.get("-v") if invert == None: - print "Match style (Invert or normal) not specified." - return -3 + print "Match style (Invert or normal) not specified." + return -3 pattern = opts.get("-pattern") if pattern == None: - print "RegEx pattern not specified." - return -4 + print "RegEx pattern not specified." + return -4 # All inputs have been specified at this point, now validate. # replace if input has been escaped, remove sq # characters that are allowed but need to be escaped - mapped_chars = { '>' :'__gt__', - '<' :'__lt__', - '\'' :'__sq__', - '"' :'__dq__', - '[' :'__ob__', - ']' :'__cb__', - '{' :'__oc__', - '}' :'__cc__' - } + mapped_chars = { '>' :'__gt__', + '<' :'__lt__', + '\'':'__sq__', + '"' :'__dq__', + '[' :'__ob__', + ']' :'__cb__', + '{' :'__oc__', + '}' :'__cc__' + } - #with new sanitizing we only need to replace for single quote, but this needs to remain for backwards compatibility + # with new sanitizing we only need to replace for single quote, + # but this needs to remain for backwards compatibility for key, value in mapped_chars.items(): pattern = pattern.replace(value, key) - - fileRegEx = re.compile("^[A-Za-z0-9./\-_]+$") #why? - invertRegEx = re.compile("(true)|(false)") #why? + # match filename and invert flag + fileRegEx = re.compile("^[A-Za-z0-9./\-_]+$") + invertRegEx = re.compile("(true)|(false)") + + # verify that filename and inversion flag are in the correct format if not fileRegEx.match(outputfile): - print "Illegal output filename." - return -5 + print "Illegal output filename." + return -5 if not fileRegEx.match(inputfile): - print "Illegal input filename." - return -6 + print "Illegal input filename." + return -6 if not invertRegEx.match(invert): - print "Illegal invert option." - return -7 + print "Illegal invert option." + return -7 # invert grep search? if invert == "true": - invertflag = " -v" + invertflag = "-v" print "Not matching pattern: %s" % pattern else: invertflag = "" print "Matching pattern: %s" % pattern + + # set version flag + versionflag = "-P" - #Create temp file holding pattern - #By using a file to hold the pattern, we don't have worry about sanitizing grep commandline and can include single quotes in pattern + # MacOS 10.8.2 does not support -P option for perl-regex anymore + versionmatch = Popen("grep -V | grep 'BSD'", shell=True, stdout=PIPE).communicate()[0]; + if versionmatch: + versionflag = "-E" + + # create temp file holding pattern + # by using a file to hold the pattern, we don't have worry about sanitizing grep commandline and can include single quotes in pattern pattern_file_name = NamedTemporaryFile().name open( pattern_file_name, 'w' ).write( pattern ) - #generate grep command - commandline = "grep -E %s -f %s %s > %s" % ( invertflag, pattern_file_name, inputfile, outputfile ) + # generate grep command + commandline = "grep %s %s -f %s %s > %s" % ( versionflag, invertflag, pattern_file_name, inputfile, outputfile ) - #run grep + # run grep errorcode, stdout = commands.getstatusoutput(commandline) - #remove temp pattern file + # remove temp pattern file os.unlink( pattern_file_name ) - #return error code + # return error code return errorcode if __name__ == "__main__": Repository URL: https://bitbucket.org/galaxy/galaxy-central/ -- This is a commit notification from bitbucket.org. You are receiving this because you have the service enabled, addressing the recipient of this email.
participants (1)
-
commits-noreply@bitbucket.org