1
0
Fork 0
mirror of https://github.com/iiab/iiab.git synced 2025-02-13 19:52:06 +00:00
iiab/roles/kiwix/templates/iiab-make-kiwix-lib.py

124 lines
3.8 KiB
Python
Raw Normal View History

2017-05-27 18:09:50 +00:00
#!/usr/bin/python
"""
2018-07-23 19:18:31 +00:00
Creates temp library.xml file for kiwix from contents of /zims/content and index
2017-05-27 18:09:50 +00:00
Author: Tim Moody <tim(at)timmoody(dot)com>
Contributors: Jerry Vonau <jvonau3(at)gmail.com>
2017-05-27 18:09:50 +00:00
"""
import os, sys, syslog
import pwd, grp
import time
from datetime import date, datetime
import json
import yaml
import re
import subprocess
import shlex
import ConfigParser
IIAB_PATH='/etc/iiab'
if not IIAB_PATH in sys.path:
sys.path.append(IIAB_PATH)
from iiab_env import get_iiab_env
2017-05-27 18:09:50 +00:00
# Config Files
iiab_config_file = "{{ iiab_config_file }}"
2017-05-27 18:09:50 +00:00
# Variables that should be read from config file
# All of these variables will be read from config files and recomputed in init()
iiab_zim_path = "{{ iiab_zim_path }}"
2018-07-23 19:18:31 +00:00
# Later we will append .tmp to file name
kiwix_library_xml = "{{ kiwix_library_xml }}"
2017-05-27 18:09:50 +00:00
iiab_base_path = "{{ iiab_base }}"
kiwix_manage = iiab_base_path + "/kiwix/bin/kiwix-manage"
doc_root = get_iiab_env('WWWROOT')
2017-05-27 18:09:50 +00:00
zim_version_idx = doc_root + "/common/assets/zim_version_idx.json"
zim_versions = {}
old_zim_map = {"bad.zim" : "unparseable name"}
2017-05-27 18:09:50 +00:00
def main():
"""Server routine"""
2018-07-23 19:18:31 +00:00
global kiwix_library_xml
2017-05-27 18:09:50 +00:00
init()
2018-07-23 19:18:31 +00:00
kiwix_library_xml += '.tmp' # write to temp file
2017-05-27 18:09:50 +00:00
# remove existing file
try:
os.remove(kiwix_library_xml)
except OSError:
pass
# add each file in /library/zims/content with corresponding index
# only add a single .zim for each .zimxx file
files_processed = {}
content = iiab_zim_path + "/content/"
index = iiab_zim_path + "/index/"
2017-05-27 18:09:50 +00:00
2017-05-30 17:23:47 +00:00
flist = os.listdir(content)
flist.sort()
for filename in flist:
2017-05-27 18:09:50 +00:00
zimpos = filename.find(".zim")
if zimpos != -1:
filename = filename[:zimpos]
if filename not in files_processed:
files_processed[filename] = True
zimname = content + filename + ".zim"
zimidx = index + filename + ".zim.idx"
command = kiwix_manage + " " + kiwix_library_xml + " add " + zimname
if os.path.isdir (zimidx): # only declare index if exists (could be embedded)
command += " -i " + zimidx
2017-05-27 18:09:50 +00:00
#print command
args = shlex.split(command)
2017-05-30 17:23:47 +00:00
try:
outp = subprocess.check_output(args)
# create map of generic zim name to actual, assumes pattern of <name>_<yyyy-mm>
# all current files follow this pattern, but some older ones, no longer in the catalog, do not
if filename in old_zim_map: # handle old names that don't parse
wiki_name = old_zim_map[filename]
else:
ulpos = filename.rfind("_")
# but gutenberg don't - future maybe put in old_zim_map (en and fr, but instance dates may change)
if "gutenberg_" in filename:
ulpos = filename[:ulpos].rfind("_")
wiki_name = filename[:ulpos]
2017-05-30 17:23:47 +00:00
zim_versions[wiki_name] = filename # if there are multiples, last should win
2017-05-30 17:23:47 +00:00
except: #skip things that don't work
print 'skipping ' + filename
pass
2017-05-27 18:09:50 +00:00
with open(zim_version_idx, 'w') as fp:
json.dump(zim_versions, fp)
sys.exit()
def init():
global iiab_base_path
global iiab_zim_path
2017-05-27 18:09:50 +00:00
global kiwix_library_xml
global kiwix_manage
config = ConfigParser.SafeConfigParser()
config.read(iiab_config_file)
iiab_base_path = config.get('location','iiab_base')
iiab_zim_path = config.get('kiwix','iiab_zim_path')
kiwix_library_xml = config.get('kiwix','kiwix_library_xml')
kiwix_manage = iiab_base_path + "/kiwix/bin/kiwix-manage"
2017-05-27 18:09:50 +00:00
# Now start the application
if __name__ == "__main__":
# Run the main routine
main()