From 25609d0b253b24bda4db407a97fc0a4f3ede4d8c Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Tue, 20 Sep 2011 12:32:40 +0530 Subject: [PATCH 1/5] allow escape characters in password --- cgi-bin/webnotes/model/db_schema.py | 5 ++++- cgi-bin/webnotes/utils/__init__.py | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/cgi-bin/webnotes/model/db_schema.py b/cgi-bin/webnotes/model/db_schema.py index e1e02058d2..eb9f73f36f 100644 --- a/cgi-bin/webnotes/model/db_schema.py +++ b/cgi-bin/webnotes/model/db_schema.py @@ -357,8 +357,11 @@ class DbManager: mysql_path = getattr(webnotes.defs, 'mysql_path', None) mysql = mysql_path and os.path.join(mysql_path, 'mysql') or 'mysql' + from webnotes.utils import make_esc + esc = make_esc('$ ') + try: - ret = os.system("%s -u root -p%s %s < %s"%(mysql, root_password.replace(" ", "\ "), target.replace("$", "\$"), source)) + ret = os.system("%s -u root -p%s %s < %s"%(mysql, esc(root_password), esc(target), source)) except Exception,e: raise e diff --git a/cgi-bin/webnotes/utils/__init__.py b/cgi-bin/webnotes/utils/__init__.py index 6e342f1802..b3d044d7dd 100644 --- a/cgi-bin/webnotes/utils/__init__.py +++ b/cgi-bin/webnotes/utils/__init__.py @@ -636,6 +636,10 @@ def get_file_timestamp(fn): else: return None - +def make_esc(esc_chars): + """ + Function generator for Escaping special characters + """ + return lambda s: ''.join(['\\' + c if c in esc_chars else c for c in s]) From 77fc4886ab8709e1c3ec8a5b40a24e3afe666aac Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Wed, 21 Sep 2011 12:02:31 +0530 Subject: [PATCH 2/5] Allow db names containing char $ --- cgi-bin/webnotes/utils/backups.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cgi-bin/webnotes/utils/backups.py b/cgi-bin/webnotes/utils/backups.py index 26e1f98771..db34c8f38b 100644 --- a/cgi-bin/webnotes/utils/backups.py +++ b/cgi-bin/webnotes/utils/backups.py @@ -24,7 +24,7 @@ class BackupGenerator: If specifying db_file_name, also append ".sql.gz" """ def __init__(self, db_name, user, password, db_file_name=None): - self.db_name = db_name + self.db_name = db_name.replace('$', '\$') self.user = user self.password = password self.db_file_name = db_file_name and db_file_name \ From 37009b6e7d075719b99c93ca4425eed5aebe9e2e Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Wed, 21 Sep 2011 12:27:56 +0530 Subject: [PATCH 3/5] os.stat takes parameter path without escaping special characters. Hence, had to un-escape the db file name to check the time stamps --- cgi-bin/webnotes/utils/backups.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cgi-bin/webnotes/utils/backups.py b/cgi-bin/webnotes/utils/backups.py index db34c8f38b..5c74ffe618 100644 --- a/cgi-bin/webnotes/utils/backups.py +++ b/cgi-bin/webnotes/utils/backups.py @@ -28,7 +28,7 @@ class BackupGenerator: self.user = user self.password = password self.db_file_name = db_file_name and db_file_name \ - or (os.path.join(backup_path, db_name + ".sql.gz")) + or (os.path.join(backup_path, self.db_name + ".sql.gz")) def take_dump(self): """ @@ -88,7 +88,7 @@ class BackupGenerator: Also, a new backup will be available for download (if requested)\ only after 24 hours.""" % {"file_url":file_url} - datetime_str = datetime.fromtimestamp(os.stat(self.db_file_name).st_ctime) + datetime_str = datetime.fromtimestamp(os.stat(self.db_file_name.replace('\$', '$')).st_ctime) subject = datetime_str.strftime("%d/%m/%Y %H:%M:%S") + """ - Backup ready to be downloaded""" sendmail(recipients=recipient_list, msg=msg, subject=subject) From 662f8da150a1c95b99fafd6561b344aca7bb6447 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Wed, 21 Sep 2011 18:13:08 +0530 Subject: [PATCH 4/5] commit after every 100 in rebuild_tree --- cgi-bin/webnotes/utils/nestedset.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/cgi-bin/webnotes/utils/nestedset.py b/cgi-bin/webnotes/utils/nestedset.py index a80bec14ba..cb9f195849 100644 --- a/cgi-bin/webnotes/utils/nestedset.py +++ b/cgi-bin/webnotes/utils/nestedset.py @@ -157,7 +157,7 @@ def rebuild_tree(doctype, parent_field): for r in result: right = rebuild_node(doctype, r[0], right, parent_field) -def rebuild_node(doctype, parent, left, parent_field): +def rebuild_node(doctype, parent, left, parent_field, cnt = 0): """ reset lft, rgt and recursive call for all children """ @@ -165,17 +165,24 @@ def rebuild_node(doctype, parent, left, parent_field): n = now() # the right value of this node is the left value + 1 - right = left+1 + right = left+1 # get all children of this node result = webnotes.conn.sql("SELECT name FROM `tab%s` WHERE `%s`='%s'" % (doctype, parent_field, parent)) for r in result: - right = rebuild_node(doctype, r[0], right, parent_field) + right = rebuild_node(doctype, r[0], right, parent_field, cnt) # we've got the left value, and now that we've processed # the children of this node we also know the right value webnotes.conn.sql("UPDATE `tab%s` SET lft=%s, rgt=%s, modified='%s' WHERE name='%s'" % (doctype,left,right,n,parent)) + # commit after every 100 + cnt += 1 + if cnt % 100 == 0: + cnt = 0 + webnotes.conn.sql("commit") + webnotes.conn.sql("start transaction") + #return the right value of this node + 1 return right+1 @@ -220,4 +227,3 @@ def update_remove_node(doctype, name): # update all on the right webnotes.conn.sql("update `tab%s` set rgt = rgt-2, modified='%s' where rgt > %s" %(doctype,n,left[0][0])) webnotes.conn.sql("update `tab%s` set lft = lft-2, modified='%s' where lft > %s" %(doctype,n,left[0][0])) - From d26bd16a452140b673631557ff1918d0c9e311b6 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Wed, 21 Sep 2011 18:30:19 +0530 Subject: [PATCH 5/5] commit after every 100 in rebuild_tree --- py/webnotes/utils/nestedset.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/py/webnotes/utils/nestedset.py b/py/webnotes/utils/nestedset.py index cb9f195849..e88f351659 100644 --- a/py/webnotes/utils/nestedset.py +++ b/py/webnotes/utils/nestedset.py @@ -156,6 +156,8 @@ def rebuild_tree(doctype, parent_field): result = webnotes.conn.sql("SELECT name FROM `tab%s` WHERE `%s`='' or `%s` IS NULL" % (doctype, parent_field, parent_field)) for r in result: right = rebuild_node(doctype, r[0], right, parent_field) + webnotes.conn.sql("commit") + webnotes.conn.sql("start transaction") def rebuild_node(doctype, parent, left, parent_field, cnt = 0): """