john-bodley closed pull request #4829: [import csv] ensure directory exists before saving csv file URL: https://github.com/apache/incubator-superset/pull/4829
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/superset/utils.py b/superset/utils.py index d465bb086c..2d06c48f22 100644 --- a/superset/utils.py +++ b/superset/utils.py @@ -12,6 +12,7 @@ from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.utils import formatdate +import errno import functools import json import logging @@ -827,3 +828,11 @@ def is_adhoc_metric(metric): def get_metric_names(metrics): return [metric['label'] if is_adhoc_metric(metric) else metric for metric in metrics] + + +def ensure_path_exists(path): + try: + os.makedirs(path) + except OSError as exc: + if not (os.path.isdir(path) and exc.errno == errno.EEXIST): + raise diff --git a/superset/views/core.py b/superset/views/core.py index 19885dcee3..f66b70ef55 100755 --- a/superset/views/core.py +++ b/superset/views/core.py @@ -347,15 +347,17 @@ def form_post(self, form): csv_file = form.csv_file.data form.csv_file.data.filename = secure_filename(form.csv_file.data.filename) csv_filename = form.csv_file.data.filename + path = os.path.join(config['UPLOAD_FOLDER'], csv_filename) try: - csv_file.save(os.path.join(config['UPLOAD_FOLDER'], csv_filename)) + utils.ensure_path_exists(config['UPLOAD_FOLDER']) + csv_file.save(path) table = SqlaTable(table_name=form.name.data) table.database = form.data.get('con') table.database_id = table.database.id table.database.db_engine_spec.create_table_from_csv(form, table) except Exception as e: try: - os.remove(os.path.join(config['UPLOAD_FOLDER'], csv_filename)) + os.remove(path) except OSError: pass message = 'Table name {} already exists. Please pick another'.format( @@ -365,7 +367,7 @@ def form_post(self, form): 'danger') return redirect('/csvtodatabaseview/form') - os.remove(os.path.join(config['UPLOAD_FOLDER'], csv_filename)) + os.remove(path) # Go back to welcome page / splash screen db_name = table.database.database_name message = _('CSV file "{0}" uploaded to table "{1}" in ' ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services