Last active
May 29, 2019 04:26
-
-
Save nekoya/af57ac574048a1957d1c02119bd27645 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import argparse | |
import json | |
import subprocess | |
from collections import namedtuple | |
def execute(arg, cwd): | |
proc = subprocess.Popen( | |
arg, | |
shell=True, | |
cwd=cwd, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
return proc.communicate() | |
class Package(namedtuple( | |
'Package', | |
['name', 'current', 'wanted', 'latest', 'type', 'url']) | |
): | |
def __eq__(self, other): | |
def f(x): | |
return ':'.join([x.name, x.current, x.latest]) | |
return f(self) == f(other) | |
def print_table(packages): | |
longest = max([len(x.name) for x in packages]) | |
fmt = '{p.name:<{width}} {p.current} -> {p.latest}' | |
print('```') | |
for p in packages: | |
print(fmt.format(width=longest, p=p)) | |
print('```') | |
print() | |
def get_packages(path): | |
(out, err) = execute('yarn outdated --json', cwd=path) | |
# if err: | |
# raise Exception(err) | |
entries = [json.loads(x) for x in out.split(b'\n') if x] | |
tables = [x for x in entries if x['type'] == 'table'] | |
assert len(tables) == 1, 'yarn outdated has multiple tables' | |
packages = [Package(*x) for x in tables[0]['data']['body']] | |
return packages | |
def parse_args(): | |
parser = argparse.ArgumentParser(description='Listing up the target packages for gardening.') | |
parser.add_argument('paths', type=str, nargs='*', help='target paths') | |
return parser.parse_args() | |
if __name__ == '__main__': | |
args = parse_args() | |
unique_packages = [] | |
print('## projects') | |
print() | |
for path in args.paths: | |
packages = get_packages(path) | |
print('### %s' % path) | |
print_table(packages) | |
[unique_packages.append(x) for x in packages | |
if x not in unique_packages] | |
print('## unique packages') | |
sorted_packages = sorted(unique_packages) | |
print_table(sorted_packages) | |
changelogs = json.loads(open('./ui/changelogs.json').read()) | |
for package in sorted_packages: | |
url = changelogs.get(package.name) | |
if url is not None: | |
print('## %s %s -> %s\n\n- %s\n' % (package.name, package.current, package.latest, url)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Python 3系を前提としていたが、先頭に
だけで2系も大丈夫そうだと感じた。