Next / Previous / Contents / Shipman's homepage

9.3. report(): Generate one tree's report

This function generates the portion of the output for one directory subtree.
# - - -   r e p o r t   - - -

def report ( dirName ):
    """Generate the report for one directory subtree.

      [ dirName is a string ->
          sys.stdout  +:=  a report listing the files below
              directory (dirName), with files in ascending
              order by pathname ]

At this level, the logic is broken into three steps: write the report header; instantiate a LinkReport object containing information on all the links in the subtree; and call the LinkReport.genLinks() method to generate the lines of the report.
    #-- 1 --
    # [ basePath  :=  dirName's absolute path name
    #   sys.stdout  +:=  report heading showing dirName's real
    #                    absolute path ]
    basePath  =  os.path.abspath ( dirName )
    print "\n   === %s ===" % os.path.realpath ( dirName )

The basePath is the path to the root directory of the subtree, so it will be a prefix to every pathname under it. The report will remove this prefix, showing each link's path name relative to basePath. For details of the report format, see Section 5, “ Find soft links in a directory tree”.
    #-- 2 --
    # [ linkReport  :=  a LinkReport instance describing all the
    #       accessible soft links in directory tree (basePath) ]
    linkReport  =  LinkReport ( basePath )

A LinkReport instance is basically a container for LinkInfo instances. The LinkReport.genLinks() method generates these LinkInfo instances in sorted order. Formatting the report is handled by the .__str__() special method in the LinkInfo class.
    #-- 3 --
    # [ linkReport is a LinkReport instance ->
    #     sys.stdout  +:=  lines describing links in linkReport
    #                      in ascending order by path name ]
    for  linkInfo in linkReport.genLinks():
        print linkInfo