May be python is easier than find+awk

In Netbeans platform icons are displayed in the toolbar editor only if the underlying action have the iconBase property defined. I had more than hundred action to change when I found this bug/limitation. So I needed to add:

putValue("iconBase","org/foobar");

in all action constructors. I first tried with a find+awk/sed combo without any success. Here is the python script I used to this refactoring operation:

#! /usr/bin/python
import sys
import re
import tempfile
import os
import shutil

def do_a_file(filename):
    input=open(filename)
    output, output_name = tempfile.mkstemp()
    output = os.fdopen(output, "w")
    print output_name
    rawstr = r"""(.*)putValue\(.*NAME"""
    compile_obj = re.compile(rawstr)
    for l in input:
        match_obj = compile_obj.search(l)
        if match_obj != None and len(match_obj.groups()) > 0:
            output.write(match_obj.group(1)+'putValue("iconBase","org/foobar");\n')
        output.write(l)
    input.close()
    output.close()
    shutil.move(output_name, filename)

for root, subFolders, files in os.walk(sys.argv[1]):
    for file in files:
        if file.endswith(".java"):
            do_a_file(os.path.join(root,file))

Now I often use it with minor changes for any refactoring operations which are to specific to be supported by my favorit IDE.

Anyway I guess such multiline context search/replacement is possible with awk or sed, and I would be glad to know how.

#netbeans, #python