Replaced distutils to shutil when copying files in a tree#21222
Replaced distutils to shutil when copying files in a tree#21222opencv-pushbot merged 1 commit intoopencv:4.xfrom asenyaev:asen/replace_distutils_copy_tree
Conversation
platforms/ios/build_framework.py
Outdated
| copied_dest_item = os.path.join(os.path.join(dstdir, "Modules"), item) | ||
| if os.path.exists(copied_dest_item): | ||
| try: | ||
| shutil.rmtree(copied_dest_item) |
There was a problem hiding this comment.
We should not cleanup destination directories.
AFAIK, artifacts of different archs are merged in one location.
There was a problem hiding this comment.
We just remove files which we want to add to this location, I mean overwrite. But yes, it removes files in the directory which we want to copy if it already exists. I'll fix it.
alalek
left a comment
There was a problem hiding this comment.
gen_objc.py
This script can be tested even on Linux through gen_opencv_objc_source target.
modules/objc/generator/gen_objc.py
Outdated
| return "/**\n " + "\n ".join(lines) + "\n */" if hasValues else "" | ||
|
|
||
| # copytree from shutil cannot handle an error when directory exists in Python < 3.8 | ||
| def _mkdir(newdir): |
There was a problem hiding this comment.
https://docs.python.org/3/library/pathlib.html#pathlib.Path.mkdir
with parents=True
There was a problem hiding this comment.
Added it to the function.
There was a problem hiding this comment.
I mean that we could replace this function implementation with pathlib.Path.mkdir call (available since Python 3.4).
modules/objc/generator/gen_objc.py
Outdated
| if tail: | ||
| os.mkdir(newdir) | ||
|
|
||
| def copytree(src, dst, symlinks=False): |
There was a problem hiding this comment.
I believe we could use https://docs.python.org/3/library/shutil.html#shutil.copytree
May be with Python 3.8 if we need dirs_exist_ok parameter.
BTW,
macosx-1 has Python 3.9
macosx-2 has Python 3.7
There was a problem hiding this comment.
Yes, but I thought dirs_exist_ok can work only with python>=3.8, which is not good for versions before 3.8.
Also, copytree cannot write to the directory, if it's already exist, because it tries to use mkdir there.
There was a problem hiding this comment.
copytree cannot write to the directory, if it's already exist
This is why I mention about dirs_exist_ok parameter.
I expecting only this change:
if sys.version_info >= (3, 8): # Python 3.8+
def copy_tree(src, dst):
shutil.copytree(src, dst, dirs_exist_ok=True)
else:
from distutils.dir_util import copy_tree
There was a problem hiding this comment.
Is it a good idea to support distutils anyway? As far as I know, it will be deprecated in Python 3.12, what is okay to use this (your suggestion) workaround.
This PR fixes following issue #21141 for files in 4.x branch which are not located in 3.4 branch.
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.