From a9f46e21adb501e15850b2bb0f80c1803a694acb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Per=20=C3=98yvind=20Karlsen?= Date: Wed, 29 Jun 2016 01:43:19 +0200 Subject: start on github ~bridge, so far only local git-svn repo import --- MgaRepo/GitHub.py | 78 ++++++++++++++++++++++++++++++++++++---- MgaRepo/commands/githubimport.py | 33 +++++++++++++++++ MgaRepo/git.py | 9 +++++ mgarepo.conf | 4 +++ 4 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 MgaRepo/commands/githubimport.py diff --git a/MgaRepo/GitHub.py b/MgaRepo/GitHub.py index 519b9e4..6d00d0f 100644 --- a/MgaRepo/GitHub.py +++ b/MgaRepo/GitHub.py @@ -1,10 +1,76 @@ -import github - from MgaRepo import Error, config +from MgaRepo.rpmutil import detectVCS, get_pkg_tag +from MgaRepo.layout import package_name, remove_current +from MgaRepo.git import GIT +from MgaRepo.svn import SVN +import github +from rpm import RPMTAG_SUMMARY, RPMTAG_URL class GitHub(object): def __init__(self, username = config.get("github", "login"), password = config.get("github", "password")): - print("username: %s password: %s" % (username, password)) - self.github = github.Github(login_or_token=username, password=password) - self.organization = self.github.get_organization(config.get("github", "organization", "mdkcauldron")) - self.repos = self.organization.get_repos() + self._github = github.Github(login_or_token=username, password=password) + self._organization = self._github.get_organization(config.get("github", "organization", "mdkcauldron")) + self._repos = self._organization.get_repos() + + def repository_exists(self, name): + for repo in self._repos: + if repo.name == name: + return repo + return None + + def create_repository(self, pkgname, **kwargs): + repository = self._organization.create_repo(pkgname, **kwargs) + return repository + + # workaround pygithub bug + @staticmethod + def __get_stats_commit_activity(self): + """ + :calls: `GET /repos/:owner/:repo/stats/commit_activity `_ + :rtype: None or list of :class:`github.StatsCommitActivity.StatsCommitActivity` + """ + headers, data = self._requester.requestJsonAndCheck( + "GET", + self.url + "/stats/commit_activity" + ) + if data == None: + return None + else: + return [ + github.StatsCommitActivity.StatsCommitActivity(self._requester, headers, attributes, completed=True) + for attributes in data + ] + + def import_package(self, target): + vcs = detectVCS(target) + top_dir = vcs.get_topdir() + info = vcs.info2(top_dir) + pkgname = package_name(remove_current(info["URL"])) + + repository = self.repository_exists(pkgname) + #if not repository or repository.get_stats_commit_activity() is None: + if not repository or self.__get_stats_commit_activity(repository) is None: + if not repository: + summary = get_pkg_tag(RPMTAG_SUMMARY, path=top_dir) + url = get_pkg_tag(RPMTAG_URL, path=top_dir) + repository = self.create_repository(pkgname, description=summary, homepage=url) + print("GitHub repository created at " + repository.html_url) + else: + print("Empty GitHub repository already created at %s, using" % repository.html_url) + + if isinstance(vcs, GIT): + status, output = vcs.remote("add", repository.full_name, repository.ssh_url, noerror=True) + if status: + if status == 128 and ("fatal: remote %s already exists." % repository.full_name) \ + in output: + pass + else: + raise Error(output) + + status, output = vcs.push(repository.full_name, "master", show=True) + if status == 0: + print("Success!") + return True + else: + raise Error("GitHub repository already exists at " + repository.html_url) + raise Error("GitHub import failed...") diff --git a/MgaRepo/commands/githubimport.py b/MgaRepo/commands/githubimport.py new file mode 100644 index 0000000..afc73c5 --- /dev/null +++ b/MgaRepo/commands/githubimport.py @@ -0,0 +1,33 @@ +#!/usr/bin/python +from MgaRepo import Error +from MgaRepo.command import * +from MgaRepo.GitHub import GitHub +import getopt +import sys + +HELP = """\ +Usage: mgarepo github-import [OPTIONS] URL + +Import a git-svn cloned repository to github + +Options: + -h Show this message + +Examples: + mgarepo githubimport existingpkg + mgarepo githubimport svn+ssh://svn.mageia.org/svn/packages/cauldron/existingpkg +""" + +def githubimport(target="."): + github = GitHub() + github.import_package(target) + +def parse_options(): + parser = OptionParser(help=HELP) + opts, args = parser.parse_args() + return opts + +def main(): + do_command(parse_options, githubimport) + +# vim:et:ts=4:sw=4 diff --git a/MgaRepo/git.py b/MgaRepo/git.py index 08cccf6..9760298 100644 --- a/MgaRepo/git.py +++ b/MgaRepo/git.py @@ -175,6 +175,15 @@ class GIT(VCS): return [x.split() for x in output.split()] return None + def remote(self, *args, **kwargs): + cmd = ["remote"] + list(args) + status, output = self._execVcs(*cmd, **kwargs) + return status, output + + def push(self, *args, **kwargs): + cmd = ["push"] + list(args) + status, output = self._execVcs(*cmd, **kwargs) + return status, output class GITLook(VCSLook): def __init__(self, repospath, txn=None, rev=None): diff --git a/mgarepo.conf b/mgarepo.conf index 932fa0d..bbb1d62 100644 --- a/mgarepo.conf +++ b/mgarepo.conf @@ -31,3 +31,7 @@ upload_host = binrepo.mageia.org host = maintdb.mageia.org url = http://maintdb.mageia.org/ +[github] +#login = someuser +#password = somepassword +organization = mdkcauldron -- cgit v1.2.1