ExtensionCrawler/create-db

109 lines
3.4 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python3
#
# Copyright (C) 2016,2017 The University of Sheffield, UK
#
# 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/>.
#
import getopt
import os
import sys
import glob
import tarfile
import tempfile
2017-06-19 15:42:35 +00:00
import traceback
from multiprocessing import Pool
2017-07-31 15:19:20 +00:00
from ExtensionCrawler.archive import *
2017-06-20 07:10:28 +00:00
from ExtensionCrawler.config import *
def help():
2017-06-30 17:39:26 +00:00
print("create-db [OPTION] DBBASEDIR")
2017-06-22 16:46:18 +00:00
print(" DBBASEDIR directory for generated db files")
print(" -h print this help text")
print(" -a=<DIR> archive directory")
print(" -p=<PREFIX> three-letter-prefix")
print(" -t=<THREADS> number of parallel threads")
2017-06-22 16:46:18 +00:00
def process_id(archivedir, dbbasedir, verbose, ext_id):
txt = ""
txt = logmsg(verbose, txt, "Processing {} ...\n".format(ext_id))
tarpath = archive_file(archivedir, ext_id)
2017-06-22 16:46:18 +00:00
dbpath = os.path.join(dbbasedir, ext_id + ".sqlite")
if os.path.exists(dbpath):
os.remove(dbpath)
with tempfile.TemporaryDirectory() as tmpdir:
with tarfile.open(tarpath) as t:
t.extractall(tmpdir)
iddir = os.path.join(tmpdir, ext_id)
for date in sorted(os.listdir(iddir)):
try:
update_txt = update_sqlite_incremental(
2017-06-22 16:46:18 +00:00
dbpath, iddir, ext_id, date, True, "")
txt = logmsg(verbose, txt, update_txt)
2017-06-22 16:46:18 +00:00
except Exception:
txt = logmsg(verbose, txt,
"Exception when handling {} on {}:\n".format(
ext_id, date))
txt = logmsg(verbose, txt, traceback.format_exc())
txt = logmsg(verbose, txt, "\n")
return txt
def main(argv):
basedir = "archive"
prefix = ""
parallel = 8
try:
opts, args = getopt.getopt(argv, "ha:p:t:",
["archive=", "prefix=", "threads="])
except getopt.GetoptError:
help()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
help()
sys.exit()
elif opt in ("-a", "--archive"):
basedir = arg
elif opt in ("-p", "--prefix"):
prefix = arg
elif opt in ("-t", "--threads"):
parallel = int(arg)
2017-06-22 16:46:18 +00:00
if len(args) < 1:
help()
sys.exit(2)
dbbasedir = args[0]
2017-06-19 15:42:35 +00:00
archivedir = os.path.join(basedir, "data")
threeletterdirs = glob.glob(os.path.join(archivedir, prefix + "*"))
for threeletterdir in threeletterdirs:
ext_ids = list(set([d[:32] for d in os.listdir(threeletterdir)]))
with Pool(parallel) as p:
2017-06-22 16:46:18 +00:00
for txt in p.imap(partial(process_id, archivedir, dbbasedir, True), ext_ids):
sys.stdout.write(txt)
sys.stdout.flush()
if __name__ == "__main__":
main(sys.argv[1:])