Commit c49972df authored by Julien Muchembled's avatar Julien Muchembled

some cleanup

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