ExtensionCrawler/crawler

235 lines
8.9 KiB
Plaintext
Raw Normal View History

2017-01-23 18:54:32 +00:00
#!/usr/bin/env python3
#
# Copyright (C) 2016,2017 The University of Sheffield, UK
#
2017-01-23 18:54:32 +00:00
# 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 os
import sys
2017-02-03 09:45:51 +00:00
import datetime
2017-01-28 13:32:47 +00:00
import time
2017-01-28 14:01:53 +00:00
import getopt
2017-07-28 18:44:51 +00:00
import sqlite3
2017-07-28 14:57:17 +00:00
from functools import reduce
from ExtensionCrawler.discover import get_new_ids
2017-07-28 18:44:51 +00:00
from ExtensionCrawler.archive import get_forum_ext_ids, get_existing_ids, update_extensions
from ExtensionCrawler.util import log
2017-01-23 18:54:32 +00:00
# Script should run with python 3.4 or 3.5
assert sys.version_info >= (3, 4) and sys.version_info < (3, 6)
2017-06-16 22:19:13 +00:00
2017-02-04 00:07:48 +00:00
2017-07-28 14:57:17 +00:00
def write_log(dirname, fname, text):
os.makedirs(dirname, exist_ok=True)
2017-07-28 18:44:51 +00:00
with open(os.path.join(dirname, fname), 'w') as logfile:
logfile.write(text)
2017-03-14 06:52:58 +00:00
2017-07-28 14:57:17 +00:00
def log_failures_to_file(dirname, today, res):
2017-02-04 00:07:48 +00:00
not_authorized = reduce(
lambda x, y: x + "\n" + y,
sorted(map(lambda x: x.id, filter(lambda x: x.not_authorized(), res))),
"")
2017-07-28 14:57:17 +00:00
write_log(dirname, today + "-not-authorized.log", not_authorized)
2017-02-04 00:07:48 +00:00
updated = reduce(
lambda x, y: x + "\n" + y,
sorted(
map(lambda x: x.id,
filter(lambda x: x.is_ok() and not x.not_modified(), res))),
"")
2017-07-28 14:57:17 +00:00
write_log(dirname, today + "-updated.log", updated)
2017-02-04 00:07:48 +00:00
has_exception = reduce(
lambda x, y: x + "\n" + y,
sorted(map(lambda x: x.id, filter(lambda x: x.has_exception(), res))),
"")
2017-07-28 14:57:17 +00:00
write_log(dirname, today + "-raised-exception.log", has_exception)
2017-02-04 00:07:48 +00:00
raised_ddos = reduce(
lambda x, y: x + "\n" + y,
sorted(
map(lambda x: x.id, filter(lambda x: x.raised_google_ddos(),
res))), "")
2017-07-28 14:57:17 +00:00
write_log(dirname, today + "-raised-ddos.log", raised_ddos)
2017-02-04 00:07:48 +00:00
not_in_store = reduce(
lambda x, y: x + "\n" + y,
sorted(map(lambda x: x.id, filter(lambda x: x.not_in_store(), res))),
"")
2017-07-28 14:57:17 +00:00
write_log(dirname, today + "-not-in-store.log", not_in_store)
2017-02-04 00:07:48 +00:00
new = reduce(
lambda x, y: x + "\n" + y,
sorted(map(lambda x: x.id, filter(lambda x: x.is_new(), res))), "")
2017-07-28 14:57:17 +00:00
write_log(dirname, today + "-new-in-store.log", new)
file_corruption = reduce(
lambda x, y: x + "\n" + y,
2017-03-30 06:53:04 +00:00
sorted(map(lambda x: x.id, filter(lambda x: x.corrupt_tar(), res))),
"")
2017-07-28 14:57:17 +00:00
write_log(dirname, today + "-file-corruption.log", file_corruption)
sql_exception = reduce(
lambda x, y: x + "\n" + y,
sorted(map(lambda x: x.id, filter(lambda x: x.sql_exception(), res))),
"")
2017-07-28 14:57:17 +00:00
write_log(dirname, today + "-sql-exception.log", sql_exception)
sql_success = reduce(
lambda x, y: x + "\n" + y,
2017-07-28 14:57:17 +00:00
sorted(
map(lambda x: x.id, filter(lambda x: not x.sql_success(), res))),
"")
2017-07-28 14:57:17 +00:00
write_log(dirname, today + "-sql-not-updated.log", sql_success)
2017-01-28 01:25:24 +00:00
2017-03-30 06:53:04 +00:00
2017-01-28 14:20:56 +00:00
def log_summary(verbose, res, stderr=False, runtime=0):
2017-07-28 18:44:51 +00:00
def printlog(msg):
2017-01-28 14:20:56 +00:00
if stderr:
2017-07-28 18:44:51 +00:00
sys.stderr.write(msg)
2017-01-28 14:20:56 +00:00
else:
2017-07-28 18:44:51 +00:00
log(verbose, msg)
2017-01-28 14:20:56 +00:00
2017-01-28 01:25:24 +00:00
total = len(res)
success = len(list(filter(lambda x: x.is_ok(), res)))
2017-01-28 00:51:21 +00:00
not_authorized = len(list(filter(lambda x: x.not_authorized(), res)))
2017-01-28 01:25:24 +00:00
has_exception = len(list(filter(lambda x: x.has_exception(), res)))
raised_ddos = len(list(filter(lambda x: x.raised_google_ddos(), res)))
not_in_store = len(list(filter(lambda x: x.not_in_store(), res)))
2017-01-28 12:21:52 +00:00
not_modified = len(list(filter(lambda x: x.not_modified(), res)))
corrupt_tar_archives = list(filter(lambda x: x.corrupt_tar(), res))
sql_exception = len(list(filter(lambda x: x.sql_exception(), res)))
sql_success = len(list(filter(lambda x: x.sql_success(), res)))
new = len(list(filter(lambda x: x.is_new(), res)))
2017-02-04 00:07:48 +00:00
updated = len(
list(filter(lambda x: x.is_ok() and not x.not_modified(), res)))
2017-01-28 13:32:47 +00:00
2017-07-28 18:44:51 +00:00
printlog("\n")
printlog("Summary:\n")
printlog(" Updated {} out of {} extensions successfully\n".format(
2017-01-28 01:25:24 +00:00
str(success), str(total)))
2017-07-28 18:44:51 +00:00
printlog(" Updated extensions: {:8d}\n".format(updated))
printlog(" Updated SQL databases: {:8d}\n".format(sql_success))
printlog(" New extensions: {:8d}\n".format(new))
printlog(" Not authorized: {:8d}\n".format(not_authorized))
printlog(" Raised Google DDOS: {:8d}\n".format(raised_ddos))
printlog(" Not modified archives: {:8d}\n".format(not_modified))
printlog(" Extensions not in store: {:8d}\n".format(not_in_store))
printlog(" Unknown exception: {:8d}\n".format(has_exception))
printlog(" Corrupt tar archives: {:8d}\n".format(
len(corrupt_tar_archives)))
printlog(" SQL exception: {:8d}\n".format(sql_exception))
printlog(" Total runtime: {}\n".format(
2017-01-28 14:20:56 +00:00
str(datetime.timedelta(seconds=int(runtime)))))
2017-01-28 01:25:24 +00:00
2017-07-28 18:44:51 +00:00
if corrupt_tar_archives != []:
printlog("\n\n")
printlog("List of extensions with corrupted files/archives:\n")
2017-03-30 06:53:04 +00:00
list(
2017-07-28 18:44:51 +00:00
map(lambda x: printlog(" " + x.id + ": " + str(x.exception) + "\n"),
2017-03-30 06:53:04 +00:00
corrupt_tar_archives))
2017-07-28 18:44:51 +00:00
printlog("\n")
2017-07-28 14:57:17 +00:00
def helpmsg():
2017-01-28 14:01:53 +00:00
print("crawler [OPTION]")
print(" -h print this help text")
print(" -s silent (no log messages)")
print(" -d disover new extensions")
print(" -a=<DIR> archive directory")
def main(argv):
today = datetime.datetime.now(datetime.timezone.utc).isoformat()
2017-01-28 14:01:53 +00:00
basedir = "archive"
parallel = 24
2017-01-24 22:04:34 +00:00
verbose = True
2017-01-28 14:01:53 +00:00
discover = False
try:
2017-07-28 18:44:51 +00:00
opts, _ = getopt.getopt(argv, "hsda:p:", ["archive=", 'parallel='])
2017-01-28 14:01:53 +00:00
except getopt.GetoptError:
2017-07-28 14:57:17 +00:00
helpmsg()
2017-01-28 14:01:53 +00:00
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
2017-07-28 14:57:17 +00:00
helpmsg()
2017-01-28 14:01:53 +00:00
sys.exit()
elif opt in ("-a", "--archive"):
basedir = arg
elif opt in ("-p", "--parallel"):
parallel = int(arg)
2017-01-28 14:01:53 +00:00
elif opt == '-s':
verbose = False
elif opt == '-d':
discover = True
2017-01-23 18:54:32 +00:00
2017-01-28 14:37:35 +00:00
archive_dir = os.path.join(basedir, "data")
os.makedirs(archive_dir, exist_ok=True)
2017-01-28 14:01:53 +00:00
conf_dir = os.path.join(basedir, "conf")
2017-01-28 17:22:28 +00:00
open(os.path.join(conf_dir, "forums.conf"), 'a').close()
2017-01-28 14:37:35 +00:00
os.makedirs(conf_dir, exist_ok=True)
log_dir = os.path.join(basedir, "log")
os.makedirs(log_dir, exist_ok=True)
2017-01-28 13:32:47 +00:00
start_time = time.time()
2017-01-27 23:16:48 +00:00
log(verbose, "Configuration:\n")
log(verbose, " Base dir: {}\n".format(basedir))
2017-07-28 14:57:17 +00:00
log(verbose,
" Archive directory: {}\n".format(archive_dir))
log(verbose, " Configuration directory: {}\n".format(conf_dir))
log(verbose, " Discover new extensions: {}\n".format(discover))
log(verbose, " Max num. of concurrent downloads: {}\n".format(parallel))
2017-07-28 14:57:17 +00:00
log(verbose, " SQLite 3 version: {}\n".format(
sqlite3.sqlite_version))
2017-01-28 14:01:53 +00:00
log(verbose, "\n")
2017-01-27 22:31:21 +00:00
2017-01-26 03:53:16 +00:00
forum_ext_ids = get_forum_ext_ids(conf_dir, verbose)
existing_ids = get_existing_ids(archive_dir, verbose)
known_ids = list(set(existing_ids) | set(forum_ext_ids))
discovered_ids = []
2017-01-28 14:01:53 +00:00
if discover:
discovered_ids = get_new_ids(verbose, known_ids)
ext_ids = list(set(discovered_ids) | set(known_ids))
2017-01-26 03:53:16 +00:00
discovered_ids = None
known_ids = None
existing_ids = None
2017-07-28 14:57:17 +00:00
res = update_extensions(archive_dir, verbose, parallel, forum_ext_ids,
ext_ids)
2017-02-04 00:07:48 +00:00
# We re-try (once) the extensions with unknown exceptions, as
# they are often temporary
2017-02-04 00:07:48 +00:00
has_exception = list(filter(lambda x: x.has_exception(), res))
2017-07-28 18:44:51 +00:00
if has_exception != []:
2017-02-04 00:07:48 +00:00
log(verbose,
" {} extensions with unknown exceptions, start another try ...\n".
format(str(len(has_exception))))
has_exception_ids = list(map(lambda x: x.id, has_exception))
oldres = list(set(res) - set(has_exception))
forum_ext_ids_except = list(
set(forum_ext_ids).intersection(set(has_exception_ids)))
ext_ids_except = sorted(
list(set(has_exception_ids) - set(forum_ext_ids_except)))
res_update = update_extensions(archive_dir, verbose, parallel,
2017-02-04 00:07:48 +00:00
forum_ext_ids_except, ext_ids_except)
res = oldres + res_update
2017-01-28 13:32:47 +00:00
end_time = time.time()
2017-01-28 14:20:56 +00:00
log_summary(verbose, res, False, end_time - start_time)
log_summary(verbose, res, True, end_time - start_time)
2017-02-04 00:07:48 +00:00
log_failures_to_file(log_dir, today, res)
2017-01-23 18:54:32 +00:00
2017-01-28 01:25:24 +00:00
2017-01-28 14:01:53 +00:00
if __name__ == "__main__":
main(sys.argv[1:])