Skip to content

Commit

Permalink
Merge 4e9b364 into a9974dd
Browse files Browse the repository at this point in the history
  • Loading branch information
flachica committed Aug 15, 2020
2 parents a9974dd + 4e9b364 commit 004b32c
Show file tree
Hide file tree
Showing 11 changed files with 519 additions and 402 deletions.
170 changes: 170 additions & 0 deletions odoo_module_migrate/base_migration_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import os
from .config import _ALLOWED_EXTENSIONS
from .tools import _execute_shell
from .log import logger
from . import tools
import re
import pathlib


class BaseMigrationScript(object):
_TEXT_REPLACES = {}
_TEXT_ERRORS = {}
_DEPRECATED_MODULES = []
_FILE_RENAMES = {}
_GLOBAL_FUNCTIONS = {}
_module_path = ''

def __init__(self):
pass

def run(self,
module_path,
manifest_path,
module_name,
migration_steps,
directory_path,
commit_enabled):
logger.debug('Running %s script' % self.name)
file_renames = self._FILE_RENAMES or {}
text_replaces = self._TEXT_REPLACES or {}
text_errors = self._TEXT_ERRORS or {}
global_functions = self._GLOBAL_FUNCTIONS or {}
deprecated_modules = self._DEPRECATED_MODULES or {}

current_manifest_file_name = manifest_path.as_posix().split('/')[-1]
if current_manifest_file_name in file_renames:
new_manifest_file_name = manifest_path.as_posix().replace(
current_manifest_file_name,
file_renames[current_manifest_file_name]
)
manifest_path = pathlib.Path(new_manifest_file_name)

for root, directories, filenames in os.walk(module_path.resolve()):
for filename in filenames:
# Skip useless file
# TODO, skip files present in some folders. (for exemple 'lib')
extension = os.path.splitext(filename)[1]
if extension not in _ALLOWED_EXTENSIONS:
continue

absolute_file_path = os.path.join(root, filename)
logger.debug("Migrate '%s' file" % absolute_file_path)

# Rename file, if required
new_name = file_renames.get(filename)
if new_name:
self._rename_file(
directory_path,
absolute_file_path,
os.path.join(root, new_name),
commit_enabled
)
absolute_file_path = os.path.join(root, new_name)

# Operate changes in the file (replacements, removals)
replaces = text_replaces.get("*", {})
replaces.update(text_replaces.get(extension, {}))

new_text = tools._replace_in_file(
absolute_file_path, replaces,
"Change file content of %s" % filename)

# Display errors if the new content contains some obsolete
# pattern
errors = text_errors.get("*", {})
errors.update(text_errors.get(extension, {}))
for pattern, error_message in errors.items():
if re.findall(pattern, new_text):
logger.error(error_message)

# Handle deprecated modules
current_manifest_text = tools._read_content(manifest_path)
new_manifest_text = current_manifest_text
for items in deprecated_modules:
old_module, action = items[0:2]
new_module = len(items) > 2 and items[2]
old_module_pattern = r"('|\"){0}('|\")".format(old_module)
if new_module:
new_module_pattern = r"('|\"){0}('|\")".format(new_module)
replace_pattern = r"\1{0}\2".format(new_module)

if not re.findall(old_module_pattern, new_manifest_text):
continue

if action == 'removed':
# The module has been removed, just log an error.
logger.error(
"Depends on removed module '%s'" % (old_module))

elif action == 'renamed':
new_manifest_text = re.sub(
old_module_pattern, replace_pattern, new_manifest_text)
logger.info(
"Replaced dependency of '%s' by '%s'." % (
old_module, new_module))

elif action == 'oca_moved':
new_manifest_text = re.sub(
old_module_pattern, replace_pattern, new_manifest_text)
logger.warning(
"Replaced dependency of '%s' by '%s' (%s)\n"
"Check that '%s' is available on your system." % (
old_module, new_module, items[3], new_module))

elif action == "merged":
if not re.findall(new_module_pattern, new_manifest_text):
# adding dependency of the merged module
new_manifest_text = re.sub(
old_module_pattern, replace_pattern, new_manifest_text)
logger.info(
"'%s' merged in '%s'. Replacing dependency." % (
old_module, new_module))
else:
# TODO, improve me. we should remove the dependency
# but it could generate coma trouble.
# maybe handling this treatment by ast lib could fix
# the problem.
logger.error(
"'%s' merged in '%s'. You should remove the"
" dependency to '%s' manually." % (
old_module, new_module, old_module))

