@@ -0,0 +1,2863 @@
+from distutils import version%0Aimport github%0Aimport urllib.request%0A%0Afrom mesonwrap import inventory%0A%0A%0AUPSTREAM_WRAP_LABEL = 'upstream.wrap'%0APATCH_ZIP_LABEL = 'patch.zip'%0A%0A%0A# TODO implement caching%0A# https://developer.github.com/v3/#conditional-requests%0A# or just some cache with timeouts%0Aclass GithubDB:%0A%0A    def __init__(self, pygithub: github.Github,%0A                 organization: str = 'mesonbuild'):%0A        assert isinstance(pygithub, github.Github)%0A        assert isinstance(organization, str)%0A        self._gh = pygithub%0A        self._org = organization%0A%0A    def close(self):%0A        pass%0A%0A    @property%0A    def _organization(self):%0A        return self._gh.get_organization(self._org)%0A%0A    def insert(self,%0A               project: str,%0A               branch: str,%0A               revision: int,%0A               wrap: str,%0A               zip: bytes):%0A        raise NotImplementedError('Only read only access is supported')%0A%0A    def name_search(self, text):%0A        return sorted(%5Brepo.name for repo in self._organization.get_repos()%0A                       if (inventory.is_wrap_project_name(repo.name) and%0A                           repo.name.startswith(text))%5D)%0A%0A    def _get_versions(self, project):%0A        repo = self._organization.get_repo(project)%0A        for release in repo.get_releases():%0A            dash = release.tag_name.rfind('-')%0A            if dash == -1:%0A                continue%0A            version = release.tag_name%5B:dash%5D%0A            revision = int(release.tag_name%5Bdash + 1:%5D)%0A            yield (version, revision)%0A%0A    def get_versions(self, project, latest=False):%0A        if latest:%0A            # get_latest_release() is not following semantic versioning%0A            latest_ver = max((version.LooseVersion(r%5B0%5D), r%5B1%5D)%0A                             for r in self._get_versions(project))%0A            return %5B(str(latest_ver%5B0%5D), latest_ver%5B1%5D)%5D%0A        else:%0A            return list(self._get_versions(project))%0A%0A    def _get_asset(self, label, project, branch, revision) -%3E bytes:%0A        try:%0A            repo = self._organization.get_repo(project)%0A            release = repo.get_release('%7B%7D-%7B%7D'.format(branch, revision))%0A            for asset in release.get_assets():%0A                if asset.label == label:%0A                    url = asset.browser_download_url%0A                    with urllib.request.urlopen(url) as a:%0A                        return a.read()%0A            # FIXME%0A            return None%0A        except Exception:%0A            return None%0A%0A    # TODO consider redirect%0A    def get_wrap(self, project, branch, revision) -%3E str:%0A        return self._get_asset(UPSTREAM_WRAP_LABEL,%0A                               project, branch, revision).decode('utf-8')%0A%0A    # TODO consider redirect%0A    def get_zip(self, project, branch, revision) -%3E bytes:%0A        return self._get_asset(PATCH_ZIP_LABEL, project, branch, revision)%0A

Add githubdb.GithubDB