Get rid of temporary files alltogether
Signed-off-by: Patrick Uiterwijk <puiterwijk@redhat.com>
This commit is contained in:
parent
2fb0b14230
commit
8797997ab2
1 changed files with 2 additions and 15 deletions
|
@ -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__':
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue