Rewrite in Python

I can't seem to get proper multiline search and replace with either sed or perl
-i. But I know how to do it in Python.
This commit is contained in:
Aurélien Gâteau 2013-12-11 08:49:59 +01:00
parent ef58fdbf6f
commit dae4e77d41

View File

@ -1,75 +1,92 @@
#!/bin/sh #!/usr/bin/env python
set -e import os
import re
import sys
repl() { def replace_one(txt, dst, src=None):
name=$1 if src is None:
if [ $# = 3 ] ; then src = "K" + dst
from=$2
to=$3
else
to=$2
from=K$to
fi
sed -i \
-e "s/find_package *($from/find_package(KF5$to/g" \
-e "s/\(find_package *( *KF5 [A-Za-z0-9 ]*\)$from/\1$to/g" \
-e "s/find_dependency($from/find_dependency(KF5$to/g" \
-e "s/KF5::$from/KF5::$to/g" \
$name
}
find -name CMakeLists.txt -o -name '*Config.cmake.in'| while read name ; do txt = re.sub(r"find_package *\(" + src, "find_package(KF5" + dst, txt)
echo $name txt = re.sub(r"(find_package *\( *KF5 [A-Za-z0-9\n ]*)\W" + src + "(\W)", r"\1 " + dst + r"\2", txt, re.M)
txt = re.sub(r"find_dependency *\(" + src, "find_dependency(KF5" + dst, txt)
return txt.replace("KF5::" + src, "KF5::" + dst)
def replace_all(path):
with open(path) as f:
txt = f.read()
# tier1 # tier1
repl $name Archive txt = replace_one(txt, "Archive")
repl $name Codecs txt = replace_one(txt, "Codecs")
repl $name Config txt = replace_one(txt, "Config")
repl $name CoreAddons txt = replace_one(txt, "CoreAddons")
repl $name DBusAddons txt = replace_one(txt, "DBusAddons")
repl $name GuiAddons txt = replace_one(txt, "GuiAddons")
repl $name IdleTime txt = replace_one(txt, "IdleTime")
repl $name ItemModels txt = replace_one(txt, "ItemModels", "ItemModels")
repl $name ItemViews txt = replace_one(txt, "ItemViews", "ItemViews")
repl $name JS txt = replace_one(txt, "JS")
repl $name JobWidgets txt = replace_one(txt, "JobWidgets")
repl $name Plotting txt = replace_one(txt, "Plotting")
repl $name Solid txt = replace_one(txt, "Solid")
repl $name Sonnet txt = replace_one(txt, "Sonnet")
repl $name ThreadWeaver txt = replace_one(txt, "ThreadWeaver", "ThreadWeaver")
repl $name WidgetsAddons txt = replace_one(txt, "WidgetsAddons")
repl $name WindowSystem txt = replace_one(txt, "WindowSystem")
# tier2 # tier2
repl $name DNSSD txt = replace_one(txt, "DNSSD")
repl $name Auth txt = replace_one(txt, "Auth")
repl $name Completion txt = replace_one(txt, "Completion")
repl $name Crash txt = replace_one(txt, "Crash")
repl $name XsltKde txt = replace_one(txt, "XsltKde")
repl $name I18n txt = replace_one(txt, "I18n")
repl $name JobWidgets txt = replace_one(txt, "JobWidgets")
repl $name Notifications txt = replace_one(txt, "Notifications")
repl $name Wallet txt = replace_one(txt, "Wallet")
# tier3 # tier3
repl $name Bookmarks txt = replace_one(txt, "Bookmarks")
repl $name ConfigWidgets txt = replace_one(txt, "ConfigWidgets")
repl $name Declarative txt = replace_one(txt, "Declarative")
repl $name KCMUtils txt = replace_one(txt, "KCMUtils", "KCMUtils")
repl $name KIOCore txt = replace_one(txt, "KIOCore", "KIOCore")
repl $name KIOFileWidgets txt = replace_one(txt, "KIOFileWidgets", "KIOFileWidgets")
repl $name KIOWidgets txt = replace_one(txt, "KIOWidgets", "KIOWidgets")
repl $name Kross txt = replace_one(txt, "Kross", "Kross")
repl $name KDESu KF5Su txt = replace_one(txt, "KF5Su", "KDESu")
repl $name WebKit txt = replace_one(txt, "WebKit")
repl $name Emoticons txt = replace_one(txt, "Emoticons")
repl $name IconThemes txt = replace_one(txt, "IconThemes")
repl $name JsEmbed txt = replace_one(txt, "JsEmbed")
repl $name MediaPlayer txt = replace_one(txt, "MediaPlayer")
repl $name NewStuff txt = replace_one(txt, "NewStuff")
repl $name NotifyConfig txt = replace_one(txt, "NotifyConfig")
repl $name Parts txt = replace_one(txt, "Parts")
repl $name PrintUtils txt = replace_one(txt, "PrintUtils")
repl $name Pty txt = replace_one(txt, "Pty")
repl $name Service txt = replace_one(txt, "Service")
repl $name TextWidgets txt = replace_one(txt, "TextWidgets")
repl $name UnitConversion txt = replace_one(txt, "UnitConversion")
repl $name XmlGui txt = replace_one(txt, "XmlGui", "XmlGui")
done # plasma
txt = replace_one(txt, "Plasma", "Plasma")
with open(path, "w") as f:
f.write(txt)
def main():
if len(sys.argv) == 2:
return replace_all(sys.argv[1])
for root, dirs, names in os.walk('.'):
for name in names:
if name == "CMakeLists.txt" or name.endswith("Config.cmake.in"):
path = os.path.join(root, name)
print(path)
replace_all(path)
if __name__ == "__main__":
sys.exit(main())