Hi List,

sometimes there are empty lines with doubled Mac style endings
in the SOGo Database (^M^M).
I don't know how and why, but sometimes it still happens.

This stops Funambol syncing:
com.funambol.common.pim.icalendar.TokenMgrError: Lexical error at line
27, column 1.  Encountered: "\r" (13), after : "\r"

See for example the following Bug #904.

I've written a little script which removes such doubled "\r\r" from 
the Database.

It's only for PostgreSQL databases and comes with no warranty of any kind.

Regards
Michael

###############################################################
#!/usr/bin/env python
# coding: utf-8

# Python script to remove "\r\r\n \n"-Entries in SOGo Apppointment tables
# in PostgreSQL Databases only

# Written by Michael Heide <m...@os-cillation.de>
# os-cillation GmbH - http://www.os-cillation.de/
# Licensed via PSF, see http://docs.python.org/license.html

# !!! this script might cause DAMAGE to your computer !!!
# !!! it comes with absolutely NO WARRANTY !!!
# !!! use it at your own risk !!!


import sys
import psycopg2
import datetime
import re

indextable="sogo_folder_info"



def usage(): 
  print " Usage:"
  print " %s sayyes - commit all changes without asking, noninteractive" % 
(sys.argv[0])
  print " %s askme - commit all changes after asking interactively" % 
(sys.argv[0])
  print " %s sayno - do not commit any changes" % (sys.argv[0])
  print
  sys.exit()

############################################################
#Code from http://code.activestate.com/recipes/134892/     #
#Licensed via PSF, see http://docs.python.org/license.html #
############################################################
class _Getch:
    """Gets a single character from standard input.  Does not echo to the
screen."""
    def __init__(self):
        try:
            self.impl = _GetchWindows()
        except ImportError:
            self.impl = _GetchUnix()

    def __call__(self): return self.impl()

class _GetchUnix:
    def __init__(self):
        import tty, sys

    def __call__(self):
        import sys, tty, termios
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(sys.stdin.fileno())
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch


class _GetchWindows:
    def __init__(self):
        import msvcrt

    def __call__(self):
        import msvcrt
        return msvcrt.getch()
##########################################################
# End activestate.com recipes                            #
##########################################################

def main():
  getch = _Getch()
  foundBadEntry = False

  conn = psycopg2.connect("dbname=sogo user=sogo password=sogo")
  cursor = conn.cursor()
  folder = ""
  row = []

  regexprr_nn = re.compile("\r\r\n \n")
  regexprr = re.compile("\r\r")

  if len(sys.argv) != 2:
    usage()

  if sys.argv[1] != 'askme' and sys.argv[1] != 'sayyes' and sys.argv[1] != 
'sayno':
    usage()

  cursor.execute("SELECT split_part(c_location, '/', 5) from %s WHERE 
c_folder_type='Appointment';" % (indextable))

  if cursor.rowcount < 1:
    print "Warning: There are no Appointment Tables?" 

  allTableNames=cursor.fetchall()

  for tableName in allTableNames:
    #print tableName[0]
    cursor.execute("SELECT c_name, c_content FROM %s WHERE c_content like 
'%%\r\r%%';" % (tableName))
    if cursor.rowcount > 0:
      print "!! Empty lines for %s." % (tableName[0]);
      foundBadEntry=True
      badEntries=cursor.fetchall()
      for badEntry in badEntries:
        #print badEntry
        print "   For %s there is: " % (badEntry[0])
        #mobject = regexp.search(badEntry[1])
        #print mobject.group(0);
        newEntry = regexprr_nn.sub("\r\n",badEntry[1])
        newEntry = regexprr.sub("\r",newEntry)
        print "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
        print badEntry[1]
        print "#############################################"
        print newEntry
        print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"

        cursor.execute("UPDATE %s SET c_content=%%s WHERE c_name=%%s" % 
(tableName), (newEntry,badEntry[0]))

    else:
      print "No empty lines for %s." % (tableName[0]);

  if not foundBadEntry:
    print "No bad entries found - Exitting."
    sys.exit()

  if sys.argv[1] == 'sayno':
    print "not committing any changes"
    sys.exit()

  if sys.argv[1] == 'sayyes':
    print "committing changes"
    conn.commit()
    sys.exit()

  print " Are you sure you want to commit that? (y/n) ",
  answer = getch()
  print "answer: ", answer
  if answer != 'y' and answer != 'j':
    print "answer not \"y\" -> stopping on user request..."
    print
    sys.exit()

  conn.commit()
  print


# Standard boilerplate to call the main() function to begin
# the program.
if __name__ == '__main__':
  main()

###############################################################

-- 
----------------------------------------------------------
os-cillation GmbH
----------------------------------------------------------
Softwareentwicklung, Auftragsprogrammierung, IT-Consulting
iPhone/iPad, Linux, Windows, Web, Android, Embedded-System
----------------------------------------------------------
GF: Oliver Schweissgut, Dipl.-Ing. technische Informatik
T: +49 271 31368-0  F: +49 271 31368-18
Hohler Weg 75, 57072 Siegen, Germany
USt-ID: DE276692229, HRB 9478, AG Siegen
---------------------------------------------------------- 
-- 
users@sogo.nu
https://inverse.ca/sogo/lists

Reply via email to