Date: prev next · Thread: first prev next last
2017 Archives by date, by thread · List index


On 19 October 2017 at 15:05, Jens Tröger <jens.troeger@light-speed.de> wrote:
The termination of the process p seems to destroy the other running soffice process. Hence using 
custom user installations with -env:UserInstallation=/tmp/random-dir but that just prolongs the 
process creation noticeably!

One alternative to allowing LO to create a new user installation each
time (which will indeed be slow) is to create a fresh user
installation once, save it somewhere as a template, then make a new
copy of the template directory each time - a file level copy should be
considerably faster.
You can accomplish this simply in Python using shutil.copytree()

Some sample code appended below - this is hacked out from a previous
project, and may or may not represent best practice as to how to do
this, but does work.

Regards
Matthew Francis


#!/usr/bin/python3

import os
import re
import shutil
import subprocess
import tempfile
import time
import uno
import uuid

from com.sun.star.connection import NoConnectException

sofficePath = '/usr/bin/soffice'
tempDir = tempfile.mkdtemp()

# Restore cached profile if available
userProfile = tempDir + '/profile'
cacheDir = os.getenv('XDG_CACHE_DIR', os.environ['HOME'] + '/.cache')
+ '/lo_profile_cache'
if os.path.isdir(cacheDir):
    shutil.copytree(cacheDir, userProfile)
    profileCached = True
else:
    os.mkdir(userProfile)
    profileCached = False

# Launch LibreOffice server
pipeName = uuid.uuid4().hex
args = [
    sofficePath,
    '-env:UserInstallation=file://' + userProfile,
    '--pidfile=' + tempDir + '/soffice.pid',
    '--accept=pipe,name=' + pipeName + ';urp;',
    '--norestore',
    '--invisible'
]
sofficeEnvironment = os.environ
sofficeEnvironment['TMPDIR'] = tempDir
proc = subprocess.Popen(args, env=sofficeEnvironment, preexec_fn=os.setsid)

# Open connection to server
for i in range(100):
    try:
        localContext = uno.getComponentContext()
        resolver =
localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver",
localContext)
        context =
resolver.resolve("uno:pipe,name=%s;urp;StarOffice.ComponentContext" %
pipeName)
        break
    except NoConnectException:
        time.sleep(0.1)
        if i == 99:
            raise

# Cache profile if required
if not profileCached:
    shutil.copytree(userProfile, cacheDir)


desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop",
context)
desktop.loadComponentFromURL('private:factory/swriter', 'blank', 0, ())

# ... clean up temporary directory when done (omitted)

Context


Privacy Policy | Impressum (Legal Info) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Creative Commons Attribution-Share Alike 3.0 License. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License (MPLv2). "LibreOffice" and "The Document Foundation" are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy.