Clean up some of our python scripts.

Change-Id: Ifa75345db43434298cfb6113fbe2f7a33b88c79d
diff --git a/libc/kernel/tools/clean_header.py b/libc/kernel/tools/clean_header.py
index 6601817..ebebe80 100755
--- a/libc/kernel/tools/clean_header.py
+++ b/libc/kernel/tools/clean_header.py
@@ -199,8 +199,7 @@
         if opt == '-u':
             noUpdate = 0
         elif opt == '-v':
-            verbose = 1
-            D_setlevel(1)
+            logging.basicConfig(level=logging.DEBUG)
         elif opt == '-k':
             kernel_original_path = arg
         elif opt == '-d':
diff --git a/libc/kernel/tools/cpp.py b/libc/kernel/tools/cpp.py
index 2be9532..0c098de 100644
--- a/libc/kernel/tools/cpp.py
+++ b/libc/kernel/tools/cpp.py
@@ -1711,7 +1711,7 @@
         while j < n and not blocks[j].isIf():
             j += 1
         if j > i:
-            D2("appending lines %d to %d" % (blocks[i].lineno, blocks[j-1].lineno))
+            logging.debug("appending lines %d to %d" % (blocks[i].lineno, blocks[j-1].lineno))
             result += blocks[i:j]
         if j >= n:
             break
@@ -1730,17 +1730,17 @@
                 break
             dir = blocks[j].directive
             if dir == "endif":
-                D2("remove 'if 0' .. 'endif' (lines %d to %d)" % (blocks[i].lineno, blocks[j].lineno))
+                logging.debug("remove 'if 0' .. 'endif' (lines %d to %d)" % (blocks[i].lineno, blocks[j].lineno))
                 i = j + 1
             elif dir == "else":
                 # convert 'else' into 'if 1'
-                D2("convert 'if 0' .. 'else' into 'if 1' (lines %d to %d)" % (blocks[i].lineno, blocks[j-1].lineno))
+                logging.debug("convert 'if 0' .. 'else' into 'if 1' (lines %d to %d)" % (blocks[i].lineno, blocks[j-1].lineno))
                 blocks[j].directive = "if"
                 blocks[j].expr      = CppExpr( CppLineTokenizer("1").toTokenList() )
                 i = j
             elif dir == "elif":
                 # convert 'elif' into 'if'
-                D2("convert 'if 0' .. 'elif' into 'if'")
+                logging.debug("convert 'if 0' .. 'elif' into 'if'")
                 blocks[j].directive = "if"
                 i = j
             continue
@@ -1749,25 +1749,25 @@
         k = find_matching_endif( blocks, j+1 )
         if k >= n:
             # unterminated #if 1, finish here
-            D2("unterminated 'if 1'")
+            logging.debug("unterminated 'if 1'")
             result += blocks[j+1:k]
             break
 
         dir = blocks[k].directive
         if dir == "endif":
-            D2("convert 'if 1' .. 'endif' (lines %d to %d)"  % (blocks[j].lineno, blocks[k].lineno))
+            logging.debug("convert 'if 1' .. 'endif' (lines %d to %d)"  % (blocks[j].lineno, blocks[k].lineno))
             result += optimize_if01(blocks[j+1:k])
             i       = k+1
         elif dir == "else":
             # convert 'else' into 'if 0'
-            D2("convert 'if 1' .. 'else' (lines %d to %d)"  % (blocks[j].lineno, blocks[k].lineno))
+            logging.debug("convert 'if 1' .. 'else' (lines %d to %d)"  % (blocks[j].lineno, blocks[k].lineno))
             result += optimize_if01(blocks[j+1:k])
             blocks[k].directive = "if"
             blocks[k].expr      = CppExpr( CppLineTokenizer("0").toTokenList() )
             i = k
         elif dir == "elif":
             # convert 'elif' into 'if 0'
-            D2("convert 'if 1' .. 'elif' (lines %d to %d)" % (blocks[j].lineno, blocks[k].lineno))
+            logging.debug("convert 'if 1' .. 'elif' (lines %d to %d)" % (blocks[j].lineno, blocks[k].lineno))
             result += optimize_if01(blocks[j+1:k])
             blocks[k].expr      = CppExpr( CppLineTokenizer("0").toTokenList() )
             i = k
