Commit 1b8cf511 authored by Christophe Dumez's avatar Christophe Dumez

- Big code clean up and optimisation

- added support for several working copies (usefull for bt in products)
note: Big code changes and small new bugs might have appeared


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@6847 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 420d2ea5
...@@ -206,13 +206,6 @@ try: ...@@ -206,13 +206,6 @@ try:
def getLogin(self, realm): def getLogin(self, realm):
return self.aq_parent._getLogin(realm) return self.aq_parent._getLogin(realm)
def _getPreferences(self):
working_path = self.getPortalObject().portal_preferences.getPreferredSubversionWorkingCopy()
if not working_path :
raise "Error: Please set Subversion working path in preferences"
#self.svn_username = self.getPortalObject().portal_preferences.getPreference('preferred_subversion_user_name')
os.chdir(working_path);
def getTimeout(self): def getTimeout(self):
return self.timeout return self.timeout
...@@ -226,7 +219,6 @@ try: ...@@ -226,7 +219,6 @@ try:
return self.exception return self.exception
def checkin(self, path, log_message, recurse): def checkin(self, path, log_message, recurse):
self._getPreferences()
try: try:
return self.client.checkin(path, log_message=log_message or 'none', recurse=recurse) return self.client.checkin(path, log_message=log_message or 'none', recurse=recurse)
except pysvn.ClientError, error: except pysvn.ClientError, error:
...@@ -237,7 +229,6 @@ try: ...@@ -237,7 +229,6 @@ try:
raise error raise error
def update(self, path): def update(self, path):
self._getPreferences()
try: try:
return self.client.update(path) return self.client.update(path)
except pysvn.ClientError, error: except pysvn.ClientError, error:
...@@ -252,7 +243,6 @@ try: ...@@ -252,7 +243,6 @@ try:
return [Status(x) for x in self.client.status(path, **kw)] return [Status(x) for x in self.client.status(path, **kw)]
def diff(self, path, revision1, revision2): def diff(self, path, revision1, revision2):
self._getPreferences()
tmp = mktemp() tmp = mktemp()
os.system('mkdir -p %s'%tmp) os.system('mkdir -p %s'%tmp)
if not revision1 or not revision2: if not revision1 or not revision2:
...@@ -264,11 +254,9 @@ try: ...@@ -264,11 +254,9 @@ try:
return diff return diff
def revert(self, path): def revert(self, path):
self._getPreferences()
return self.client.revert(path) return self.client.revert(path)
def log(self, path): def log(self, path):
self._getPreferences()
try: try:
log_list = self.client.log(path) log_list = self.client.log(path)
except pysvn.ClientError, error: except pysvn.ClientError, error:
...@@ -286,11 +274,9 @@ try: ...@@ -286,11 +274,9 @@ try:
return log_list return log_list
def add(self, path): def add(self, path):
self._getPreferences()
return self.client.add(path=path, force=True) return self.client.add(path=path, force=True)
def info(self, path): def info(self, path):
self._getPreferences()
try: try:
entry = self.client.info(path=path) entry = self.client.info(path=path)
except pysvn.ClientError, error: except pysvn.ClientError, error:
...@@ -308,7 +294,6 @@ try: ...@@ -308,7 +294,6 @@ try:
return entry_dict return entry_dict
def ls(self, path): def ls(self, path):
self._getPreferences()
try: try:
dict_list = self.client.ls(url_or_path=path, recurse=False) dict_list = self.client.ls(url_or_path=path, recurse=False)
except pysvn.ClientError, error: except pysvn.ClientError, error:
...@@ -326,11 +311,9 @@ try: ...@@ -326,11 +311,9 @@ try:
return dict_list return dict_list
def cleanup(self, path): def cleanup(self, path):
self._getPreferences()
return self.client.cleanup(path=path) return self.client.cleanup(path=path)
def remove(self, path): def remove(self, path):
self._getPreferences()
return self.client.remove(url_or_path=path, force=True) return self.client.remove(url_or_path=path, force=True)
def newSubversionClient(container, **kw): def newSubversionClient(container, **kw):
......
...@@ -441,19 +441,16 @@ class SubversionTool(UniqueObject, Folder): ...@@ -441,19 +441,16 @@ class SubversionTool(UniqueObject, Folder):
# return '#' if no zodb path is found # return '#' if no zodb path is found
def editPath(self, bt, path): def editPath(self, bt, path):
"""Return path to edit file """Return path to edit file
path can be relative or absolute
""" """
path = path.replace('\\', '/') path = self.relativeToAbsolute(path, bt).replace('\\', '/')
if 'bt' in path.split('/'): if 'bt' in path.split('/'):
# not in zodb # not in zodb
return '#' return '#'
# if file have been deleted then not in zodb # if file have been deleted then not in zodb
if not os.path.exists(path): if not os.path.exists(path):
return '#' return '#'
svn_path = bt.getPortalObject().portal_preferences.getPreferredSubversionWorkingCopy() svn_path = self.getSubversionPath(bt).replace('\\', '/')
if not svn_path:
raise 'Error: Please set working copy path in Subversion preferences !'
svn_path = os.path.join(svn_path, bt.getTitle())
svn_path = svn_path.replace('\\', '/')
edit_path = path.replace(svn_path, '') edit_path = path.replace(svn_path, '')
if edit_path.strip() == '': if edit_path.strip() == '':
# not in zodb # not in zodb
...@@ -479,6 +476,10 @@ class SubversionTool(UniqueObject, Folder): ...@@ -479,6 +476,10 @@ class SubversionTool(UniqueObject, Folder):
# Decode login information. # Decode login information.
return loads(b64decode(login)) return loads(b64decode(login))
def goToWorkingCopy(self, bt):
working_path = self.getSubversionPath(bt)
os.chdir(working_path)
def setLogin(self, realm, user, password): def setLogin(self, realm, user, password):
"""Set login information. """Set login information.
""" """
...@@ -520,12 +521,13 @@ class SubversionTool(UniqueObject, Folder): ...@@ -520,12 +521,13 @@ class SubversionTool(UniqueObject, Folder):
trust_item_list, permanent = loads(b64decode(trust)) trust_item_list, permanent = loads(b64decode(trust))
return dict(trust_item_list), permanent return dict(trust_item_list), permanent
def diffHTML(self, file_path, revision1=None, revision2=None): def diffHTML(self, file_path, bt, revision1=None, revision2=None):
raw_diff = self.diff(file_path, revision1, revision2) raw_diff = self.diff(file_path, bt, revision1, revision2)
return DiffFile(raw_diff).toHTML() return DiffFile(raw_diff).toHTML()
# Display a file content in HTML # Display a file content in HTML
def fileHTML(self, bt, file_path): def fileHTML(self, bt, file_path):
file_path = self.relativeToAbsolute(file_path, bt)
if os.path.exists(file_path): if os.path.exists(file_path):
if os.path.isdir(file_path): if os.path.isdir(file_path):
text = "<b>"+file_path+"</b><hr>" text = "<b>"+file_path+"</b><hr>"
...@@ -541,7 +543,8 @@ class SubversionTool(UniqueObject, Folder): ...@@ -541,7 +543,8 @@ class SubversionTool(UniqueObject, Folder):
file_path=file_path[:-1] file_path=file_path[:-1]
filename = file_path.split(os.sep)[-1] filename = file_path.split(os.sep)[-1]
tmp_path = os.sep.join(file_path.split(os.sep)[:-1]) tmp_path = os.sep.join(file_path.split(os.sep)[:-1])
tmp_path = os.path.join(tmp_path,'.svn','text-base',filename,'.svn-base') tmp_path = os.path.join(tmp_path,'.svn','text-base',filename+'.svn-base')
LOG('path_HD', 1, tmp_path)
if os.path.exists(tmp_path): if os.path.exists(tmp_path):
head = "<b>"+tmp_path+"</b> (svn temporary file)<hr>" head = "<b>"+tmp_path+"</b> (svn temporary file)<hr>"
text = commands.getoutput('enscript -B --color --line-numbers --highlight=html --language=html -o - %s'%tmp_path) text = commands.getoutput('enscript -B --color --line-numbers --highlight=html --language=html -o - %s'%tmp_path)
...@@ -589,6 +592,28 @@ class SubversionTool(UniqueObject, Folder): ...@@ -589,6 +592,28 @@ class SubversionTool(UniqueObject, Folder):
# Get the svn client object. # Get the svn client object.
return newSubversionClient(self, **kw) return newSubversionClient(self, **kw)
security.declareProtected('Import/Export objects', 'getSubversionPath')
# with_name : with business template name at the end of the path
def getSubversionPath(self, bt, with_name=True):
# return the working copy path corresponding to
# the given business template browsing
# working copy list in preferences (looking
# only at first level of directories)
wc_list = self.getPortalObject().portal_preferences.getPreferredSubversionWorkingCopyList()
bt_name = bt.getTitle()
if len(wc_list) == 0 :
raise 'Preferences Error', 'Please set at least one Subversion Working Copy in preferences first!'
for wc in wc_list:
if bt_name in os.listdir(wc) :
wc_path = os.path.join(wc, bt_name)
if os.path.isdir(wc_path):
LOG("wc path", 1, wc_path);
if with_name:
return wc_path
else:
return os.sep.join(wc_path.split(os.sep)[:-1])
raise 'Unknown Business Template', "Could not find '"+bt_name+"' at first level of working copies!"
security.declareProtected('Import/Export objects', 'update') security.declareProtected('Import/Export objects', 'update')
def update(self, path): def update(self, path):
"""Update a working copy. """Update a working copy.
...@@ -597,43 +622,52 @@ class SubversionTool(UniqueObject, Folder): ...@@ -597,43 +622,52 @@ class SubversionTool(UniqueObject, Folder):
return client.update(path) return client.update(path)
security.declareProtected('Import/Export objects', 'add') security.declareProtected('Import/Export objects', 'add')
def add(self, path): # path can be a list or not (relative or absolute)
def add(self, path, bt=None):
"""Add a file or a directory. """Add a file or a directory.
""" """
if bt:
if isinstance(path, list) :
path = [self.relativeToAbsolute(x, bt) for x in path]
else:
path = self.relativeToAbsolute(path, bt)
client = self._getClient() client = self._getClient()
return client.add(path) return client.add(path)
security.declareProtected('Import/Export objects', 'info') security.declareProtected('Import/Export objects', 'info')
def info(self): def info(self, bt):
"""return info of working copy """return info of working copy
""" """
working_copy = self.getPortalObject().portal_preferences.getPreferredSubversionWorkingCopy() working_copy = self.getSubversionPath(bt)
if not working_copy :
raise 'Please set Working copy path in preferences'
client = self._getClient() client = self._getClient()
return client.info(working_copy) return client.info(working_copy)
security.declareProtected('Import/Export objects', 'log') security.declareProtected('Import/Export objects', 'log')
def log(self, path): # path can be absolute or relative
def log(self, path, bt):
"""return log of a file or dir """return log of a file or dir
""" """
client = self._getClient() client = self._getClient()
return client.log(path) return client.log(self.relativeToAbsolute(path, bt))
security.declareProtected('Import/Export objects', 'cleanup') security.declareProtected('Import/Export objects', 'cleanup')
def cleanup(self): def cleanup(self, bt):
"""remove svn locks in working copy """remove svn locks in working copy
""" """
working_copy = self.getPortalObject().portal_preferences.getPreferredSubversionWorkingCopy() working_copy = self.getSubversionPath(bt)
if not working_copy :
raise 'Please set Working copy path in preferences'
client = self._getClient() client = self._getClient()
return client.cleanup(working_copy) return client.cleanup(working_copy)
security.declareProtected('Import/Export objects', 'remove') security.declareProtected('Import/Export objects', 'remove')
def remove(self, path): # path can be a list or not (relative or absolute)
def remove(self, path, bt=None):
"""Remove a file or a directory. """Remove a file or a directory.
""" """
if bt:
if isinstance(path, list) :
path = [self.relativeToAbsolute(x, bt) for x in path]
else:
path = self.relativeToAbsolute(path, bt)
client = self._getClient() client = self._getClient()
return client.remove(path) return client.remove(path)
...@@ -645,30 +679,52 @@ class SubversionTool(UniqueObject, Folder): ...@@ -645,30 +679,52 @@ class SubversionTool(UniqueObject, Folder):
return client.move(src, dest) return client.move(src, dest)
security.declareProtected('Import/Export objects', 'ls') security.declareProtected('Import/Export objects', 'ls')
def ls(self, path): # path can be relative or absolute
def ls(self, path, bt):
"""Display infos about a file. """Display infos about a file.
""" """
client = self._getClient() client = self._getClient()
return client.ls(path) return client.ls(self.relativeToAbsolute(path, bt))
security.declareProtected('Import/Export objects', 'diff') security.declareProtected('Import/Export objects', 'diff')
def diff(self, path, revision1=None, revision2=None): # path can be relative or absolute
def diff(self, path, bt, revision1=None, revision2=None):
"""Make a diff for a file or a directory. """Make a diff for a file or a directory.
""" """
client = self._getClient() client = self._getClient()
return client.diff(path, revision1, revision2) return client.diff(self.relativeToAbsolute(path, bt), revision1, revision2)
security.declareProtected('Import/Export objects', 'revert') security.declareProtected('Import/Export objects', 'revert')
def revert(self, path): # path can be absolute or relative
def revert(self, path, bt):
"""Revert local changes in a file or a directory. """Revert local changes in a file or a directory.
""" """
client = self._getClient() client = self._getClient()
if isinstance(path, list) :
path = [self.relativeToAbsolute(x, bt) for x in path]
else:
path = self.relativeToAbsolute(path, bt)
return client.revert(path) return client.revert(path)
def relativeToAbsolute(self, path, bt) :
if path[0] == os.sep:
# already absolute
return path
# relative path
if path.split(os.sep)[0] == bt.getTitle():
return os.path.join(self.getSubversionPath(bt, False), path)
else:
return os.path.join(self.getSubversionPath(bt), path)
security.declareProtected('Import/Export objects', 'checkin') security.declareProtected('Import/Export objects', 'checkin')
def checkin(self, path, log_message=None, recurse=True): # path can be relative or absolute (can be a list of paths too)
def checkin(self, path, bt, log_message=None, recurse=True):
"""Commit local changes. """Commit local changes.
""" """
if isinstance(path, list) :
path = [self.relativeToAbsolute(x, bt) for x in path]
else:
path = self.relativeToAbsolute(path, bt)
client = self._getClient() client = self._getClient()
return client.checkin(path, log_message, recurse) return client.checkin(path, log_message, recurse)
...@@ -700,11 +756,9 @@ class SubversionTool(UniqueObject, Folder): ...@@ -700,11 +756,9 @@ class SubversionTool(UniqueObject, Folder):
for file in list: for file in list:
removeAll(file) removeAll(file)
def getModifiedTree(self, path) : def getModifiedTree(self, bt) :
# Remove trailing slash if it's present # Remove trailing slash if it's present
if path[-1] == os.sep : path = self.getSubversionPath(bt)
path = path[:-1]
root = Dir(path, "normal") root = Dir(path, "normal")
somethingModified = False somethingModified = False
...@@ -744,19 +798,15 @@ class SubversionTool(UniqueObject, Folder): ...@@ -744,19 +798,15 @@ class SubversionTool(UniqueObject, Folder):
return somethingModified and root return somethingModified and root
def extractBT(self, bt): def extractBT(self, bt):
path = mktemp() path = mktemp() +os.sep
bt.export(path=path, local=1) bt.export(path=path, local=1)
svn_path = self.getPortalObject().portal_preferences.getPreferredSubversionWorkingCopy() svn_path = self.getSubversionPath(bt) + os.sep
if not svn_path :
raise "Error: Please set Subversion working path in preferences"
svn_path=os.path.join(svn_path,bt.getTitle())+os.sep
path+=os.sep
# svn del deleted files # svn del deleted files
self.deleteOldFiles(svn_path, path, bt) self.deleteOldFiles(svn_path, path, bt)
# add new files and copy # add new files and copy
self.addNewFiles(svn_path, path, bt) self.addNewFiles(svn_path, path, bt)
self.goToWorkingCopy(bt)
# Clean up # Clean up
#removeAll(path)
self.activate().removeAllInList([path,]) self.activate().removeAllInList([path,])
# return a set with directories present in the directory # return a set with directories present in the directory
...@@ -839,20 +889,15 @@ class SubversionTool(UniqueObject, Folder): ...@@ -839,20 +889,15 @@ class SubversionTool(UniqueObject, Folder):
list.sort() list.sort()
self.add([os.path.join(old_dir, x[1]) for x in list]) self.add([os.path.join(old_dir, x[1]) for x in list])
def treeToXML(self, item) : def treeToXML(self, item, bt) :
working_copy = self.getSubversionPath(bt, False) + os.sep
output = "<?xml version='1.0' encoding='iso-8859-1'?>"+ os.linesep output = "<?xml version='1.0' encoding='iso-8859-1'?>"+ os.linesep
output += "<tree id='0'>" + os.linesep output += "<tree id='0'>" + os.linesep
output = self._treeToXML(item, output, 1, True) output = self._treeToXML(item, working_copy, output, 1, True)
output += "</tree>" + os.linesep output += "</tree>" + os.linesep
return output return output
def _treeToXML(self, item, output, ident, first) : def _treeToXML(self, item, working_copy, output, ident, first) :
# svn path
svn_path = self.getPortalObject().portal_preferences.getPreferredSubversionWorkingCopy()
if not svn_path :
raise "Error: Please set Subversion working path in preferences"
if svn_path[-1] != os.sep:
svn_path += os.sep
# Choosing a color coresponding to the status # Choosing a color coresponding to the status
itemStatus = item.msg_status itemStatus = item.msg_status
if itemStatus == 'added' : if itemStatus == 'added' :
...@@ -865,23 +910,21 @@ class SubversionTool(UniqueObject, Folder): ...@@ -865,23 +910,21 @@ class SubversionTool(UniqueObject, Folder):
itemColor='grey' itemColor='grey'
else : else :
itemColor='black' itemColor='black'
if isinstance(item, Dir) : if isinstance(item, Dir) :
for i in range(ident) : for i in range(ident) :
output += '\t' output += '\t'
if first : if first :
output += '<item open="1" text="%s" id="%s" aCol="%s" '\ output += '<item open="1" text="%s" id="%s" aCol="%s" '\
'im0="folder.png" im1="folder_open.png" '\ 'im0="folder.png" im1="folder_open.png" '\
'im2="folder.png">'%(item.name, 'im2="folder.png">'%(item.name, item.full_path.replace(working_copy, ''), itemColor,) + os.linesep
item.full_path.replace(svn_path, ''), itemColor,) + os.linesep first = False
first=False
else : else :
output += '<item text="%s" id="%s" aCol="%s" im0="folder.png" ' \ output += '<item text="%s" id="%s" aCol="%s" im0="folder.png" ' \
'im1="folder_open.png" im2="folder.png">'%(item.name, 'im1="folder_open.png" im2="folder.png">'%(item.name,
item.full_path.replace(svn_path, ''), itemColor,) + os.linesep item.full_path.replace(working_copy, ''), itemColor,) + os.linesep
for it in item.sub_dirs: for it in item.sub_dirs:
ident += 1 ident += 1
output = self._treeToXML(item.getDir(it.name), output, ident, output = self._treeToXML(item.getDir(it.name), working_copy, output, ident,
first) first)
ident -= 1 ident -= 1
for i in range(ident) : for i in range(ident) :
...@@ -891,7 +934,7 @@ first) ...@@ -891,7 +934,7 @@ first)
for i in range(ident) : for i in range(ident) :
output += '\t' output += '\t'
output += '<item text="%s" id="%s" aCol="%s" im0="document.png"/>'\ output += '<item text="%s" id="%s" aCol="%s" im0="document.png"/>'\
%(item.name, item.full_path.replace(svn_path, ''), itemColor,) + os.linesep %(item.name, item.full_path.replace(working_copy, ''), itemColor,) + os.linesep
return output return output
InitializeClass(SubversionTool) InitializeClass(SubversionTool)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment