Change command parsing and do file walking manually
authorSverre Rabbelier <srabbelier@gmail.com>
Thu, 27 Nov 2008 23:40:30 +0000
changeset 598 c1f9435bb645
parent 597 66088092f849
child 599 32a30f609530
Change command parsing and do file walking manually Also print warnings to stderr Patch by: Sverre Rabbelier
app/check_includes.py
--- a/app/check_includes.py	Thu Nov 27 21:57:24 2008 +0000
+++ b/app/check_includes.py	Thu Nov 27 23:40:30 2008 +0000
@@ -6,6 +6,10 @@
 import os
 import graph
 
+
+ROOTDIR = "soc"
+
+
 def parseFile(file_name):
   if os.path.exists("imports.dat"):
     log = open("imports.dat", "r")
@@ -34,7 +38,7 @@
 
   for idx, imp in enumerate(imports):
     if imp in set(imports[idx+1:]):
-      print "Warning: file '%s' has a redundant import: '%s'." % (file_name, imp)
+      sys.stderr.write("Warning: file '%s' has a redundant import: '%s'.\n" % (file_name, imp))
 
   if file_name.endswith("__init__.py"):
     normalized = file_name[:-12].replace('/', '.')
@@ -52,7 +56,9 @@
 
 
 def buildGraph():
-  import graph
+  if not os.path.exists("imports.dat"):
+    sys.stderr.write("Missing imports.dat file, run 'build' first\n")
+    return
 
   log = open("imports.dat", "r")
   all_imports = cPickle.load(log)
@@ -136,36 +142,53 @@
   gv.render(gvv,'png','imports.png')
 
 
-def main(args):
-  if len(args) == 1 and args[0].startswith("."):
-    gr = buildGraph()
-    if args[0] == '.':
-      return showGraph(gr)
-
-    if args[0] == '..':
-      return drawGraph(gr)
+def accumulate(arg, dirname, fnames):
+  for fname in fnames:
+    if not fname.endswith(".py"):
+      continue
 
-    if args[0] == '...':
-      cycles = findCycles(gr)
-      for cycle in cycles:
-        print cycle
-      return 0
+    arg.append(os.path.join(dirname, fname))
+
 
-  if not args:
-    print "Please specify a filename to parse, or '.' to list the parsed imports"
+def main(args):
+  if len(args) != 1:
+    print "Supported options: 'print', 'build', 'find', and 'draw'."
     return -1
 
-  res = 0
+  action = args[0]
+
+  if action == "build":
+    fnames = []
+    os.path.walk(ROOTDIR, accumulate, fnames)
+
+    for fname in fnames:
+      parseFile(fname)
+
+    print "Done parsing."
+    return 0
+
+  gr = buildGraph()
+  if not gr:
+    return 1
 
-  for file_name in args:
-    res += parseFile(file_name)
+  if action == "show":
+    return showGraph(gr)
+
+  if action == "draw":
+    return drawGraph(gr)
 
-  print "Done parsing."
+  if action == "find":
+    cycles = findCycles(gr)
+    for cycle in cycles:
+      print cycle
+    return 0
 
-  return res
+  print "Unknown option."
+  return -1
+
 
 if __name__ == '__main__':
   import sys
+  os.chdir("../app")
   res = main(sys.argv[1:])
-  sys.exit(res)
-
+  sys.exit(0)