@@ -1835,7 +1835,6 @@
     out = StringOutput()
     lines = string.split(text, '\n')
     list = BlockParser().parse( CppLinesTokenizer(lines) )
-    #D_setlevel(2)
     list.replaceTokens( kernel_token_replacements )
     list.optimizeAll( {"__KERNEL__":kCppUndefinedMacro} )
     list.write(out)
diff --git a/libc/kernel/tools/utils.py b/libc/kernel/tools/utils.py
index 0478e93..e5a310e 100644
--- a/libc/kernel/tools/utils.py
+++ b/libc/kernel/tools/utils.py
@@ -1,59 +1,29 @@
 # common python utility routines for the Bionic tool scripts
 
-import sys, os, commands, string, commands
+import commands
+import logging
+import os
+import string
+import sys
 
-# basic debugging trace support
-# call D_setlevel to set the verbosity level
-# and D(), D2(), D3(), D4() to add traces
-#
-verbose = 0
 
 def panic(msg):
-    sys.stderr.write( find_program_name() + ": error: " )
-    sys.stderr.write( msg )
+    sys.stderr.write(os.path.basename(sys.argv[0]) + ": error: ")
+    sys.stderr.write(msg)
     sys.exit(1)
 
-def D(msg):
-    global verbose
-    if verbose > 0:
-        print msg
-
-def D2(msg):
-    global verbose
-    if verbose >= 2:
-        print msg
-
-def D3(msg):
-    global verbose
-    if verbose >= 3:
-        print msg
-
-def D4(msg):
-    global verbose
-    if verbose >= 4:
-        print msg
-
-def D_setlevel(level):
-    global verbose
-    verbose = level
-
-
-#  other stuff
-#
-#
-def find_program_name():
-    return os.path.basename(sys.argv[0])
 
 def find_program_dir():
     return os.path.dirname(sys.argv[0])
 
+
 class StringOutput:
     def __init__(self):
         self.line = ""
 
     def write(self,msg):
         self.line += msg
-        D2("write '%s'" % msg)
+        logging.debug("write '%s'" % msg)
 
     def get(self):
         return self.line
@@ -76,47 +46,6 @@
             continue
         os.mkdir(dir)
 
-def walk_source_files(paths,callback,args,excludes=[]):
-    """recursively walk a list of paths and files, only keeping the source files in directories"""
-    for path in paths:
-        if len(path) > 0 and path[0] == '@':
-            # this is the name of another file, include it and parse it
-            path = path[1:]
-            if os.path.exists(path):
-                for line in open(path):
-                    if len(line) > 0 and line[-1] == '\n':
-                        line = line[:-1]
-                    walk_source_files([line],callback,args,excludes)
-            continue
-        if not os.path.isdir(path):
-            callback(path,args)
-        else:
-            for root, dirs, files in os.walk(path):
-                #print "w-- %s (ex: %s)" % (repr((root,dirs)), repr(excludes))
-                if len(excludes):
-                    for d in dirs[:]:
-                        if os.path.join(root,d) in excludes:
-                            dirs.remove(d)
-                for f in files:
-                    r, ext = os.path.splitext(f)
-                    if ext in [ ".h", ".c", ".cpp", ".S" ]:
-                        callback( "%s/%s" % (root,f), args )
-
-def cleanup_dir(path):
-    """create a directory if needed, and ensure that it is totally empty
-       by removing any existing content in it"""
-    if not os.path.exists(path):
-        os.mkdir(path)
-    else:
-        for root, dirs, files in os.walk(path, topdown=False):
-            if root.endswith("kernel_headers/"):
-                # skip 'kernel_headers'
-                continue
-            for name in files:
-                os.remove(os.path.join(root, name))
-            for name in dirs:
-                os.rmdir(os.path.join(root, name))
-
 
 class BatchFileUpdater:
     """a class used to edit several files at once"""