Exemple #1
0
    def test_directory_cache_twice(self):
        """
        Setup two caches in a row.

        The second run will reload the same cache directory.
        """
        cache = parser.directory_cache_t(directory=self.cache_dir)
        parser.parse([self.header], self.config, cache=cache)
        cache = parser.directory_cache_t(directory=self.cache_dir)
        parser.parse([self.header], self.config, cache=cache)
    def test_directory_cache_twice(self):
        """
        Setup two caches in a row.

        The second run will reload the same cache directory.
        """
        cache = parser.directory_cache_t(directory=self.cache_dir)
        parser.parse([self.header], self.config, cache=cache)
        cache = parser.directory_cache_t(directory=self.cache_dir)
        parser.parse([self.header], self.config, cache=cache)
Exemple #3
0
 def test_directory_existing_dir(self):
     """
     Setup a cache when there is already a file at the cache's location.
     """
     open(self.cache_dir, "a").close()
     self.assertRaises(
         ValueError,
         lambda: parser.directory_cache_t(directory=self.cache_dir))
     os.remove(self.cache_dir)
 def test_directory_existing_dir(self):
     """
     Setup a cache when there is already a file at the cache's location.
     """
     open(self.cache_dir, "a").close()
     self.assertRaises(
         ValueError,
         lambda: parser.directory_cache_t(directory=self.cache_dir))
     os.remove(self.cache_dir)
Exemple #5
0
    def test_directory_cache_without_compression(self):
        """
        Test the directory cache without compression

        """
        # Test with compression OFF
        cache = parser.directory_cache_t(directory=self.cache_dir)
        # Generate a cache on first read
        parser.parse([self.header], self.config, cache=cache)
        # Read from the cache the second time
        parser.parse([self.header], self.config, cache=cache)
    def test_directory_cache_without_compression(self):
        """
        Test the directory cache without compression

        """
        # Test with compression OFF
        cache = parser.directory_cache_t(directory=self.cache_dir)
        # Generate a cache on first read
        parser.parse([self.header], self.config, cache=cache)
        # Read from the cache the second time
        parser.parse([self.header], self.config, cache=cache)
Exemple #7
0
    def test_directory_cache(self):
        """
        Test the directory cache

        """

        cache = parser.directory_cache_t(
            dir="unittests/data/directory_cache_test")
        # Generate a cache on first read
        parser.parse([self.header], self.config, cache=cache)
        # Read from the cache the second time
        parser.parse([self.header], self.config, cache=cache)
Exemple #8
0
    def test_dir_compatibility(self):
        """
        For retro-compatibility, test if the dir argument is still working

        This test can be removed in v2.0.0.

        """

        # Do not clutter the tests with warnings
        warnings.simplefilter("ignore", DeprecationWarning)

        cache = parser.directory_cache_t(dir=self.cache_dir, compression=True)
        parser.parse([self.header], self.config, cache=cache)

        # Reset this warning to always
        warnings.simplefilter("error", DeprecationWarning)
    def test_dir_compatibility(self):
        """
        For retro-compatibility, test if the dir argument is still working

        This test can be removed in v2.0.0.

        """

        # Do not clutter the tests with warnings
        warnings.simplefilter("ignore", DeprecationWarning)

        cache = parser.directory_cache_t(dir=self.cache_dir, compression=True)
        parser.parse([self.header], self.config, cache=cache)

        # Reset this warning to always
        warnings.simplefilter("error", DeprecationWarning)
Exemple #10
0
   def parse(self):
      """Parse the header files and setup the known declarations.
      
      Currently this method can only be called once.
      This method can be called anytime after initialization and all
      Template() calls have been made.

      @returns: Returns the root of the declaration tree
      @rtype: L{IDecl<declwrapper.IDecl>}
      @postcondition: This class can act as a wrapper for namespace("::") and all declarations are set to be ignored.
      """
      if self.mHeaderFiles==[]:
         raise ValueError, "No header files specified"
      
      if self.mVerbose:
         print "Parsing headers: ", self.mHeaderFiles

      # Record the time when parsing started...
      self.mStartTime = time.time()

      # Create and initialize the config object
      parser_cfg = parser.config_t(self.mGccXmlPath,
                                   self.mWorkingDir,
                                   self.mIncludePaths,
                                   define_symbols=self.mDefines,
                                   undefine_symbols=self.mUndefines,
                                   start_with_declarations=None)

      full_header_list = self.mHeaderFiles[:]
      
      # Handle template instantiation as needed      
      temp_file, temp_filename = (None,None)
      template_instantiation_text = self.buildTemplateFileContents()
      if None != template_instantiation_text:
         temp_filename = pygccxml.utils.create_temp_file_name(suffix=".h")
         temp_file = file(temp_filename, 'w')
         temp_file.write(template_instantiation_text)
         temp_file.close()
         if self.mVerbose:
            print "   creating template instantiation file: ", temp_filename
         full_header_list.append(temp_filename)      

      # Create the cache object...
      if self.mCacheDir!=None:
         if self.mVerbose:
            print "Using directory cache in '%s'"%self.mCacheDir
         cache = parser.directory_cache_t(self.mCacheDir)
      elif self.mCacheFile!=None:
         if self.mVerbose:
            print "Using file cache '%s'"%self.mCacheFile
         cache = parser.file_cache_t(self.mCacheFile)         
      else:
         if self.mVerbose:
            print "No cache in use"
         cache = None

      # Create the parser object...
      the_parser = parser.project_reader_t(config=parser_cfg, 
                                           cache=cache,
                                           decl_factory=decl_wrappers.dwfactory_t())
      # ...and parse the headers
      parsed_decls = the_parser.read_files(full_header_list, 
                                           parser.project_reader.COMPILATION_MODE.FILE_BY_FILE)
      
      assert len(parsed_decls) == 1    # assume that we get root of full tree
      self.mDeclRoot = parsed_decls[0]
      
      # Parse the files and add to decl root
      # - then traverse tree setting everything to ignore
      self.mDeclRootWrapper = DeclWrapper(self.mDeclRoot)
      # Set the module builder instance (this is done here and not in the
      # constructor so that Allen's DeclWrapper object still work as well)
      self.mDeclRootWrapper.modulebuilder = self
      self.mDeclRootWrapper.ignore()
      
      # Cleanup
      if temp_filename:
         pygccxml.utils.remove_file_no_raise( temp_filename )
      
      typedef_decls = declarations.make_flatten(parsed_decls)
      typedef_decls = decls = filter( lambda x: (isinstance( x, declarations.typedef_t ) and 
                                                 not x.name.startswith('__') and 
                                                 x.location.file_name != "<internal>")
                                      , typedef_decls )

      self.mTypeDefMap = {}
      for d in typedef_decls:
         type_def_name = d.name
         full_name = declarations.full_name(d)
         if full_name.startswith("::"):    # Remove the base namespace
            full_name = full_name[2:]
         real_type_name = d.type.decl_string
         if real_type_name.startswith("::"):    # Remove base namespace
            real_type_name = real_type_name[2:]   
         self.mTypeDefMap[full_name] = real_type_name

      self.mParseEndTime = time.time()
      if self.mVerbose:
         print "Completed parsing in %s."%self._time2str(self.mParseEndTime-self.mStartTime)

      return self.mDeclRootWrapper
Exemple #11
0
    def parse(self):
        """Parse the header files and setup the known declarations.
      
      Currently this method can only be called once.
      This method can be called anytime after initialization and all
      Template() calls have been made.

      :rtype: Returns the root of the declaration tree
      @rtype: L{IDecl<declwrapper.IDecl>}
      @postcondition: This class can act as a wrapper for namespace("::") and all declarations are set to be ignored.
      """
        if self.mHeaderFiles == []:
            raise ValueError, "No header files specified"

        if self.mVerbose:
            print "Parsing headers: ", self.mHeaderFiles

        # Record the time when parsing started...
        self.mStartTime = time.time()

        # Create and initialize the config object
        parser_cfg = parser.config_t(self.mGccXmlPath,
                                     self.mWorkingDir,
                                     self.mIncludePaths,
                                     define_symbols=self.mDefines,
                                     undefine_symbols=self.mUndefines,
                                     start_with_declarations=None)

        full_header_list = self.mHeaderFiles[:]

        # Handle template instantiation as needed
        temp_file, temp_filename = (None, None)
        template_instantiation_text = self.buildTemplateFileContents()
        if None != template_instantiation_text:
            temp_filename = pygccxml.utils.create_temp_file_name(suffix=".h")
            temp_file = file(temp_filename, 'w')
            temp_file.write(template_instantiation_text)
            temp_file.close()
            if self.mVerbose:
                print "   creating template instantiation file: ", temp_filename
            full_header_list.append(temp_filename)

        # Create the cache object...
        if self.mCacheDir != None:
            if self.mVerbose:
                print "Using directory cache in '%s'" % self.mCacheDir
            cache = parser.directory_cache_t(self.mCacheDir)
        elif self.mCacheFile != None:
            if self.mVerbose:
                print "Using file cache '%s'" % self.mCacheFile
            cache = parser.file_cache_t(self.mCacheFile)
        else:
            if self.mVerbose:
                print "No cache in use"
            cache = None

        # Create the parser object...
        the_parser = parser.project_reader_t(
            config=parser_cfg,
            cache=cache,
            decl_factory=decl_wrappers.dwfactory_t())
        # ...and parse the headers
        parsed_decls = the_parser.read_files(
            full_header_list,
            parser.project_reader.COMPILATION_MODE.FILE_BY_FILE)

        assert len(parsed_decls) == 1  # assume that we get root of full tree
        self.mDeclRoot = parsed_decls[0]

        # Parse the files and add to decl root
        # - then traverse tree setting everything to ignore
        self.mDeclRootWrapper = DeclWrapper(self.mDeclRoot)
        # Set the module builder instance (this is done here and not in the
        # constructor so that Allen's DeclWrapper object still work as well)
        self.mDeclRootWrapper.modulebuilder = self
        self.mDeclRootWrapper.ignore()

        # Cleanup
        if temp_filename:
            pygccxml.utils.remove_file_no_raise(temp_filename)

        typedef_decls = declarations.make_flatten(parsed_decls)
        typedef_decls = decls = filter(
            lambda x:
            (isinstance(x, declarations.typedef_t) and not x.name.startswith(
                '__') and x.location.file_name != "<internal>"), typedef_decls)

        self.mTypeDefMap = {}
        for d in typedef_decls:
            type_def_name = d.name
            full_name = declarations.full_name(d)
            if full_name.startswith("::"):  # Remove the base namespace
                full_name = full_name[2:]
            real_type_name = d.type.decl_string
            if real_type_name.startswith("::"):  # Remove base namespace
                real_type_name = real_type_name[2:]
            self.mTypeDefMap[full_name] = real_type_name

        self.mParseEndTime = time.time()
        if self.mVerbose:
            print "Completed parsing in %s." % self._time2str(
                self.mParseEndTime - self.mStartTime)

        return self.mDeclRootWrapper