Commits

Manabu TERADA committed b5293f6

fix some bugs, maybe finish

Comments (0)

Files changed (4)

     recipe = c2.recipe.bitbucketbk
     username = xxxxxxxxxxxxx
     password = xxxxxxxxxxxxxxxxxx
-	location = backups
+	location = /backups
 
 Run the buildout ::
 
 
     bin/bitbucketbk
 
-You will see backups in  `var/backups`.
+You will see backups in  `/backups`.
 
+
+
+Cron job integration
+===========================
+
+For example ::
+
+	[backupcronjob]
+	recipe = z3c.recipe.usercrontab
+	times = 0 12 * * *
+	command = ${buildout:directory}/bin/bitbucketbk

c2/recipe/bitbucketbk/__init__.py

 
 import os
 import zc.buildout
+import zc.recipe.egg
 import logging
 
 logger = logging.getLogger('bitbucketbk')
         bin_dir = self.buildout['buildout']['bin-directory']
         buildout_dir = os.path.join(bin_dir, os.path.pardir)
         options.setdefault('buildout_dir', buildout_dir)
-        
+                
         options.setdefault('username', '')
         options.setdefault('password', '')
         options.setdefault('location', 'backups')
         
+        self.egg = zc.recipe.egg.Egg(buildout, options['recipe'], options)
+        
         python = buildout['buildout']['python']
         options['executable'] = buildout[python]['executable']
         options['bin-directory'] = buildout['buildout']['bin-directory']
         options['backup_name'] = self.name
         check_for_true(options, [])
+        
         self.options = options
 
     def install(self):
 """
         opts = self.options.copy()
         initialization = initialization_template % opts
-        requirements, ws = self.egg.working_set(['c2.recipe.bkups3',
+        requirements, ws = self.egg.working_set(['c2.recipe.bitbucketbk',
                                                  'zc.buildout',
                                                  'zc.recipe.egg'])
         

c2/recipe/bitbucketbk/backuprunner.py

 import os
 import json
 
-from urllib2 #import urlopen, HTTPBasicAuthHandler, build_opener, install_opener
+import urllib2
 import base64
 
 from mercurial import hg, ui, commands
 
-BITBUCKET_API = "https://api.bitbucket.org/1.0/"
-BITBUCKET_BASE_URL = "https://%(user)s:%(passwd)s@bitbucket.org/"
+BITBUCKET_API = u"https://api.bitbucket.org/1.0/"
+BITBUCKET_BASE_URL = u"https://%(user)s:%(passwd)s@bitbucket.org/"
 
 def get_repositories(user, passwd):
-    """no Longin"""
+    """Longin & get repositories"""
     api_url = BITBUCKET_API + "users/" + user
 
     authString = base64.encodestring('%s:%s' % (user, passwd))
     headers = {'Authorization':"Basic %s" % authString}
     req = urllib2.Request(api_url, None, headers)
     bitbucket_obj = urllib2.urlopen(req)
-    
-    repos = json.load(bitbucket_obj).get('repositoryes', [])
+
+    repos = json.load(bitbucket_obj).get('repositories', [])
     for repo in repos:
         yield repo.get('name', None), repo.get('scm', '')
 
 def backup_main(username, password, location):
     """Main method, gets called by generated bin/bitbucketbk.
     """
-    repos = get_repositories(user, passwd)
-    print list(repos)
+    repos = get_repositories(username, password)
     for repo_name, scm in repos:
-        if scm == "hg":
+        if scm == u"hg":
             base_url = BITBUCKET_BASE_URL % {'user' : username, 'passwd' : password}
-            repo_url = base_url + user + '/' + repo_name
+            repo_url = base_url + username + u'/' + repo_name.lower()
+            if not isinstance(repo_url, str):
+                repo_url = repo_url.encode('utf-8')
             folder_location = os.path.join(location, repo_name)
+            if not isinstance(folder_location, str):
+                folder_location = folder_location.encode('utf-8')
             if not has_repo(repo_name, location):
                 create_hg_repo(repo_url, folder_location)
             else:
       include_package_data=True,
       zip_safe=False,
       install_requires=['setuptools',
-                        'zc.buildout'
+                        'zc.buildout',
+                        'zc.recipe.egg',
                         # -*- Extra requirements: -*-
                         ],
       tests_require=tests_require,