[REF] add ensure_folder_exists system

This commit is contained in:
Sylvain LE GAL 2022-03-28 12:46:07 +02:00
parent 5c98761086
commit 507d368b30
5 changed files with 78 additions and 42 deletions

41
DEVELOP.md Normal file
View File

@ -0,0 +1,41 @@
# Requirements
TODO (poetry, etc...)
# Installation
```
git clone https://gitlab.com/odoo-openupgrade-wizard/odoo-openupgrade-wizard/
cd odoo-openupgrade-wizard
virtualenv env --python=python3.X
. ./env/bin/activate
poetry install
```
Note : ``python3.X`` should be >= to ``python3.6``
``odoo-openupgrade-wizard`` commands are now available in your virutalenv.
# Run tests
## Via pytest
This will run tests only for the current ``python3.X`` version.
(in your virtualenv)
```
poetry run pytest --cov odoo_openupgrade_wizard -v
```
## Via Tox
This will run tests for all the python versions put in the ``tox.ini`` folder.
(in your virtualenv)
```
tox
```
Note : you should have all the python versions available in your local system.
# Structure of the project

View File

@ -4,7 +4,7 @@ from pathlib import Path
import click
import yaml
from loguru import logger
from plumbum.cmd import mkdir
from tools_system import ensure_folder_exists
import odoo_openupgrade_wizard
from odoo_openupgrade_wizard.cli_build import build
@ -46,28 +46,21 @@ def main(ctx, env_folder, filestore_folder):
if not isinstance(ctx.obj, dict):
ctx.obj = {}
# Define all the folder required by the tools
env_folder_path = Path(env_folder)
src_folder_path = env_folder_path / Path("./src/")
repo_folder_path = env_folder_path / Path("./repos/")
requirement_folder_path = env_folder_path / Path("./requirements/")
script_folder_path = env_folder_path / Path("./scripts/")
config_file_path = env_folder_path / Path("config.yml")
log_folder_path = env_folder_path / Path("./log/")
if not filestore_folder:
filestore_folder_path = env_folder_path / Path("./filestore/")
else:
filestore_folder_path = Path(filestore_folder)
config_file_path = env_folder_path / Path("config.yml")
# ###
# Handle log
# ###
log_folder_path = env_folder_path / Path("./log/")
# create log directory if not exists
if not log_folder_path.exists():
logger.info("Creating folder '%s' ..." % (log_folder_path))
mkdir(["--mode", "777", log_folder_path])
# ensure log folder exists
ensure_folder_exists(log_folder_path)
# Create log file
log_file_path = log_folder_path / Path(
@ -76,6 +69,8 @@ def main(ctx, env_folder, filestore_folder):
)
)
logger.add(log_file_path)
# Add all global values in the context
ctx.obj["env_folder_path"] = env_folder_path
ctx.obj["src_folder_path"] = src_folder_path
ctx.obj["repo_folder_path"] = repo_folder_path
@ -84,6 +79,7 @@ def main(ctx, env_folder, filestore_folder):
ctx.obj["config_file_path"] = config_file_path
ctx.obj["requirement_folder_path"] = requirement_folder_path
# Load the main configuration file
if config_file_path.exists():
with open(config_file_path) as file:
config = yaml.safe_load(file)

View File

@ -3,7 +3,7 @@ from pathlib import Path
import click
from jinja2 import Template
from loguru import logger
from plumbum.cmd import mkdir
from tools_system import ensure_folder_exists
from odoo_openupgrade_wizard.configuration_version_dependant import (
_get_odoo_version_str_list,
@ -47,17 +47,11 @@ def init(ctx, initial_version, final_version, extra_repository_list):
Initialize OpenUpgrade Wizard Environment based on the initial and
the final version of Odoo you want to migrate.
"""
# 1. create src directory if not exists
if not ctx.obj["src_folder_path"].exists():
logger.info("Creating folder '%s' ..." % (ctx.obj["src_folder_path"]))
mkdir(["--mode", "777", ctx.obj["src_folder_path"]])
# 1. ensure src folder exists
ensure_folder_exists(ctx.obj["src_folder_path"], mode="777")
# 2. create filestore directory if not exists
if not ctx.obj["filestore_folder_path"].exists():
logger.info(
"Creating folder '%s' ..." % (ctx.obj["filestore_folder_path"])
)
mkdir(["--mode", "777", ctx.obj["filestore_folder_path"]])
# 2. ensure filestore folder exists
ensure_folder_exists(ctx.obj["filestore_folder_path"], mode="777")
# 3. Create main config file
series = _get_odoo_versions(float(initial_version), float(final_version))
@ -110,9 +104,7 @@ def init(ctx, initial_version, final_version, extra_repository_list):
distinct_versions = list(set(x["version"] for x in series))
# 4. Create Repo folder and files
if not ctx.obj["repo_folder_path"].exists():
logger.info("Creating folder '%s' ..." % (ctx.obj["repo_folder_path"]))
mkdir([ctx.obj["repo_folder_path"]])
ensure_folder_exists(ctx.obj["repo_folder_path"])
extra_repositories = extra_repository_list.split(",")
@ -131,11 +123,7 @@ def init(ctx, initial_version, final_version, extra_repository_list):
f.close()
# 5. Create Requirements folder and files
if not ctx.obj["requirement_folder_path"].exists():
logger.info(
"Creating folder '%s' ..." % (ctx.obj["requirement_folder_path"])
)
mkdir([ctx.obj["requirement_folder_path"]])
ensure_folder_exists(ctx.obj["requirement_folder_path"])
for serie in series:
template = Template(_REQUIREMENTS_TXT_TEMPLATE)
@ -149,17 +137,11 @@ def init(ctx, initial_version, final_version, extra_repository_list):
f.close()
# 6. Create Scripts folder and files
if not ctx.obj["script_folder_path"].exists():
logger.info(
"Creating folder '%s' ..." % (ctx.obj["script_folder_path"])
)
mkdir([ctx.obj["script_folder_path"]])
ensure_folder_exists(ctx.obj["script_folder_path"])
for step in steps:
step_path = ctx.obj["script_folder_path"] / step["complete_name"]
if not step_path.exists():
logger.info("Creating folder '%s' ..." % (step_path))
mkdir([step_path])
ensure_folder_exists(step_path)
template = Template(_PRE_MIGRATION_SQL_TEMPLATE)
output = template.render()
file_name = step_path / Path("pre-migration.sql")

View File

@ -0,0 +1,18 @@
# from pathlib import Path
from loguru import logger
from plumbum.cmd import mkdir
def ensure_folder_exists(path, mode=False):
"""Create a local folder.
- directory is created if it doesn't exist.
- mode is applied if defined.
- a log is done at INFO level.
"""
if not path.exists():
cmd = ["--parents", path]
if mode:
cmd = ["--mode", "777"] + cmd
logger.info("Creating folder '%s' ..." % (path))
mkdir(cmd)

View File

@ -1,8 +1,7 @@
[tox]
isolated_build = true
skipsdist = True
; envlist = py35, py36, py37, py38
envlist = py36
envlist = py36, py37, py38
[testenv]
whitelist_externals = poetry