# HG changeset patch
# User Vincent Hatakeyama <vincent.hatakeyama@xcg-consulting.fr>
# Date 1686044461 -7200
#      Tue Jun 06 11:41:01 2023 +0200
# Node ID 31234ceee1633a90193553c341236311216aaede
# Parent  950344bf7562c68b6ccce739989565fb10264c5c
✨ docker_build_copy: use hg archive (via hglib) to copy mercurial files (rather than rsync)

diff --git a/NEWS.rst b/NEWS.rst
--- a/NEWS.rst
+++ b/NEWS.rst
@@ -2,6 +2,11 @@
 History
 =======
 
+20.4.0
+------
+
+When copying python packages to build the image, use hg archive rather than rsync.
+
 20.3.1
 ------
 
diff --git a/odoo_scripts/docker_build.py b/odoo_scripts/docker_build.py
--- a/odoo_scripts/docker_build.py
+++ b/odoo_scripts/docker_build.py
@@ -140,6 +140,7 @@
 
     buildargs = {}
     if os.path.exists(".hg"):
+        # TODO change to use hglib
         description = check_output(
             [
                 "hg",
@@ -152,9 +153,11 @@
         ).decode()
         buildargs["VERSION"] = description
         buildargs["SENTRY_RELEASE_ARG"] = description
+        # TODO change to use hglib
         tags_string = check_output(["hg", "identify", "--tags"]).decode()
         if tags_string:
             tags = tags_string.split()
+        # TODO change to use hglib
         buildargs["VCS_URL"] = check_output(["hg", "paths", "default"]).decode()
         buildargs["VCS_REF"] = check_output(["hg", "identify", "--id"]).decode()
     elif os.path.exists(".git"):
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
@@ -4,18 +4,20 @@
 import logging
 import os
 import sys
-from subprocess import check_call
+from subprocess import DEVNULL, check_call
 from typing import List, Optional
 
+import hglib  # type: ignore[import]
+
 from .config import Config
 from .list_modules import list_modules
 from .parsing import apply, basic_parser
 
 _logger = logging.getLogger(__name__)
 
-__version__ = "2.2.0"
+__version__ = "2.3.0"
 __date__ = "2020-06-30"
-__updated__ = "2023-03-31"
+__updated__ = "2023-06-06"
 
 PYTHON_PACKAGES_DIR = "python_packages"
 
@@ -81,12 +83,11 @@
         + [target]
     )
     _logger.debug(" ".join(cmd))
-    check_call(cmd)
+    check_call(cmd, stdout=DEVNULL)
 
     target = PYTHON_PACKAGES_DIR
     if not os.path.exists(PYTHON_PACKAGES_DIR):
         os.mkdir(target)
-    copy_paths = set()
     packages_to_compile = []
     requirements = []
     ci_mode = "CI" in os.environ and os.environ["CI"].lower() == "true"
@@ -113,30 +114,21 @@
                     # stop at one level in the super project
                     break
                 cvs_parent = os.path.dirname(cvs_parent)
-            copy_paths.add(cvs_parent)
+            this_package_target = os.path.join(target, _target_path(cvs_parent))
+            # Use hg archive
+            if os.path.exists(os.path.join(cvs_parent, ".hg")):
+                _logger.info(
+                    "Using mercurial archive for %s to %s",
+                    cvs_parent,
+                    this_package_target,
+                )
+                client = hglib.open(cvs_parent)
+                client.archive(this_package_target.encode("UTF-8"))
+            else:
+                _copy_package(cvs_parent + os.path.sep, this_package_target)
             requirements.append(
-                os.path.join(target, _target_path(cvs_parent), package_path_in_target)
+                os.path.join(this_package_target, package_path_in_target)
             )
-    for copy_path in copy_paths:
-        src, dest = copy_path + os.path.sep, os.path.join(
-            target, _target_path(copy_path)
-        )
-        _logger.info("Copying package %s to %s", src, dest)
-        if not os.path.exists(target):
-            os.mkdir(target)
-        cmd = [
-            "rsync",
-            "--delete",
-            "--include=core",
-            "--copy-links",
-            "--exclude='*.pyc'",
-            "-r",
-            "--times",
-            src,
-            dest,
-        ]
-        _logger.debug(" ".join(cmd))
-        check_call(cmd)
     # The option to ignore the python version can be an issue with newer syntax.
     if packages_to_compile:
         # try with build if present, fallback to pip wheel otherwise
@@ -213,5 +205,24 @@
     list_modules()
 
 
+def _copy_package(src, dest):
+    _logger.info("Copying package %s to %s", src, dest)
+    cmd = [
+        "rsync",
+        "--delete",
+        "--include=core",
+        "--links",
+        "--exclude='*.pyc'",
+        "--exclude=__pycache__",
+        "--exclude='*.orig'",
+        "-r",
+        "--times",
+        src,
+        dest,
+    ]
+    _logger.debug(" ".join(cmd))
+    check_call(cmd)
+
+
 if __name__ == "__main__":
     sys.exit(main())
diff --git a/odoo_scripts/docker_dev_start.py b/odoo_scripts/docker_dev_start.py
--- a/odoo_scripts/docker_dev_start.py
+++ b/odoo_scripts/docker_dev_start.py
@@ -1190,6 +1190,7 @@
     if os.path.exists(".hg"):
         # Next version guessing could be done by using marabunta yaml and using it
         # rather than doing latesttag.1
+        # TODO change to use hglib
         project_version = check_output(
             [
                 "hg",
diff --git a/pyproject.toml b/pyproject.toml
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -14,13 +14,14 @@
 ]
 keywords = ["odoo"]
 dependencies = [
-  "odoorpc>=0.8.0",
-  "odoorpc==0.8.0; python_version=='3.6'",
-  "odoorpc>=0.9.0; python_version>='3.10'",
-  "python-dateutil>=2.7.0",
+  "odoorpc >=0.8.0; python_version>='3.7' and python_version<'3.10'",
+  "odoorpc ==0.8.0; python_version=='3.6'",
+  "odoorpc >=0.9.0; python_version>='3.10'",
+  "python-dateutil >=2.7.0",
   "coloredlogs",
   "PyYAML",
-  "importlib_metadata; python_version<'3.8'"
+  "importlib_metadata; python_version<'3.8'",
+  "python-hglib",
 ]
 
 [project.optional-dependencies]
@@ -32,7 +33,7 @@
 conf2reST = []
 docker = [
   "docker >=3.4",
-  "docker < 6.0.0; python_version<'3.7'",
+  "docker <6.0.0; python_version<'3.7'",
   "dockerpty",
   # used in docker_rednerd
   "requests_unixsocket",
@@ -42,7 +43,6 @@
 source_control = [
   # Only mercurial 5.2 support Python 3.5+
   "mercurial >=5.2",
-  "python-hglib",
 ]
 import_base_import = ["requests-toolbelt>=0.8.0"]