def create_graph(): """Create graph of code quality evolution """ t0 = time() cwd = getcwd() project_name = cwd.split('/')[-1] # We copy project to tmp (for security) tmp_directory = '%s/ipkg_quality.git' % mkdtemp() call(['git', 'clone', cwd, tmp_directory]) chdir(tmp_directory) # First step: we create a list of statistics statistics = {} commit_ids = git.get_revisions() commit_ids.reverse() print 'Script will analyse %s commits.' % len(commit_ids) for commit_id in commit_ids: # We move to a given commit call(['git', 'reset', '--hard', commit_id]) # Print script evolution stdout.write('.') stdout.flush() # We list files filenames = git.get_filenames() filenames = [ x for x in filenames if x.endswith('.py') ] # We get code quality for this files stats, files_db = analyse(filenames, ignore_errors=True) metadata = git.get_metadata() date_time = metadata['committer'][1] commit_date = date(date_time.year, date_time.month, date_time.day) if commit_date not in statistics: statistics[commit_date] = stats else: # Same day => Avg for key in statistics[commit_date]: avg = (statistics[commit_date][key] + stats[key])/2 statistics[commit_date][key] = avg print # Get dates values = [] dates = statistics.keys() dates.sort() for a_date in dates: values.append(statistics[a_date]) # Base graph informations base_title = '[%s %s]' % (project_name, git.get_branch_name()) # We generate graphs chdir(cwd) for problem_dict in ['code_length', 'aesthetics_problems', 'exception_problems', 'import_problems']: current_problems = eval(problem_dict) graph_title = '%s %s' % (base_title, current_problems['title']) lines = [] labels = [] fig = Figure() graph = fig.add_subplot(111) for key in current_problems['keys']: if current_problems['pourcent']: problem_values = [((x[key]*100.0)/x['lines']) for x in values] else: problem_values = [x[key] for x in values] lines.append(graph.plot_date(dates, problem_values, '-')) labels.append(current_problems['keys'][key]) graph.set_title(graph_title) graph.xaxis.set_major_formatter(DateFormatter("%b '%y'")) if current_problems['pourcent']: graph.set_ylabel('Pourcent') graph.yaxis.set_major_formatter(FormatStrFormatter("%5.02f %%")) else: graph.set_ylabel('Quantity') graph.set_xlabel('') graph.autoscale_view() graph.grid(True) fig.autofmt_xdate() fig.set_figheight(fig.get_figheight()+5) legend = fig.legend(lines, labels, loc=8, axespad=0.0) legend.get_frame().set_linewidth(0) canvas = FigureCanvasAgg(fig) destination = 'graph_%s.png' % problem_dict canvas.print_figure(destination, dpi=80) print '%s -> %s ' % (graph_title, destination) t1 = time() print 'Generation time: %d minutes.' % ((t1 - t0)/60)
def create_graph(): """Create graph of code quality evolution """ t0 = time() cwd = getcwd() project_name = cwd.split('/')[-1] # We copy project to tmp (for security) tmp_directory = '%s/ipkg_quality.git' % mkdtemp() call(['git', 'clone', cwd, tmp_directory]) chdir(tmp_directory) # First step: we create a list of statistics statistics = {} commit_ids = git.get_revisions() commit_ids.reverse() print 'Script will analyse %s commits.' % len(commit_ids) for commit_id in commit_ids: # We move to a given commit call(['git', 'reset', '--hard', commit_id]) # Print script evolution stdout.write('.') stdout.flush() # We list files filenames = git.get_filenames() filenames = [x for x in filenames if x.endswith('.py')] # We get code quality for this files stats, files_db = analyse(filenames, ignore_errors=True) metadata = git.get_metadata() date_time = metadata['committer'][1] commit_date = date(date_time.year, date_time.month, date_time.day) if commit_date not in statistics: statistics[commit_date] = stats else: # Same day => Avg for key in statistics[commit_date]: avg = (statistics[commit_date][key] + stats[key]) / 2 statistics[commit_date][key] = avg print # Get dates values = [] dates = statistics.keys() dates.sort() for a_date in dates: values.append(statistics[a_date]) # Base graph informations base_title = '[%s %s]' % (project_name, git.get_branch_name()) # We generate graphs chdir(cwd) for problem_dict in [ 'code_length', 'aesthetics_problems', 'exception_problems', 'import_problems' ]: current_problems = eval(problem_dict) graph_title = '%s %s' % (base_title, current_problems['title']) lines = [] labels = [] fig = Figure() graph = fig.add_subplot(111) for key in current_problems['keys']: if current_problems['pourcent']: problem_values = [((x[key] * 100.0) / x['lines']) for x in values] else: problem_values = [x[key] for x in values] lines.append(graph.plot_date(dates, problem_values, '-')) labels.append(current_problems['keys'][key]) graph.set_title(graph_title) graph.xaxis.set_major_formatter(DateFormatter("%b '%y'")) if current_problems['pourcent']: graph.set_ylabel('Pourcent') graph.yaxis.set_major_formatter(FormatStrFormatter("%5.02f %%")) else: graph.set_ylabel('Quantity') graph.set_xlabel('') graph.autoscale_view() graph.grid(True) fig.autofmt_xdate() fig.set_figheight(fig.get_figheight() + 5) legend = fig.legend(lines, labels, loc=8, axespad=0.0) legend.get_frame().set_linewidth(0) canvas = FigureCanvasAgg(fig) destination = 'graph_%s.png' % problem_dict canvas.print_figure(destination, dpi=80) print '%s -> %s ' % (graph_title, destination) t1 = time() print 'Generation time: %d minutes.' % ((t1 - t0) / 60)
# Show graph parser.add_option('-g', '--graph', action="store_true", dest='graph', default=False, help='create graphs of code quality evolution.') options, args = parser.parse_args() # Filenames if args: filenames = set([]) for arg in args: filenames = filenames.union(glob(arg)) filenames = list(filenames) elif git.is_available(): filenames = git.get_filenames() filenames = [ x for x in filenames if x.endswith('.py') ] else: filenames = [] for path in lfs.traverse(): if lfs.is_file(path) and basename(path).endswith('.py'): filenames.append(relpath(path)) # Check options if len(filenames) == 0: parser.error(u'Please give at least one file to analyse.') if options.worse > 0 and options.show_lines is True: parser.error( u'Options --worse and --show-lines are mutually exclusive.') if options.show_lines == True and len(filenames) != 1: parser.error(
'--graph', action="store_true", dest='graph', default=False, help='create graphs of code quality evolution.') options, args = parser.parse_args() # Filenames if args: filenames = set([]) for arg in args: filenames = filenames.union(glob(arg)) filenames = list(filenames) elif git.is_available(): filenames = git.get_filenames() filenames = [x for x in filenames if x.endswith('.py')] else: filenames = [] for path in lfs.traverse(): if lfs.is_file(path) and basename(path).endswith('.py'): filenames.append(relpath(path)) # Check options if len(filenames) == 0: parser.error(u'Please give at least one file to analyse.') if options.worse > 0 and options.show_lines is True: parser.error( u'Options --worse and --show-lines are mutually exclusive.') if options.show_lines == True and len(filenames) != 1: parser.error(u'The option --show-lines takes one file in parameter.')