Get rid of temporary files alltogether

Signed-off-by: Patrick Uiterwijk <puiterwijk@redhat.com>
This commit is contained in:
Patrick Uiterwijk 2018-05-18 13:13:47 +02:00
parent 2fb0b14230
commit 8797997ab2

View file

@ -3,11 +3,9 @@
from __future__ import unicode_literals, print_function from __future__ import unicode_literals, print_function
import os import os
import random
import string import string
import subprocess import subprocess
import sys import sys
import tempfile
def main(): def main():
@ -21,11 +19,6 @@ def main():
print('An username is required to query datagrepper') print('An username is required to query datagrepper')
return 1 return 1
tempfilename = '/tmp/sar_{0}_{1}'.format(username, ''.join(
[random.choice(string.ascii_letters + string.digits)
for n in xrange(10)]
))
# Get all messages related to this user. # Get all messages related to this user.
query = ''' query = '''
COPY ( COPY (
@ -41,17 +34,11 @@ COPY (
WHERE messages.username = '{username}' WHERE messages.username = '{username}'
) )
) )
TO '{tmpfile}' delimiter ',' CSV header; TO STDOUT delimiter ',' CSV header;
''' '''
query = query.format(username=username, tmpfile=tempfilename) query = query.format(username=username, tmpfile=tempfilename)
command = ['sudo', '-u', 'postgres', 'psql', 'datanommer', '-c', '"%s"' % query] command = ['sudo', '-u', 'postgres', 'psql', 'datanommer', '-c', '"%s"' % query]
subprocess.check_call( subprocess.check_call(' '.join(command), shell=True, cwd='/tmp')
' '.join(command), shell=True, stdout=subprocess.PIPE,
cwd='/tmp')
with open(tempfilename) as stream:
for line in stream:
print(line)
os.unlink(tempfilename)
if __name__ == '__main__': if __name__ == '__main__':