# HG changeset patch
# User Vincent Hatakeyama <vincent.hatakeyama@xcg-consulting.fr>
# Date 1680248584 -7200
#      Fri Mar 31 09:43:04 2023 +0200
# Node ID 5b25b6abd29eb0ae38272059ae96ab1eec4ebd29
# Parent  95175b1d6eed1758c9aa69bdb10f62be5af0f1c5
✨ On docker_build_copy, when not run localy, do not build.

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -53,7 +53,7 @@
   image: $TEMP_IMAGE
   script:
     - python3 -m pip install mypy types-PyYAML types-psycopg2 types-python-dateutil
-      types-requests
+      types-requests build twine
     - mypy odoo_scripts tests
   rules:
     - if: $CI_COMMIT_TAG == null
diff --git a/NEWS.rst b/NEWS.rst
--- a/NEWS.rst
+++ b/NEWS.rst
@@ -2,6 +2,11 @@
 History
 =======
 
+20.2.0
+------
+
+docker_build_copy: when not in CI, default to copying packages and let the package be build in the image.
+
 20.1.0
 ------
 
diff --git a/odoo_scripts/docker_build_copy.py b/odoo_scripts/docker_build_copy.py
--- a/odoo_scripts/docker_build_copy.py
+++ b/odoo_scripts/docker_build_copy.py
@@ -13,9 +13,9 @@
 
 _logger = logging.getLogger(__name__)
 
-__version__ = "2.1.0"
+__version__ = "2.2.0"
 __date__ = "2020-06-30"
-__updated__ = "2023-02-28"
+__updated__ = "2023-03-31"
 
 PYTHON_PACKAGES_DIR = "python_packages"
 
@@ -52,8 +52,13 @@
     return path.replace(os.path.sep, "-")
 
 
-def copy():
-    """Copy modules for a build"""
+def copy(build_package_locally: Optional[bool] = None):
+    """Copy modules for a build
+
+    :param build_package_locally: build package that have package compile set to True
+      rather than just copy them. (in CI, build package to reduce artifact size, locally
+      default to copying them)
+    """
     c = Config()
     modules = [os.path.realpath(module) for module in c.modules]
     target = "odoo_modules"
@@ -84,10 +89,13 @@
     copy_paths = set()
     packages_to_compile = []
     requirements = []
+    ci_mode = "CI" in os.environ and os.environ["CI"].lower() == "true"
     # Try to include the CVS information in target so that setuptools-scm and
     # setuptools-odoo work as expected
     for package, options in c.python_packages.items():
-        if options["compile"] in ("True", "true"):
+        if options["compile"] in ("True", "true") and (
+            ci_mode or build_package_locally
+        ):
             packages_to_compile.append(package)
         else:
             cvs_parent = package
@@ -129,21 +137,48 @@
         ]
         _logger.debug(" ".join(cmd))
         check_call(cmd)
-    # The option to ignore the python verion can be an issue with newer syntax.
-    # In the CI it will work fine, locally, maybe add an option to avoid compiling?
+    # The option to ignore the python version can be an issue with newer syntax.
     if packages_to_compile:
-        cmd = [
-            sys.executable,
-            "-m",
-            "pip",
-            "wheel",
-            "--no-deps",
-            "-w",
-            target,
-            "--ignore-requires-python",
-        ] + packages_to_compile
+        # try with build if present, fallback to pip wheel otherwise
+        try:
+            # build is not usable directly so use a subprocess.
+            # Disable flake8, this is just to test that the package is installed.
+            import build  # type: ignore[import] # noqa: F401
+
+            cmd = [
+                sys.executable,
+                "-m",
+                "build",
+                "--outdir",
+                target,
+            ] + packages_to_compile
+            _logger.debug(" ".join(cmd))
+            check_call(cmd)
+        except ImportError:
+            # pip is not usable directly, it is indicated in its doc to use subprocess
+            # instead.
+            cmd = [
+                sys.executable,
+                "-m",
+                "pip",
+                "wheel",
+                "--no-deps",
+                "-w",
+                target,
+                "--ignore-requires-python",
+            ] + packages_to_compile
         _logger.debug(" ".join(cmd))
         check_call(cmd)
+
+        try:
+            # Disable flake8, this is just to test that the package is installed
+            import twine  # type: ignore[import] # noqa: F401
+
+            cmd = [sys.executable, "-m", "twine", "check", f"{target}/*"]
+            check_call(cmd)
+        except ImportError:
+            _logger.warn("No twine, no check done")
+
     for entry in os.listdir(target):
         if entry.endswith(".whl"):
             requirements.append(os.path.join(".", target, entry))