|
| 1 | +from collections import defaultdict, Counter |
| 2 | +import glob |
| 3 | +import os |
| 4 | +import re |
| 5 | + |
| 6 | +from stdlib import is_std_lib |
| 7 | + |
| 8 | +index = defaultdict(set) |
| 9 | + |
| 10 | +import_regex = re.compile('^(?:from|import)\s(?P<module>\w+).*') |
| 11 | + |
| 12 | +dirname = os.getcwd() |
| 13 | + |
| 14 | + |
| 15 | +def get_dirs(): |
| 16 | + for path in glob.glob('{}/[0-9]*'.format(dirname)): |
| 17 | + yield path |
| 18 | + |
| 19 | + |
| 20 | +def get_files(path): |
| 21 | + for fi in os.listdir(path): |
| 22 | + if fi.endswith('.py'): |
| 23 | + yield os.path.join(path, fi) |
| 24 | + |
| 25 | + |
| 26 | +def get_lines(src): |
| 27 | + with open(scr) as f: |
| 28 | + for line in f: |
| 29 | + yield line |
| 30 | + |
| 31 | + |
| 32 | +if __name__ == '__main__': |
| 33 | + for path in get_dirs(): |
| 34 | + day = os.path.basename(path) |
| 35 | + for scr in get_files(path): |
| 36 | + for line in get_lines(scr): |
| 37 | + m = import_regex.match(line) |
| 38 | + if m: |
| 39 | + mod = m.groupdict()['module'] |
| 40 | + index[mod].add(day) |
| 41 | + |
| 42 | + cnt = Counter() |
| 43 | + |
| 44 | + for mod, scripts in sorted(index.items()): |
| 45 | + if mod == 'common' or \ |
| 46 | + any(glob.glob(os.path.join(dirname, day, mod + '.py')) |
| 47 | + for day in scripts): |
| 48 | + source = 'own' |
| 49 | + else: |
| 50 | + source = 'stdlib' if is_std_lib(mod) else 'pypi' |
| 51 | + cnt[source] += 1 |
| 52 | + appeared_in = ', '.join(sorted(scripts)) |
| 53 | + print(f'{mod:<12} | {source:<6} | {appeared_in}') |
| 54 | + |
| 55 | + total = sum(cnt.values()) |
| 56 | + print() |
| 57 | + for source, count in cnt.most_common(): |
| 58 | + print(f'{source:<10}: {count:>3} ({count/total*100:.1f}%)') |
| 59 | + print('-' * 30) |
| 60 | + print(f'Total: {total}') |
0 commit comments