summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--chimere/models.py37
1 files changed, 24 insertions, 13 deletions
diff --git a/chimere/models.py b/chimere/models.py
index d4b3219..7d953cd 100644
--- a/chimere/models.py
+++ b/chimere/models.py
@@ -26,6 +26,8 @@ import json
import os
import pyexiv2
import re
+import tempfile
+import shutil
from lxml import etree
from PIL import Image
@@ -1512,24 +1514,33 @@ class RouteFile(models.Model):
if self.simplified_file:
return
input_name = settings.MEDIA_ROOT + self.raw_file.name
- output_name = settings.MEDIA_ROOT + self.raw_file.name[:-4] + \
- "_simplified" + ".gpx"
+ temp_dir = tempfile.gettempdir()
+ temp_path = os.path.join(temp_dir, 'temp_filename.gpx')
+ temp_out_path = os.path.join(temp_dir, 'temp_out_filename.gpx')
+ shutil.copy2(input_name, temp_path)
+
cli_args = [settings.GPSBABEL, '-i']
if self.file_type == 'K':
cli_args.append('kml')
elif self.file_type == 'G':
cli_args.append('gpx')
- cli_args += ['-f', input_name, '-x', settings.GPSBABEL_OPTIONS,
- '-o', 'gpx', '-F', output_name]
- p = Popen(cli_args, stderr=PIPE)
- p.wait()
- if p.returncode:
- print(p.stderr.read())
- # logger.error(p.stderr.read())
- else:
- self.simplified_file = File(open(output_name))
- self.save()
- os.remove(output_name)
+ cli_args += ['-f', temp_path, '-x',
+ settings.GPSBABEL_OPTIONS, '-o', 'gpx', '-F',
+ temp_out_path]
+ with Popen(cli_args, stderr=PIPE) as p:
+ p.wait()
+ if p.returncode:
+ print(p.stderr.read())
+ # logger.error(p.stderr.read())
+ else:
+ output_name = settings.MEDIA_ROOT + self.raw_file.name[:-4] + \
+ "_simplified" + ".gpx"
+ shutil.copy2(temp_out_path, output_name)
+ self.simplified_file = File(open(output_name))
+ self.save()
+ os.remove(output_name)
+ os.remove(temp_out_path)
+ os.remove(temp_path)
@property
def route(self):