Anonymous avatar Anonymous committed 7ffdbb8 Draft

Bash was not cutting it, working on script to automate include changes... Maybe should find library instead

Comments (0)

Files changed (1)

utils/include_change.py

+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+# <nbformat>2</nbformat>
+
+# <codecell>
+
+# Not fully tested, might not run
+# Need to debug parent path replacement, add some info on running
+
+import os, sys, re
+import yaml
+import argparse
+
+# <codecell>
+
+doSave = False
+
+def file_read(filePath):
+    with open(filePath, 'r') as f:
+        return f.read()
+
+def file_write(filePath, text):
+    if doSave:
+        with open(filePath, 'w') as f:
+            f.write(text)
+    else:
+        print "--- FILE: {} ---\n\n{}\n\n\n".format(filePath, text)
+
+# <codecell>
+
+includePattern = r'#include ([\<"])(.*?)[\>"]'
+
+def isInclude(includePath, path):
+    return os.path.exists(os.path.join(includePath, path))
+
+def includeReplacer(match, pairs):
+    matchPath = match.group(2)
+    for pair in pairs:
+        path = matchPath
+        if len(pair) == 2:
+            (includePath, subPath) = pair
+        else:
+            (includePath, subPath, parentPath) = pair
+            if path.startswith(parentPath):
+                path = os.path.relpath(parentPath)
+        if isInclude(includePath, path):
+            #TODO Replace with same character?
+            return "#include <{}>".format(os.path.join(subPath, path))
+    return match.group(0)
+
+# <codecell>
+
+def includeSub(text, pairs):
+    includeReplacerLocal = lambda match: includeReplacer(match, pairs)
+    return re.sub(includePattern, includeReplacerLocal, text)
+
+def includeSubPairs(text, includePairs):
+    for pieces in includePairs:
+        text = includeSub(text, pairs)
+    return text
+
+def includeSubFilePairs(filePath, includePairs):
+    text = file_read(filePath)
+    text = includeSubPairs(text, includePairs)
+    file_write(filePath, text)
+
+# <codecell>
+
+parser = argparse.ArgumentParser(description = 'Sub in include files based on paths')
+parser.add_argument('-n', '--dry-run', action = 'store_true')
+parser.add_argument('pair_yaml', type = str)
+parser.add_argument('files', type = str, nargs = '+')
+
+# <codecell>
+
+#args = parser.parse_args(['-n', '[[include/rbdl, chimful, rbdl], [vendor/UnitTest++/src, biscuit]]', 'tests/main.cc', 'src/Contacts.cc'])
+#print args
+#test = """#include <Logging.h>"""
+#print includeSubPairs(test, (("include/rbdl", "chimful", "rbdl")))
+
+# <codecell>
+
+args = parser.parse_args()
+
+doSave = not args.dry_run
+pairs = yaml.load(args.pair_yaml)
+
+# <codecell>
+
+for filePath in args.files:
+    includeSubFilePairs(filePath, pairs)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.