Child: [2c2279] (diff)

Download this file

uprclindex.py    94 lines (80 with data), 2.8 kB

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