def show_detection_result(): detected_smells = list_csv_contents('logs/smells/detected_smells.csv') parsing_errors = list_csv_contents( 'logs/errors/token_parsing_exceptions.csv') loading_errors = list_csv_contents( 'logs/errors/token_loading_exceptions.csv') detection_errors = list_csv_contents( 'logs/errors/token_detection_exceptions.csv') print_results_and_errors(detected_smells, parsing_errors, loading_errors, detection_errors)
def test_octal_permission(self): code_snippet = '''os.chmod('/etc/hosts', 0x777)''' code_analyze = RunOnSingleSourceCode(None, False, False, code_snippet) code_analyze.analyze_single_code() detected_smells = list_csv_contents('logs/smells/detected_smells.csv') self.assertEqual(detected_smells[0][2], 'bad file permission')
def test_subprocess_call(self): code_snippet = '''subprocess.call(['chmod', 0x777, 'path'])''' code_analyze = RunOnSingleSourceCode(None, False, False, code_snippet) code_analyze.analyze_single_code() detected_smells = list_csv_contents('logs/smells/detected_smells.csv') self.assertEqual(detected_smells[0][2], 'bad file permission')
def test_variable_with_constant_value(self): code_snippet = '''assert x == "goodbye", "x should be goodbye"''' code_analyze = RunOnSingleSourceCode(None, False, False, code_snippet) code_analyze.analyze_single_code() detected_smells = list_csv_contents('logs/smells/detected_smells.csv') self.assertEqual(detected_smells[0][2], 'use of assert statement')
def test_variable_with_function_return(self): code_snippet = '''assert y == getHello(), "value should be x"''' code_analyze = RunOnSingleSourceCode(None, False, False, code_snippet) code_analyze.analyze_single_code() detected_smells = list_csv_contents('logs/smells/detected_smells.csv') self.assertEqual(detected_smells[0][2], 'use of assert statement')
def test_function_call(self): code_snippet = '''assert isinstance(x, int), "x should be int"''' code_analyze = RunOnSingleSourceCode(None, False, False, code_snippet) code_analyze.analyze_single_code() detected_smells = list_csv_contents('logs/smells/detected_smells.csv') self.assertEqual(detected_smells[0][2], 'use of assert statement')
def test_object_attribute(self): code_snippet = '''assert self.url, "All clients must have a URL attribute"''' code_analyze = RunOnSingleSourceCode(None, False, False, code_snippet) code_analyze.analyze_single_code() detected_smells = list_csv_contents('logs/smells/detected_smells.csv') self.assertEqual(detected_smells[0][2], 'use of assert statement')
def test_yaml_load_call(self): code_snippet = '''mark_safe(unsafe_strings)''' code_analyze = RunOnSingleSourceCode(None, False, False, code_snippet) code_analyze.analyze_single_code() detected_smells = list_csv_contents('logs/smells/detected_smells.csv') self.assertEqual(detected_smells[0][2], 'cross site scripting')
def test_single_variable(self): code_snippet = '''assert x, "x"''' code_analyze = RunOnSingleSourceCode(None, False, False, code_snippet) code_analyze.analyze_single_code() detected_smells = list_csv_contents('logs/smells/detected_smells.csv') self.assertEqual(detected_smells[0][2], 'use of assert statement')
def test_ignore_exception_continue(self): code_snippet = '''try: to_something() except: pass''' code_analyze = RunOnSingleSourceCode(None, False, False, code_snippet) code_analyze.analyze_single_code() detected_smells = list_csv_contents('logs/smells/detected_smells.csv') self.assertEqual(detected_smells[0][2], 'ignoring except block')
def show_categories_in_project_descriptions(self): unique_names = [] projects = list_csv_contents('./project-descriptions.csv') projects.pop(0) for project in projects: if project[2] not in unique_names: unique_names.append(project[2]) for name in unique_names: print(name)
def save_unique_smell_counts_in_projects(): projects = [] smells = list_csv_contents( 'logs/projects/different_smells_in_projects.csv') for smell in smells: project_found = False for project in projects: if smell[0] in project: project[1] += 1 project_found = True if project_found is True: break if project_found is False: projects.append([smell[0], 1]) descriptions = list_csv_contents('project-descriptions.csv') descriptions.pop(0) #removing column names for description in descriptions: already_included = False for project in projects: if description[0] in project: already_included = True break if already_included is False: projects.append([description[0], 0]) projects.sort(key=lambda x: x[0][0]) for project in projects: write_to_csv_file( 'logs/projects/total_unique_smell_counts_in_projects.csv', project) write_to_csv_file('logs/projects/x.csv', [project[0]]) write_to_csv_file('logs/projects/y.csv', [project[1]])
def save_total_smell_counts_in_projects(): project_smells = [] smells = list_csv_contents( 'logs/projects/different_smells_in_projects.csv') for smell in smells: already_included = False for project_smell in project_smells: if smell[0] in project_smell: project_smell[1] = int(project_smell[1]) + int(smell[2]) already_included = True break if already_included is False: project_smells.append([smell[0], int(smell[2])]) descriptions = list_csv_contents('project-descriptions.csv') descriptions.pop(0) #removing column names for description in descriptions: already_included = False for project_smell in project_smells: if description[0] in project_smell: already_included = True break if already_included is False: project_smells.append([description[0], 0]) project_smells.sort(key=lambda x: x[0][0] ) #sorting the smells according to alphabetical name for project_smell in project_smells: write_to_csv_file('logs/projects/total_smell_counts_in_projects.csv', project_smell) write_to_csv_file('logs/projects/x.csv', [project_smell[0]]) write_to_csv_file('logs/projects/y.csv', [project_smell[1]])
def find_correlation(): project_smells = list_csv_contents( 'logs/projects/total_smell_counts_in_projects.csv') project_descriptions = list_csv_contents('project-descriptions.csv') relations = [] for project in project_smells: for description in project_descriptions: if project[0] == description[0]: relations.append([ project[0], int(project[1]), description[0], int(description[4]) ]) break relations.sort( key=lambda x: x[3]) #sorting according to smell count #ascending x = [] y = [] for relation in relations: x.append(relation[1]) y.append(int(relation[3])) print(relation) write_to_csv_file('logs/relations/x.csv', [relation[3]]) write_to_csv_file('logs/relations/y.csv', [relation[1]]) write_to_csv_file('logs/relations/x-y.csv', [relation[1], relation[3]]) # print(relations) # print(x) # print(y) correlation, _ = pearsonr(x, y) print('Pearsons correlation: %.4f' % correlation)
def save_detected_smells_in_separate_file(): smells = list_csv_contents('logs/smells/detected_smells.csv') for smell in smells: token = smell[-1] filename = smell[1] identified_smell = smell[2] file_path = os.path.join( './logs/smell-categories/{filename}.csv'.format( filename=identified_smell)) if os.path.isfile(file_path) is False: with open(file_path, 'w') as fp: pass write_to_csv_file(file_path, [filename, token])
def individual_smell_introduction_in_total_number_of_projects(): unique_projects_count = [] smells_in_projects = list_csv_contents('logs/projects/different_smells_in_projects.csv') for smells_in_project in smells_in_projects: already_in = False for project in unique_projects_count: if project[0] == smells_in_project[1]: already_in = True project[1] += 1 if already_in == False: unique_projects_count.append([smells_in_project[1],1]) for project in unique_projects_count: write_to_csv_file('logs/smells/smell_in_unique_projects.csv',project)
def save_individual_smell_occurence_count(): unique_smell_counts = [] smells = list_csv_contents('logs/smells/detected_smells.csv') for smell in smells: found = False for row in unique_smell_counts: if smell[2] in row: row[1] = row[1] + 1 found = True break if found is False: unique_smell_counts.append([smell[2], 1]) unique_smell_counts.sort(key=lambda x: x[1], reverse=True) for smell_count in unique_smell_counts: write_to_csv_file('logs/smells/smell_frequency.csv', smell_count)
def save_detected_different_smells_frequency_in_projects(): project_smells = [] smells = list_csv_contents('logs/smells/detected_smells.csv') for smell in smells: smell = [smell[0], smell[2]] match_found = False for project_smell in project_smells: if smell[0] in project_smell and smell[1] in project_smell: project_smell[2] = project_smell[2] + 1 match_found = True break if match_found is False: project_smells.append([smell[0], smell[1], 1]) project_smells.sort( key=lambda x: x[0]) #sorting the smells according to project names for project_smell in project_smells: write_to_csv_file('logs/projects/different_smells_in_projects.csv', project_smell)
def save_smells_categorized_according_to_project_type(): descriptions = list_csv_contents('project-descriptions.csv') projects = list_csv_contents( 'logs/projects/different_smells_in_projects.csv') categorized_smell_in_projects = [] for project in projects: name = project[0] name_found = False smell = project[1] smell_count = project[2] project_type = None for description in descriptions: if name == description[0]: project_type = description[2] break for category in categorized_smell_in_projects: if category[0] == project_type and category[1] == smell: category[2] += int(smell_count) name_found = True break if name_found is False and project_type is not None: categorized_smell_in_projects.append( [project_type, smell, int(smell_count)]) categorized_smell_in_projects.sort(key=lambda x: x[0]) for item in categorized_smell_in_projects: print(item) write_to_csv_file( 'logs/projects/project-type-total-unique-smells.csv', [item[0], '%s & %s & \\\\ \\hline' % (item[1], str(item[2]))]) unique_smell_count = [] for category in categorized_smell_in_projects: already_included = False for smell in unique_smell_count: if smell[0] == category[0]: smell[1] += 1 already_included = True break if already_included is False: unique_smell_count.append([category[0], 1]) for smell in unique_smell_count: # write_to_csv_file('logs/projects/x.csv', [smell[0]]) # write_to_csv_file('logs/projects/y.csv', [smell[1]]) write_to_csv_file('logs/projects/proejct-type-unique-smell-counts.csv', [smell[0], smell[1]]) unqiue_smell_in_project_categories = [] for category in categorized_smell_in_projects: already_included = False for unique_smell in unqiue_smell_in_project_categories: if unique_smell[0] == category[0]: unique_smell[1].append(category[1]) already_included = True break if already_included is False: unqiue_smell_in_project_categories.append( [category[0], [category[1]]]) for unqiue_smell_in_project_category in unqiue_smell_in_project_categories: file_path = os.path.join( './logs/project-categories/{filename}.csv'.format( filename=unqiue_smell_in_project_category[0])) if os.path.isfile(file_path) is False: with open(file_path, 'w') as fp: pass for smell in unqiue_smell_in_project_category[1]: write_to_csv_file(file_path, [smell]) total_smell_count = [] for category in categorized_smell_in_projects: already_included = False for smell in total_smell_count: if smell[0] == category[0]: smell[1] += int(category[2]) already_included = True break if already_included is False: total_smell_count.append([category[0], int(category[2])]) for smell in total_smell_count: # print(smell) # write_to_csv_file('logs/projects/x.csv', [smell[0]]) # write_to_csv_file('logs/projects/y.csv', [smell[1]]) write_to_csv_file('logs/projects/project-type-total-smell-counts.csv', [smell[0], smell[1]])