2017-05-27 18:09:50 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
Creates library.xml file for kiwix from contents of /zims/content and index
|
|
|
|
|
|
|
|
Author: Tim Moody <tim(at)timmoody(dot)com>
|
2017-07-04 20:05:10 +00:00
|
|
|
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
|
2017-07-04 20:05:10 +00:00
|
|
|
IIAB_PATH='/etc/iiab'
|
|
|
|
if not IIAB_PATH in sys.path:
|
|
|
|
sys.path.append(IIAB_PATH)
|
2017-06-09 23:25:56 +00:00
|
|
|
from iiab_env import get_iiab_env
|
2017-05-27 18:09:50 +00:00
|
|
|
|
|
|
|
# Config Files
|
2017-07-04 20:05:10 +00:00
|
|
|
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()
|
2017-07-04 20:05:10 +00:00
|
|
|
iiab_zim_path = "{{ iiab_zim_path }}"
|
|
|
|
kiwix_library_xml = "{{ kiwix_library_xml }}"
|
2017-05-27 18:09:50 +00:00
|
|
|
|
2017-07-04 20:05:10 +00:00
|
|
|
iiab_base_path = "{{ iiab_base }}"
|
2017-06-09 23:25:56 +00:00
|
|
|
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 = {}
|
2017-08-12 20:16:00 +00:00
|
|
|
old_zim_map = {"bad.zim" : "unparseable name"}
|
2017-05-27 18:09:50 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
"""Server routine"""
|
|
|
|
|
|
|
|
init()
|
|
|
|
|
|
|
|
# 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 = {}
|
2017-06-09 23:25:56 +00:00
|
|
|
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
|
2017-08-12 20:16:00 +00:00
|
|
|
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
|
2017-06-17 13:11:55 +00:00
|
|
|
|
2017-08-12 20:16:00 +00:00
|
|
|
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-06-17 13:11:55 +00:00
|
|
|
|
2017-05-30 17:23:47 +00:00
|
|
|
zim_versions[wiki_name] = filename # if there are multiples, last should win
|
2017-08-12 20:16:00 +00:00
|
|
|
|
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():
|
|
|
|
|
2017-06-09 23:25:56 +00:00
|
|
|
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()
|
2017-06-09 23:25:56 +00:00
|
|
|
config.read(iiab_config_file)
|
|
|
|
iiab_base_path = config.get('location','iiab_base')
|
|
|
|
iiab_zim_path = config.get('kiwix-serve','iiab_zim_path')
|
2017-05-27 18:09:50 +00:00
|
|
|
kiwix_library_xml = config.get('kiwix-serve','kiwix_library_xml')
|
2017-06-09 23:25:56 +00:00
|
|
|
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()
|