aboutsummaryrefslogtreecommitdiff
path: root/b4/mbox.py
diff options
context:
space:
mode:
authorKonstantin Ryabitsev <konstantin@linuxfoundation.org>2020-03-20 16:35:08 -0400
committerKonstantin Ryabitsev <konstantin@linuxfoundation.org>2020-03-20 16:35:08 -0400
commit0f63a3f6a7398321bd799c7b778e0e721c811287 (patch)
treed617ad0aa9e361e8bb579941ee22a7149cf1c5ed /b4/mbox.py
parent4cad662b69be9fa62460a342e2fd1aa87a7bd548 (diff)
downloadb4-0f63a3f6a7398321bd799c7b778e0e721c811287.tar.gz
Add caching layer
Many lore.kernel.org operations can be repeated within quick succession of each-other (e.g. someone reruns a query with -t). This commit adds a caching layer that keeps lookups in local cache for 10 minutes (default). It can be made longer or shorter by editing the 'cache-expire' setting, or running "b4 am" with -C,--no-cache. Signed-off-by: Konstantin Ryabitsev <konstantin@linuxfoundation.org>
Diffstat (limited to 'b4/mbox.py')
-rw-r--r--b4/mbox.py22
1 files changed, 16 insertions, 6 deletions
diff --git a/b4/mbox.py b/b4/mbox.py
index 3c06bd2..ccd15ed 100644
--- a/b4/mbox.py
+++ b/b4/mbox.py
@@ -13,6 +13,7 @@ import email.message
import email.utils
import re
import time
+import shutil
import urllib.parse
import xml.etree.ElementTree
@@ -53,6 +54,20 @@ def get_pi_thread_by_url(t_mbx_url, savefile):
def get_pi_thread_by_msgid(msgid, config, cmdargs):
wantname = cmdargs.wantname
outdir = cmdargs.outdir
+ if wantname:
+ savefile = os.path.join(outdir, wantname)
+ else:
+ # Save it into msgid.mbox
+ savefile = '%s.t.mbx' % msgid
+ savefile = os.path.join(outdir, savefile)
+
+ cachedir = b4.get_cache_dir()
+ cachefile = os.path.join(cachedir, '%s.pi.mbx' % urllib.parse.quote_plus(msgid))
+ if os.path.exists(cachefile) and not cmdargs.nocache:
+ logger.debug('Using cached copy: %s', cachefile)
+ shutil.copyfile(cachefile, savefile)
+ return savefile
+
# Grab the head from lore, to see where we are redirected
midmask = config['midmask'] % msgid
logger.info('Looking up %s', midmask)
@@ -64,12 +79,6 @@ def get_pi_thread_by_msgid(msgid, config, cmdargs):
canonical = resp.headers['Location'].rstrip('/')
resp.close()
t_mbx_url = '%s/t.mbox.gz' % canonical
- if wantname:
- savefile = os.path.join(outdir, wantname)
- else:
- # Save it into msgid.mbox
- savefile = '%s.t.mbx' % msgid
- savefile = os.path.join(outdir, savefile)
loc = urllib.parse.urlparse(t_mbx_url)
if cmdargs.useproject:
@@ -87,6 +96,7 @@ def get_pi_thread_by_msgid(msgid, config, cmdargs):
in_mbx.close()
out_mbx.close()
os.unlink(in_mbxf)
+ shutil.copyfile(savefile, cachefile)
return savefile