def _sed(r, regexpr, repl, force=False, recursive=False, dpath_list=None): if True: import utool as ut force = ut.smart_cast2(force) include_patterns = [ '*.py', '*.cxx', '*.cpp', '*.hxx', '*.hpp', '*.c', '*.h', '*.pyx', '*.pxi', '*.cmake', 'CMakeLists.txt' ] ut.sed(regexpr, repl, force=force, recursive=recursive, dpath_list=dpath_list, verbose=True, include_patterns=include_patterns) return else: #_grep(r, [repl], dpath_list=dpath_list, recursive=recursive) force = rutil.cast(force, bool) recursive = rutil.cast(recursive, bool) import utool as ut pyext = ut.get_argflag('--pyext') if pyext: include_patterns = ['*.py'] else: include_patterns = [ '*.py', '*.cxx', '*.cpp', '*.hxx', '*.hpp', '*.c', '*.h', '*.pyx', '*.pxi', '*.cmake', 'CMakeLists.txt' ] if ut.get_argflag('--all'): include_patterns = ['*'] #if ut.get_argflag('--tex'): include_patterns = ['*.tex'] if dpath_list is None: dpath_list = [os.getcwd()] regexpr = extend_regex(regexpr) #import re print('sed-ing %r' % (dpath_list, )) print(' * regular include_patterns : %r' % (include_patterns, )) print(' * (orig) regular expression : %r' % (regexpr, )) print(' * (origstr) regular expression : %s' % (regexpr, )) #regexpr = re.escape(regexpr) print(' * regular expression : %r' % (regexpr, )) print(' * (str)regular expression : %s' % (regexpr, )) print(' * replacement : %r' % (repl, )) print(' * recursive: %r' % (recursive, )) print(' * force: %r' % (force, )) if '\x08' in regexpr: print('Remember \\x08 != \\b') print('subsituting for you for you') regexpr = regexpr.replace('\x08', '\\b') print(' * regular expression : %r' % (regexpr, )) # Walk through each directory recursively num_changed = 0 for fpath in _matching_fnames(dpath_list, include_patterns, recursive=recursive): num_changed += len(__regex_sedfile(fpath, regexpr, repl, force)) print('total lines changed = %r' % (num_changed, ))
def _matching_fnames(dpath_list, include_patterns, exclude_dirs=None, recursive=True): if isinstance(dpath_list, (str)): dpath_list = [dpath_list] recursive = rutil.cast(recursive, bool) if exclude_dirs is None: exclude_dirs = HS_EXCLUDE if __DEBUG__: print("Excluding: %r" % (exclude_dirs,)) # exclude_dirs = HS_EXCLUDE # exclude_dirs = [] # fname_list = [] for dpath in dpath_list: for root, dname_list, fname_list in os.walk(dpath): # Look at all subdirs subdirs = relpath(root, dpath).replace("\\", "/").split("/") greater_exclude_dirs = ["lib.linux-x86_64-2.7", "lib.linux-x86_64-3.4", "lib", "bundle"] if any([dir_ in greater_exclude_dirs for dir_ in subdirs]): continue # Look at one subdir if split(root)[1] in exclude_dirs: continue for name in fname_list: # For the filesnames which match the patterns if any([fnmatch.fnmatch(name, pat) for pat in include_patterns]): yield join(root, name) # fname_list.append((root, name)) if not recursive: break
def _sed(r, regexpr, repl, force=False, recursive=False, dpath_list=None): if True: import utool as ut force = ut.smart_cast2(force) ut.sed(regexpr, repl, force=force, recursive=recursive, dpath_list=dpath_list, verbose=True) return else: # _grep(r, [repl], dpath_list=dpath_list, recursive=recursive) force = rutil.cast(force, bool) recursive = rutil.cast(recursive, bool) import utool as ut pyext = ut.get_argflag("--pyext") if pyext: include_patterns = ["*.py"] else: include_patterns = ["*.py", "*.cxx", "*.cpp", "*.hxx", "*.hpp", "*.c", "*.h", "*.pyx", "*.pxi"] if ut.get_argflag("--all"): include_patterns = ["*"] # if ut.get_argflag('--tex'): include_patterns = ["*.tex"] if dpath_list is None: dpath_list = [os.getcwd()] regexpr = extend_regex(regexpr) # import re print("sed-ing %r" % (dpath_list,)) print(" * regular include_patterns : %r" % (include_patterns,)) print(" * (orig) regular expression : %r" % (regexpr,)) print(" * (origstr) regular expression : %s" % (regexpr,)) # regexpr = re.escape(regexpr) print(" * regular expression : %r" % (regexpr,)) print(" * (str)regular expression : %s" % (regexpr,)) print(" * replacement : %r" % (repl,)) print(" * recursive: %r" % (recursive,)) print(" * force: %r" % (force,)) if "\x08" in regexpr: print("Remember \\x08 != \\b") print("subsituting for you for you") regexpr = regexpr.replace("\x08", "\\b") print(" * regular expression : %r" % (regexpr,)) # Walk through each directory recursively num_changed = 0 for fpath in _matching_fnames(dpath_list, include_patterns, recursive=recursive): num_changed += len(__regex_sedfile(fpath, regexpr, repl, force)) print("total lines changed = %r" % (num_changed,))
def _grep(r, tofind_list, recursive=True, case_insensitive=True, regex=False, dpath_list=None, invert=False): include_patterns = [ '*.py', '*.cxx', '*.cpp', '*.hxx', '*.hpp', '*.c', '*.h', '*.vim' ] # , '*.txt'] exclude_dirs = HS_EXCLUDE # ensure list input if isinstance(include_patterns, str): include_patterns = [include_patterns] if dpath_list is None: dpath_list = [os.getcwd()] recursive = rutil.cast(recursive, bool) recursive_stat_str = ['flat', 'recursive'][recursive] print('Greping (%s) %r for %r' % (recursive_stat_str, dpath_list, tofind_list)) found_filestrs = [] found_fpaths = [] # Walk through each directory recursively for fpath in _matching_fnames(dpath_list, include_patterns, exclude_dirs, recursive=recursive): if regex: if len(tofind_list) > 1: print('WARNING IN ROB NAV 133') #import re regexpr = extend_regex(tofind_list[0]) regexpr = tofind_list[0] #regexpr = re.escape(regexpr) ret = __regex_grepfile(fpath, regexpr, verbose=not invert) else: ret = __grepfile(fpath, tofind_list, case_insensitive, verbose=not invert) if ret is None and invert: found_filestrs.append(fpath) # regular matching elif ret is not None and not invert: found_filestrs.append(ret) # inverse matching if ret is not None: found_fpaths.append(fpath) print('====================') print('====================') print('\n'.join(found_filestrs)) print('') print('gvim -o ' + ' '.join(found_fpaths)) return found_filestrs
def _grep(r, tofind_list, recursive=True, case_insensitive=True, regex=False, dpath_list=None, invert=False): include_patterns = ['*.py', '*.cxx', '*.cpp', '*.hxx', '*.hpp', '*.c', '*.h', '*.vim'] # , '*.txt'] exclude_dirs = HS_EXCLUDE # ensure list input if isinstance(include_patterns, str): include_patterns = [include_patterns] if dpath_list is None: dpath_list = [os.getcwd()] recursive = rutil.cast(recursive, bool) recursive_stat_str = ['flat', 'recursive'][recursive] print('Greping (%s) %r for %r' % (recursive_stat_str, dpath_list, tofind_list)) found_filestrs = [] found_fpaths = [] # Walk through each directory recursively for fpath in _matching_fnames(dpath_list, include_patterns, exclude_dirs, recursive=recursive): if regex: if len(tofind_list) > 1: print('WARNING IN ROB NAV 133') #import re regexpr = extend_regex(tofind_list[0]) regexpr = tofind_list[0] #regexpr = re.escape(regexpr) ret = __regex_grepfile(fpath, regexpr, verbose=not invert) else: ret = __grepfile(fpath, tofind_list, case_insensitive, verbose=not invert) if ret is None and invert: found_filestrs.append(fpath) # regular matching elif ret is not None and not invert: found_filestrs.append(ret) # inverse matching if ret is not None: found_fpaths.append(fpath) print('====================') print('====================') print('\n'.join(found_filestrs)) print('') print('gvim -o ' + ' '.join(found_fpaths)) return found_filestrs
def _matching_fnames(dpath_list, include_patterns, exclude_dirs=None, recursive=True): if isinstance(dpath_list, (str)): dpath_list = [dpath_list] recursive = rutil.cast(recursive, bool) if exclude_dirs is None: exclude_dirs = HS_EXCLUDE if __DEBUG__: print('Excluding: %r' % (exclude_dirs, )) #exclude_dirs = HS_EXCLUDE #exclude_dirs = [] #fname_list = [] for dpath in dpath_list: for root, dname_list, fname_list in os.walk(dpath): # Look at all subdirs subdirs = relpath(root, dpath).replace('\\', '/').split('/') greater_exclude_dirs = [ 'lib.linux-x86_64-2.7', 'lib.linux-x86_64-3.4', 'lib', 'bundle', ] if any([dir_ in greater_exclude_dirs for dir_ in subdirs]): continue # Look at one subdir if split(root)[1] in exclude_dirs: continue for name in fname_list: # For the filesnames which match the patterns if any( [fnmatch.fnmatch(name, pat) for pat in include_patterns]): yield join(root, name) #fname_list.append((root, name)) if not recursive: break