Commit e258e183 authored by Gabriel Monnerat's avatar Gabriel Monnerat

refactor code to use python provided by openoffice

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@38771 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 86263538
...@@ -31,7 +31,6 @@ from os import environ ...@@ -31,7 +31,6 @@ from os import environ
from os.path import exists, join from os.path import exists, join
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from threading import Lock from threading import Lock
from sys import executable as python_path
from zope.interface import implements from zope.interface import implements
from application import Application from application import Application
from xvfb import xvfb from xvfb import xvfb
...@@ -58,7 +57,7 @@ class OpenOffice(Application): ...@@ -58,7 +57,7 @@ class OpenOffice(Application):
def _testOpenOffice(self, host, port): def _testOpenOffice(self, host, port):
"""Test if OpenOffice was started correctly""" """Test if OpenOffice was started correctly"""
logger.debug("Test OpenOffice %s - Pid %s" % (self.getAddress()[-1], self.pid())) logger.debug("Test OpenOffice %s - Pid %s" % (self.getAddress()[-1], self.pid()))
command = [python_path command = [join(self.office_binary_path, "python")
, pkg_resources.resource_filename("cloudooo", , pkg_resources.resource_filename("cloudooo",
join("helper", "openoffice_tester.py")) join("helper", "openoffice_tester.py"))
, "'--hostname=%s'" % host , "'--hostname=%s'" % host
......
...@@ -67,14 +67,17 @@ class OOHandler: ...@@ -67,14 +67,17 @@ class OOHandler:
def _getCommand(self, *args, **kw): def _getCommand(self, *args, **kw):
"""Transforms all parameters passed in a command""" """Transforms all parameters passed in a command"""
hostname, port = openoffice.getAddress() hostname, port = openoffice.getAddress()
jsonpickle_path = "/".join(pkg_resources.resource_filename("jsonpickle",
"").split("/")[:-1])
kw['hostname'] = hostname kw['hostname'] = hostname
kw['port'] = port kw['port'] = port
command_list = [python_path command_list = [path.join(self.office_binary_path, "python")
, pkg_resources.resource_filename("cloudooo", , pkg_resources.resource_filename("cloudooo",
path.join("helper", "unoconverter.py")) path.join("helper", "unoconverter.py"))
, "--uno_path='%s'" % self.uno_path , "--uno_path='%s'" % self.uno_path
, "--office_binary_path='%s'" % self.office_binary_path , "--office_binary_path='%s'" % self.office_binary_path
, "--document_url='%s'" % self.document.getUrl()] , "--document_url='%s'" % self.document.getUrl()
, "--jsonpickle_path='%s'" % jsonpickle_path]
for arg in args: for arg in args:
command_list.insert(3, "'--%s'" % arg) command_list.insert(3, "'--%s'" % arg)
for k, v in kw.iteritems(): for k, v in kw.iteritems():
......
...@@ -28,7 +28,6 @@ ...@@ -28,7 +28,6 @@
############################################################################## ##############################################################################
import sys import sys
import jsonpickle
import helper_utils import helper_utils
from types import UnicodeType, InstanceType from types import UnicodeType, InstanceType
from os import environ, putenv from os import environ, putenv
...@@ -264,15 +263,22 @@ def main(): ...@@ -264,15 +263,22 @@ def main():
"hostname=", "port=", "source_format=", "hostname=", "port=", "source_format=",
"document_url=", "destination_format=", "document_url=", "destination_format=",
"mimemapper=", "metadata=", "mimemapper=", "metadata=",
"unomimemapper_bin="]) "unomimemapper_bin=", "jsonpickle_path="])
except GetoptError, msg: except GetoptError, msg:
msg = msg.msg + help_msg msg = msg.msg + help_msg
print >> sys.stderr, msg print >> sys.stderr, msg
sys.exit(2) sys.exit(2)
param_list = [tuple[0] for tuple in opt_list] param_list = [tuple[0] for tuple in iter(opt_list)]
for opt, arg in opt_list: for opt, arg in iter(opt_list):
if opt == "--jsonpickle_path":
sys.path.append(arg)
break
import jsonpickle
for opt, arg in iter(opt_list):
if opt in ('-h', '--help'): if opt in ('-h', '--help'):
help() help()
elif opt == '--hostname': elif opt == '--hostname':
......
...@@ -107,7 +107,7 @@ class MimeMapper(object): ...@@ -107,7 +107,7 @@ class MimeMapper(object):
uno_path = kw.get("uno_path", environ.get('uno_path')) uno_path = kw.get("uno_path", environ.get('uno_path'))
office_binary_path = kw.get("office_binary_path", office_binary_path = kw.get("office_binary_path",
environ.get('office_binary_path')) environ.get('office_binary_path'))
command = [python_path command = [path.join(office_binary_path, "python")
, pkg_resources.resource_filename(__name__, , pkg_resources.resource_filename(__name__,
path.join("helper","unomimemapper.py")) path.join("helper","unomimemapper.py"))
, "'--uno_path=%s'" % uno_path , "'--uno_path=%s'" % uno_path
......
...@@ -26,10 +26,10 @@ ...@@ -26,10 +26,10 @@
# #
############################################################################## ##############################################################################
import unittest import unittest, pkg_resources
from cloudooo.application.openoffice import openoffice from cloudooo.application.openoffice import openoffice
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from os import environ from os import environ, path
from cloudoooTestCase import cloudoooTestCase, make_suite from cloudoooTestCase import cloudoooTestCase, make_suite
class TestUnoMimeMapper(cloudoooTestCase): class TestUnoMimeMapper(cloudoooTestCase):
...@@ -50,15 +50,15 @@ class TestUnoMimeMapper(cloudoooTestCase): ...@@ -50,15 +50,15 @@ class TestUnoMimeMapper(cloudoooTestCase):
def testCreateLocalAttributes(self): def testCreateLocalAttributes(self):
"""Test if filters returns correctly the filters and types in dict""" """Test if filters returns correctly the filters and types in dict"""
hostname, host = openoffice.getAddress() hostname, host = openoffice.getAddress()
command = [self.python_path, command = [path.join(self.office_binary_path, "python"),
"-c", pkg_resources.resource_filename("cloudooo", "helper/unomimemapper.py"),
"'from cloudooo.bin.unomimemapper import main; main()'",
"'--uno_path=%s'" % self.uno_path, "'--uno_path=%s'" % self.uno_path,
"'--office_binary_path=%s'" % self.uno_path, "'--office_binary_path=%s'" % self.office_binary_path,
"'--hostname=%s'" % self.hostname, "'--hostname=%s'" % self.hostname,
"'--port=%s'" % self.openoffice_port] "'--port=%s'" % self.openoffice_port]
stdout, stderr = Popen(' '.join(command), shell=True, stdout, stderr = Popen(' '.join(command), shell=True,
stdout=PIPE, stderr=PIPE).communicate() stdout=PIPE, stderr=PIPE).communicate()
self.assertEquals(stderr, '')
exec(stdout) exec(stdout)
self.assertEquals('filter_dict' in locals(), True) self.assertEquals('filter_dict' in locals(), True)
self.assertEquals('type_dict' in locals(), True) self.assertEquals('type_dict' in locals(), True)
...@@ -68,29 +68,36 @@ class TestUnoMimeMapper(cloudoooTestCase): ...@@ -68,29 +68,36 @@ class TestUnoMimeMapper(cloudoooTestCase):
self.assertEquals(type_dict.get('writer8').get('PreferredFilter'), 'writer8') self.assertEquals(type_dict.get('writer8').get('PreferredFilter'), 'writer8')
self.assertEquals(stderr, '') self.assertEquals(stderr, '')
def testCallUnoMimemapperWithoutSomeParameters(self): def testCallUnoMimemapperOnlyHostNameAndPort(self):
""" Test call unomimemapper without uno_path and office_binary_path""" """ Test call unomimemapper without uno_path and office_binary_path"""
hostname, host = openoffice.getAddress() hostname, host = openoffice.getAddress()
command = [self.python_path, command = [path.join(self.office_binary_path, "python"),
"-c", pkg_resources.resource_filename("cloudooo",
"'from cloudooo.bin.unomimemapper import main; main()'", "helper/unomimemapper.py"),
"'--hostname=%s'" % self.hostname, "'--hostname=%s'" % self.hostname,
"'--port=%s'" % self.openoffice_port] "'--port=%s'" % self.openoffice_port]
stdout, stderr = Popen(' '.join(command), shell=True, stdout, stderr = Popen(' '.join(command), shell=True,
stdout=PIPE, stderr=PIPE).communicate() stdout=PIPE, stderr=PIPE).communicate()
self.assertEquals(stderr.endswith('No module named uno\n'), True) self.assertEquals(stderr, '')
self.assertEquals(stdout, '') exec(stdout)
self.assertEquals('filter_dict' in locals(), True)
self.assertEquals('type_dict' in locals(), True)
self.assertNotEquals(filter_dict.get('writer8'), None)
self.assertEquals(type_dict.get('writer8').get('Name'), 'writer8')
self.assertNotEquals(filter_dict.get('writer8'), None)
self.assertEquals(type_dict.get('writer8').get('PreferredFilter'), 'writer8')
self.assertEquals(stderr, '')
def testWithoutOpenOffice(self): def testWithoutOpenOffice(self):
"""Test when the openoffice is stopped""" """Test when the openoffice is stopped"""
error_msg = "couldn\'t connect to socket (Success)\n" error_msg = "couldn\'t connect to socket (Success)\n"
hostname, host = openoffice.getAddress() hostname, host = openoffice.getAddress()
openoffice.stop() openoffice.stop()
command = [self.python_path, command = [path.join(self.office_binary_path, "python"),
"-c", pkg_resources.resource_filename("cloudooo",
"'from cloudooo.bin.unomimemapper import main; main()'", "helper/unomimemapper.py"),
"'--uno_path=%s'" % self.uno_path, "'--uno_path=%s'" % self.uno_path,
"'--office_binary_path=%s'" % self.uno_path, "'--office_binary_path=%s'" % self.office_binary_path,
"'--hostname=%s'" % self.hostname, "'--hostname=%s'" % self.hostname,
"'--port=%s'" % self.openoffice_port] "'--port=%s'" % self.openoffice_port]
stdout, stderr = Popen(' '.join(command), shell=True, stdout, stderr = Popen(' '.join(command), shell=True,
......
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