Example #1
0
    def __init__(self, details=False, kind=LIZARD_REPOSITORY):
        self.details = details
        self.kind = kind

        if kind == LIZARD_REPOSITORY:
            self.analyzer = Lizard()
        else:
            self.analyzer = SCC()
Example #2
0
    def __init__(self, details=False, kind=LIZARD_FILE):
        self.details = details
        self.kind = kind

        if self.kind == LIZARD_FILE:
            self.cloc = Cloc()
            self.lizard = Lizard()
        else:
            self.scc = SCC()
Example #3
0
    def test_analyze_error(self, check_output_mock):
        """Test whether an exception is thrown in case of errors"""

        check_output_mock.side_effect = subprocess.CalledProcessError(
            -1, "command", output=b'output')

        scc = SCC()
        kwargs = {'repository_path': self.tmp_data_path,
                  'repository_level': True,
                  'files_affected': [],
                  'details': False}
        _ = scc.analyze(**kwargs)
Example #4
0
    def test_analyze_file_details(self):
        """Test whether SCC returns the expected fields data for files"""

        scc = SCC()
        kwargs = {'file_path': os.path.join(self.tmp_data_path, ANALYZER_TEST_FILE),
                  'details': True}
        result = scc.analyze(**kwargs)

        self.assertIn('ccn', result)
        self.assertTrue(type(result['ccn']), int)
        self.assertIn('loc', result)
        self.assertTrue(type(result['loc']), int)
        self.assertIn('comments', result)
        self.assertTrue(type(result['comments']), int)
        self.assertIn('blanks', result)
        self.assertTrue(type(result['blanks']), int)
Example #5
0
class FileAnalyzer:
    """Class to analyse the content of files"""

    ALLOWED_EXTENSIONS = ['java', 'py', 'php', 'scala', 'js', 'rb', 'cs', 'cpp', 'c', 'lua', 'go', 'swift']
    FORBIDDEN_EXTENSIONS = ['tar', 'bz2', "gz", "lz", "apk", "tbz2",
                            "lzma", "tlz", "war", "xar", "zip", "zipx"]

    def __init__(self, details=False, kind=LIZARD_FILE):
        self.details = details
        self.kind = kind

        if self.kind == LIZARD_FILE:
            self.cloc = Cloc()
            self.lizard = Lizard()
        else:
            self.scc = SCC()

    def analyze(self, file_path):
        """Analyze the content of a file using CLOC, Lizard and SCC

        :param file_path: file path

        :returns a dict containing the results of the analysis, like the one below
        {
          'blanks': ..,
          'comments': ..,
          'loc': ..,
          'ccn': ..,
          'avg_ccn': ..,
          'avg_loc': ..,
          'avg_tokens': ..,
          'num_funs': ..,
          'tokens': ..,
          'funs': [..]
        }
        """
        kwargs = {'file_path': file_path}

        if self.kind == LIZARD_FILE:
            cloc_analysis = self.cloc.analyze(**kwargs)

            if GraalRepository.extension(file_path) not in self.ALLOWED_EXTENSIONS:
                return cloc_analysis

            kwargs['details'] = self.details
            file_analysis = self.lizard.analyze(**kwargs)
            # the LOC returned by CLOC is replaced by the one obtained with Lizard
            # for consistency purposes

            file_analysis['blanks'] = cloc_analysis['blanks']
            file_analysis['comments'] = cloc_analysis['comments']
        else:
            file_analysis = self.scc.analyze(**kwargs)

        return file_analysis
Example #6
0
    def test_analyze_repository(self):
        """Test whether SCC returns the expected fields data for repository"""

        scc = SCC()
        kwargs = {'repository_path': self.tmp_data_path,
                  'repository_level': True,
                  'files_affected': [],
                  'details': False}
        result = scc.analyze(**kwargs)

        for language in result.keys():
            language_result = result[language]
            self.assertIn('ccn', language_result)
            self.assertTrue(type(language_result['ccn']), int)
            self.assertIn('loc', language_result)
            self.assertTrue(type(language_result['loc']), int)
            self.assertIn('blanks', language_result)
            self.assertTrue(type(language_result['blanks']), int)
            self.assertIn('comments', language_result)
            self.assertTrue(type(language_result['comments']), int)
            self.assertIn('total_files', language_result)
            self.assertTrue(type(language_result['total_files']), int)
Example #7
0
class RepositoryAnalyzer:
    """Class to analyse the content of a repository

    param kind: the analyzer kind (e.g., Lizard, SCC)
    """

    def __init__(self, details=False, kind=LIZARD_REPOSITORY):
        self.details = details
        self.kind = kind

        if kind == LIZARD_REPOSITORY:
            self.analyzer = Lizard()
        else:
            self.analyzer = SCC()

    def analyze(self, repository_path, files_affected):
        """Analyze the content of a repository using SCC or Lizard.

        :param repository_path: repository path

        :returns a list containing the results of the analysis
        [ {
          'loc': ..,
          'ccn': ..,
          'tokens': ..,
          'num_funs': ..,
          'file_path': ..,
          'in_commit': ..,
          'blanks': ..,
          'comments': ..,
          },
          ...
        ]
        """
        kwargs = {
            'repository_path': repository_path,
            'repository_level': True,
            'files_affected': files_affected,
            'details': self.details
        }

        repository_analysis = self.analyzer.analyze(**kwargs)

        return repository_analysis