Last active
December 20, 2015 22:38
-
-
Save nemith/6206104 to your computer and use it in GitHub Desktop.
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
| import requests | |
| import json | |
| import sys | |
| import argparse | |
| import urlparse | |
| from lxml import etree | |
| import logbook | |
| from logbook.more import ColorizedStderrHandler | |
| log = logbook.Logger(sys.argv[0]) | |
| # Default to no output | |
| log_handler = logbook.NullHandler() | |
| log_handler.push_application() | |
| BASE_LOG_LEVEL = logbook.WARNING | |
| def init_stderr_logging(modifier=0, log_level=BASE_LOG_LEVEL): | |
| if modifier: | |
| log_level -= modifier | |
| if log_level < 1: | |
| log_level = 1 | |
| stderr_handler = ColorizedStderrHandler(level=log_level, bubble=True) | |
| stderr_handler.push_application() | |
| log.debug("Logging level set to: %s" % (logbook.get_level_name(log_level))) | |
| def get_api_url(url, token=None): | |
| log.debug("Fetching url '%s" % url) | |
| headers = {} | |
| if token: | |
| headers['Authorization'] = "token {}".format(token) | |
| r = requests.get(url, headers=headers) | |
| r.raise_for_status() | |
| return json.loads(r.text or r.content) | |
| def reponame_to_path(name): | |
| return name.replace('proprietary_', '').replace('_', '/') | |
| class RepoGenerator(object): | |
| GITHUB_API_BASE_URL = "https://api.github.com" | |
| def __init__(self, branch, owner, token=None): | |
| self.branch = branch | |
| self.owner = owner | |
| self.token = token | |
| def _get_repo_url(self): | |
| return urlparse.urljoin(self.GITHUB_API_BASE_URL, "/users/{}/repos".format(self.owner)) | |
| def matching_repos(self): | |
| repos = get_api_url(self._get_repo_url(), token=self.token) | |
| for repo in repos: | |
| branch_url = repo.get('branches_url') | |
| repo_name = repo['name'] | |
| log.info("Found repo '%s'" % repo_name) | |
| if branch_url: | |
| branch_url = branch_url.replace('{/branch}', '') | |
| branches = get_api_url(branch_url) | |
| branch_names = [ x['name'] for x in branches ] | |
| log.debug("Found branches '%s'" % ', '.join(branch_names)) | |
| if self.branch in branch_names: | |
| yield repo_name | |
| else: | |
| log.info("Could not find branch '{0}' for repo '{1}'".format(self.branch, repo_name)) | |
| else: | |
| log.info("No 'branch_url' found for repo '%s'" % repo_name) | |
| def to_xml(self): | |
| root = etree.Element("manifest") | |
| for repo in self.matching_repos(): | |
| etree.SubElement(root, 'project', | |
| path=reponame_to_path(repo), | |
| name="{}/{}".format(self.owner, repo)) | |
| return etree.tostring(root, pretty_print=True, xml_declaration=True, encoding="UTF-8") | |
| def main(argv=None): | |
| if argv is None: | |
| argv = sys.argv | |
| parser = argparse.ArgumentParser(description='Grab latest propritary blob repos for a branch from github user account.') | |
| parser.add_argument('--branch', '-b', required=True, | |
| help="Which git branch to use look for. (e.g 'cm-10.1')") | |
| parser.add_argument('-v', action='count', dest='verbosity', | |
| help="Increase verbosity level to stderr") | |
| parser.add_argument('--owner', '-g', default='TheMuppets', | |
| help="Github owner repository") | |
| parser.add_argument('--token', '-t', | |
| help="Github API token") | |
| opts = parser.parse_args(argv[1:]) | |
| init_stderr_logging(opts.verbosity) | |
| generator = RepoGenerator(opts.branch, opts.owner, token=opts.token) | |
| print generator.to_xml() | |
| if __name__ == "__main__": | |
| sys.exit(main()) |
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
| homestar:cm-10.2 bbennett$ python ~/prop.py --branch cm-10.2 > .repo/local_manifests/propritary_files.xml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment