Skip to content

Commit 874c7e4

Browse files
committed
fixup! [IMP] snippets: preserve same file module
1 parent 1e7abe7 commit 874c7e4

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

src/util/misc.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44
import collections
55
import datetime
66
import functools
7+
import hashlib
8+
import inspect
79
import logging
810
import os
911
import re
12+
import sys
1013
import textwrap
1114
import uuid
1215
from contextlib import contextmanager
1316
from itertools import chain, islice
1417

18+
from .exceptions import MigrationError
19+
1520
try:
1621
from odoo import release
1722
from odoo.modules.module import get_module_path
@@ -388,6 +393,30 @@ def log(chunk_num, size=chunk_size):
388393
log(i // chunk_size + 1, i % chunk_size)
389394

390395

396+
def make_pickleable_callback(callback):
397+
"""
398+
.. noautodoc::
399+
Make a callable importable.
400+
401+
`ProcessPoolExecutor.map` arguments needs to be pickleable
402+
Functions can only be pickled if they are importable.
403+
However, the callback's file is not importable due to the dash in the filename.
404+
We should then put the executed function in its own importable file.
405+
"""
406+
callback_filepath = inspect.getfile(callback)
407+
name = "_upgrade_" + hashlib.sha256(callback_filepath.encode()).hexdigest()
408+
if name not in sys.modules:
409+
sys.modules[name] = import_script(callback_filepath, name=name)
410+
try:
411+
return getattr(sys.modules[name], callback.__name__)
412+
except AttributeError:
413+
error_msg = (
414+
"The converter callback `{}` is a nested function in `{}`.\n"
415+
"Move it outside the `migrate()` function to make it top-level."
416+
).format(callback.__name__, callback.__module__)
417+
raise MigrationError(error_msg) from None
418+
419+
391420
class SelfPrint(object):
392421
"""
393422
Class that will return a self representing string. Used to evaluate domains.

src/util/snippets.py

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# -*- coding: utf-8 -*-
2-
import hashlib
3-
import inspect
42
import logging
53
import re
6-
import sys
74
from concurrent.futures import ProcessPoolExecutor
85

96
from lxml import etree, html
@@ -12,9 +9,8 @@
129
from psycopg2.extras import Json
1310

1411
from .const import NEARLYWARN
15-
from .exceptions import MigrationError
1612
from .helpers import table_of_model
17-
from .misc import import_script, log_progress
13+
from .misc import log_progress, make_pickleable_callback
1814
from .pg import column_exists, column_type, get_max_workers, table_exists
1915

2016
_logger = logging.getLogger(__name__)
@@ -161,29 +157,6 @@ def html_converter(transform_callback, selector=None):
161157
return HTMLConverter(make_pickleable_callback(transform_callback), selector)
162158

163159

164-
def make_pickleable_callback(callback):
165-
"""
166-
Make a callable importable.
167-
168-
`ProcessPoolExecutor.map` arguments needs to be pickleable
169-
Functions can only be pickled if they are importable.
170-
However, the callback's file is not importable due to the dash in the filename.
171-
We should then put the executed function in its own importable file.
172-
"""
173-
callback_filepath = inspect.getfile(callback)
174-
name = f"_upgrade_{hashlib.sha256(callback_filepath.encode()).hexdigest()}"
175-
if name not in sys.modules:
176-
sys.modules[name] = import_script(callback_filepath, name=name)
177-
try:
178-
return getattr(sys.modules[name], callback.__name__)
179-
except AttributeError:
180-
error_msg = (
181-
f"The converter callback `{callback.__name__}` is a nested function in `{callback.__module__}`.\n"
182-
"Move it outside the `migrate()` function to make it top-level."
183-
)
184-
raise MigrationError(error_msg) from None
185-
186-
187160
class BaseConverter:
188161
def __init__(self, callback, selector=None):
189162
self.callback = callback

0 commit comments

Comments
 (0)