/
migrate.py
83 lines (64 loc) · 2.47 KB
/
migrate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# coding: utf-8
from __future__ import unicode_literals
import os
import shutil
from ..gotomagic.text import GotoError, GotoWarning
from ..gotomagic.utils import detect_unmigrated_data, prompt_to_migrate_data, list_jfiles, create_project_folder
from .. import settings
def migrate(magic, command, args, options):
"""
migrate magicwords.
"""
if not detect_unmigrated_data():
return "Nothing to migrate", None
elif '--check-migrate' in command:
return None, GotoWarning('data_not_migrated')
if prompt_to_migrate_data():
return migrate_data()
else:
return None, GotoWarning('goto_wont_work_without_migrating_data')
def migrate_data():
''' Migrating data
goto format:
from v0 - 1.5.x
to v1.6.0 and above
'''
GOTOPATH = settings.GOTOPATH
projects_folder = os.path.join(GOTOPATH, 'projects')
jfiles = list_jfiles()
migrations = 0
projects = []
unmigrated_projects = []
output = ['Found {} projects to migrate'.format(len(jfiles))]
warning = None
for jfile in jfiles:
project = jfile.split('.json')[0]
from_file = os.path.join(projects_folder, jfile)
destination = os.path.join(projects_folder, project, 'private', 'magicwords.json') # noqa
# project cmd used to leave empty files for each project
stubfile = os.path.join(projects_folder, project)
if os.path.isfile(stubfile):
os.remove(stubfile)
create_project_folder(project)
if move_magicwords(from_file, destination):
migrations += 1
projects += [project]
else:
unmigrated_projects += [project]
output.append('Skipping project {} (source file: {} destination file already exists: {}'.format(project, from_file, destination)) # noqa
output.append('{} of {} projects were migrated'.format(migrations, len(jfiles))) # noqa
output.append('Migrated projects: {}'.format(" ".join(projects))) # noqa
if not migrations == len(jfiles):
warning = GotoWarning('not_all_projects_migrated', projects=" ".join(unmigrated_projects)) # noqa
output = '\n'.join(output)
return output, warning
def move_magicwords(from_file, destination):
try:
if not os.path.exists(destination):
shutil.move(from_file, destination)
return True
else:
return False
except shutil.Error:
# todo: user-friendly warning here.
raise