Page 1 of 1

Merge Folders

Posted: Sat Oct 02, 2021 1:23 am
by decompile
Hey!

I'm currently trying to write an updater for my plugin and I encountered a problem.

So what it basically does is download the master version, unzip it into a temporary folder, remove existing files and then extract the unzipped files to the server files.

Syntax: Select all

from distutils.dir_util import copy_tree
from paths import GAME_PATH

SOURCE_PATH = GAME_PATH / "update"

copy_tree(SOURCE_PATH, GAME_PATH)


The only problem with this is, if SOURCE_PATH contains a newly created directory, which does not exist in GAME_PATH, it raises:

SOURCE_PATH:
../source-python/packages/custom/certifi/cacert.pem

GAME_PATH:
../source-python/packages/custom


Code: Select all

distutils.errors.DistutilsFileError: could not create '..\addons\source-python\packages\custom\certifi\cacert.pem': No such file or directory


Is there a possible way to create all missing directories and sub-directories of SOURCE_PATH e.g: ../addons/source-python or ..addons/source-python/custom/certifi or is there maybe even a better way to extract SOURCE_PATH into GAME_PATH

Re: Merge Folders

Posted: Sat Oct 02, 2021 8:41 am
by L'In20Cible

Syntax: Select all

SOURCE_PATH.copytree(GAME_PATH)

Re: Merge Folders

Posted: Sat Oct 02, 2021 12:55 pm
by decompile
L'In20Cible wrote:

Syntax: Select all

SOURCE_PATH.copytree(GAME_PATH)


Sadly results in an FileExistsError:

NEW_UPDATE_PATH.copytree(GAME_PATH)
File "..\addons\source-python\Python3\shutil.py", line 315, in copytree
os.makedirs(dst)
File "..\addons\source-python\Python3\os.py", line 220, in makedirs
mkdir(name, mode)

FileExistsError: [WinError 183] Cannot create a file when that file already exists: Path('D:\\GameServer\\css_tricksurf\\cstrike')

Re: Merge Folders

Posted: Sat Oct 02, 2021 4:51 pm
by satoon101
Unfortunately, I don't think we have updated SP to Python3.8 (iirc). If we did, this would work:
https://stackoverflow.com/a/58916847

But, there are other answers on that SO thread that you might try.

Re: Merge Folders

Posted: Sat Oct 02, 2021 6:40 pm
by decompile
Thanks!

I'm gonna try this:
https://stackoverflow.com/a/22331852

Re: Merge Folders

Posted: Sat Oct 02, 2021 9:51 pm
by L'In20Cible
decompile wrote:
L'In20Cible wrote:

Syntax: Select all

SOURCE_PATH.copytree(GAME_PATH)


Sadly results in an FileExistsError:

NEW_UPDATE_PATH.copytree(GAME_PATH)
File "..\addons\source-python\Python3\shutil.py", line 315, in copytree
os.makedirs(dst)
File "..\addons\source-python\Python3\os.py", line 220, in makedirs
mkdir(name, mode)

FileExistsError: [WinError 183] Cannot create a file when that file already exists: Path('D:\\GameServer\\css_tricksurf\\cstrike')


Sorry, didn't look close enough at your paths. The problem is that your source is inside your destination. Using merge_tree instead of copytree should do the trick.

Re: Merge Folders

Posted: Tue Oct 05, 2021 3:55 pm
by decompile
Works perfectly, thanks!