Commit 51958577 authored by Gabriel Monnerat's avatar Gabriel Monnerat

split utils.py and add folder for this file

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@41958 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 4d5cce43
...@@ -32,7 +32,7 @@ from signal import signal, SIGHUP ...@@ -32,7 +32,7 @@ from signal import signal, SIGHUP
from handler.ooo.application.openoffice import openoffice from handler.ooo.application.openoffice import openoffice
from handler.ooo.application.xvfb import xvfb from handler.ooo.application.xvfb import xvfb
from wsgixmlrpcapplication import WSGIXMLRPCApplication from wsgixmlrpcapplication import WSGIXMLRPCApplication
from handler.ooo.utils import convertStringToBool, configureLogger from utils.utils import convertStringToBool, configureLogger
from os import path, mkdir from os import path, mkdir
from handler.ooo.mimemapper import mimemapper from handler.ooo.mimemapper import mimemapper
......
...@@ -28,7 +28,8 @@ ...@@ -28,7 +28,8 @@
from zope.interface import implements from zope.interface import implements
from cloudooo.interfaces.application import IApplication from cloudooo.interfaces.application import IApplication
from cloudooo.handler.ooo.utils import logger, socketStatus, waitStopDaemon from cloudooo.utils.utils import logger
from cloudooo.handler.ooo.utils.utils import socketStatus, waitStopDaemon
from psutil import pid_exists, Process from psutil import pid_exists, Process
......
...@@ -36,9 +36,10 @@ from zope.interface import implements ...@@ -36,9 +36,10 @@ from zope.interface import implements
from application import Application from application import Application
from xvfb import xvfb from xvfb import xvfb
from cloudooo.interfaces.lockable import ILockable from cloudooo.interfaces.lockable import ILockable
from cloudooo.handler.ooo.utils import logger, waitStartDaemon, \ from cloudooo.utils.utils import logger, convertStringToBool
from cloudooo.handler.ooo.utils.utils import waitStartDaemon, \
removeDirectory, waitStopDaemon, \ removeDirectory, waitStopDaemon, \
convertStringToBool, socketStatus socketStatus
class OpenOffice(Application): class OpenOffice(Application):
...@@ -114,10 +115,10 @@ class OpenOffice(Application): ...@@ -114,10 +115,10 @@ class OpenOffice(Application):
connection_list[0].local_address[1] == self.port: connection_list[0].local_address[1] == self.port:
process.terminate() process.terminate()
except psutil.error.AccessDenied, e: except psutil.error.AccessDenied, e:
logger.error(e) logger.debug(e)
except TypeError, e: except TypeError, e:
# exception to prevent one psutil issue with svn processes # exception to prevent one psutil issue with svn processes
logger.error(e) logger.debug(e)
except NotImplementedError, e: except NotImplementedError, e:
logger.error("lsof isn't installed on this machine: " + str(e)) logger.error("lsof isn't installed on this machine: " + str(e))
......
...@@ -27,7 +27,8 @@ ...@@ -27,7 +27,8 @@
############################################################################## ##############################################################################
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from cloudooo.handler.ooo.utils import logger, waitStartDaemon, remove_file from cloudooo.utils.utils import logger
from cloudooo.handler.ooo.utils.utils import waitStartDaemon, remove_file
from zope.interface import implements from zope.interface import implements
from application import Application from application import Application
from cloudooo.interfaces.application import IApplication from cloudooo.interfaces.application import IApplication
...@@ -55,7 +56,6 @@ class Xvfb(Application): ...@@ -55,7 +56,6 @@ class Xvfb(Application):
def start(self): def start(self):
"""Method to start Virtual Frame Buffer.""" """Method to start Virtual Frame Buffer."""
self.command = ["Xvfb", "-ac", ":%s" % self.display_id, \ self.command = ["Xvfb", "-ac", ":%s" % self.display_id, \
# "-nolisten", "tcp",
"-screen", self.virtual_screen, "800x600x16", \ "-screen", self.virtual_screen, "800x600x16", \
"-fbdir", self.path_run_dir] "-fbdir", self.path_run_dir]
self.process = Popen(" ".join(self.command), self.process = Popen(" ".join(self.command),
......
...@@ -31,7 +31,7 @@ from zipfile import ZipFile ...@@ -31,7 +31,7 @@ from zipfile import ZipFile
from StringIO import StringIO from StringIO import StringIO
from lxml import etree from lxml import etree
from os import path from os import path
from cloudooo.handler.ooo.utils import logger from cloudooo.utils.utils import logger
from cloudooo.handler.ooo.document import OdfDocument from cloudooo.handler.ooo.document import OdfDocument
from cloudooo.interfaces.granulate import ITableGranulator, \ from cloudooo.interfaces.granulate import ITableGranulator, \
IImageGranulator, \ IImageGranulator, \
......
...@@ -39,7 +39,7 @@ from cloudooo.interfaces.handler import IHandler ...@@ -39,7 +39,7 @@ from cloudooo.interfaces.handler import IHandler
from cloudooo.handler.ooo.mimemapper import mimemapper from cloudooo.handler.ooo.mimemapper import mimemapper
from cloudooo.handler.ooo.document import FileSystemDocument from cloudooo.handler.ooo.document import FileSystemDocument
from cloudooo.handler.ooo.monitor.timeout import MonitorTimeout from cloudooo.handler.ooo.monitor.timeout import MonitorTimeout
from cloudooo.handler.ooo.utils import logger from cloudooo.utils.utils import logger
from psutil import pid_exists from psutil import pid_exists
......
...@@ -34,7 +34,7 @@ from filter import Filter ...@@ -34,7 +34,7 @@ from filter import Filter
from os import environ, path from os import environ, path
from cloudooo.interfaces.mimemapper import IMimemapper from cloudooo.interfaces.mimemapper import IMimemapper
from types import InstanceType from types import InstanceType
from utils import getCleanPythonEnvironment from cloudooo.utils.utils import getCleanPythonEnvironment
import json import json
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
from monitor import Monitor from monitor import Monitor
from multiprocessing import Process from multiprocessing import Process
import psutil import psutil
from cloudooo.handler.ooo.utils import logger from cloudooo.utils.utils import logger
from time import sleep from time import sleep
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
from monitor import Monitor from monitor import Monitor
from threading import Thread from threading import Thread
from cloudooo.handler.ooo.utils import logger from cloudooo.utils.utils import logger
from time import sleep from time import sleep
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
from monitor import Monitor from monitor import Monitor
from multiprocessing import Process from multiprocessing import Process
from time import sleep from time import sleep
from cloudooo.handler.ooo.utils import logger from cloudooo.utils.utils import logger
class MonitorTimeout(Monitor, Process): class MonitorTimeout(Monitor, Process):
......
...@@ -29,7 +29,6 @@ ...@@ -29,7 +29,6 @@
import sys import sys
from multiprocessing import Process from multiprocessing import Process
from os import listdir from os import listdir
from cloudooo.utils import usage
from xmlrpclib import ServerProxy from xmlrpclib import ServerProxy
from os.path import join from os.path import join
from getopt import getopt, GetoptError from getopt import getopt, GetoptError
...@@ -133,13 +132,13 @@ def main(): ...@@ -133,13 +132,13 @@ def main():
opt_list, arg_list = getopt(sys.argv[1:], "hc:n:f:s:l:", ["help"]) opt_list, arg_list = getopt(sys.argv[1:], "hc:n:f:s:l:", ["help"])
except GetoptError, msg: except GetoptError, msg:
msg = msg.msg + help_msg msg = msg.msg + help_msg
usage(sys.stderr, msg) print >> sys.stderr, msg
sys.exit(2) sys.exit(2)
kw = {} kw = {}
for opt, arg in opt_list: for opt, arg in opt_list:
if opt in ('-h', '--help'): if opt in ('-h', '--help'):
usage(sys.stdout, __doc__) print >> sys.stdout, __doc__
sys.exit(2) sys.exit(2)
elif opt == '-c': elif opt == '-c':
number_client = int(arg) number_client = int(arg)
......
...@@ -33,7 +33,7 @@ from os import path, mkdir ...@@ -33,7 +33,7 @@ from os import path, mkdir
from os import environ, putenv from os import environ, putenv
from cloudooo.handler.ooo.application.xvfb import xvfb from cloudooo.handler.ooo.application.xvfb import xvfb
from cloudooo.handler.ooo.application.openoffice import openoffice from cloudooo.handler.ooo.application.openoffice import openoffice
from cloudooo.handler.ooo.utils import waitStartDaemon from cloudooo.handler.ooo.utils.utils import waitStartDaemon
from cloudooo.handler.ooo.mimemapper import mimemapper from cloudooo.handler.ooo.mimemapper import mimemapper
config = ConfigParser() config = ConfigParser()
......
...@@ -4,7 +4,7 @@ import sys ...@@ -4,7 +4,7 @@ import sys
import unittest import unittest
from getopt import getopt, GetoptError from getopt import getopt, GetoptError
from time import sleep from time import sleep
from cloudooo.handler.ooo.utils import socketStatus from cloudooo.handler.ooo.utils.utils import socketStatus
from ConfigParser import ConfigParser from ConfigParser import ConfigParser
from os import chdir, path, environ, curdir from os import chdir, path, environ, curdir
from subprocess import Popen from subprocess import Popen
......
...@@ -30,7 +30,7 @@ import unittest ...@@ -30,7 +30,7 @@ import unittest
from cloudoooTestCase import cloudoooTestCase from cloudoooTestCase import cloudoooTestCase
from cloudooo.handler.ooo.application.openoffice import OpenOffice from cloudooo.handler.ooo.application.openoffice import OpenOffice
from cloudoooTestCase import make_suite from cloudoooTestCase import make_suite
from cloudooo.handler.ooo.utils import waitStopDaemon from cloudooo.handler.ooo.utils.utils import waitStopDaemon
from psutil import Process, AccessDenied from psutil import Process, AccessDenied
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
import unittest import unittest
import logging import logging
from cloudooo.handler.ooo.utils import logger, configureLogger, \ from cloudooo.utils.utils import logger, configureLogger, \
convertStringToBool convertStringToBool
from cloudoooTestCase import make_suite from cloudoooTestCase import make_suite
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
import unittest import unittest
from cloudoooTestCase import cloudoooTestCase, make_suite from cloudoooTestCase import cloudoooTestCase, make_suite
from cloudooo.handler.ooo.application.xvfb import Xvfb from cloudooo.handler.ooo.application.xvfb import Xvfb
from cloudooo.hanlder.ooo.utils import waitStopDaemon from cloudooo.handler.ooo.utils.utils import waitStopDaemon
class TestXvfb(cloudoooTestCase): class TestXvfb(cloudoooTestCase):
......
############################################################################## ##############################################################################
# #
# Copyright (c) 2010 Nexedi SA and Contributors. All Rights Reserved. # Copyright (c) 2009-2010 Nexedi SA and Contributors. All Rights Reserved.
# Hugo H. Maia Vieira <hugomaia@tiolive.com> # Gabriel M. Monnerat <gabriel@tiolive.com>
# #
# WARNING: This program as such is intended to be used by professional # WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential # programmers who take the whole responsibility of assessing all potential
...@@ -26,23 +26,52 @@ ...@@ -26,23 +26,52 @@
# #
############################################################################## ##############################################################################
import unittest from socket import socket, error
from cloudoooTestCase import cloudoooTestCase, make_suite from errno import EADDRINUSE
from cloudooo.manager import Manager from time import sleep
from os import remove, environ
from shutil import rmtree
from cloudooo.utils.utils import logger
class TestGranulate(cloudoooTestCase): def removeDirectory(path):
"""Remove directory"""
try:
rmtree(path)
except OSError, msg:
logger.error(msg)
def testGranulateFile(self):
"""Test if the granulateFile returns the grains correctly"""
manager = Manager('some/path')
self.assertRaises(NotImplementedError, manager.granulateFile, 'some/file',
'odt')
def socketStatus(hostname, port):
"""Verify if the address is busy."""
try:
socket().bind((hostname, port),)
# False if the is free
return False
except error, (num, err):
if num == EADDRINUSE:
# True if the isn't free
return True
def test_suite():
return make_suite(TestGranulate)
if __name__ == "__main__": def waitStartDaemon(daemon, attempts):
suite = unittest.TestLoader().loadTestsFromTestCase(TestGranulate) """Wait a certain time to start the daemon."""
unittest.TextTestRunner(verbosity=2).run(suite) for num in range(attempts):
sleep(1)
if daemon.status():
return
def waitStopDaemon(daemon, attempts=5):
"""Wait a certain time to stop the daemon."""
for num in range(attempts):
sleep(1)
if not daemon.status():
break
def remove_file(filepath):
try:
remove(filepath)
except OSError, msg:
print msg.strerror
...@@ -33,7 +33,7 @@ from zope.interface import implements ...@@ -33,7 +33,7 @@ from zope.interface import implements
from interfaces.manager import IManager, IERP5Compatibility from interfaces.manager import IManager, IERP5Compatibility
from handler.ooo.handler import OOHandler from handler.ooo.handler import OOHandler
from handler.ooo.mimemapper import mimemapper from handler.ooo.mimemapper import mimemapper
from handler.ooo.utils import logger from utils.utils import logger
class Manager(object): class Manager(object):
......
...@@ -26,11 +26,7 @@ ...@@ -26,11 +26,7 @@
# #
############################################################################## ##############################################################################
from socket import socket, error from os import environ
from errno import EADDRINUSE
from time import sleep
from os import remove, environ
from shutil import rmtree
import logging import logging
logger = logging.getLogger('Cloudooo') logger = logging.getLogger('Cloudooo')
...@@ -59,42 +55,6 @@ def getCleanPythonEnvironment(): ...@@ -59,42 +55,6 @@ def getCleanPythonEnvironment():
return env return env
def removeDirectory(path):
"""Remove directory"""
try:
rmtree(path)
except OSError, msg:
logger.error(msg)
def socketStatus(hostname, port):
"""Verify if the address is busy."""
try:
socket().bind((hostname, port),)
# False if the is free
return False
except error, (num, err):
if num == EADDRINUSE:
# True if the isn't free
return True
def waitStartDaemon(daemon, attempts):
"""Wait a certain time to start the daemon."""
for num in range(attempts):
sleep(1)
if daemon.status():
return
def waitStopDaemon(daemon, attempts=5):
"""Wait a certain time to stop the daemon."""
for num in range(attempts):
sleep(1)
if not daemon.status():
break
def configureLogger(level=None, debug_mode=False): def configureLogger(level=None, debug_mode=False):
"""Configure logger. """Configure logger.
Keyword arguments: Keyword arguments:
...@@ -127,13 +87,6 @@ def configureLogger(level=None, debug_mode=False): ...@@ -127,13 +87,6 @@ def configureLogger(level=None, debug_mode=False):
logger.addHandler(ch) logger.addHandler(ch)
def remove_file(filepath):
try:
remove(filepath)
except OSError, msg:
print msg.strerror
def convertStringToBool(string): def convertStringToBool(string):
"""This function is used to convert string 'true' and 'false' only. """This function is used to convert string 'true' and 'false' only.
Keyword arguments: Keyword arguments:
......
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