Ejemplo n.º 1
0
def get_code_complexity_measures(n1, n2):

	globzipfiles = glob.glob('../../../NewData_html/Nova/github_archives/nova-*.zip')
	for zipfile2 in globzipfiles[int(n1):int(n2)] :
		print zipfile2
		commit_id = zipfile2.split('/')[6][:-4]
		
		outf1 = open('/media/mukherjee/OpenStack_Ext_Ha/Nova/Functionname_complexity/'+str(commit_id)+'.txt', 'w')
		outf2 = open('/media/mukherjee/OpenStack_Ext_Ha/Nova/Filename_complexity/'+str(commit_id)+'.txt', 'w')

		print >> outf1, 'filename|commit_id|function_name|function_letter|function_startline|function_endline|function_complexity'

		print >> outf2, 'filename|commit_id|MI|LOC|LLOC|SLOC|multi|blank|single_comments|halstead_volume|cyclomatic_complexity'

		with zipfile.ZipFile(zipfile2) as z:
			for filename in z.namelist():
				if filename.endswith(".py") :
					if not os.path.isdir(filename):

# iter through filenames starting from the current directory
# you can pass ignore or exclude patterns here (as strings)
# for example: ignore='tests,docs'

						with z.open(filename) as fobj:
							source = fobj.read()

							# get cc blocks
							try :
								blocks = cc_visit(source)
								if len(blocks) > 0 :
									for k in blocks :
										function_startline = k.lineno
										function_endline = k.endline
										function_letter = k.letter
										function_classname = k.name
										function_complexity = k.complexity

										print >> outf1, '%s|%s|%s|%s|%s|%s|%s' % (filename, commit_id, function_classname, function_letter, function_startline, function_endline, function_complexity)


								# get MI score
								mi = mi_visit(source, True)

								# get raw metrics
								raw = analyze(source)
								LOC = raw[0]
								LLOC = raw[1]
								SLOC = raw[2]
								multi = raw[4]
								blank = raw[5]
								single_comments = raw[6]

								# get MI parameters 
								mi_par = mi_parameters(source)
								halstead_volume = mi_par[0]
								cyclomatic_complexity = mi_par[1]

								print >> outf2, '%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s' % ( filename, commit_id, mi, LOC, LLOC, SLOC, multi, blank, single_comments, halstead_volume, cyclomatic_complexity )
							except SyntaxError,e :
								continue
Ejemplo n.º 2
0
def _number_of_lines(report: Report, filepath: str, library: str,
                     max_entrypoint_count: int):
    """Returns the number of logical lines of code in a given python file
        :filepath: Path of the python file
        :library: relative path of the file
        :max_entypoint_line_count: max value allowed in any entrypoint file
    """

    try:
        with open(filepath, 'r', encoding="utf-8") as file:
            data = file.read()

        lineno = analyze(data).lloc
        if lineno >= max_entrypoint_count:
            report.add(
                Record(
                    WARNING,
                    "Complex entry point. Check: %s | Counted lines: %d | Lines allowed: %d"
                    % (library, lineno, max_entrypoint_count)))

    except UnicodeDecodeError as e:
        report.add(Record(PROBLEM, "UnicodeDecodeError: {}".format(e)))

    except SyntaxError as e:
        if e.msg == 'SyntaxError at line: 1':
            report.add(
                Record(PROBLEM, (
                    "Error parsing file, is your file saved with UTF-8 encoding? "
                    "Make sure it has no BOM. Check: %s") % library))
        else:
            report.add(
                Record(
                    PROBLEM,
                    "Error parsing file, is there a syntax error in your file? Check: %s"
                    % library))
Ejemplo n.º 3
0
def collect_metrics(code):
	d = {key:None for key in keys}
	content = h_visit(code)
	d["uniq_Op"] = content.h1
	d["uniq_Opnd"] = content.h2
	d["total_Op"] = content.N1
	d["total_Opnd"] = content.N2
	d["n"] = float(content.N1) + float(content.N2)
	d["volume"] = content.volume
	d["prolength"] = content.length
	d["difficulty"] = content.difficulty
	d["effort"] = content.effort
	d["bug"] = content.bugs
	d["time_est"] = content.time

	content = analyze(code)
	d["loc"] = content.loc
	d["lOCode"] = content.loc
	d["lOComment"] = content.comments
	d["lOBlank"] = content.blank
	d["lOCodeAndComment"] = float(content.loc) + float(content.comments)

	content = mi_parameters(code)
	d["cc"] = content[1]

	return d
