python-stdlib /

Task 001:

  Given a relative path from the Python checkout, detect to
which module it belongs.

Task 002:

  Generate module definition file directly from Python source
or at least validate existing definition file against it to
ensure no modules are missing.

Task 003:

  Find modified files in Python installation (to create
patches be sent upstream).
import os
import json

DIR = os.path.abspath(os.path.dirname(__file__))
MODULES = json.load(open(DIR + '/stdlib.json'))

{'distutils': ['Doc/distutils', 
 'json': ['Doc/library/json.rst',

# --- helpers ---
def pathsplit(pathstr, maxsplit=1):
    """Split a path into list.

       :param maxsplit:  left for compatibility with os.path.split
    path = [pathstr]
    while True:
        oldpath = path[:]
        path[:1] = list(os.path.split(path[0]))
        if path[0] == '':
            path = path[1:]
        elif path[1] == '':
            path = path[:1] + path[2:]
        if path == oldpath:
            return path
        if maxsplit is not None and len(path) > maxsplit:
            return path
# --- /helpers ---

def getmodname(path):
  Return module name for the module from standard library given
  its relative path from source checkout directory.
  [ ] unify source checkout and target installation layout for
      standard library
  global MODULES
  path = pathsplit(path, None)
  for m in MODULES:
    for p in MODULES[m]:
      p = pathsplit(p, None)
      if path[:len(p)] == p:
        return m
    return ''

if __name__ == '__main__':
  for test in [
    if getmodname(test) != 'json':
      print('Test failed.')