Last active
January 28, 2017 16:37
-
-
Save ikrauchanka/424f6efca5097bd6ca5654dc187f7456 to your computer and use it in GitHub Desktop.
list_outdated_cookbooks
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 | |
import os | |
import urllib2 | |
import json | |
from packaging import version | |
import sys | |
from prettytable import PrettyTable | |
repo = "/path/to/repo/folder" | |
exclude_cookbooks = "xxx-" | |
strip_pattern = ' \t\n\r\'\"' | |
# check if cookbook name passed | |
if len(sys.argv) > 1: | |
watch_cookbook = sys.argv[1] | |
else: | |
print "sys.argv[1] is empty" | |
sys.exit(1) | |
cookbooks = [] | |
# read local cookbook metadata file | |
if os.path.isfile(repo + '/' + watch_cookbook + '/' + 'metadata.json'): | |
# gather the list of depended cookbooks from metadata.json | |
with open(repo + '/' + watch_cookbook + '/' + 'metadata.json') as data_file: | |
data = json.load(data_file) | |
for dep_cookbook in data['dependencies']: | |
if exclude_cookbooks not in dep_cookbook: | |
cookbooks.append(dep_cookbook) | |
elif os.path.isfile(repo + '/' + watch_cookbook + '/' + 'metadata.rb'): | |
f = open(repo + '/' + watch_cookbook + '/' + 'metadata.rb', 'r') | |
# gather the list of depended cookbooks from metadata.rb | |
for line in f: | |
if "depends" in line: | |
dep_cookbook = line.split("depends")[-1].strip(strip_pattern) | |
if exclude_cookbooks not in dep_cookbook: | |
cookbooks.append(dep_cookbook) | |
else: | |
print "Neither metadata.rb nor metadata.json has been found " | |
# calling market for latest version number | |
t = PrettyTable(['cookbook', 'local', 'remote']) | |
#print cookbooks | |
for cb in cookbooks: | |
req = urllib2.urlopen("https://supermarket.chef.io/api/v1/cookbooks/" + cb + "/versions/latest").read() | |
resp = json.loads(req) | |
market_version = resp["version"] | |
if os.path.isfile(repo + '/' + cb + '/' + 'metadata.json'): | |
with open(repo + '/' + cb + '/' + 'metadata.json') as data_file: | |
data = json.load(data_file) | |
local_version = data["version"] | |
elif os.path.isfile(repo + '/' + cb + '/' + 'metadata.rb'): | |
f = open(repo + '/' + cb + '/' + 'metadata.rb', 'r') | |
for line in f: | |
if "version" in line: | |
local_version = line.split("version")[-1].strip(strip_pattern) | |
else: | |
print "Neither metadata.rb nor metadata.json has been found " | |
# comparing versions and show which cookbooks outdated | |
if version.parse(local_version) < version.parse(market_version): | |
t.add_row([cb, local_version, market_version]) | |
t.align["cookbook"] = "l" | |
t.align["local"] = "l" | |
t.align["remote"] = "l" | |
print t |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment