Skip to content

[IMP] snippets: preserve same file module #295

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
30 changes: 29 additions & 1 deletion src/util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import collections
import datetime
import functools
import hashlib
import inspect
import logging
import os
import re
import sys
import textwrap
import uuid
from contextlib import contextmanager
Expand All @@ -21,7 +24,7 @@
from openerp.modules.module import get_module_path
from openerp.tools.parse_version import parse_version

from .exceptions import SleepyDeveloperError
from .exceptions import MigrationError, SleepyDeveloperError

# python3 shim
try:
Expand Down Expand Up @@ -388,6 +391,31 @@ def log(chunk_num, size=chunk_size):
log(i // chunk_size + 1, i % chunk_size)


def make_pickleable_callback(callback):
"""
Make a callable importable.

`ProcessPoolExecutor.map` arguments needs to be pickleable
Functions can only be pickled if they are importable.
However, the callback's file is not importable due to the dash in the filename.
We should then put the executed function in its own importable file.

:meta private: exclude from online docs
"""
callback_filepath = inspect.getfile(callback)
name = "_upgrade_" + hashlib.sha256(callback_filepath.encode()).hexdigest()
if name not in sys.modules:
sys.modules[name] = import_script(callback_filepath, name=name)
try:
return getattr(sys.modules[name], callback.__name__)
except AttributeError:
error_msg = (
"The converter callback `{}` is a nested function in `{}`.\n"
"Move it outside the `migrate()` function to make it top-level."
).format(callback.__name__, callback.__module__)
raise MigrationError(error_msg)


class SelfPrint(object):
"""
Class that will return a self representing string. Used to evaluate domains.
Expand Down
28 changes: 1 addition & 27 deletions src/util/snippets.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# -*- coding: utf-8 -*-
import inspect
import logging
import re
import sys
import uuid
from concurrent.futures import ProcessPoolExecutor

from lxml import etree, html
Expand All @@ -12,9 +9,8 @@
from psycopg2.extras import Json

from .const import NEARLYWARN
from .exceptions import MigrationError
from .helpers import table_of_model
from .misc import import_script, log_progress
from .misc import log_progress, make_pickleable_callback
from .pg import column_exists, column_type, get_max_workers, table_exists

_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -161,28 +157,6 @@ def html_converter(transform_callback, selector=None):
return HTMLConverter(make_pickleable_callback(transform_callback), selector)


def make_pickleable_callback(callback):
"""
Make a callable importable.

`ProcessPoolExecutor.map` arguments needs to be pickleable
Functions can only be pickled if they are importable.
However, the callback's file is not importable due to the dash in the filename.
We should then put the executed function in its own importable file.
"""
callback_filepath = inspect.getfile(callback)
name = f"_upgrade_{uuid.uuid4().hex}"
mod = sys.modules[name] = import_script(callback_filepath, name=name)
try:
return getattr(mod, callback.__name__)
except AttributeError:
error_msg = (
f"The converter callback `{callback.__name__}` is a nested function in `{callback.__module__}`.\n"
"Move it outside the `migrate()` function to make it top-level."
)
raise MigrationError(error_msg) from None


class BaseConverter:
def __init__(self, callback, selector=None):
self.callback = callback
Expand Down