From d2dd47fb238359cef1cc49568f1240229d4a5298 Mon Sep 17 00:00:00 2001 From: Miroslav Stampar Date: Thu, 14 Jun 2012 13:52:56 +0000 Subject: [PATCH] some more refactoring --- lib/core/common.py | 26 ++++++++++---------------- lib/utils/crawler.py | 21 +++++++++------------ 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/lib/core/common.py b/lib/core/common.py index f1ce9216e..753d19899 100644 --- a/lib/core/common.py +++ b/lib/core/common.py @@ -1645,15 +1645,12 @@ def readCachedFileContent(filename, mode='rb'): """ if filename not in kb.cache.content: - kb.locks.cache.acquire() - - if filename not in kb.cache.content: - checkFile(filename) - with codecs.open(filename, mode, UNICODE_ENCODING) as f: - content = f.read() - kb.cache.content[filename] = content - - kb.locks.cache.release() + with kb.locks.cache: + if filename not in kb.cache.content: + checkFile(filename) + with codecs.open(filename, mode, UNICODE_ENCODING) as f: + content = f.read() + kb.cache.content[filename] = content return kb.cache.content[filename] @@ -2113,13 +2110,10 @@ def logHTTPTraffic(requestLogMsg, responseLogMsg): if not conf.trafficFile: return - kb.locks.log.acquire() - - dataToTrafficFile("%s%s" % (requestLogMsg, os.linesep)) - dataToTrafficFile("%s%s" % (responseLogMsg, os.linesep)) - dataToTrafficFile("%s%s%s%s" % (os.linesep, 76 * '#', os.linesep, os.linesep)) - - kb.locks.log.release() + with kb.locks.log: + dataToTrafficFile("%s%s" % (requestLogMsg, os.linesep)) + dataToTrafficFile("%s%s" % (responseLogMsg, os.linesep)) + dataToTrafficFile("%s%s%s%s" % (os.linesep, 76 * '#', os.linesep, os.linesep)) def getPageTemplate(payload, place): """ diff --git a/lib/utils/crawler.py b/lib/utils/crawler.py index d2bdf93f3..abfcd2a30 100644 --- a/lib/utils/crawler.py +++ b/lib/utils/crawler.py @@ -42,13 +42,11 @@ class Crawler: threadData = getCurrentThreadData() while kb.threadContinue: - kb.locks.limits.acquire() - if threadData.shared.unprocessed: - current = threadData.shared.unprocessed.pop() - kb.locks.limits.release() - else: - kb.locks.limits.release() - break + with kb.locks.limits: + if threadData.shared.unprocessed: + current = threadData.shared.unprocessed.pop() + else: + break content = None try: @@ -83,11 +81,10 @@ class Crawler: continue if url.split('.')[-1].lower() not in CRAWL_EXCLUDE_EXTENSIONS: - kb.locks.outputs.acquire() - threadData.shared.deeper.add(url) - if re.search(r"(.*?)\?(.+)", url): - threadData.shared.outputs.add(url) - kb.locks.outputs.release() + with kb.locks.outputs: + threadData.shared.deeper.add(url) + if re.search(r"(.*?)\?(.+)", url): + threadData.shared.outputs.add(url) except UnicodeEncodeError: # for non-HTML files pass finally: