From 999ad9577f8af7127b93e51157ba10cd2d794b6c Mon Sep 17 00:00:00 2001 From: Hidden Z Date: Tue, 2 Nov 2010 16:07:08 +0000 Subject: [PATCH] py-i2phosts-fetcher: use temp files for storing fetched content --- py-i2phosts-fetcher | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/py-i2phosts-fetcher b/py-i2phosts-fetcher index 8e412dc..fa66639 100755 --- a/py-i2phosts-fetcher +++ b/py-i2phosts-fetcher @@ -9,6 +9,7 @@ import urllib2 import subprocess import argparse import configobj +import tempfile # parse command line options parser = argparse.ArgumentParser( @@ -57,8 +58,6 @@ opener = urllib2.build_opener(proxy_handler) all_sources = ExternalSource.objects.filter(active=True) for source in all_sources: - # use separate file for each host - filename = 'hosts.txt.' + source.name if source.last_modified: last_modified = source.last_modified.strftime('%a, %d %b %Y %H:%M:%S GMT') # prevent redownloading of hosts-file by passing If-Modified-Since http header @@ -86,7 +85,9 @@ for source in all_sources: except: log.warning('failed to read data from %s', source.name) continue - f = open(filename, 'w') + # save fetched content into temporary file + fd, tmpfile = tempfile.mkstemp(text=True) + f = os.fdopen(fd, 'w') f.write(content) f.close() # get last-modified info from header @@ -104,9 +105,10 @@ for source in all_sources: source.save() # form commnd-line for invoke injector log.info('adding hosts from: %s', source.name) - sp_args = ['py-i2phosts-injector', '-s', '-a', '-f', filename, '-d', + sp_args = ['py-i2phosts-injector', '-s', '-a', '-f', tmpfile, '-d', 'Auto-added from ' + source.name] p = subprocess.Popen(sp_args, shell=False, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) out = p.communicate()[0] + os.remove(tmpfile) log.info('injector output: \n%s', out)