if current_manifest_text != new_manifest_text:
tools._write_content(manifest_path, new_manifest_text)

if global_functions:
for function in global_functions:
function(
logger=logger,
module_path=module_path,
module_name=module_name,
manifest_path=manifest_path,
migration_steps=migration_steps,
tools=tools,
)

def _rename_file(self,
module_path,
old_file_path,
new_file_path,
commit_enabled):
"""
Rename a file. try to execute 'git mv', to avoid huge diff.
if 'git mv' fails, make a classical rename
"""
logger.info(
"Renaming file: '%s' by '%s' " % (
old_file_path.replace(str(module_path.resolve()), ""),
new_file_path.replace(str(module_path.resolve()), ""))
)
if commit_enabled:
_execute_shell(
"git mv %s %s" % (old_file_path, new_file_path),
path=module_path)
else:
_execute_shell(
"mv %s %s" % (old_file_path, new_file_path),
path=module_path
)
17 changes: 13 additions & 4 deletions odoo_module_migrate/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,11 @@ def _get_code_from_previous_branch(self, module_name, remote_name):
def _get_migration_scripts(self):

# Add the script that will be allways executed
self._migration_scripts.append(importlib.import_module(
"odoo_module_migrate.migration_scripts.migrate_allways"))
module = importlib.import_module(
"odoo_module_migrate.migration_scripts.migrate_allways")
migrationScript = getattr(module, "MigrationScript")()
migrationScript.name = module.__file__.split('/')[-1].split('.')[-2]
self._migration_scripts.append(migrationScript)

all_packages = importlib.import_module(
"odoo_module_migrate.migration_scripts")
Expand Down Expand Up @@ -155,12 +158,18 @@ def _get_migration_scripts(self):
if script_start >= migration_end or script_end <= migration_start:
continue

self._migration_scripts.append(importlib.import_module(full_name))
module = importlib.import_module(full_name)
migrationScript = getattr(module, "MigrationScript")()
migrationScript.name = module.\
__file__.\
split('/')[-1].\
split('.')[-2]
self._migration_scripts.append(migrationScript)

logger.debug(
"The following migration script will be"
" executed:\n- %s" % '\n- '.join(
[x.__file__.split('/')[-1] for x in self._migration_scripts]))
[x.name for x in self._migration_scripts]))

def run(self):
logger.debug(
Expand Down
124 changes: 66 additions & 58 deletions odoo_module_migrate/migration_scripts/migrate_080_090.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,71 @@
# Copyright (C) 2019 - Today: GRAP (http://www.grap.coop)
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo_module_migrate.base_migration_script import BaseMigrationScript

_TEXT_REPLACES = {
".py": {"select=True": "index=True"},
}

_DEPRECATED_MODULES = [
("account_analytic_analysis", "oca_moved", "contract",
"Moved to OCA/contract"),
("account_analytic_plans", "oca_moved", "account_analytic_distribution",
"Moved to OCA/account_analytic"),
("account_anglo_saxon", "removed"),
("account_bank_statement_extensions", "removed"),
("account_chart", "merged", "account"),
("account_check_writing", "renamed", "account_check_printing"),
("account_followup", "removed"),
("account_payment", "oca_moved", "account_payment_order",
"Moved to OCA/bank-payment"),
("account_sequence", "removed"),
("analytic_contract_hr_expense", "removed"),
("analytic_user_function", "removed"),
("anglo_saxon_dropshipping", "removed"),
("auth_openid", "removed"),
("base_report_designer", "removed"),
("contacts", "merged", "mail"),
("crm_helpdesk", "removed"),
("crm_mass_mailing", "removed"),
("crm_profiling", "removed"),
("edi", "removed"),
("email_template", "merged", "mail_template"),
("hr_applicant_document", "removed"),
("hr_timesheet_invoice", "removed"),
("im_chat", "merged", "mail"),
("knowledge", "oca_moved", "knowledge", "Moved to OCA/knowledge"),
("l10n_be_coda", "removed"),
("l10n_fr_rib", "removed"),
("marketing_crm", "merged", "crm"),
("multi_company", "removed"),
("portal_claim", "renamed", "website_crm_claim"),
("portal_project", "merged", "project"),
("portal_project_issue", "merged", "project_issue"),
("procurement_jit_stock", "merged", "procurement_jit"),
("purchase_analytic_plans", "oca_moved", "purchase_analytic_distribution",
"Moved to OCA/account-analytic"),
("purchase_double_validation", "removed"),
("sale_analytic_plans", "oca_moved", "sale_analytic_distribution",
"Moved to OCA/account-analytic"),
("sale_journal", "removed"),
("share", "removed"),
("stock_invoice_directly", "removed"),
("web_api", "removed"),
("web_gantt", "merged", "web"),
("web_graph", "merged", "web"),
("web_kanban_sparkline", "merged", "web"),
("web_tests", "merged", "web"),
("web_tests_demo", "removed"),
("website_certification", "removed"),
("website_instantclick", "removed"),
("website_mail_group", "renamed", "website_mail_channel"),
("website_report", "merged", "report"),
]
class MigrationScript(BaseMigrationScript):

def __init__(self):
self._TEXT_REPLACES = {
".py": {"select=True": "index=True"},
}

self._DEPRECATED_MODULES = [
("account_analytic_analysis", "oca_moved", "contract",
"Moved to OCA/contract"),
("account_analytic_plans", "oca_moved",
"account_analytic_distribution",
"Moved to OCA/account_analytic"),
("account_anglo_saxon", "removed"),
("account_bank_statement_extensions", "removed"),
("account_chart", "merged", "account"),
("account_check_writing", "renamed", "account_check_printing"),
("account_followup", "removed"),
("account_payment", "oca_moved", "account_payment_order",
"Moved to OCA/bank-payment"),
("account_sequence", "removed"),
("analytic_contract_hr_expense", "removed"),
("analytic_user_function", "removed"),
("anglo_saxon_dropshipping", "removed"),
("auth_openid", "removed"),
("base_report_designer", "removed"),
("contacts", "merged", "mail"),
("crm_helpdesk", "removed"),
("crm_mass_mailing", "removed"),
("crm_profiling", "removed"),
("edi", "removed"),
("email_template", "merged", "mail_template"),
("hr_applicant_document", "removed"),
("hr_timesheet_invoice", "removed"),
("im_chat", "merged", "mail"),
("knowledge", "oca_moved", "knowledge", "Moved to OCA/knowledge"),
("l10n_be_coda", "removed"),
("l10n_fr_rib", "removed"),
("marketing_crm", "merged", "crm"),
("multi_company", "removed"),
("portal_claim", "renamed", "website_crm_claim"),
("portal_project", "merged", "project"),
("portal_project_issue", "merged", "project_issue"),
("procurement_jit_stock", "merged", "procurement_jit"),
("purchase_analytic_plans",
"oca_moved",
"purchase_analytic_distribution",
"Moved to OCA/account-analytic"),
("purchase_double_validation", "removed"),
("sale_analytic_plans", "oca_moved", "sale_analytic_distribution",
"Moved to OCA/account-analytic"),
("sale_journal", "removed"),
("share", "removed"),
("stock_invoice_directly", "removed"),
("web_api", "removed"),
("web_gantt", "merged", "web"),
("web_graph", "merged", "web"),
("web_kanban_sparkline", "merged", "web"),
("web_tests", "merged", "web"),
("web_tests_demo", "removed"),
("website_certification", "removed"),
("website_instantclick", "removed"),
("website_mail_group", "renamed", "website_mail_channel"),
("website_report", "merged", "report"),
]
23 changes: 15 additions & 8 deletions odoo_module_migrate/migration_scripts/migrate_080_allways.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# Copyright (C) 2019 - Today: GRAP (http://www.grap.coop)
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo_module_migrate.base_migration_script import BaseMigrationScript

_FILE_RENAMES = {"__openerp__.py": "__manifest__.py"}

_TEXT_REPLACES = {
".py": {"from openerp": "from odoo", "import openerp": "import odoo"},
".xml": {
r"( |\t)*<openerp>(\n| |\t)*<data>": "<odoo>",
r"( |\t)*<\/data>(\n| |\t)*<\/openerp>": "</odoo>",
}
}
class MigrationScript(BaseMigrationScript):

def __init__(self):
self._FILE_RENAMES = {"__openerp__.py": "__manifest__.py"}

self._TEXT_REPLACES = {
".py": {
"from openerp": "from odoo", "import openerp": "import odoo"
},
".xml": {
r"( |\t)*<openerp>(\n| |\t)*<data>": "<odoo>",
r"( |\t)*<\/data>(\n| |\t)*<\/openerp>": "</odoo>",
}
}
Loading

0 comments on commit 004b32c

Please sign in to comment.