Beispiel #1
0
def main():
  arg_parser = ArgumentParser(description='Count lines of source code.')
  arg_parser.add_argument('paths', nargs='+', help='Directories to explore.')
  args = arg_parser.parse_args()

  files = Counter()
  lines = Counter()
  blank = Counter()
  other_exts = Counter()

  # iterate over all paths.
  for top in args.paths:
    if is_file(top):
      count_path(top, files, lines, blank, other_exts)
      continue
    if ignore_dir_name(path_name(top)):
      continue
    for dirpath, dirnames, filenames in os.walk(top):
      # filter dirnames.
      dirnames[:] = [n for n in dirnames if not ignore_dir_name(n)]
      for name in filenames:
        path = path_join(dirpath, name)
        count_path(path, files, lines, blank, other_exts)

  for group_key in group_keys:
    group = groups[group_key]
    non_zero = False
    total_key = group_key.upper() + ' TOTAL'
    sorted_keys = sorted(group['exts'], key=lambda k: lines[k]) # sort by substantial line count.
    # tricky: count values for total_key as we go, then print values for total_key last.
    for e in sorted_keys + [total_key]:
      f = files[e]
      if f < 1:
        continue
      non_zero = True
      print('{:>12}: {:>5}  '.format(e, f), end='')
      files[total_key] += f
      if e in lines:
        l = lines[e]
        b = blank[e]
        t = l + b
        frac = float(l / t) if t > 0 else 0.0
        print(' {:>12,} lines; {:>12,} ({:.2f}) full'.format(t, l, frac), end='')
        lines[total_key] += l
        blank[total_key] += b
      print()
    if non_zero:
      print()

  if other_exts:
    items = sorted(other_exts.items(), key=lambda i: (-i[1], i[0]))
    print('; '.join('{}: {}'.format(ext, count) for (ext, count) in items))
Beispiel #2
0
def count_path(path, files, lines, blank, other_exts):
  name = path_name(path)
  ext = path_ext(name)
  if ext in ignored_exts:
    return
  if ext not in exts_to_re:
    other_exts[ext] += 1
    return
  files[ext] += 1
  r = exts_to_re[ext]
  try:
    with open(path) as f:
      for line in f:
        if r.fullmatch(line):
          blank[ext] += 1
        else:
          lines[ext] += 1
  except (IOError, UnicodeDecodeError) as e:
    print('skipping ({}): {}'.format(e, path), file=stderr)