Ejemplo n.º 4
0
def check_code_complexity(lab_answer, lab_key):

    result = {}
    source = inspect.getsource(lab_answer)
    h = rm.h_visit(source)
    hm = rm.h_visit_ast(h)
    v = analyze(source)
    return result
Ejemplo n.º 5
0
    def sloc(self):
        if self._sloc is None:
            try:
                raw = analyze(self._code)
                self._sloc = raw.sloc
            except SyntaxError as e:
                raise MetricsException(
                    'Error while computing Sloc:\n{0!r}'.format(e))

        return self._sloc
Ejemplo n.º 6
0
def raw(exclude=None, ignore=None, summary=False, *paths):
    '''Analyze the given Python modules and compute raw metrics.

    Raw metrics include:

        * LOC: The number of lines of code (total)
        * LLOC: The number of logical lines of code
        * SLOC: The number of source lines of code (not necessarily
            corresponding to the LLOC)
        * comments: The number of Python comment lines
        * multi: The number of lines which represent multi-line strings
        * blank: The number of blank lines (or whitespace-only ones)

    The equation:

        sloc + blanks = loc

    should always hold.

    :param -e, --exclude <str>: Comma separated list of patterns to exclude.
        By default hidden directories (those starting with '.') are excluded.
    :param -i, --ignore <str>: Comma separated list of patterns to ignore.
        Radon won't even descend into those directories.
    :param -s, --summary:  If given, at the end of the analysis display the
        summary of the gathered metrics. Default to False.
    :param paths: The modules or packages to analyze.
    '''
    headers = ['LOC', 'LLOC', 'SLOC', 'Comments', 'Multi', 'Blank']
    sum_metrics = collections.defaultdict(int, zip(headers, [0] * 6))

    for path in iter_filenames(paths, exclude, ignore):
        with open(path) as fobj:
            log(path)
            try:
                mod = analyze(fobj.read())
            except Exception as e:
                log_error(e, indent=1)
                continue
            for header, value in zip(headers, mod):
                log('{0}: {1}', header, value, indent=1)
                sum_metrics[header] = sum_metrics[header] + value
            if not mod.loc:
                continue
            log('- Comment Stats', indent=1)
            comments = mod.comments
            log('(C % L): {0:.0%}', comments / (float(mod.loc) or 1), indent=2)
            log('(C % S): {0:.0%}', comments / (float(mod.sloc) or 1), indent=2)
            log('(C + M % L): {0:.0%}', (comments + mod.multi) / float(mod.loc),
                indent=2)

    if summary:
        log('** Total **')
        for header in sum_metrics:
            log('{0}: {1}', header, sum_metrics[header], indent=1)
Ejemplo n.º 7
0
def radon_test(f):
    filename = 'a1/a1_solution_' + f + '.py'
    with open(filename) as file:
        source = file.read()
        cv = ComplexityVisitor.from_code(source)
        res = sorted_results(cv.functions + cv.classes, order=LINES)
        output = {}
        for r in res:
            # print(f'Function: {r.name}, CC: {r.complexity}')
            output['CC'] = r.complexity

        res = analyze(source)
        # pprint(res)

        basic = {'loc': res[0],
                 'lloc': res[1],
                 'sloc': res[2],
                 'comments': res[3],
                 'multi': res[4],
                 'blank': res[5],
                 'single_comment': res[6]}
        output['Lines'] = basic

        config = Config(min='A',
                        max='F',
                        exclude=None,
                        ignore=None,
                        no_assert=False,
                        show_closures=False,
                        order=LINES)

        ch = CCHarvester([filename], config)
        res = ch.results
        x = json.loads(ch.as_json())
        # pprint(x)

        res = h_visit(source)

        hals = {'h1': res[0],
                'h2': res[1],
                'N1': res[2],
                'N2': res[3],
                'vocabulary': res[4],
                'length': res[5],
                'calculated_length': res[6],
                'volume': res[7],
                'difficulty': res[8],
                'effort': res[9],
                'time': res[10],
                'bugs': res[11]}

    output['Halstead'] = hals
    pprint({f: output})
Ejemplo n.º 8
0
def build_stats(parent, proj):
    cntr = Counter()
    path = os.path.abspath(os.path.join(parent, proj))
    for fP in iter_filenames([path], exclude="*setup.py"):
        with open(fP, 'r') as fObj:
            sys.stderr.write("analyzing {}\n".format(fP))
            mod = analyze(fObj.read())
            cntr.update(vars(mod))

    rel = subprocess.check_output(
        [sys.executable, "setup.py", "--version"],
        cwd=path).decode("utf-8").strip()
    return (proj, rel, cntr["loc"], cntr["lloc"])
Ejemplo n.º 9
0
 async def analyze(self, tup: NativeBlobMetricInput) -> Iterable[Metric]:
     try:
         r_data = analyze(tup.blob.data.decode())
     except (SyntaxError, UnicodeDecodeError):
         return []  # TODO get an error output?
     result = [
         Metric(
             self.name,
             r_data._asdict(),
             False,
             ObjectIdentifier(tup.blob.id, tup.path),
         )
     ]
     return result
Ejemplo n.º 10
0
Archivo: cli.py Proyecto: b4dtR1p/radon
def analyze_raw(paths, exclude, ignore):
    '''Analyze the files located under `paths`.

    :param paths: A list of paths to analyze.
    :param exclude: A comma-separated string of fnmatch patterns.
    :param ignore: A comma-separated string of patterns to ignore.'''
    for name in iter_filenames(paths, exclude, ignore):
        with open(name) as fobj:
            try:
                yield name, analyze(fobj.read())
            except Exception as e:
                log(name)
                log_error(e, indent=1)
                continue
Ejemplo n.º 11
0
def analyze_raw(paths, exclude, ignore):
    '''Analyze the files located under `paths`.

    :param paths: A list of paths to analyze.
    :param exclude: A comma-separated string of fnmatch patterns.
    :param ignore: A comma-separated string of patterns to ignore.'''
    for name in iter_filenames(paths, exclude, ignore):
        with open(name) as fobj:
            try:
                yield name, analyze(fobj.read())
            except Exception as e:
                log(name)
                log_error(e, indent=1)
                continue
Ejemplo n.º 12
0
    def get_code_metrics(sort_name):
        with open(f"sort_algorithms/{sort_name}/code.txt", "r") as f:
            code = "".join(f.readlines())
            metricas = metrics.h_visit(code)
            mi_parameters = metrics.mi_parameters(code)
            analize = raw.analyze(code)

            return OrderedDict({
                "Nome": sort_name,
                "Número de linhas de código": analize.lloc,
                "Número de operadores": metricas.total.N1,
                "Número de operandos": metricas.total.N2,
                "Dificuldade": metricas.total.difficulty,
                "Esforço": metricas.total.effort,
                "Complexidade ciclomática": mi_parameters[1],
            })
Ejemplo n.º 13
0
 def collect_metrics(self, program_name):
     program_file = open(program_name, 'r')
     code = program_file.read()
     cc_response = cc_visit(code)
     raw_response = analyze(code)
     self.cyclomatic_complexity = None
     if cc_response:
         self.cyclomatic_complexity = cc_response[0].complexity
     self.source_lines_of_code = 0
     self.comments = 0
     if raw_response:
         self.source_lines_of_code = raw_response.sloc
         self.comments = raw_response.comments
     self.user_defined_functions = self.collect_user_defined_functions(code)
     self.program_name = program_name
     self.level = self.collect_level_being_used(code)
Ejemplo n.º 14
0
def test_coding_recipes_complexity(params):
    client = dataikuapi.DSSClient(params["host"], params["api"])
    project = client.get_project(params["project"])

    recipes = project.list_recipes()
    for recipe in recipes:
        if recipe["type"] == "python":
            print(recipe)
            payload = project.get_recipe(
                recipe["name"]).get_definition_and_payload().get_payload()
            code_analysis = cc_raw.analyze(payload)
            print(code_analysis)
            assert code_analysis.loc < 100
            assert code_analysis.lloc < 50
            v = cc_visitors.ComplexityVisitor.from_code(payload)
            assert v.complexity < 21, "Code complexity of recipe " + recipe[
                "name"] + " is too complex: " + v.complexity + " > max value (21)"
Ejemplo n.º 15
0
def mi_parameters(code, count_multi=True):
    '''Given a source code snippet, compute the necessary parameters to
    compute the Maintainability Index metric. These include:
        * the Halstead Volume
        * the Cyclomatic Complexity
        * the number of LLOC (Logical Lines of Code)
        * the percent of lines of comment
    :param multi: If True, then count multiline strings as comment lines as
        well. This is not always safe because Python multiline strings are not
        always docstrings.
    '''
    ast_node = ast.parse(code)
    raw = analyze(code)
    comments_lines = raw.comments + (raw.multi if count_multi else 0)
    comments = comments_lines / float(raw.sloc) * 100 if raw.sloc != 0 else 0
    return (h_visit_ast(ast_node).volume,
            ComplexityVisitor.from_ast(ast_node).total_complexity, raw.lloc,
            comments)
Ejemplo n.º 16
0
    def analyse(self):
        output = {}
        cv = ComplexityVisitor.from_code(self._source)
        res = sorted_results(cv.functions + cv.classes, order=LINES)
        # should be one result, since giving one function
        # if len(res) > 1:
        #     raise ValueError('Complexity Analysis returned multiple results')
        output['cc'] = res[0].complexity

        res = analyze(self._source)
        lines_comments = dict(res._asdict())
        output.update(lines_comments)

        res = h_visit(self._source)
        hals = dict(res._asdict())
        output.update(hals)

        self._res = output
Ejemplo n.º 17
0
def cloneAndReadFileAndGetLoc(repo_path, tag):
    g.checkout(tag)
    print("Lendo arquivos do Repositório e calculando LOC.....")
    global totalLoc
    loc = 0
    for root, dirs, files in os.walk(repo_path):
        for file in files:
            if file.endswith('.py'):
                fullpath = os.path.join(root, file)
                with open(fullpath, encoding="utf8") as f:
                    content = f.read()
                    b = analyze(content)
                    i = 0
                    for item in b:
                        if i == 0:
                            loc += item
                            i += 1
    totalLoc.append(loc)
Ejemplo n.º 18
0
def mi_parameters(code, count_multi=True):
    '''Given a source code snippet, compute the necessary parameters to
    compute the Maintainability Index metric. These include:
        * the Halstead Volume
        * the Cyclomatic Complexity
        * the number of LLOC (Logical Lines of Code)
        * the percent of lines of comment
    :param multi: If True, then count multiline strings as comment lines as
        well. This is not always safe because Python multiline strings are not
        always docstrings.
    '''
    ast_node = ast.parse(code)
    raw = analyze(code)
    comments_lines = raw.comments + (raw.multi if count_multi else 0)
    comments = comments_lines / float(raw.sloc) * 100 if raw.sloc != 0 else 0
    return (h_visit_ast(ast_node).volume,
            ComplexityVisitor.from_ast(ast_node).total_complexity, raw.lloc,
            comments)
def generate_radon_metrics(code_input):
    radon_metrics = make_default_radon_metrics()
    try:
        # raw metrics
        raw_metrics = analyze(code_input)
        radon_metrics['loc'] = raw_metrics.loc
        radon_metrics['lloc'] = raw_metrics.lloc
        radon_metrics['sloc'] = raw_metrics.sloc
        radon_metrics['comments'] = raw_metrics.comments
        radon_metrics['multi'] = raw_metrics.multi
        radon_metrics['single_comments'] = raw_metrics.single_comments

        # cyclomatic complexity
        cc = ComplexityVisitor.from_code(code_input)
        radon_metrics['function_num'] = len(cc.functions)
        total_function_complexity = 0.0
        for fun in cc.functions:
            total_function_complexity += fun.complexity
        radon_metrics['total_function_complexity'] = total_function_complexity
        radon_metrics['radon_functions_complexity'] = cc.functions_complexity

        # calculate based on AST tree
        v = h_visit_ast(h_visit(code_input))
        radon_metrics['h1'] = v.h1
        radon_metrics['h2'] = v.h2
        radon_metrics['N1'] = v.N1
        radon_metrics['N2'] = v.N2
        radon_metrics['vocabulary'] = v.vocabulary
        radon_metrics['length'] = v.length
        radon_metrics['calculated_length'] = v.calculated_length
        radon_metrics['volume'] = v.volume
        radon_metrics['difficulty'] = v.difficulty
        radon_metrics['effort'] = v.effort
        radon_metrics['time'] = v.time
        radon_metrics['bugs'] = v.bugs

        # Maintainability Index (MI) based on
        ## the Halstead Volume, the Cyclomatic Complexity, the SLOC number and the number of comment lines
        mi = mi_visit(code_input, multi=True)
        radon_metrics['Maintainability_Index'] = mi

        return radon_metrics
    except:
        return radon_metrics
