Commits

anatoly techtonik  committed ccf19a0

Add "stdlib.py check" - check missing files from Python stdlib module definition, show stats

  • Participants
  • Parent commits 3374ea1

Comments (0)

Files changed (1)

 
 # --- helpers ---
 def pathsplit(pathstr, maxsplit=1):
-    """Split a path into list.
+    """
+    Split a path into list.
 
-       :param maxsplit:  left for compatibility with os.path.split
+    :param maxsplit:  left for compatibility with os.path.split
     """
     path = [pathstr]
     while True:
             return path
         if maxsplit is not None and len(path) > maxsplit:
             return path
+
+def filepathgen(rootdir):
+  """
+  Generate relative full filename paths starting from rootdir
+  """
+  for root, dirs, files in os.walk(rootdir):
+    # calculate relative root
+    relroot = root.replace(rootdir, '', 1)
+    if relroot.startswith(os.sep):  # may happen after replace
+      relroot = relroot.replace(os.sep, '', 1)
+    for f in files:   # dirs are not checked against map
+      yield os.path.join(relroot, f)
+
+def echo(msg):
+    """
+    Print msg to the screen without linefeed and flush the output.
+    
+    Standard print() function doesn't flush, see:
+    https://groups.google.com/forum/#!topic/python-ideas/8vLtBO4rzBU
+    """
+    sys.stdout.write(msg)
+    sys.stdout.flush()
+
 # --- /helpers ---
 
 
     return ''
 
 
+def checklib(srcroot):
+  """
+  Given path to the source root directory:
+    [x] calculate stdlib coverage % for current module definition
+    [ ] show unknown files
+    [ ] show files that belong to two modules at once
+    [ ] mechanizm to ignore .hg and stuff
+  """
+  FILES = []
+  MATCHED = 0
+  for num, path in enumerate(filepathgen(srcroot)):
+    echo("Reading files: %d\r" % (num+1))
+    FILES.append(path)
+  print("Reading files.. Done.")
+
+  if not FILES:
+    sys.exit("Error: No files found at '%s'" % srcroot)
+
+  for path in FILES:
+    name = getmodname(path)
+    if name:
+      print(name, path)
+      MATCHED += 1
+
+  print("Total: %s, Matched: %s, Coverage: %.2f%%" %
+        (len(FILES), MATCHED, float(MATCHED)/len(FILES)))
+  # [ ] .json entries that were not found
+  #  [ ] id for .json entries
+
+
 usage = """\
 stdlib.py v%s
 usage: stdlib.py <command> [params]
 
 commands:
-  check     - check Python sources, show stats
+  check     - check sources for Python stdlib, show stats
   test      - run internal tests \
 """ % __version__
 
     print("Tests passed ok.")
 
   elif sys.argv[1] == 'check':
-   # [ ]
-   sys.exit("TODO")
+   if not 'pyconfig.h.in' in os.listdir('.'):
+     print("Error: No Python source code checkout is found.")
+     print("       Please run the check from its directory.")
+     sys.exit(-1)
+   
+   checklib(".")
 
   else:
     sys.exit('Error: Unknown command "%s"' % ' '.join(sys.argv[1:]))