Skip to content

Commit

Permalink
Merge PR #55 into master
Browse files Browse the repository at this point in the history
Signed-off-by legalsylvain
  • Loading branch information
OCA-git-bot committed Jul 14, 2021
2 parents 97a4488 + 39e725e commit c32d5a2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
8 changes: 7 additions & 1 deletion odoo_module_migrate/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ def get_parser():
" the changes. (using git add and git commit command)"
)

# TODO: Move to `argparse.BooleanOptionalAction` once in Python 3.9+
main_parser.add_argument(
"-npc", "--no-pre-commit", dest="pre_commit", action="store_false",
help="Skip pre-commit execution",
)

return main_parser


Expand All @@ -124,7 +130,7 @@ def main(args=False):
migration = Migration(
args.directory, args.init_version_name, args.target_version_name,
module_names, args.format_patch, args.remote_name,
not args.no_commit,
not args.no_commit, args.pre_commit,
)

# run Migration
Expand Down
20 changes: 19 additions & 1 deletion odoo_module_migrate/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ class Migration:
def __init__(
self, relative_directory_path, init_version_name, target_version_name,
module_names=None, format_patch=False, remote_name='origin',
commit_enabled=True,
commit_enabled=True, pre_commit=True,
):
if not module_names:
module_names = []
self._commit_enabled = commit_enabled
self._pre_commit = pre_commit
self._migration_steps = []
self._migration_scripts = []
self._module_migrations = []
Expand Down Expand Up @@ -91,9 +92,26 @@ def __init__(
for module_name in module_names:
self._module_migrations.append(ModuleMigration(self, module_name))

if os.path.exists(".pre-commit-config.yaml") and self._pre_commit:
self._run_pre_commit(module_names)

# get migration scripts, depending to the migration list
self._get_migration_scripts()

def _run_pre_commit(self, module_names):
logger.info("Run pre-commit")
_execute_shell(
"pre-commit run -a", path=self._directory_path, raise_error=False)
if self._commit_enabled:
logger.info("Stage and commit changes done by pre-commit")
_execute_shell("git add -A", path=self._directory_path)
_execute_shell(
"git commit -m '[IMP] %s: pre-commit execution' --no-verify"
% ", ".join(module_names),
path=self._directory_path,
raise_error=False, # Don't fail if there is nothing to commit
)

def _is_module_path(self, module_path):
return any([(module_path / x).exists() for x in _MANIFEST_NAMES])

Expand Down
7 changes: 5 additions & 2 deletions odoo_module_migrate/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ def _get_latest_version_code():
return _AVAILABLE_MIGRATION_STEPS[-1]["target_version_code"]


def _execute_shell(shell_command, path=False):
def _execute_shell(shell_command, path=False, raise_error=True):
if path:
shell_command = "cd %s && %s" % (str(path.resolve()), shell_command)
logger.debug("Execute Shell:\n%s" % (shell_command))
return subprocess.check_output(shell_command, shell=True)
if raise_error:
return subprocess.check_output(shell_command, shell=True)
else:
return subprocess.run(shell_command, shell=True)


def _read_content(file_path):
Expand Down

0 comments on commit c32d5a2

Please sign in to comment.