def generate_radon_metrics(code_input):
    radon_metrics = make_default_radon_metrics()
    try:
        # raw metrics
        raw_metrics = analyze(code_input)
        radon_metrics['loc'] = raw_metrics.loc
        radon_metrics['lloc'] = raw_metrics.lloc
        radon_metrics['sloc'] = raw_metrics.sloc
        radon_metrics['comments'] = raw_metrics.comments
        radon_metrics['multi'] = raw_metrics.multi
        radon_metrics['single_comments'] = raw_metrics.single_comments

        # cyclomatic complexity
        cc = ComplexityVisitor.from_code(code_input)
        radon_metrics['function_num'] = len(cc.functions)
        total_function_complexity = 0.0
        for fun in cc.functions:
            total_function_complexity += fun.complexity
        radon_metrics['total_function_complexity'] = total_function_complexity
        radon_metrics['radon_functions_complexity'] = cc.functions_complexity

        # calculate based on AST tree
        v = h_visit_ast(h_visit(code_input))
        radon_metrics['h1'] = v.h1
        radon_metrics['h2'] = v.h2
        radon_metrics['N1'] = v.N1
        radon_metrics['N2'] = v.N2
        radon_metrics['vocabulary'] = v.vocabulary
        radon_metrics['length'] = v.length
        radon_metrics['calculated_length'] = v.calculated_length
        radon_metrics['volume'] = v.volume
        radon_metrics['difficulty'] = v.difficulty
        radon_metrics['effort'] = v.effort
        radon_metrics['time'] = v.time
        radon_metrics['bugs'] = v.bugs

        # Maintainability Index (MI) based on
        ## the Halstead Volume, the Cyclomatic Complexity, the SLOC number and the number of comment lines
        mi = mi_visit(code_input, multi=True)
        radon_metrics['Maintainability_Index'] = mi

        return radon_metrics
    except:
        return radon_metrics
Ejemplo n.º 21
0
def raw(exclude=None, *paths):
    '''Analyze the given Python modules and compute raw metrics.

    Raw metrics include:

        * LOC: The number of lines of code (total)
        * LLOC: The number of logical lines of code
        * SLOC: The number of source lines of code (not necessarily
            corresponding to the LLOC)
        * comments: The number of Python comment lines
        * multi: The number of lines which represent multi-line strings
        * blank: The number of blank lines (or whitespace-only ones)

    The equation:

        sloc + blanks = loc

    should always hold.

    :param paths: The modules or packages to analyze.
    '''
    for path in iter_filenames(paths, exclude or []):
        with open(path) as fobj:
            log(path)
            try:
                mod = analyze(fobj.read())
            except Exception as e:
                log('{0}ERROR: {1}', ' ' * 4, str(e))
                continue
            for header, value in zip(
                ['LOC', 'LLOC', 'SLOC', 'Comments', 'Multi', 'Blank'], mod):
                log('{0}{1}: {2}', ' ' * 4, header, value)
            if not mod.loc:
                continue
            log(' ' * 4 + '- Comment Stats')
            indent = ' ' * 8
            comments = mod.comments
            log('{0}(C % L): {1:.0%}', indent,
                comments / (float(mod.loc) or 1))
            log('{0}(C % S): {1:.0%}', indent,
                comments / (float(mod.sloc) or 1))
            log('{0}(C + M % L): {1:.0%}', indent,
                (comments + mod.multi) / float(mod.loc))
Ejemplo n.º 22
0
def raw(exclude=None, *paths):
    '''Analyze the given Python modules and compute raw metrics.

    Raw metrics include:

        * LOC: The number of lines of code (total)
        * LLOC: The number of logical lines of code
        * SLOC: The number of source lines of code (not necessarily
            corresponding to the LLOC)
        * comments: The number of Python comment lines
        * multi: The number of lines which represent multi-line strings
        * blank: The number of blank lines (or whitespace-only ones)

    The equation:

        sloc + blanks = loc

    should always hold.

    :param paths: The modules or packages to analyze.
    '''
    for path in iter_filenames(paths, exclude or []):
        with open(path) as fobj:
            log(path)
            try:
                mod = analyze(fobj.read())
            except Exception as e:
                log('{0}ERROR: {1}', ' ' * 4, str(e))
                continue
            for header, value in zip(['LOC', 'LLOC', 'SLOC', 'Comments',
                                      'Multi', 'Blank'], mod):
                log('{0}{1}: {2}', ' ' * 4, header, value)
            if not mod.loc:
                continue
            log(' ' * 4 + '- Comment Stats')
            indent = ' ' * 8
            comments = mod.comments
            log('{0}(C % L): {1:.0%}', indent, comments / (float(mod.loc) or 1))
            log('{0}(C % S): {1:.0%}', indent, comments / (float(mod.sloc) or 1))
            log('{0}(C + M % L): {1:.0%}', indent,
                (comments + mod.multi) / float(mod.loc))
