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
import os
import random
import string
import subprocess
import sys
import tempfile
def main():
@ -21,11 +19,6 @@ def main():
print('An username is required to query datagrepper')
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.
query = '''
COPY (
@ -41,17 +34,11 @@ COPY (
WHERE messages.username = '{username}'
)
)
TO '{tmpfile}' delimiter ',' CSV header;
TO STDOUT delimiter ',' CSV header;
'''
query = query.format(username=username, tmpfile=tempfilename)
command = ['sudo', '-u', 'postgres', 'psql', 'datanommer', '-c', '"%s"' % query]
subprocess.check_call(
' '.join(command), shell=True, stdout=subprocess.PIPE,
cwd='/tmp')
with open(tempfilename) as stream:
for line in stream:
print(line)
os.unlink(tempfilename)
subprocess.check_call(' '.join(command), shell=True, cwd='/tmp')
if __name__ == '__main__':