git-migration: pldpkg.py - creates and removes repos to project

glen glen at pld-linux.org
Fri Feb 18 23:50:07 CET 2011


Author: glen                         Date: Fri Feb 18 22:50:07 2011 GMT
Module: git-migration                 Tag: HEAD
---- Log message:
- creates and removes repos to project

---- Files affected:
git-migration:
   pldpkg.py (1.2 -> 1.3) 

---- Diffs:

================================================================
Index: git-migration/pldpkg.py
diff -u git-migration/pldpkg.py:1.2 git-migration/pldpkg.py:1.3
--- git-migration/pldpkg.py:1.2	Fri Feb 11 08:10:44 2011
+++ git-migration/pldpkg.py	Fri Feb 18 23:50:02 2011
@@ -1,70 +1,117 @@
 #!/usr/bin/python
 import sys
-import optparse
+from optparse import OptionParser
 from subprocess import Popen, PIPE
-
 from github2.client import Github
 
-OPTION_LIST = (
-    optparse.make_option('-t', '--api-token',
-            default=None, action="store", dest="api_token", type="str",
-            help="Github API token. Default is to find this from git config"),
-    optparse.make_option('-u', '--api-user',
-            default=None, action="store", dest="api_user", type="str",
-            help="Github Username. Default is to find this from git config"),
-)
-
-# to use, setup your github user and api key:
+# To use, setup your github user and api token:
+# API Token - can be found on the lower right of https://github.com/account
 # git config --global github.user USER
 # git config --global github.token API_TOKEN
 
-class Repository(object):
-    def __init__(self, username=None, api_user=None, api_token=None):
+class Package(object):
+    def __init__(self, login=None, account=None, apitoken=None, debug = None):
         self.project = "pld-linux"
-        self.api_user = api_user or self.git_config_get("github.user")
-        self.api_token = api_token or self.git_config_get("github.token")
-        self.username = username or self.api_user
-        print("U:(%s) T:(%s) F:(%s)" % (self.api_user, self.api_token, self.username))
-        self.client = Github(self.api_user, self.api_token, requests_per_second=1)
+        self.account = account or self.git_config_get("github.user")
+        self.apitoken = apitoken or self.git_config_get("github.token")
+        self.login = login or self.account
+        self.debug = debug
+        self.client = Github(self.account, self.apitoken, debug=self.debug)
 
     def git_config_get(self, key):
         pipe = Popen(["git", "config", "--get", key], stdout=PIPE)
         return pipe.communicate()[0].strip()
 
-    def add(self, package, description = '', homepage = ''):
+    def exists(self, package):
         name = "%s/%s" % (self.project, package)
-        repo = None
         try:
             repo = self.client.repos.show(name)
         except RuntimeError, e:
             if e.message.count("Repository not found"):
-                print "OK: %s not present, ok to add" % package
+                return False
             else:
                 raise
-        if repo:
-            print "OK: %s already exists" % package
-            return
+        return True
 
+    """ add package repository in GitHub """
+    def add(self, package, description = '', homepage = '', team='Developers'):
+        name = "%s/%s" % (self.project, package)
         repo = self.client.repos.create(name, description, homepage, public=True)
+        if team:
+            self.add_team(name, team)
+        return repo
 
+    """ delete repository for package """
     def delete(self, package):
         name = "%s/%s" % (self.project, package)
         res = self.client.repos.delete(name)
-        print res['delete_token']
-        # TODO process delete_token (dig source how)
-#        req = Github(self.api_user, res['delete_token'], requests_per_second=1)
-#        res = req.repos.delete(name)
-#        print res
-        print "OK: %s deleted" % package
-
-def parse_options(arguments):
-    parser = optparse.OptionParser(option_list=OPTION_LIST)
-    options, values = parser.parse_args(arguments)
-    return options, values
-
-
-options, values = parse_options(sys.argv[1:])
-username = values and values[0] or None
-f = Repository(username=username, **vars(options))
-#f.delete('eventum')
-#f.add('eventum', 'Eventum Issue / Bug tracking system', 'http://eventum.mysql.org/')
+        token = res['delete_token']
+        self.client.repos.delete(name, token)
+
+    """ add package to team """
+    def add_team(self, package, team):
+        name = "%s/%s" % (self.project, package)
+        # find team id
+        team_id = None
+        for t in self.client.organizations.teams(self.project):
+            if t.name == team:
+                team_id = t.id
+        if team_id == None:
+            raise RuntimeError, "Team '%s' not found" % team
+
+        team = self.client.teams.add_repository(str(team_id), name)
+        return team
+
+def parse_commandline():
+    """Parse the comandline and return parsed options."""
+
+    parser = OptionParser()
+    parser.description = __doc__
+
+    parser.set_usage('usage: %prog [options] (add|delete) [package].\n'
+                     'Try %prog --help for details.')
+    parser.add_option('-d', '--debug', action='store_true',
+                      help='Enables debugging mode')
+    parser.add_option('-l', '--login',
+                      help='Username to login with')
+    parser.add_option('-a', '--account',
+                      help='User owning the repositories to be changed ' \
+                           '[default: same as --login]')
+    parser.add_option('-t', '--apitoken',
+                      help='API Token - can be found on the lower right of ' \
+                           'https://github.com/account')
+
+    options, args = parser.parse_args()
+    if len(args) != 2:
+        parser.error('wrong number of arguments')
+    if (len(args) == 1 and args[0] in ['add', 'delete']):
+        parser.error('%r needs a package name as second parameter\n' % args[0])
+    if (len(args) == 2 and args[0] not in ['add', 'delete']):
+        parser.error('unknown command %r. Try "add" or "delete"\n' % args[0])
+
+    return options, args
+
+def main(options, args):
+    """This implements the actual program functionality"""
+
+    if not options.account:
+        options.account = options.login
+
+    p = Package(**vars(options))
+
+    command, package = args
+    if command == 'add':
+        if p.exists(package):
+            print "%r already exists in %r" % (package, p.project)
+        else:
+            p.add(package)
+            print "added %r to %r" % (package, p.project)
+    if command == 'delete':
+        if not p.exists(package):
+            print "%r does not exist in %r" % (package, p.project)
+        else:
+            p.delete(package)
+            print "removed %r from %r" % (package, p.project)
+
+if __name__ == '__main__':
+    main(*parse_commandline())
================================================================

---- CVS-web:
    http://cvs.pld-linux.org/cgi-bin/cvsweb.cgi/git-migration/pldpkg.py?r1=1.2&r2=1.3&f=u



More information about the pld-cvs-commit mailing list