Ejemplo n.º 23
0
def analyse_raw_metrics(iface, code):
    metrics = analyze(code)
    iface.report_metric("lloc", metrics.lloc)
    iface.report_metric("sloc", metrics.sloc)
    locom = metrics.comments + metrics.multi
    iface.report_metric("comments", locom)
    # ploc = max(0, metrics.sloc - locom)
    iface.report_metric("ploc", metrics.sloc)
    if ploc > 400:
        iface.report_violation("max_file_length_400",
                               "File length (PLOC) of " + str(ploc))
    ratio = locom / float(metrics.sloc) if metrics.sloc != 0 else 0
    iface.report_metric("comment_ratio", ratio)
    if ratio < 0.2:
        iface.report_violation("min_comment_ratio_20",
                                "Comment ratio is below 20%.")
    if ratio > 0.3:
        iface.report_violation("max_comment_ratio_30",
                                "Comment ratio is above 30%.")
    if ratio > 0.4:
        iface.report_violation("max_comment_ratio_40",
                                "Comment ratio is above 40%.")
    return (metrics.lloc, ratio * 100)
def GetRawMetrics(m):
	""" Return Raw metrics values
	"""

	if RAW_METRIC:

		### class obj
		cls = m.__class__
		block = m.getBlockModel()

		### beware to use tab for devs code of models

		L = [eval("cls.%s"%fct) for fct in block.activity.values()]

		source_list = map(inspect.getsource, L)

		raw = {'loc':0, 'lloc':0, 'sloc':0, 'comments':0, 'multi':0, 'blank':0}
		for text in map(textwrap.dedent, source_list):
			a = analyze(text.strip())
			for key in raw:
				raw[key] += getattr(a, key)

		return raw
Ejemplo n.º 25
0
 def getMetricsByCode(self, code=""):
     hulp = {
         'comments': 0,
         'blank_line': 0,
         'lloc': 0,
         'count_if': 0,
         'count_loop': 0,
         'count_var': 0,
         'syntax_grade': 0
     }
     all_variables = []
     arq = open(f'{self.path}/codes/{code}.py')
     try:
         a = analyze(arq.read())
         hulp['comments'] = a[3]
         hulp['blank_line'] = a[5]
         hulp['lloc'] = a[1]
         hulp['syntax_grade'] = 1
     except Exception as e:
         hulp['syntax_grade'] = 0
     arq = open(f'{self.path}/codes/{code}.py')
     for line in arq:
         if (('if' in line or 'else' in line) and ':' in line):
             hulp['count_if'] = hulp['count_if'] + 1
         elif (('while' in line or 'for' in line) and ":" in line):
             hulp['count_loop'] = hulp['count_loop'] + 1
         elif condition(line):
             variables = line.strip().split('=')[0].split(',')
             variables = [
                 transformVariables(variable) for variable in variables
             ]
             for variable in variables:
                 if variable not in all_variables:
                     all_variables.append(variable)
     hulp['count_var'] = len(all_variables)
     return hulp
Ejemplo n.º 26
0
 def count_lines(self):
     logging.info("Counting lines with code , comments and blank lines")
     a = analyze(self.__code)
     self.lines=a.loc
     self.blank_lines = a.blank
     self.comments = a.comments
Ejemplo n.º 27
0
def number_of_lines(filepath):
    with open(filepath, 'r') as file:
        data = file.read()

    return (analyze(data).lloc)
