Commit 0511eed7b269f292f8542e40cc6de8ffa50df0f0

Authored by Goutte
1 parent 2fedd73b

Tarball generation seems to work okay.

Showing 1 changed file with 12 additions and 39 deletions   Show diff stats
@@ -435,8 +435,8 @@ def get_target_csv(source, started_at, stopped_at): @@ -435,8 +435,8 @@ def get_target_csv(source, started_at, stopped_at):
435 return send_from_directory(get_path("../cache/"), filename) 435 return send_from_directory(get_path("../cache/"), filename)
436 436
437 437
438 -@app.route("/<targets>_<started_at>_<stopped_at>.zip")  
439 -def download_targets_zip(targets, started_at, stopped_at): 438 +@app.route("/<targets>_<started_at>_<stopped_at>.tar.gz")
  439 +def download_targets_tarball(targets, started_at, stopped_at):
440 """ 440 """
441 Grab data and orbit data for the specified `target`, 441 Grab data and orbit data for the specified `target`,
442 rearrange it and return it as a CSV file. 442 rearrange it and return it as a CSV file.
@@ -444,12 +444,10 @@ def download_targets_zip(targets, started_at, stopped_at): @@ -444,12 +444,10 @@ def download_targets_zip(targets, started_at, stopped_at):
444 444
445 targets: string list of targets' slugs, separated by `-`. 445 targets: string list of targets' slugs, separated by `-`.
446 This will fail hard if targets' slugs start having `-` in them. 446 This will fail hard if targets' slugs start having `-` in them.
447 -  
448 - toreview  
449 -  
450 """ 447 """
451 separator = '-' 448 separator = '-'
452 - targets = targets.split(separator).sort() 449 + targets = targets.split(separator)
  450 + targets.sort()
453 targets_configs = [] 451 targets_configs = []
454 for target in targets: 452 for target in targets:
455 if not target: 453 if not target:
@@ -467,34 +465,16 @@ def download_targets_zip(targets, started_at, stopped_at): @@ -467,34 +465,16 @@ def download_targets_zip(targets, started_at, stopped_at):
467 stopped_at = datetime.datetime.strptime(stopped_at, date_fmt) 465 stopped_at = datetime.datetime.strptime(stopped_at, date_fmt)
468 except: 466 except:
469 abort(400, "Invalid stopped_at parameter : '%s'." % stopped_at) 467 abort(400, "Invalid stopped_at parameter : '%s'." % stopped_at)
  468 + sta = started_at.strftime(date_fmt)
  469 + sto = stopped_at.strftime(date_fmt)
470 470
471 - gzip_filename = "%s_%s_%s.tar.gz" % (separator.join(targets),  
472 - started_at.strftime(date_fmt),  
473 - stopped_at.strftime(date_fmt)) 471 + gzip_filename = "%s_%s_%s.tar.gz" % (separator.join(targets), sta, sto)
474 local_gzip_file = get_path("../cache/%s" % gzip_filename) 472 local_gzip_file = get_path("../cache/%s" % gzip_filename)
475 473
476 if not isfile(local_gzip_file): 474 if not isfile(local_gzip_file):
477 - log.debug("Creating tarball '%s'..." % local_gzip_file)  
478 - # success = True  
479 - # try:  
480 - # with gzip.open(local_gzip_file, 'rb') as f:  
481 - # file_content = f.read()  
482 - # with open(local_netc_file, 'w+b') as g:  
483 - # g.write(file_content)  
484 - # except Exception as e:  
485 - # success = False  
486 - # log.warning("Cannot process gz file '%s' from '%s' : %s" %  
487 - # (local_gzip_file, url, e))  
488 - # if success:  
489 - # log.debug("Unzipped '%s'." % local_gzip_file)  
490 -  
491 - log.debug("Creating the CSV files themselves...") 475 + log.debug("Creating the CSV files for the tarball...")
492 for target_config in targets_configs: 476 for target_config in targets_configs:
493 - # get_target_csv(target_config['slug'], started_at.strftime(date_fmt), stopped_at.strftime(date_fmt))  
494 -  
495 - filename = "%s_%s_%s.csv" % (target_config['slug'],  
496 - started_at.strftime(date_fmt),  
497 - stopped_at.strftime(date_fmt)) 477 + filename = "%s_%s_%s.csv" % (target_config['slug'], sta, sto)
498 local_csv_file = get_path("../cache/%s" % filename) 478 local_csv_file = get_path("../cache/%s" % filename)
499 if not isfile(local_csv_file): 479 if not isfile(local_csv_file):
500 with open(local_csv_file, mode="w+") as f: 480 with open(local_csv_file, mode="w+") as f:
@@ -502,22 +482,15 @@ def download_targets_zip(targets, started_at, stopped_at): @@ -502,22 +482,15 @@ def download_targets_zip(targets, started_at, stopped_at):
502 started_at=started_at, 482 started_at=started_at,
503 stopped_at=stopped_at)) 483 stopped_at=stopped_at))
504 484
505 - # tar_filename = "%s_%s_%s.tar" % (separator.join(targets),  
506 - # started_at.strftime(date_fmt),  
507 - # stopped_at.strftime(date_fmt))  
508 - # tar_file = get_path("../cache/%s" % tar_filename)  
509 -  
510 - log.debug("Make the tarball '%s'..." % local_gzip_file) 485 + log.debug("Creating the tarball '%s'..." % local_gzip_file)
511 with tarfile.open(local_gzip_file, "w:gz") as tar: 486 with tarfile.open(local_gzip_file, "w:gz") as tar:
512 for target_config in targets_configs: 487 for target_config in targets_configs:
513 - filename = "%s_%s_%s.csv" % (target_config['slug'],  
514 - started_at.strftime(date_fmt),  
515 - stopped_at.strftime(date_fmt)) 488 + filename = "%s_%s_%s.csv" % (target_config['slug'], sta, sto)
516 local_csv_file = get_path("../cache/%s" % filename) 489 local_csv_file = get_path("../cache/%s" % filename)
517 tar.add(local_csv_file, arcname=filename) 490 tar.add(local_csv_file, arcname=filename)
518 491
519 if not isfile(local_gzip_file): 492 if not isfile(local_gzip_file):
520 - abort(500, "Could not cache tarball at '%s'." % local_gzip_file) 493 + abort(500, "No tarball to serve. Looked at '%s'." % local_gzip_file)
521 494
522 return send_from_directory(get_path("../cache/"), gzip_filename) 495 return send_from_directory(get_path("../cache/"), gzip_filename)
523 496