Skip to content

Commit

Permalink
Merge pull request #554 from petrjasek/fix-data-updates-mkdir
Browse files Browse the repository at this point in the history
fix(updates): avoid creating of directories in get_data_update_files
  • Loading branch information
ioanpocol committed Sep 6, 2016
2 parents 7c77d1e + df0379f commit 580c56e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 3 additions & 1 deletion superdesk/commands/data_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def get_data_updates_files(strip_file_extension=False):
# create folder if doens't exist
for folder in get_dirs():
if not os.path.exists(folder):
os.makedirs(folder)
continue
# list all files from data updates directory
if os.path.exists(folder):
files += [f for f in os.listdir(folder) if os.path.isfile(os.path.join(folder, f))]
Expand Down Expand Up @@ -242,6 +242,8 @@ def run(self, resource_name, global_update=False):
update_dir = MAIN_DATA_UPDATES_DIR
else:
update_dir = get_dirs(only_relative_folder=True)[0]
if not os.path.exists(update_dir):
os.makedirs(update_dir)
data_update_filename = os.path.join(update_dir, '{:05d}_{}_{}.py'.format(name_id, timestamp, resource_name))
if os.path.exists(data_update_filename):
raise Exception('The file "%s" already exists' % (data_update_filename))
Expand Down
16 changes: 14 additions & 2 deletions tests/data_updates_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import os
import shutil
import tempfile

from superdesk.tests import TestCase
import superdesk.commands.data_updates
import superdesk
from superdesk.commands.data_updates import get_data_updates_files, GenerateUpdate, Upgrade, Downgrade
import shutil
import os

# change the folder where to store updates for test purpose
DEFAULT_DATA_UPDATE_DIR_NAME = '/tmp/data_updates'
Expand Down Expand Up @@ -34,6 +36,16 @@ def test_data_update_generation(self):
GenerateUpdate().run(resource_name='RESOURNCE_NAME')
assert len(get_data_updates_files()) is 2, get_data_updates_files()

def test_data_update_generation_create_updates_dir(self):
updates_dir = tempfile.mkdtemp()
shutil.rmtree(updates_dir)
self.assertFalse(os.path.exists(updates_dir))
self.app.config['DATA_UPDATES_PATH'] = updates_dir
GenerateUpdate().run('tmp')
print(updates_dir)
self.assertTrue(os.path.exists(updates_dir))
shutil.rmtree(updates_dir)

def number_of_data_updates_applied(self):
return superdesk.get_resource_service('data_updates').find({}).count()

Expand Down

0 comments on commit 580c56e

Please sign in to comment.