Ejemplo n.º 28
0
					To remove any add the following argument, -h or --high, -m or --midum, or -l or --low'''
                      )
                sys.exit()

input_path = sys.argv[1]
if not os.path.isfile(input_path) or not input_path[-2:] == "py":
    print("The path specified does not exist or not the same type 'py'")
    sys.exit()
#CL-end

#------org_analyse-------
with open(input_path, "r") as testContent:
    source_con = ""
    for content in testContent:
        source_con += content + "\n"
    in_b = analyze(source_con)
    c = radon.complexity.cc_visit(source_con)
    v = radon.complexity.sorted_results(c)

if high_obf:
    v = 3  #class, function or variables
    n = 3  #number of random
if mid_obf:
    v = 2  #class, function or variables
    n = 3  #number of random
if low_obf:
    v = 1  #class, function or variables
    n = 3  #number of random

t = ""
if v == 1:
Ejemplo n.º 29
0
 def gobble(self, fobj):
     '''Analyze the content of the file object.'''
     return raw_to_dict(analyze(fobj.read()))
Ejemplo n.º 30
0
 def generateRawMetrics(self):
     raw = analyze(self.sourceFile)
     return raw
Ejemplo n.º 31
0
    def __get_code_metrics(codigo: str):
        """
        Recupera as métricas de um código Python.

        McCabe's (Complexidade)
            - complexity: Complexidade Total
            - n_classes: Quantidade de Classes
            - n_functions: Quantidade de Funções
        Métricas Brutas (Código)
            - loc: Número Total de Linhas
            - lloc: Número de Linhas Lógicas de Código
            - sloc: Número de Linhas de Código
            - comments: Número de Comentários
            - single_comments: Número de Comentários Simples
            - multilines: Número de Multi-line Strings
            - blank_lines: Número de Linhas em Branco
        Halstead (Métricas de SW)
            - h1: Número de Operadores Distintos
            - h2: Número de Operandos Distintos
            - N1: Número Total de Operadores
            - N2: Número Total de Operandos
            - vocabulary: Vocabulário (h = h1 + h2)
            - length: Tamanho (N = N1 + N2)
            - calculated_length: Tamanho Calculado (h1 * log2(h1) + h2 * log2(h2))
            - volume: Volume (V = N * log2(h))
            - difficulty: Dificuldade (D = h1/2 * N2/h2)
            - effort: Esforço (E = D * V)
            - time: Tempo (T = E / 18 segundos)
            - bugs: Bugs (B = V / 3000), estivativa de erros na implementação

        :param path: Caminho absoluto para o arquivo de código fonte (.py).
        :type path: str
        :return: As métricas que puderam ser extraídas do código.
        """
        metricas = Metricas()
        try:
            v = ComplexityVisitor.from_code(codigo)
            metricas.complexity = v.complexity
            metricas.n_functions = len(v.functions)
            metricas.n_classes = len(v.functions)
        except Exception as e:
            pass

        try:
            a = analyze(codigo)
            metricas.loc = a.loc
            metricas.lloc = a.lloc
            metricas.sloc = a.sloc
            metricas.blank_lines = a.blank
            metricas.multilines = a.multi
            metricas.comments = a.comments
            metricas.single_comments = a.single_comments
        except Exception as e:
            pass

        try:
            h = h_visit(codigo)
            metricas.h1 = h.total.h1
            metricas.h2 = h.total.h2
            metricas.N1 = h.total.N1
            metricas.N2 = h.total.N2
            metricas.h = h.total.vocabulary
            metricas.N = h.total.length
            metricas.calculated_N = h.total.calculated_length
            metricas.volume = h.total.volume
            metricas.difficulty = h.total.difficulty
            metricas.effort = h.total.effort
            metricas.bugs = h.total.bugs
            metricas.time = h.total.time
        except Exception as e:
            pass

        return metricas
Ejemplo n.º 32
0
def other_metrics(fnc_str):
    stats = analyze(fnc_str)
    return stats
Ejemplo n.º 33
0
def compute_metrics(df_series):
    '''
    Computes metrics from source code

    Parameters:

    # df: Pandas dataframe containing source code column

    Returns:

    Tuple comprising:

    - Dataframe with computed metrics
    - List of records' indices for which metrics could not be computed

    '''

    init_data = {
        'sample': [],
        'loc': [],
        'lloc': [],
        'sloc': [],
        'comments': [],
        'multi': [],
        'blank': [],
        'single_comments': [],
        'h1': [],
        'h2': [],
        'N1': [],
        'N2': [],
        'vocabulary': [],
        'length': [],
        'calculated_length': [],
        'volume': [],
        'difficulty': [],
        'effort': [],
        'time': [],
        'bugs': [],
        'complexity': [],
        'maint_idx': [],
        'maint_idx_rank': []
    }

    # Empty DataFrame
    metrics_df = pd.DataFrame(init_data)

    problem_records_indices = []

    for idx, code in df_series.iteritems():
        new_row = {}
        try:
            # Computes available metrics if possible
            raw = analyze(code)
            halstead = h_visit(code)
            cc_met = cc_visit(code)
            maint_idx = mi_visit(
                code, False
            )  # False indicates not to consider multi-line strings as comments
            maint_idx_rank = mi_rank(maint_idx)

            total_complexity = 0

            for func in cc_met:
                total_complexity += func.complexity

            new_row = {
                'sample': int(idx),
                'loc': raw.loc,
                'lloc': raw.lloc,
                'sloc': raw.sloc,
                'comments': raw.comments,
                'multi': raw.multi,
                'blank': raw.blank,
                'single_comments': raw.single_comments,
                'h1': halstead.total.h1,
                'h2': halstead.total.h2,
                'N1': halstead.total.N1,
                'N2': halstead.total.N2,
                'vocabulary': halstead.total.vocabulary,
                'length': halstead.total.length,
                'calculated_length': halstead.total.calculated_length,
                'volume': halstead.total.volume,
                'difficulty': halstead.total.difficulty,
                'effort': halstead.total.effort,
                'time': halstead.total.time,
                'bugs': halstead.total.bugs,
                'complexity': total_complexity,
                'maint_idx': maint_idx,
                'maint_idx_rank': maint_idx_rank
            }

        except:
            problem_records_indices.append(idx)
            new_row = {
                'sample': int(idx),
                'loc': float('nan'),
                'lloc': float('nan'),
                'sloc': float('nan'),
                'comments': float('nan'),
                'multi': float('nan'),
                'blank': float('nan'),
                'single_comments': float('nan'),
                'h1': float('nan'),
                'h2': float('nan'),
                'N1': float('nan'),
                'N2': float('nan'),
                'vocabulary': float('nan'),
                'length': float('nan'),
                'calculated_length': float('nan'),
                'volume': float('nan'),
                'difficulty': float('nan'),
                'effort': float('nan'),
                'time': float('nan'),
                'bugs': float('nan'),
                'complexity': float('nan'),
                'maint_idx': float('nan'),
                'maint_idx_rank': float('nan')
            }

        finally:
            metrics_df = metrics_df.append(new_row, ignore_index=True)

    if problem_records_indices:
        print(
            f'There was a problem computing metrics for {len(problem_records_indices)} records.'
        )

    return metrics_df, problem_records_indices
Ejemplo n.º 34
0
 def gobble(self, fobj):
     '''Analyze the content of the file object.'''
     return raw_to_dict(analyze(fobj.read()))
Ejemplo n.º 35
0
def raw_metrics(input_file):
    with open(input_file, 'r') as file:
        return raw.analyze(file.read())
Ejemplo n.º 36
0
def mi_parameters_without_comments(code):
    ast_node = ast.parse(code)
    raw = analyze(code)
    return (h_visit_ast(ast_node).total.volume,
            ComplexityVisitor.from_ast(ast_node).total_complexity, raw.lloc)
Ejemplo n.º 37
0
 #cleanRepository("Repository/.git/objects/pack")
 #cleanRepository(repo_path)
 totalLoc = 0
 gitURL = node['url'] + ".git"
 print("\n" + "Começa o git clone")
 print(node['url'])
 repo = clone_repository(gitURL, repo_path)
 print("Termina o git clone \n")
 for root, dirs, files in os.walk(repo_path):
     for file in files:
         if file.endswith('.py'):
             fullpath = os.path.join(root, file)
             with open(fullpath, encoding="utf8") as f:
                 content = f.read()
                 print("\nLendo arquivo " + fullpath)
                 b = analyze(content)
                 print(b)
                 i = 0
                 for item in b:
                     if i == 0:
                         totalLoc += item
                         print("loc = " + str(item))
                         print("Total loc até agora: " +
                               str(totalLoc))
                         i += 1
 print("\nTotal loc final para o repo " + node['nameWithOwner'] +
       " é " + str(totalLoc))
 print("\n ------ Fim de um repositorio ------ \n")
 cleanRepository(repo_path)
 with open("repos.csv", 'a') as the_file:
     the_file.write(node['nameWithOwner'] + ";" + node['url'] +
Ejemplo n.º 38
0
        file_methods = []
        file_complexities = []
        methods_dictionary = OrderedDict()

        with open(filename) as fobj:
            source = fobj.read()

        # get cc blocks
        blocks = cc_visit(source)

        # get MI score
        mi = mi_visit(source, True)
        mi_dict[mi] = filename

        # get raw metrics
        raw = analyze(source)
        hal_vol, complexity, logic_lines, com_lines = mi_parameters(source)
        param_dict[complexity] = (complexity, filename, hal_vol, logic_lines,
                                  com_lines)

        # get metrics for each file
        file_complexity = sorted_results(blocks)
        try:
            file_methods.append(re.findall('(?<=.Function\(name=\')[^\']'
                                           '*(?=\')', str(file_complexity)))

            file_complexities.append(re.findall('(?<=.complexity=)\d*',
                                                str(file_complexity)))
        except IndexError:
            continue
Ejemplo n.º 39
0
 def count_lines(self):
     logging.info("Counting lines with code , comments and blank lines")
     a = analyze(self.__code)
     self.lines = a.loc
     self.blank_lines = a.blank
     self.comments = a.comments