Commits

John Chandler  committed 8f7f980

Quick caller script added to glue the bits together

  • Participants
  • Parent commits 62acec4

Comments (0)

Files changed (5)

File go_qadpid.py

+#!/usr/bin/env python
+"""
+qadpid - takes a PHP project directory structure and generates a DOT representation of the dependencies
+
+Copyright 2011 John Chandler
+This software is distributed under the terms of the GNU General Public License version 3
+"""
+import os
+import sys
+from optparse import OptionParser
+
+from qadpid.hierarchy import PHPHierarchyCreator
+from qadpid.parser    import PHPFileParser
+from qadpid.writer    import DOTWriter
+
+
+def source_directory( source_dir ):
+    """ Generator returning the next location to look at """
+    dir_walker = os.walk( source_dir )
+
+    while True:
+        yield dir_walker.next()
+
+
+if __name__ == "__main__":    
+    parser = OptionParser( "usage: %prog [options] dir" )
+    parser.add_option( "-o", "--output", dest="output_filename", help="write DOT output to FILE", metavar="FILE")
+
+    options, args = parser.parse_args()
+
+    if len(args) != 1:
+        parser.error( "no directory specified!" )
+
+    # set output
+    if options.output_filename:
+        output_file = open(options. output_filename, "w" )
+    else:
+        output_file = sys.stdout
+
+    # set things up
+    parser  = PHPFileParser()
+    creator = PHPHierarchyCreator( parser=parser )
+
+    # walk source directory structure
+    for location in source_directory( args[0] ):
+        current_directory = location[0]
+
+        #print "walking %s" % str(current_directory)
+        creator.add( location )
+
+    # write file
+    writer = DOTWriter( creator.hierarchy )
+    writer.write( output_file )
+    

File qadpid/hierarchy.py

         for filepath in [ os.path.join( dirpath, x ) for x in files if x.endswith(".php") ]:
             dependencies = []
             for dep_type, dep_filepath in self.parser.parse( self.reader(filepath) ):
-                dependencies.append( ( dep_type, os.path.join(dirpath, dep_filepath) ) )
+                dependencies.append( ( dep_type, os.path.normpath( os.path.join(dirpath, dep_filepath) ) ) )
+                
             self.hierarchy[ filepath ] = dependencies

File qadpid/writer.py

 
         # Relationships
         for filename in self._code_hierarchy:
-            for dependency in self._code_hierarchy[filename]:
-                file_object.write( "  %s -> %s;\n" % ( labels[dependency], labels[filename] ) )
+            for dep_type, dep_filename in self._code_hierarchy[filename]:
+                try:
+                    file_object.write( "  %s -> %s;\n" % ( labels[dep_filename], labels[filename] ) )
+                except KeyError:
+                    # Doesn't current handle include/require files without the .php suffix, so ignore
+                    pass
 
         # Footer
         file_object.write( "}\n" )

File tests/test_hierarchy.py

         self.creator.reader = fake_reader  # replace our reader for the test
         self.creator.add( (".", [], ["foo.php", "bar.php"]) )
 
-        self.assertEquals( self.creator.hierarchy, { "./foo.php": [ ("include", "./bar.php") ],
+        self.assertEquals( self.creator.hierarchy, { "./foo.php": [ ("include", "bar.php") ],
                                                      "./bar.php": []
                                                    } )
         
+    def test_parent_dir( self ):
+        """ Tests when we have a parent dir reference """
+        def fake_reader( filepath ):
+            """ Simulates text in the foo.php file """
+            if filepath == "./wibble/foo.php":
+                return 'some php code\ninclude "../bar.php"\nmore php\n'
+            return ""
+        
+        self.creator.reader = fake_reader  # replace our reader for the test
+        self.creator.add( (".",        [], ["bar.php"]) )
+        self.creator.add( ("./wibble", [], ["foo.php"]) )
+
+        self.assertEquals( self.creator.hierarchy.get("./wibble/foo.php"), [("include", "bar.php")] )
 
 
 if __name__ == "__main__":

File tests/test_parser.py

         """ Test for all-in-one string - intended for debugging something, but useful to add more confidence """
         php_code = 'some php code\ninclude "bar.php"\nmore php\n'
         self.assertEquals( self.parser.parse(php_code), [ ("include", "bar.php") ] )
-        
+
 
 if __name__ == "__main__":
     unittest.main()