Skip to content

Instantly share code, notes, and snippets.

@hapo31
Last active March 18, 2018 08:49
Show Gist options
  • Save hapo31/a617de4374a19560b2a488d648cbfef4 to your computer and use it in GitHub Desktop.
Save hapo31/a617de4374a19560b2a488d648cbfef4 to your computer and use it in GitHub Desktop.
package.jsonのdependenciesなどから使われてるパッケージのバージョンの更新日時などを抜いてくるスクリプト
#-*- coding: utf-8 -*-
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)
with open("versions.csv", "w") as output:
output.write("package,version,date,homepage\n")
for package, version in dependencies.items():
r = get_npm_version_date(package, version)
print("%s(%s)..." % (package, version))
if r[0]:
output.write("%s,%s,%s,%s\n" % r)
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