From 0511eed7b269f292f8542e40cc6de8ffa50df0f0 Mon Sep 17 00:00:00 2001
From: Goutte <antoine.goutenoir@gmail.com>
Date: Tue, 1 Aug 2017 12:05:47 +0200
Subject: [PATCH] Tarball generation seems to work okay.

---
 web/run.py | 51 ++++++++++++---------------------------------------
 1 file changed, 12 insertions(+), 39 deletions(-)

diff --git a/web/run.py b/web/run.py
index 85039a7..99386f2 100755
--- a/web/run.py
+++ b/web/run.py
@@ -435,8 +435,8 @@ def get_target_csv(source, started_at, stopped_at):
     return send_from_directory(get_path("../cache/"), filename)
 
 
-@app.route("/<targets>_<started_at>_<stopped_at>.zip")
-def download_targets_zip(targets, started_at, stopped_at):
+@app.route("/<targets>_<started_at>_<stopped_at>.tar.gz")
+def download_targets_tarball(targets, started_at, stopped_at):
     """
     Grab data and orbit data for the specified `target`,
     rearrange it and return it as a CSV file.
@@ -444,12 +444,10 @@ def download_targets_zip(targets, started_at, stopped_at):
 
     targets: string list of targets' slugs, separated by `-`.
     This will fail hard if targets' slugs start having `-` in them.
-
-    toreview
-
     """
     separator = '-'
-    targets = targets.split(separator).sort()
+    targets = targets.split(separator)
+    targets.sort()
     targets_configs = []
     for target in targets:
         if not target:
@@ -467,34 +465,16 @@ def download_targets_zip(targets, started_at, stopped_at):
         stopped_at = datetime.datetime.strptime(stopped_at, date_fmt)
     except:
         abort(400, "Invalid stopped_at parameter : '%s'." % stopped_at)
+    sta = started_at.strftime(date_fmt)
+    sto = stopped_at.strftime(date_fmt)
 
-    gzip_filename = "%s_%s_%s.tar.gz" % (separator.join(targets),
-                                         started_at.strftime(date_fmt),
-                                         stopped_at.strftime(date_fmt))
+    gzip_filename = "%s_%s_%s.tar.gz" % (separator.join(targets), sta, sto)
     local_gzip_file = get_path("../cache/%s" % gzip_filename)
 
     if not isfile(local_gzip_file):
-        log.debug("Creating tarball '%s'..." % local_gzip_file)
-        # success = True
-        # try:
-        #     with gzip.open(local_gzip_file, 'rb') as f:
-        #         file_content = f.read()
-        #         with open(local_netc_file, 'w+b') as g:
-        #             g.write(file_content)
-        # except Exception as e:
-        #     success = False
-        #     log.warning("Cannot process gz file '%s' from '%s' : %s" %
-        #                 (local_gzip_file, url, e))
-        # if success:
-        #     log.debug("Unzipped '%s'." % local_gzip_file)
-
-        log.debug("Creating the CSV files themselves...")
+        log.debug("Creating the CSV files for the tarball...")
         for target_config in targets_configs:
-            # get_target_csv(target_config['slug'], started_at.strftime(date_fmt), stopped_at.strftime(date_fmt))
-
-            filename = "%s_%s_%s.csv" % (target_config['slug'],
-                                         started_at.strftime(date_fmt),
-                                         stopped_at.strftime(date_fmt))
+            filename = "%s_%s_%s.csv" % (target_config['slug'], sta, sto)
             local_csv_file = get_path("../cache/%s" % filename)
             if not isfile(local_csv_file):
                 with open(local_csv_file, mode="w+") as f:
@@ -502,22 +482,15 @@ def download_targets_zip(targets, started_at, stopped_at):
                                                   started_at=started_at,
                                                   stopped_at=stopped_at))
 
-        # tar_filename = "%s_%s_%s.tar" % (separator.join(targets),
-        #                                  started_at.strftime(date_fmt),
-        #                                  stopped_at.strftime(date_fmt))
-        # tar_file = get_path("../cache/%s" % tar_filename)
-
-        log.debug("Make the tarball '%s'..." % local_gzip_file)
+        log.debug("Creating the tarball '%s'..." % local_gzip_file)
         with tarfile.open(local_gzip_file, "w:gz") as tar:
             for target_config in targets_configs:
-                filename = "%s_%s_%s.csv" % (target_config['slug'],
-                                             started_at.strftime(date_fmt),
-                                             stopped_at.strftime(date_fmt))
+                filename = "%s_%s_%s.csv" % (target_config['slug'], sta, sto)
                 local_csv_file = get_path("../cache/%s" % filename)
                 tar.add(local_csv_file, arcname=filename)
 
     if not isfile(local_gzip_file):
-        abort(500, "Could not cache tarball at '%s'." % local_gzip_file)
+        abort(500, "No tarball to serve. Looked at '%s'." % local_gzip_file)
 
     return send_from_directory(get_path("../cache/"), gzip_filename)
 
--
libgit2 0.21.2