Created
September 18, 2015 21:40
-
-
Save acmiyaguchi/8595b85b73231e895500 to your computer and use it in GitHub Desktop.
Tooltool manifest checker
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 argparse | |
import logging | |
import os | |
import json | |
import subprocess | |
import sys | |
log = logging.getLogger(__name__) | |
def locate_manifests(directory, tooltool): | |
""" locate manifests that end with *.tt and *.manifests""" | |
matches = [] | |
for root, dirs, files in os.walk(directory): | |
for filename in files: | |
if filename.endswith(('.tt', '.manifest')): | |
fullpath = os.path.join(root, filename) | |
# Test if its valid JSON | |
try: | |
with open(fullpath, 'r') as f: | |
val = json.load(f) | |
if not isinstance(val, list): | |
raise ValueError | |
except ValueError: | |
log.debug("Invalid manifest {}".format(fullpath)) | |
continue | |
# Test if this is a valid tt-manifest by seeing if tooltool | |
# will provide a listing. This is kind of slow but it lets us | |
# verify that the manifest is valid without rewriting all the | |
# logic (does it have the right fields?) | |
cmd = [tooltool, 'list', '--manifest', fullpath] | |
with open(os.devnull, 'w') as devnull: | |
if subprocess.call(cmd, stdout=devnull, stderr=devnull): | |
log.debug("Invalid manifest {}".format(fullpath)) | |
continue | |
# Valid manifest | |
matches.append(fullpath) | |
return matches | |
def check_ttl(directory, tooltool, auth=None, url=None): | |
manifests = locate_manifests(directory, tooltool) | |
log.debug(manifests) | |
def main(argv): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('directory', | |
help='Directory to search for manifests') | |
parser.add_argument('tooltool', | |
help='Path to the tooltool client.') | |
parser.add_argument('---authentication-file', dest='auth_file', default=None, | |
help='Use the RelengAPI token found in the given file to ' | |
'authenticate to the RelengAPI server.') | |
parser.add_argument('--url', dest='base_url', default=None, | |
help='RelengAPI URL ending with /tooltool/; default ' | |
'is appropriate for Mozilla') | |
parser.add_argument('-q', '--quiet', default=logging.INFO, | |
dest='loglevel', action='store_const', const=logging.ERROR) | |
parser.add_argument('-v', '--verbose', | |
dest='loglevel', action='store_const', const=logging.DEBUG) | |
args = parser.parse_args(argv[1:]) | |
# set up logging to the console | |
log.setLevel(args.loglevel) | |
ch = logging.StreamHandler() | |
cf = logging.Formatter("%(levelname)s - %(message)s") | |
ch.setFormatter(cf) | |
log.addHandler(ch) | |
if not os.path.exists(args.directory): | |
log.error("Directory does not exists") | |
return 1 | |
if not os.path.isfile(args.tooltool): | |
log.error("Tooltool path is invalid") | |
return 1 | |
# test if tooltool will run | |
try: | |
with open(os.devnull, 'w') as devnull: | |
subprocess.call([args.tooltool], stdout=devnull, stderr=devnull) | |
except Exception as e: | |
log.error("There was an error executing tooltool: {}".format(str(e))) | |
return 1 | |
return 0 if check_ttl(args.directory, args.tooltool, args.auth_file, args.base_url) else 1 | |
if __name__ == '__main__': | |
sys.exit(main(sys.argv)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment