flyte
4/3/2014 - 4:19 PM

Loop through cookbook directories and find out which versions you've got.

Loop through cookbook directories and find out which versions you've got.

import os
from distutils.version import LooseVersion as Version

if __name__ == "__main__":
	cb_versions = {}
	for cb_dir in os.listdir("."):
		if cb_dir.startswith("cookbooks"):
			print "Scanning %s" % cb_dir
			for cb in os.listdir(cb_dir):
				if not os.path.isdir(os.path.join(cb_dir, cb)):
					print "Skipping %s as it is not a directory" % cb
					continue
				try:
					metadata = open(os.path.join(cb_dir, cb, "metadata.rb")).readlines()
				except IOError, e:
					print "Failed to open metadata.rb: %s" % e
					continue
				version_line = [x for x in metadata if x.startswith("version")][0]
				version = Version(version_line.split()[1].strip('"'))
				if cb in cb_versions and cb_versions[cb] >= version:
					continue
				if cb in cb_versions:
					print "Replacing %s with new version." % cb
				cb_versions[cb] = version
	print cb_versions