def find_and_replace_in_dir(build,
                            root_dir,
                            find,
                            replace,
                            file_suffixes=("html", ),
                            template=False,
                            **kw):
    'For all files ending with one of the suffixes, under the root_dir, replace ``find`` with ``replace``'
    if template:
        replace = utils.render_string(build.config, replace)

    build.log.debug("replacing {find} with {replace} in {files}".format(
        find=find,
        replace=replace,
        files="{0}/**/*.{1}".format(root_dir, file_suffixes)))

    found_roots = glob.glob(root_dir)
    if len(found_roots) == 0:
        build.log.warning('No files were found to match pattern "%s"' %
                          root_dir)
    for found_root in found_roots:
        for root, _, files, depth in walk_with_depth(found_root):
            for file_ in files:
                if file_.rpartition('.')[2] in file_suffixes:
                    find_with_fixed_path = find.replace(
                        "%{back_to_parent}%", "../" * (depth + 1))
                    replace_with_fixed_path = replace.replace(
                        "%{back_to_parent}%", "../" * (depth + 1))
                    _replace_in_file(build, path.join(root, file_),
                                     find_with_fixed_path,
                                     replace_with_fixed_path)
Example #2
0
def find_and_replace_in_dir(build, root_dir, find, replace, file_suffixes=("html",), template=False, **kw):
	'For all files ending with one of the suffixes, under the root_dir, replace ``find`` with ``replace``'
	if template:
		replace = utils.render_string(build.config, replace)

	build.log.debug("replacing {find} with {replace} in {files}".format(
		find=find, replace=replace, files="{0}/**/*.{1}".format(root_dir, file_suffixes)
	))
	
	found_roots = glob.glob(root_dir)
	if len(found_roots) == 0:
		build.log.warning('No files were found to match pattern "%s"' % root_dir)
	for found_root in found_roots:
		for root, _, files, depth in walk_with_depth(found_root):
			for file_ in files:
				if file_.rpartition('.')[2] in file_suffixes:
					find_with_fixed_path = find.replace("%{back_to_parent}%", "../" * (depth+1))
					replace_with_fixed_path = replace.replace("%{back_to_parent}%", "../" * (depth+1))
					_replace_in_file(build, path.join(root, file_), find_with_fixed_path, replace_with_fixed_path)