Commit 7a9b6952 authored by Jinsuk Kim's avatar Jinsuk Kim Committed by Commit Bot

Android: Update modulcation LOC stat script

  - Allow running from outside the repo (--git-dir)
  - Date range for how many days to look back (--past-days)
  - Write metadata about start and end date parameters

Also moved the script under tools/android/modularization/loc

Bug: 1146478
Change-Id: Ic63ea085836d8b5750ada2299ba992221d97b9f9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2526863
Commit-Queue: Jinsuk Kim <jinsukkim@chromium.org>
Reviewed-by: default avatarPeter Wen <wnwen@chromium.org>
Reviewed-by: default avatarHenrique Nakashima <hnakashima@chromium.org>
Cr-Commit-Position: refs/heads/master@{#826482}
parent 92f89de1
......@@ -8,7 +8,9 @@
"""
import argparse
import datetime
import json
import os
import subprocess
import sys
from collections import OrderedDict
......@@ -22,14 +24,22 @@ _M12N_DIRS = [
_LEGACY_DIR = 'chrome/android'
def GenerateLOCStats(dates, quiet, json_format):
def GenerateLOCStats(start_date,
end_date,
*,
quiet=False,
json_format=False,
git_dir=None):
"""Generate modulazation LOC stats.
Args:
dates: A tuple containing the range of dates for git patches to process.
start_date: The date to analyze the stat from.
end_date: The date to analyze the stat to.
quiet: True if no message is output during the processing.
json_format: True if the output should be in json format. Otherwise
a plain, human-readable table is generated.
git_dir: Git repo directory to use for stats. If None, the current directory
is used.
Return:
Text string containing the stat in a specified format.
......@@ -50,9 +60,11 @@ def GenerateLOCStats(dates, quiet, json_format):
# added-lines deleted-lines file-path1
# added-lines deleted-lines file-path2
# ...
repo_dir = git_dir or os.getcwd()
command = [
'git', 'log', '--numstat', '--no-renames', '--format=#:%al:%cs:%s',
'--after=' + dates[0], '--before=' + dates[1], 'chrome', 'components'
'git', '-C', repo_dir, 'log', '--numstat', '--no-renames',
'--format=#:%al:%cs:%s', '--after=' + start_date, '--before=' + end_date,
'chrome', 'components'
]
try:
proc = subprocess.Popen(
......@@ -111,23 +123,29 @@ def GenerateLOCStats(dates, quiet, json_format):
'loc_modularized': total_m12n,
'loc_legacy': total_legacy,
'rankings': rankings,
'start_date': start_date,
'end_date': end_date,
})
else:
output = []
percentage = 100.0 * total_m12n / (total_m12n + total_legacy)
total = total_m12n + total_legacy
percentage = 100.0 * total_m12n / total if total > 0 else 0
output.append(f'# of lines added in modularized files: {total_m12n}')
output.append(f'# of lines added in legacy files: {total_legacy}')
output.append(f'% of lines landing in modularized files: {percentage:2.2f}')
# Shows the top 50 contributors to modularized files.
output.append('\nTop contributors:')
output.append('No lines % author')
for rank, author in enumerate(list(rankings.keys())[:50], 1):
lines = rankings[author]
if lines == 0:
break
ratio = 100 * lines / total_m12n
output.append(f'{rank:2d} {lines:6d} {ratio:5.1f} {author}')
if rankings:
output.append('No lines % author')
for rank, author in enumerate(list(rankings.keys())[:50], 1):
lines = rankings[author]
if lines == 0:
break
ratio = 100 * lines / total_m12n
output.append(f'{rank:2d} {lines:6d} {ratio:5.1f} {author}')
else:
output.append('...none found.')
return '\n'.join(output)
......@@ -151,15 +169,30 @@ def _print_progress(msg, prev_msg_len):
print(msg, end='\r')
def _get_date_range(args):
if args.date:
return args.date
today = datetime.date.today()
delta = datetime.timedelta(days=args.past_days)
past = datetime.datetime(today.year, today.month, today.day) - delta
return (past.date().isoformat(), today.isoformat())
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generates LOC stats for modularization effort.")
parser.add_argument('--date',
required=True,
type=str,
metavar=('<date-from>', '<date-to>'),
nargs=2,
help='date range (YYYY-MM-DD)~(YYYY-MM-DD)')
date_group = parser.add_mutually_exclusive_group(required=True)
date_group.add_argument('--date',
type=str,
metavar=('<date-from>', '<date-to>'),
nargs=2,
help='date range (YYYY-MM-DD)~(YYYY-MM-DD)')
date_group.add_argument('--past-days',
type=int,
help='The number of days to look back for stats. '
'0 for today only.')
parser.add_argument('-q',
'--quiet',
action='store_true',
......@@ -174,9 +207,20 @@ if __name__ == "__main__":
type=str,
help='File to write the result to in json format. '
'If not specified, outputs to console.')
parser.add_argument('--git-dir',
type=str,
help='Root directory of the git repo to look into. '
'If not specified, use the current directory.')
args = parser.parse_args()
result = GenerateLOCStats(args.date, args.quiet, args.json)
if args.past_days and args.past_days < 0:
raise parser.error('--past-days must be non-negative.')
start_date, end_date = _get_date_range(args)
result = GenerateLOCStats(start_date,
end_date,
quiet=args.quiet,
json_format=args.json,
git_dir=args.git_dir)
if args.output:
with open(args.output, 'w') as f:
f.write(result)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment