def matchFileOnDirPath(curpath, pathdir):
if os.path.exists(curpath):<EOL><INDENT>return curpath<EOL><DEDENT>filedirs = curpath.split('<STR_LIT:/>')[<NUM_LIT:1>:]<EOL>filename = filedirs[-<NUM_LIT:1>]<EOL>filedirs = filedirs[:-<NUM_LIT:1>]<EOL>if pathdir[-<NUM_LIT:1>] == '<STR_LIT:/>':<EOL><INDENT>pathdir = pathdir[:-<NUM_LIT:1>]<EOL><DEDENT>pathdirs = pathdir.split('<STR_LIT:/>')[<NUM_LIT:1>:]<EOL>lp = len(pathdirs)<EOL>for x in range(<NUM_LIT:1>, min(len(filedirs), lp)):<EOL><INDENT>if filedirs[-<NUM_LIT:1>] == pathdirs[-x]:<EOL><INDENT>filedirs = filedirs[:-<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>break<EOL><DEDENT><DEDENT>while filedirs:<EOL><INDENT>tmppath = '<STR_LIT:/>' + '<STR_LIT:/>'.join(pathdirs + filedirs + [filename])<EOL>if os.path.exists(tmppath):<EOL><INDENT>return tmppath<EOL><DEDENT>filedirs = filedirs[<NUM_LIT:1>:]<EOL><DEDENT>tmppath = '<STR_LIT:/>' + '<STR_LIT:/>'.join(pathdirs + [filename])<EOL>if os.path.exists(tmppath):<EOL><INDENT>return tmppath<EOL><DEDENT>return None<EOL>
Find match for a file by slicing away its directory elements
       from the front and replacing them with pathdir.  Assume that the
       end of curpath is right and but that the beginning may contain
       some garbage (or it may be short)
       Overlaps are allowed:
       e.g /tmp/fdjsklf/real/path/elements, /all/the/real/ =>
       /all/the/real/path/elements (assuming that this combined
       path exists)