Last active
August 31, 2017 08:59
-
-
Save hapo31/75c3c0d65be6c765761f1dd42623feaf to your computer and use it in GitHub Desktop.
package.jsonからバージョンの更新日時抜いてくるやつ。マルチスレッドandプログレスバーもどき表示バージョン
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
| #-*- coding: utf-8 -*- | |
| import time | |
| import threading | |
| import subprocess | |
| import json | |
| import sys | |
| import re | |
| from datetime import datetime | |
| timeformat = "%Y-%m-%d" | |
| out_timeformat = "%Y/%m/%d" | |
| timeregex = re.compile(r" *'(.*)': '(.*)T.*Z'.*$") | |
| homepageregex = re.compile(r".*homepage: '(.*)',?") | |
| def main(): | |
| if len(sys.argv) <= 1: | |
| print("Usage: python3 npmpackage_versions.py package.json") | |
| exit(1) | |
| with open(sys.argv[1], "r") as f: | |
| package_json = json.loads(f.read()) | |
| dependencies = package_json["dependencies"] | |
| devDependencies = package_json["devDependencies"] | |
| # devDependenciesは別のファイルで欲しい、などの場合はこのあたりをいじる | |
| dependencies.update(devDependencies) | |
| results = [] | |
| threads = [] | |
| for package, version in dependencies.items(): | |
| t = threading.Thread( | |
| name=package, target=store_results, args=(package, version, results)) | |
| t.start() | |
| threads.append(t) | |
| n = 0 | |
| max = len(threads) | |
| for thread in threads: | |
| print("[", end="") | |
| print("=" * n, end="") | |
| if n != 0 and n < max - 1: | |
| print(">", end="") | |
| print(" " * (max - n - 1), end="") | |
| print("]%02d/%02d\r" % (n, max - 1), end="") | |
| thread.join() | |
| time.sleep(0.01) | |
| n += 1 | |
| with open("versions.txt", "w") as output: | |
| output.write("package,version,date,homepage\n") | |
| for result in results: | |
| if result[0]: | |
| output.write("%s,%s,%s,%s\n" % result) | |
| print("") | |
| def store_results(package_name, version, dest): | |
| print("%s(%s)..." % (package_name, version)) | |
| result = get_npm_version_date(package_name, version) | |
| dest.append(result) | |
| def get_npm_version_date(package_name, version): | |
| """ | |
| @returns (package_name: String, package_version: String, date_str: String) | |
| """ | |
| std_out = subprocess.check_output( | |
| ["npm", "info", package_name]).decode("utf-8") | |
| if std_out.find("npm ERR!") >= 0: | |
| print("%s not found, skiped." % package_name) | |
| return (None, None, None, None) | |
| if version[0] == "^": | |
| version = version[1:] | |
| homepage_line = std_out[std_out.find("homepage"):] | |
| times = std_out[std_out.find( | |
| "time:"):].split(",") | |
| m = homepageregex.match(homepage_line) | |
| homepage = "" | |
| if m: | |
| homepage = m.group(1).replace(" ", "") | |
| for time in times: | |
| m = timeregex.match(time.strip()) | |
| if m and m.group(1) == version: | |
| date = datetime.strptime(m.group(2), timeformat) | |
| return (package_name, version, date.strftime(out_timeformat), homepage) | |
| return (None, None, None, None) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment