Created
July 26, 2016 22:58
-
-
Save alexras/930f803124742d9ff94902c52dc161d0 to your computer and use it in GitHub Desktop.
Print number of lines and files covered by each user in `arc cover`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import collections | |
import fileinput | |
import operator | |
import sys | |
user = None | |
def sum_line_ranges(line_ranges): | |
line_range_sum = 0 | |
for line_range in line_ranges: | |
if len(line_range) == 1: | |
line_range_sum += 1 | |
else: | |
line_range_sum += line_range[1] - line_range[0] | |
return line_range_sum | |
line_counts_per_user = collections.defaultdict(int) | |
file_counts_per_user = collections.defaultdict(int) | |
for line in fileinput.input(): | |
if len(line.strip()) == 0: | |
continue | |
if line[0] != ' ': | |
user = line.strip() | |
else: | |
if ': lines' in line: | |
split_string = ': lines ' | |
elif ': line ' in line: | |
split_string = ': line ' | |
else: | |
sys.exit('Unknown line format %s' % (line)) | |
line_ranges = map(lambda x: map(int, x.split('-')), line.split(split_string)[1].split(',')) | |
line_counts_per_user[user] += sum_line_ranges(line_ranges) | |
file_counts_per_user[user] += 1 | |
max_user_name_length = max(map(len, line_counts_per_user.keys())) | |
print('%s\t%s\t%s' % ('Username'.ljust(max_user_name_length + 2), 'Lines', 'Files')) | |
print('%s\t%s\t%s' % ('--------'.ljust(max_user_name_length + 2), '-----', '-----')) | |
for user, line_count in sorted(line_counts_per_user.items(), | |
key=operator.itemgetter(1), | |
reverse=True): | |
print('%s\t%d\t%d' % (user.ljust(max_user_name_length + 2), | |
line_count, file_counts_per_user[user])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment