Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch for reading mepo1 state #279

Merged
merged 2 commits into from
May 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/mepo/state/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from mepo.config.config_file import ConfigFile
from mepo.state.component import MepoComponent
from mepo.utilities import shellcmd
from mepo.utilities import colors
from mepo.state.exceptions import StateDoesNotExistError, StateAlreadyInitializedError

class MepoState(object):
Expand Down Expand Up @@ -71,12 +72,32 @@ def initialize(cls, project_config_file, directory_style):
complist.append(MepoComponent().to_component(name, comp, directory_style))
cls.write_state(complist)

@staticmethod
def __mepo1_patch():
"""
mepo1 to mepo2 includes reanming of directories
mepo.d/state/state.py -> src/mepo/state/state.py
Since pickle requires that "the class definition must be importable
and live in the same module as when the object was stored", we need to
patch sys.modules to be able to read mepo1 state
"""
print(colors.YELLOW + 'Converting mepo1 state to mepo2 state' + colors.RESET)
import mepo.state
import mepo.utilities
sys.modules['state'] = mepo.state
sys.modules['utilities'] = mepo.utilities

@classmethod
def read_state(cls):
if not cls.exists():
raise StateDoesNotExistError('Error! mepo state does not exist')
with open(cls.get_file(), 'rb') as fin:
allcomps = pickle.load(fin)
try:
with open(cls.get_file(), 'rb') as fin:
allcomps = pickle.load(fin)
except ModuleNotFoundError:
cls.__mepo1_patch()
with open(cls.get_file(), 'rb') as fin:
allcomps = pickle.load(fin)
return allcomps

@classmethod
Expand Down
Loading