svn.py

Go to the documentation of this file.
00001 import silme.io
00002 from silme.core import Blob
00003 from silme.io.clients import IOClient, RCSClient
00004 import silme.format
00005 from silme.core import L10nPackage
00006 from file import FileClient
00007 
00008 import pysvn
00009 import os
00010 import shutil
00011 import codecs
00012 import re
00013 
00014 def register(Manager):
00015     Manager.register(SVNClient)
00016 
00017 class SVNClient (RCSClient):
00018     name = 'svn'
00019     desc = "SVN Client"
00020     type = IOClient.__name__
00021     client = None
00022 
00023     def __init__ (self):
00024         self.user = {'login':None, 'password':None}
00025 
00026     @classmethod
00027     def get_blob(cls, path, source=True):
00028         (p, rev) = cls._explode_path(path)
00029         
00030         blob = Blob()
00031         blob.id = os.path.basename(path)
00032         if source:
00033             blob.source = cls.get_source_without_encoding(p)
00034         blob.uri = p
00035         return blob
00036 
00037     @classmethod
00038     def get_entitylist(cls, path, source=False, code='default', parser=None):
00039         (p, rev) = cls._explode_path(path)
00040 
00041         if not parser:
00042             parser = silme.format.Manager.get(path=p)
00043         src = cls.get_source(p, encoding = parser.encoding,
00044                             fallback = parser.fallback)
00045         entitylist = parser.get_entitylist(src[0], code=code)
00046         entitylist.id = os.path.basename(p)
00047         entitylist.uri = p
00048         if source:
00049             entitylist.source = src[0]
00050         entitylist.encoding = src[1]
00051         return entitylist
00052 
00053     @classmethod
00054     def get_l10nobject(cls, path, source=False, code='default', parser=None):
00055         (p, rev) = cls._explode_path(path)
00056 
00057         if not parser:
00058             parser = silme.format.Manager.get(path=p)
00059         src = cls.get_source(p, encoding = parser.encoding,
00060                             fallback = parser.fallback)
00061         l10nobject = parser.get_l10nobject(src[0], code=code)
00062         l10nobject.id = os.path.basename(p)
00063         l10nobject.uri = p
00064         if source:
00065             l10nobject.source = src[0]
00066         l10nobject.encoding = src[1]
00067         return l10nobject
00068 
00069     @classmethod
00070     def get_l10npackage(cls, path,
00071                         code='default',
00072                         object_type='l10nobject',
00073                         source=None,
00074                         ignore=['CVS','.svn','.DS_Store', '.hg']):
00075         (p, rev) = cls._explode_path(path)
00076 
00077         l10npackage = L10nPackage()
00078         l10npackage.id = os.path.basename(p)
00079         l10npackage.uri = p
00080         
00081         # if source is None, load it for blob
00082         if source is None:
00083             b_source = True # blob source
00084             oe_source = False # l10nobject & entitylist source
00085         elif source is False: # don't load it for anyone
00086             b_source = False
00087             oe_source = False
00088         else: # load it for everyone
00089             b_source = True
00090             oe_source = True
00091 
00092         cls.client = pysvn.Client()
00093         entry_list = cls.client.list(path, recurse=True)
00094         for i in entry_list:
00095             elem = os.path.basename(i[0].path)
00096             if ignore.__class__.__name__=='function': # is function
00097                 if ignore(i[0].path):
00098                     continue
00099                 else:
00100                     if os.path.basename(i[0].path) in ignore:
00101                         continue
00102             if i[0].kind == pysvn.node_kind.file:
00103                 dirname = os.path.dirname(i[0].path)
00104                 filename =    os.path.basename(i[0].path)
00105                 # @var relpath: relative-path to the given path'
00106                 relpath = re.sub(os.path.dirname(p), '', dirname, 1)
00107                 if relpath.startswith('/'):
00108                     relpath = os.path.split(relpath)[1]
00109                 try:
00110                     parser = silme.format.Manager.get(path=elem)
00111                 except Exception:
00112                     l10npackage.add_object(cls.get_blob(i[0].path, source=b_source), relpath)
00113                 else:
00114                     if object_type=='object':
00115                         l10npackage.add_object(cls.get_blob(i[0].path, source=b_source), relpath)
00116                     elif object_type=='entitylist':
00117                         l10npackage.add_objects(cls.get_entitylist(i[0].path, source=oe_source, code=code, parser=parser), relpath)
00118                     else:
00119                         l10npackage.add_objects(cls.get_l10nobject(i[0].path, source=oe_source, code=code, parser=parser), relpath)
00120         return l10npackage
00121 
00122 #========================
00123 
00124     @classmethod
00125     def _explode_path(cls, path):
00126         return (path, 0)
00127 
00128     @classmethod
00129     def _read_with_encoding(cls, path, encoding):
00130         client = pysvn.Client()
00131         text = client.cat(path, revision=pysvn.Revision( pysvn.opt_revision_kind.head ))
00132         return text
00133 
00134     @classmethod
00135     def _read_without_encoding(cls, path):
00136         client = pysvn.Client()
00137         text = client.cat(path, revision=pysvn.Revision( pysvn.opt_revision_kind.head ))
00138         return text
00139 
00140 #=========
00141 """
00142     @classmethod
00143     def get_source (cls, path, revision=None):
00144         string    = u''
00145         client = pysvn.Client()
00146         if revision == None:
00147             string = client.cat(path, revision=pysvn.Revision( pysvn.opt_revision_kind.head ))
00148         else:    
00149             string = client.cat(path, revision=pysvn.Revision( pysvn.opt_revision_kind.number, revision ))
00150         return string
00151 
00152     @classmethod
00153     def get_l10nobject (cls, path):
00154         format_parser = silme.format.Manager.get(path)
00155         source = cls.get_source(path)
00156         l10nobject = format_parser.parse(source)
00157         l10nobject.id = os.path.basename(path)
00158         return l10nobject
00159 
00160     @classmethod
00161     def get_l10npackage (cls, path, load_objects = True):
00162         l10npackage = L10nPackage()
00163         l10npackage.id = os.path.basename(path)
00164         client = pysvn.Client()
00165         entry_list = client.list(path, recurse=False)
00166         for i in entry_list:
00167             if i[0].kind == pysvn.node_kind.dir and i[0].path != path:
00168                 l10npackage.add_package(cls.get_l10npackage(i[0].path))
00169             if i[0].kind == pysvn.node_kind.file:
00170                 l10npackage.add_object(cls.get_l10nobject(path=i[0].path))
00171         return l10npackage
00172 
00173     @classmethod
00174     def writeObject (cls, object, path):
00175         tmppath = '/tmp/testtmp'
00176         format_parser = silme.format.Manager.get(object.id)
00177         if not format_parser:
00178             if isinstance(object, Object):
00179                 string = object.source
00180             else:
00181                 return
00182         else:
00183             string = formatParser.dump_l10nobject(object)
00184         cls.write_to_file(path, string)
00185 
00186 
00187     @classmethod
00188     def write_to_file (cls, url, content):
00189         (path, file) = os.path.split(url)
00190         tmppath = '/tmp/testtmp'
00191         client = pysvn.Client()
00192         #client.callback_get_login = self.login
00193         client.checkout(path, tmppath)
00194         ioClient = IOManager.get('file')
00195         ioClient.write_to_file(content, os.path.join(tmppath, file))
00196         client.checkin(tmppath, log_message="test checkin from library")
00197         shutil.rmtree(tmppath)
00198 
00199     @classmethod
00200     def write_l10npackage (cls, l10npackage, url, message=None):
00201         tmppath = '/tmp/testtmp'
00202         client = pysvn.Client()
00203         #client.callback_get_login = self.login
00204         client.checkout(url, tmppath, recurse=True)
00205         ioClient = silme.io.Manager.get('file')
00206         ioClient.write_l10npackage(l10npackage, tmppath)
00207         client.checkin(tmppath, log_message="test checkin from library")
00208         shutil.rmtree(tmppath)
00209 
00210     def set_login_data (self, user, password):
00211         self.user['login'] = user
00212         self.user['password'] = password
00213 
00214     def login (self, realm, username, may_save):
00215         retcode=True
00216         username = self.user['login']
00217         password = self.user['password']
00218         save = True
00219         return retcode, username, password, save
00220 """

Generated on Tue May 12 17:37:27 2009 for silme by  doxygen 1.5.8