Commit c49972df authored by Julien Muchembled's avatar Julien Muchembled

some cleanup

parent 6b0b9c40
...@@ -30,14 +30,14 @@ ...@@ -30,14 +30,14 @@
import pkg_resources import pkg_resources
import psutil import psutil
import subprocess
from psutil import AccessDenied, NoSuchProcess from psutil import AccessDenied, NoSuchProcess
from os.path import exists, join from os.path import exists, join
from subprocess import Popen, PIPE
from threading import Lock from threading import Lock
from zope.interface import implements from zope.interface import implements
from application import Application from application import Application
from cloudooo.interfaces.lockable import ILockable from cloudooo.interfaces.lockable import ILockable
from cloudooo.util import logger, convertStringToBool from cloudooo.util import logger
from cloudooo.handler.ooo.util import waitStartDaemon, \ from cloudooo.handler.ooo.util import waitStartDaemon, \
removeDirectory, waitStopDaemon, \ removeDirectory, waitStopDaemon, \
socketStatus socketStatus
...@@ -64,7 +64,7 @@ class OpenOffice(Application): ...@@ -64,7 +64,7 @@ class OpenOffice(Application):
logger.debug("Test OpenOffice %s - Pid %s" % (self.getAddress()[-1], logger.debug("Test OpenOffice %s - Pid %s" % (self.getAddress()[-1],
self.pid())) self.pid()))
python = join(self.office_binary_path, "python") python = join(self.office_binary_path, "python")
args = [exists(python) and python or "python", args = [exists(python) and python or "python3",
pkg_resources.resource_filename("cloudooo", pkg_resources.resource_filename("cloudooo",
join('handler', 'ooo', join('handler', 'ooo',
"helper", "openoffice_tester.py")), "helper", "openoffice_tester.py")),
...@@ -72,11 +72,10 @@ class OpenOffice(Application): ...@@ -72,11 +72,10 @@ class OpenOffice(Application):
"--port=%s" % port, "--port=%s" % port,
"--uno_path=%s" % self.uno_path] "--uno_path=%s" % self.uno_path]
logger.debug("Testing Openoffice Instance %s" % port) logger.debug("Testing Openoffice Instance %s" % port)
stdout, stderr = Popen(args, stdout=PIPE, try:
stderr=PIPE, close_fds=True).communicate() subprocess.check_output(args, stderr=subprocess.STDOUT, close_fds=True)
stdout_bool = convertStringToBool(stdout.replace("\n", "")) except subprocess.CalledProcessError as e:
if stdout_bool and stderr != "": logger.warning(e.output)
logger.debug("%s\n%s" % (stderr, stdout))
return False return False
else: else:
logger.debug("Instance %s works" % port) logger.debug("Instance %s works" % port)
...@@ -108,9 +107,7 @@ class OpenOffice(Application): ...@@ -108,9 +107,7 @@ class OpenOffice(Application):
for i in range(5): for i in range(5):
self.stop() self.stop()
waitStopDaemon(self, self.timeout) waitStopDaemon(self, self.timeout)
self.process = Popen(command, self.process = subprocess.Popen(command, close_fds=True, env=env)
close_fds=True,
env=env)
if not waitStartDaemon(self, self.timeout): if not waitStartDaemon(self, self.timeout):
continue continue
if self._testOpenOffice(self.hostname, self.port): if self._testOpenOffice(self.hostname, self.port):
......
#!/usr/bin/env python #!/usr/bin/env python
import sys import sys
import helper_util import helper_util
from getopt import getopt, GetoptError from getopt import getopt
def test_openoffice(hostname, port):
try:
helper_util.getServiceManager(hostname, port)
return True
except Exception, err:
print err
return False
def main(): def main():
try: opt_list, arg_list = getopt(sys.argv[1:], "",
opt_list, arg_list = getopt(sys.argv[1:], "", ("port=", "hostname=", "uno_path=", "office_binary_path="))
["port=", "hostname=", "uno_path=",
"office_binary_path="])
except GetoptError, e:
print >> sys.stderr, "%s \nUse --port and --hostname" % e
sys.exit(2)
port = hostname = uno_path = office_binary_path = None port = hostname = uno_path = office_binary_path = None
for opt, arg in opt_list: for opt, arg in opt_list:
...@@ -33,7 +18,7 @@ def main(): ...@@ -33,7 +18,7 @@ def main():
elif opt == "--office_binary_path": elif opt == "--office_binary_path":
office_binary_path = arg office_binary_path = arg
print test_openoffice(hostname, port, uno_path, office_binary_path) helper_util.getServiceManager(hostname, port, uno_path, office_binary_path)
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -33,9 +33,11 @@ import sys ...@@ -33,9 +33,11 @@ import sys
import csv import csv
import codecs import codecs
import helper_util import helper_util
from unohelper import systemPathToFileUrl
from os.path import dirname, splitext from os.path import dirname, splitext
from tempfile import mktemp from tempfile import mktemp
from base64 import b64encode, b64decode from base64 import b64encode, b64decode
from functools import partial
from getopt import getopt, GetoptError from getopt import getopt, GetoptError
try: try:
...@@ -45,7 +47,7 @@ except NameError: ...@@ -45,7 +47,7 @@ except NameError:
__doc__ = """ __doc__ = """
usage: unoconverter [options] usage: unodocument [options]
Options: Options:
-h, --help this help screen -h, --help this help screen
...@@ -74,21 +76,20 @@ Options: ...@@ -74,21 +76,20 @@ Options:
""" """
class UnoConverter(object): class UnoDocument(object):
"""A module to easily work with OpenOffice.org.""" """A module to easily work with OpenOffice.org."""
def __init__(self, service_manager, document_url, def __init__(self, service_manager, document_url,
source_format, destination_format, refresh): source_format, destination_format, *args):
self.service_manager = service_manager self.service_manager = service_manager
self.document_url = document_url self.document_url = document_url
self.source_format = source_format self.source_format = source_format
self.refresh = refresh
self.destination_format = destination_format self.destination_format = destination_format
self.filter_list = [(x[1], x[2]) self.filter_list = [(x[1], x[2])
for x in mimemapper.get("filter_list", ()) for x in mimemapper.get("filter_list", ())
if destination_format == x[0] and x[2] if destination_format == x[0] and x[2]
] if mimemapper else () ] if mimemapper else ()
self._load() self._load(*args)
def _createProperty(self, name, value): def _createProperty(self, name, value):
"""Create property""" """Create property"""
...@@ -157,37 +158,32 @@ class UnoConverter(object): ...@@ -157,37 +158,32 @@ class UnoConverter(object):
return () return ()
def _load(self): def _load(self, refresh):
"""Create one document with basic properties """Create one document with basic properties
refresh argument tells to uno environment to refresh argument tells to uno environment to
replace dynamic properties of document before conversion replace dynamic properties of document before conversion
""" """
createInstance = self.service_manager.createInstance createInstance = self.service_manager.createInstance
desktop = createInstance("com.sun.star.frame.Desktop") self.desktop = createInstance("com.sun.star.frame.Desktop")
uno_url = self.systemPathToFileUrl(self.document_url) uno_url = systemPathToFileUrl(self.document_url)
uno_document = desktop.loadComponentFromURL( self.document_loaded = uno_document = self.desktop.loadComponentFromURL(
uno_url, uno_url,
"_blank", "_blank",
0, 0,
self._getPropertyToImport()) self._getPropertyToImport())
if not uno_document: if not uno_document:
raise AttributeError("This document can not be loaded or is empty") raise AttributeError("This document can not be loaded or is empty")
if self.refresh: if refresh:
# Before converting to expected format, refresh dynamic # Before converting to expected format, refresh dynamic
# value inside document. # value inside document.
dispatcher = createInstance("com.sun.star.frame.DispatchHelper") dispatch = partial(
createInstance("com.sun.star.frame.DispatchHelper").executeDispatch,
uno_document.CurrentController.Frame)
for uno_command in ('UpdateFields', 'UpdateAll', 'UpdateInputFields', for uno_command in ('UpdateFields', 'UpdateAll', 'UpdateInputFields',
'UpdateAllLinks', 'UpdateCharts',): 'UpdateAllLinks', 'UpdateCharts',):
dispatcher.executeDispatch(uno_document.getCurrentController().getFrame(), dispatch('.uno:%s' % uno_command, '', 0, ())
'.uno:%s' % uno_command, '', 0, ())
module_manager = createInstance("com.sun.star.frame.ModuleManager") module_manager = createInstance("com.sun.star.frame.ModuleManager")
self.document_type = module_manager.identify(uno_document) self.document_type = module_manager.identify(uno_document)
self.document_loaded = uno_document
def systemPathToFileUrl(self, path):
"""Returns a path in uno library patterns"""
from unohelper import systemPathToFileUrl
return systemPathToFileUrl(path)
def convert(self): def convert(self):
"""it converts a document to specific format""" """it converts a document to specific format"""
...@@ -209,7 +205,7 @@ class UnoConverter(object): ...@@ -209,7 +205,7 @@ class UnoConverter(object):
output_url = mktemp(suffix='.' + ext if ext else '', output_url = mktemp(suffix='.' + ext if ext else '',
dir=dirname(self.document_url)) dir=dirname(self.document_url))
try: try:
self.document_loaded.storeToURL(self.systemPathToFileUrl(output_url), self.document_loaded.storeToURL(systemPathToFileUrl(output_url),
property_list) property_list)
finally: finally:
self.document_loaded.dispose() self.document_loaded.dispose()
...@@ -243,7 +239,7 @@ class UnoConverter(object): ...@@ -243,7 +239,7 @@ class UnoConverter(object):
createInstance = self.service_manager.createInstance createInstance = self.service_manager.createInstance
type_detection = createInstance("com.sun.star.document.TypeDetection") type_detection = createInstance("com.sun.star.document.TypeDetection")
uno_file_access = createInstance("com.sun.star.ucb.SimpleFileAccess") uno_file_access = createInstance("com.sun.star.ucb.SimpleFileAccess")
doc = uno_file_access.openFileRead(self.systemPathToFileUrl(self.document_url)) doc = uno_file_access.openFileRead(systemPathToFileUrl(self.document_url))
input_stream = self._createProperty("InputStream", doc) input_stream = self._createProperty("InputStream", doc)
open_new_view = self._createProperty("OpenNewView", True) open_new_view = self._createProperty("OpenNewView", True)
filter_name = type_detection.queryTypeByDescriptor((input_stream, filter_name = type_detection.queryTypeByDescriptor((input_stream,
...@@ -313,9 +309,9 @@ def main(): ...@@ -313,9 +309,9 @@ def main():
import json import json
except ImportError: except ImportError:
import simplejson as json import simplejson as json
refresh = None metadata = mimemapper = None
hostname = port = document_url = office_binary_path = uno_path =\ hostname = port = office_binary_path = uno_path = None
destination_format = source_format = refresh = metadata = mimemapper = None document_url = destination_format = source_format = refresh = None
for opt, arg in iter(opt_list): for opt, arg in iter(opt_list):
if opt in ('-h', '--help'): if opt in ('-h', '--help'):
help() help()
...@@ -343,19 +339,19 @@ def main(): ...@@ -343,19 +339,19 @@ def main():
service_manager = helper_util.getServiceManager( service_manager = helper_util.getServiceManager(
hostname, port, uno_path, office_binary_path) hostname, port, uno_path, office_binary_path)
unoconverter = UnoConverter(service_manager, document_url, unodocument = UnoDocument(service_manager, document_url,
source_format, destination_format, refresh) source_format, destination_format, refresh)
if '--setmetadata' in param_list: if '--setmetadata' in param_list:
unoconverter.setMetadata(metadata) unodocument.setMetadata(metadata)
output = document_url output = document_url
else: else:
output = unoconverter.convert() if "--convert" in param_list else None output = unodocument.convert() if "--convert" in param_list else None
if '--getmetadata' in param_list: if '--getmetadata' in param_list:
if output: if output:
# Instanciate new UnoConverter instance with new url # Instanciate new UnoDocument instance with new url
unoconverter = UnoConverter(service_manager, output, unodocument = UnoDocument(service_manager, output,
destination_format or source_format, None, refresh) destination_format or source_format, None, refresh)
metadata_dict = unoconverter.getMetadata() metadata_dict = unodocument.getMetadata()
if output: if output:
metadata_dict['document_url'] = output metadata_dict['document_url'] = output
output = b64encode(json.dumps(metadata_dict).encode('utf-8')).decode() output = b64encode(json.dumps(metadata_dict).encode('utf-8')).decode()
......
...@@ -2,6 +2,7 @@ from request import MonitorRequest ...@@ -2,6 +2,7 @@ from request import MonitorRequest
from memory import MonitorMemory from memory import MonitorMemory
from sleeping_time import MonitorSpleepingTime from sleeping_time import MonitorSpleepingTime
from cloudooo.handler.ooo.application.openoffice import openoffice from cloudooo.handler.ooo.application.openoffice import openoffice
from cloudooo.util import convertStringToBool
monitor_request = None monitor_request = None
monitor_memory = None monitor_memory = None
...@@ -17,7 +18,9 @@ def load(local_config): ...@@ -17,7 +18,9 @@ def load(local_config):
int(local_config.get('limit_number_request'))) int(local_config.get('limit_number_request')))
monitor_request.start() monitor_request.start()
if bool(local_config.get('enable_memory_monitor')): # .lower() is for backward compatibility
if convertStringToBool(local_config.get(
'enable_memory_monitor', 'false').lower()):
global monitor_memory global monitor_memory
monitor_memory = MonitorMemory(openoffice, monitor_memory = MonitorMemory(openoffice,
monitor_interval, monitor_interval,
......
...@@ -41,10 +41,11 @@ class TestMonitorInit(HandlerTestCase): ...@@ -41,10 +41,11 @@ class TestMonitorInit(HandlerTestCase):
def afterSetUp(self): def afterSetUp(self):
"""Create one fake file configuration""" """Create one fake file configuration"""
self.load_config = {} self.load_config = {
self.load_config['monitor_interval'] = 1 'monitor_interval': '1',
self.load_config['limit_number_request'] = 100 'limit_number_request': '100',
self.load_config['limit_memory_used'] = 500 'limit_memory_used': '500',
}
def tearDown(self): def tearDown(self):
"""stop all monitors""" """stop all monitors"""
...@@ -64,7 +65,7 @@ class TestMonitorInit(HandlerTestCase): ...@@ -64,7 +65,7 @@ class TestMonitorInit(HandlerTestCase):
def testMonitorLoadMonitorMemory(self): def testMonitorLoadMonitorMemory(self):
"""Check if the MemoryMemory is started""" """Check if the MemoryMemory is started"""
self.load_config['enable_memory_monitor'] = True self.load_config['enable_memory_monitor'] = 'true'
monitor.load(self.load_config) monitor.load(self.load_config)
self.assertEquals(isinstance(monitor.monitor_request, self.assertEquals(isinstance(monitor.monitor_request,
MonitorRequest), MonitorRequest),
......
...@@ -46,14 +46,6 @@ class TestUtil(unittest.TestCase): ...@@ -46,14 +46,6 @@ class TestUtil(unittest.TestCase):
util.logger.info("Test Log") util.logger.info("Test Log")
util.logger.debug("Test Log") util.logger.debug("Test Log")
def testConversion(self):
"""Test convertion to bool"""
self.assertTrue(util.convertStringToBool('true'))
self.assertEquals(util.convertStringToBool('false'), False)
self.assertTrue(util.convertStringToBool('truE'))
self.assertEquals(util.convertStringToBool('faLse'), False)
self.assertEquals(util.convertStringToBool(''), None)
def testLoadMimetypelist(self): def testLoadMimetypelist(self):
"""Test if the file with mimetypes is loaded correctly""" """Test if the file with mimetypes is loaded correctly"""
self.assertEquals(mimetypes.types_map.get(".ogv"), None) self.assertEquals(mimetypes.types_map.get(".ogv"), None)
......
...@@ -103,8 +103,8 @@ class Manager(object): ...@@ -103,8 +103,8 @@ class Manager(object):
"""Need pass the path where the temporary document will be created.""" """Need pass the path where the temporary document will be created."""
self._path_tmp_dir = path_tmp_dir self._path_tmp_dir = path_tmp_dir
self.kw = kw self.kw = kw
self.mimetype_registry = self.kw.pop("mimetype_registry") self.mimetype_registry = kw.pop("mimetype_registry")
self.handler_dict = self.kw.pop("handler_dict") self.handler_dict = kw.pop("handler_dict")
def convertFile(self, file, source_format, destination_format, zip=False, def convertFile(self, file, source_format, destination_format, zip=False,
refresh=False, conversion_kw={}): refresh=False, conversion_kw={}):
...@@ -116,8 +116,8 @@ class Manager(object): ...@@ -116,8 +116,8 @@ class Manager(object):
zip -- Boolean Attribute. If true, returns the file in the form of a zip -- Boolean Attribute. If true, returns the file in the form of a
zip archive zip archive
""" """
self.kw['zip'] = zip kw = self.kw.copy()
self.kw['refresh'] = refresh kw.update(zip=zip, refresh=refresh)
# XXX Force the use of wkhtmltopdf handler if converting from html to pdf # XXX Force the use of wkhtmltopdf handler if converting from html to pdf
# with conversion parameters. # with conversion parameters.
# This is a hack that quickly enables the use of wkhtmltopdf without # This is a hack that quickly enables the use of wkhtmltopdf without
...@@ -138,7 +138,7 @@ class Manager(object): ...@@ -138,7 +138,7 @@ class Manager(object):
handler = handler_class(self._path_tmp_dir, handler = handler_class(self._path_tmp_dir,
decodestring(file), decodestring(file),
source_format, source_format,
**self.kw) **kw)
decode_data = handler.convert(destination_format, **conversion_kw) decode_data = handler.convert(destination_format, **conversion_kw)
return encodestring(decode_data) return encodestring(decode_data)
......
...@@ -65,8 +65,9 @@ def application(global_config, **local_config): ...@@ -65,8 +65,9 @@ def application(global_config, **local_config):
local_config["env"] = environment_dict local_config["env"] = environment_dict
gc.enable() gc.enable()
debug_mode = util.convertStringToBool(local_config.get('debug_mode')) util.configureLogger(debug_mode=util.convertStringToBool(
util.configureLogger(debug_mode=debug_mode) # .lower() is for backward compatibility
local_config.get('debug_mode', 'false').lower()))
# path of directory to run cloudooo # path of directory to run cloudooo
working_path = local_config.get('working_path') working_path = local_config.get('working_path')
if not path.exists(working_path): if not path.exists(working_path):
......
...@@ -3,7 +3,7 @@ use = egg:cloudooo ...@@ -3,7 +3,7 @@ use = egg:cloudooo
# #
## System config ## System config
# #
debug_mode = True debug_mode = true
# Folder where pid files, lock files and virtual frame buffer mappings # Folder where pid files, lock files and virtual frame buffer mappings
# are stored. In this folder is necessary create a folder tmp, because this # are stored. In this folder is necessary create a folder tmp, because this
# folder is used to create all temporary documents. # folder is used to create all temporary documents.
...@@ -21,7 +21,7 @@ limit_number_request = 100 ...@@ -21,7 +21,7 @@ limit_number_request = 100
# Interval to check the factory # Interval to check the factory
monitor_interval = 10 monitor_interval = 10
timeout_response = 180 timeout_response = 180
enable_memory_monitor = True enable_memory_monitor = true
# Set the limit in MB # Set the limit in MB
# e.g 1000 = 1 GB, 100 = 100 MB # e.g 1000 = 1 GB, 100 = 100 MB
limit_memory_used = 3000 limit_memory_used = 3000
......
...@@ -3,7 +3,7 @@ use = egg:cloudooo ...@@ -3,7 +3,7 @@ use = egg:cloudooo
# #
## System config ## System config
# #
debug_mode = True debug_mode = true
# Folder where pid files, lock files and virtual frame buffer mappings # Folder where pid files, lock files and virtual frame buffer mappings
# are stored. In this folder is necessary create a folder tmp, because this # are stored. In this folder is necessary create a folder tmp, because this
# folder is used to create all temporary documents. # folder is used to create all temporary documents.
...@@ -21,7 +21,7 @@ limit_number_request = 100 ...@@ -21,7 +21,7 @@ limit_number_request = 100
# Interval to check the factory # Interval to check the factory
monitor_interval = 10 monitor_interval = 10
timeout_response = 180 timeout_response = 180
enable_memory_monitor = True enable_memory_monitor = true
# Set the limit in MB # Set the limit in MB
# e.g 1000 = 1 GB, 100 = 100 MB # e.g 1000 = 1 GB, 100 = 100 MB
limit_memory_used = 3000 limit_memory_used = 3000
......
...@@ -92,18 +92,7 @@ def configureLogger(level=None, debug_mode=False): ...@@ -92,18 +92,7 @@ def configureLogger(level=None, debug_mode=False):
# add ch to logger # add ch to logger
logger.addHandler(ch) logger.addHandler(ch)
convertStringToBool = ('false', 'true').index
def convertStringToBool(string):
"""This function is used to convert string 'true' and 'false' only.
Keyword arguments:
string -- string to convert to boolean
"""
if string.upper() == "TRUE":
return True
elif string.upper() == "FALSE":
return False
else:
return None
def zipTree(destination, *tree_path_list): def zipTree(destination, *tree_path_list):
""" """
......
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