Created
January 4, 2017 11:04
-
-
Save spacewander/6f22ac2a68847f343d8056e28eb1081c to your computer and use it in GitHub Desktop.
Quick and dirty script for git-extras
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 | |
# coding: utf-8 | |
""" | |
Run this quick-and-dirty script to list dependencies used in git-extras/bin. | |
With -t option, print result with markdown table syntax. | |
With -v option, grep usage to filter out false positive result. | |
""" | |
from collections import defaultdict | |
import os | |
import re | |
import sys | |
import subprocess | |
def is_cmd(cmd): | |
try: | |
path = subprocess.check_output(['which', cmd]) | |
return os.path.isfile(path.strip()) | |
except subprocess.CalledProcessError: | |
return False | |
print_as_table = False | |
print_verbose = False | |
if len(sys.argv) > 1: | |
print_as_table = sys.argv[1] == '-t' | |
print_verbose = sys.argv[1] == '-v' | |
word_pattern = re.compile('[^\-_$\w]([a-z]+)[^=_\-(\w]') | |
file_token = {} | |
for fn in os.listdir('.'): | |
if not fn.startswith('git-'): | |
continue | |
with open(fn) as f: | |
context = f.read() | |
file_token[fn] = set(filter(lambda x: len(x) > 1, re.findall(word_pattern, context))) | |
token_file = defaultdict(set) | |
blacklist = set() | |
whitelist = set() | |
for fn in file_token: | |
for token in file_token[fn]: | |
if token in blacklist: | |
continue | |
if token not in whitelist and not is_cmd(token): | |
blacklist.add(token) | |
continue | |
else: | |
whitelist.add(token) | |
token_file[token].add(fn) | |
if print_as_table: | |
print('| dependency | script |') | |
print('| --- | --- |') | |
sorted_token = sorted(token_file.keys()) | |
for token in sorted_token: | |
if print_as_table: | |
files = ' '.join(map(lambda x: '`%s`'%x, token_file[token])) | |
print('| %-20s | %s |' % (token, files)) | |
elif print_verbose: | |
args = ['grep', '--color', token] | |
args.extend(token_file[token]) | |
print(' '.join(args)) | |
subprocess.call(args) | |
print('') | |
else: | |
files = ' '.join(token_file[token]) | |
print('%-20s: %s' % (token, files)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment