Source

python-stdlib / stdlib.py

"""
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', 
               'Lib/distutils', 
               'Lib/test/test_distutils.py'],
 'json': ['Doc/library/json.rst',
          'Lib/json',
          'Lib/test/json_tests',
          'Lib/test/test_json.py']}
"""

# --- 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
  else:
    return ''


if __name__ == '__main__':
  for test in [
      'Lib/json',
      'Lib/json/',
      'Lib/json/foobar',
      'Doc/library/json.rst']:
    if getmodname(test) != 'json':
      print('Test failed.')
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.