svn: r7212
This commit is contained in:
Alex Roitman 2006-08-20 18:00:29 +00:00
parent cce28d2a5d
commit a49fa92860

View File

@ -542,7 +542,7 @@ class Convert2Rel(BatchOp):
if not self.prepared: if not self.prepared:
self.prepare() self.prepare()
self.set_total(len(self.handle_list)) self.set_total(len(self.handle_list))
db_dir = os.path.dirname(self.db.full_name) db_dir = os.path.normpath(os.path.dirname(self.db.full_name))
for handle in self.handle_list: for handle in self.handle_list:
obj = self.db.get_object_from_handle(handle) obj = self.db.get_object_from_handle(handle)
new_path = self.get_rel_path(db_dir,obj.path) new_path = self.get_rel_path(db_dir,obj.path)
@ -552,26 +552,27 @@ class Convert2Rel(BatchOp):
return True return True
def get_rel_path(self,db_dir,obj_path): def get_rel_path(self,db_dir,obj_path):
obj_dir = os.path.dirname(obj_path) obj_dir = os.path.dirname(os.path.normpath(obj_path))
obj_name = os.path.basename(obj_path) obj_name = os.path.basename(os.path.normpath(obj_path))
# This is the common directory in which both dirs are: # Get the list of dirnames for each
common_path = os.path.commonprefix([db_dir,obj_dir]) db_dir_list = [word for word in db_dir.split(os.path.sep) if word]
obj_dir_list = [word for word in obj_dir.split(os.path.sep) if word]
# These are both dirs relative to the common dir # The worst case scenario: nothing in common:
db_dir_rem = db_dir.replace(common_path,'',1) # we would need to go ndirs up and then use the full obj path
obj_dir_rem = obj_dir.replace(common_path,'',1) ndirs = len(db_dir_list)
if db_dir_rem: # Compare words in both lists
# If the db_dir_rem not empty then we need to go up for word_ix in range(len(db_dir_list)):
# once for each word, that is, nseps + 1 (no trailing sep) # A common word reduces the trip by one .. and one word
ndirs = db_dir.replace(common_path,'',1).count(os.path.sep) + 1 if db_dir_list[word_ix] == obj_dir_list[word_ix]:
up_from_db = '../'*ndirs ndirs -= 1
else: else:
# If the db_dir_rem empty then we don't need to go up break
up_from_db = ''
if obj_dir_rem[0] == os.path.sep: up_from_db = '../'*ndirs
obj_dir_rem = obj_dir_rem[1:] obj_dir_rem = os.path.sep.join(obj_dir_list[-ndirs:])
return os.path.join(up_from_db,obj_dir_rem,obj_name) return os.path.join(up_from_db,obj_dir_rem,obj_name)
#------------------------------------------------------------------------ #------------------------------------------------------------------------