def evaluate_script_with_rule(filePath): try: with open(filePath, 'r', encoding='utf8') as f: yml = f.read() yml = StringIO(yml.expandtabs(2)) blueprint = BlueprintMetric(yml) imports = blueprint.getyml.get('imports') loc_count = LOC(yml).count() smell_detected = False if isinstance(imports, list): interproject_imports = [] intraproject_imports = [] for imp in imports: if 'http' in imp or 'snapshot' in imp: interproject_imports.append(imp) else: interproject_imports.append(imp) try: if len(interproject_imports) / loc_count > 0.1: smell_detected = True except: pass return smell_detected except Exception as e: return e
def evaluate_script_with_rule(filePath): try: with open(filePath, 'r', encoding='utf8') as f: yml = f.read() yml = StringIO(yml.expandtabs(2)) depth_per_line = calculate_depth(yml) interfaces_loc = [] start = False for line in depth_per_line: if 'interface' in line[0]: depth = line[1] lines = 0 start = True continue if start == True: if line[1] > depth: lines += 1 else: start = False interfaces_loc.append(lines) return len( [interface for interface in interfaces_loc if interface > 7]) except Exception as e: return e
def evaluate_script_with_rule(filePath): try: with open(filePath, 'r', encoding='utf8') as f: yml = f.read() yml = StringIO(yml.expandtabs(2)) depth_per_line = calculate_depth(yml) # Situation 2: Too long topology template topology_template_loc = [] start = False for line in depth_per_line: if 'topology_template' in line[0]: depth = line[1] lines = 0 start = True continue if start == True: if line[1] > depth: lines += 1 else: start = False topology_template_loc.append(lines) topology_template_loc = [et for et in topology_template_loc if et > 40] #Situation 3: Too complex topology template complexity_elements = ['interfaces:', 'requirements:', 'node_filter:'] complexity_depths = [] start = False for line in depth_per_line: if line[0] in complexity_elements: depth = line[1] max_depth = [] start = True continue if start == True: if line[1] >= depth: max_depth.append(line[1] - depth) else: start = False if len(max_depth) != 0: complexity_depths.append(max(max_depth)) complexity_depths = [dp for dp in complexity_depths if dp > 3] return len(topology_template_loc) + len(complexity_depths) except Exception as e: return e
def test(script, expected): script = StringIO(script.expandtabs(2)) raised = Raised.FALSE try: metric = LinesMetric(script) except TypeError: raised = Raised.TRUE finally: script.close() assert raised == expected
def evaluate_script_with_rule(filePath): try: with open(filePath, 'r', encoding='utf8') as f: yml = f.read() yml = StringIO(yml.expandtabs(2)) cpl_list = CPL(yml).count() smell_counter = 0 for element in cpl_list: if element > 140: smell_counter += 1 return smell_counter except Exception as e: return e
def evaluate_script_with_rule(filePath): try: with open(filePath, 'r', encoding='utf8') as f: yml = f.read() yml = StringIO(yml.expandtabs(2)) attributes = NGF(yml).count() loc_count = LOC(yml).count() smell_detected = False try: if attributes / loc_count > 0.5: smell_detected = True except: pass return smell_detected except Exception as e: return e
def evaluate_script_with_rule(filePath): try: if isinstance(filePath, StringIO): strio = filePath.getvalue() else: with open(filePath, 'r', encoding='utf8') as f: raw_yml = f.read() raw_yml = StringIO(raw_yml.expandtabs(2)) strio = BlueprintMetric(raw_yml).getStringIOobject strio_length = len(strio) code_chunks = [strio[i:150 + i] for i in range(0, strio_length - 149)] duplicate_blocks = [ chunk for chunk in code_chunks if strio.count(chunk) > 1 ] return len(duplicate_blocks) except Exception as e: return e
def test(script, expected): script = StringIO(script.expandtabs(2)) count = LinesCode(script).count() script.close() assert count == expected
def test(script, expected): script = StringIO(script.expandtabs(2)) count = NumBlocksErrorHandling(script).count() script.close() assert count == expected
def test(script, expected): script = StringIO(script.expandtabs(2)) count = NumDistinctModules(script).count() script.close() assert count == expected
def test(script, expected): script = StringIO(script.expandtabs(2)) count = TextEntropy(script).count() script.close() assert count == expected
def test(script, expected): script = StringIO(script.expandtabs(2)) count = NumDeprecatedKeywords(script).count() script.close() assert count == expected
def test(script, expected): script = StringIO(script.expandtabs(2)) count = NumSuspiciousComments(script).count() script.close() assert count == expected
def test(script, expected): script = StringIO(script.expandtabs(2)) count = NumMathOperations(script).count() script.close() assert count == expected
def test(script, expected): script = StringIO(script.expandtabs(2)) count = AveragePlaySize(script).count() script.close() assert count == expected
def test(script, expected): script = StringIO(script.expandtabs(2)) count = NumImportPlaybook(script).count() script.close() assert count == expected
def test(script, expected): script = StringIO(script.expandtabs(2)) count = NumRegex(script).count() script.close() assert count == expected
def test(script, expected): script = StringIO(script.expandtabs(2)) count = NumAuthorizedKey(script).count() script.close() assert count == expected