Switch to unified view

a b/src/mediaserver/cdplugins/uprcl/uprclindex.py
1
#!/usr/bin/env python
2
#
3
# Copyright (C) 2017 J.F.Dockes
4
#
5
# This program is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation, either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
from __future__ import print_function
19
20
import sys
21
import os
22
import shutil
23
import conftree
24
import subprocess
25
import time
26
import copy
27
28
from uprclutils import uplog
29
30
def _initconfdir(confdir, topdirs):
31
    if os.path.exists(confdir):
32
        raise Exception("_initconfdir: exists already: %s" % confdir)
33
    os.mkdir(confdir)
34
    datadir = os.path.join(os.path.dirname(__file__), "rclconfig")
35
    uplog("datadir: %s" % datadir)
36
    shutil.copyfile(os.path.join(datadir, "fields"),
37
                    os.path.join(confdir, "fields"))
38
    f = open(os.path.join(confdir, "recoll.conf"), "w")
39
    f.write("topdirs=%s\n" % topdirs)
40
    f.write("idxabsmlen=0\n")
41
    f.write("loglevel=2\n")
42
    f.close()
43
44
_idxproc = None
45
_lastidxstatus = None
46
47
def runindexer(confdir, topdirs):
48
    global _idxproc, _lastidxstatus
49
    if _idxproc is not None:
50
        raise Exception("uprclrunindexer: already running")
51
52
    if not os.path.isdir(confdir):
53
        if os.path.exists(confdir):
54
            raise Exception("Exists and not directory: %s" % confdir)
55
        _initconfdir(confdir, topdirs)
56
    else:
57
        cf = conftree.ConfSimple(os.path.join(confdir, "recoll.conf"),
58
                                 readonly = False)
59
        td = cf.get("topdirs", '')
60
        if td != topdirs:
61
            cf.set("topdirs", topdirs)
62
63
    env = copy.deepcopy(os.environ)
64
    env["HOME"] = confdir
65
    _idxproc = subprocess.Popen(["recollindex", "-c", confdir])
66
67
def indexerdone():
68
    global _idxproc, _lastidxstatus
69
    if _idxproc is None:
70
        return True
71
    _lastidxstatus = _idxproc.poll()
72
    if _lastidxstatus is None:
73
        return False
74
    _idxproc = None
75
    return True
76
77
def indexerstatus():
78
    return _lastidxstatus
79
    
80
81
82
# Only used for testing
83
if __name__ == '__main__':
84
    if len(sys.argv) != 3:
85
        print("Usage: uprclindex.py <confdir> <topdirs>", file=sys.stderr)
86
        sys.exit(1)
87
    runindexer(sys.argv[1], sys.argv[2])
88
    while True:
89
        if indexerdone():
90
            uplog("Indexing done, status: %d" % indexerstatus())
91
            sys.exit(0)
92
        uplog("Waiting for indexer")
93
        time.sleep(1);