few fixes and smaller modifications

This commit is contained in:
Jakub Trllo 2022-02-02 16:21:13 +01:00
parent 7d19db96c4
commit ea4e5b0407
3 changed files with 25 additions and 12 deletions

View file

@ -419,9 +419,9 @@ def repack_version(directory):
@click.option( @click.option(
"--dirpath", help="Directory where package is stored", default=None "--dirpath", help="Directory where package is stored", default=None
) )
def pack_project(project_name, dirpath): def pack_project(project, dirpath):
"""Create a package of project with all files and database dump.""" """Create a package of project with all files and database dump."""
PypeCommands().pack_project(project_name, dirpath) PypeCommands().pack_project(project, dirpath)
@main.command() @main.command()
@ -429,6 +429,6 @@ def pack_project(project_name, dirpath):
@click.option( @click.option(
"--root", help="Replace root which was stored in project", default=None "--root", help="Replace root which was stored in project", default=None
) )
def uppack_project(zipfile, root): def unpack_project(zipfile, root):
"""Create a package of project with all files and database dump.""" """Create a package of project with all files and database dump."""
PypeCommands().unpack_project(zipfile, root) PypeCommands().unpack_project(zipfile, root)

View file

@ -53,6 +53,7 @@ def pack_project(project_name, destination_dir=None):
destination_dir(str): Optinal path where zip will be stored. Project's destination_dir(str): Optinal path where zip will be stored. Project's
root is used if not passed. root is used if not passed.
""" """
print("Creating package of project \"{}\"".format(project_name))
# Validate existence of project # Validate existence of project
dbcon = AvalonMongoDB() dbcon = AvalonMongoDB()
dbcon.Session["AVALON_PROJECT"] = project_name dbcon.Session["AVALON_PROJECT"] = project_name
@ -74,7 +75,7 @@ def pack_project(project_name, destination_dir=None):
root_path = source_root[platform.system().lower()] root_path = source_root[platform.system().lower()]
print("Using root \"{}\" with path \"{}\"".format( print("Using root \"{}\" with path \"{}\"".format(
root_name, root_value root_name, root_path
)) ))
project_source_path = os.path.join(root_path, project_name) project_source_path = os.path.join(root_path, project_name)
@ -85,12 +86,13 @@ def pack_project(project_name, destination_dir=None):
if not destination_dir: if not destination_dir:
destination_dir = root_path destination_dir = root_path
destination_dir = os.path.normpath(destination_dir)
if not os.path.exists(destination_dir): if not os.path.exists(destination_dir):
os.makedirs(destination_dir) os.makedirs(destination_dir)
zip_path = os.path.join(destination_dir, project_name + ".zip") zip_path = os.path.join(destination_dir, project_name + ".zip")
print("Project will be packaged into {}".format(zip_path)) print("Project will be packaged into \"{}\"".format(zip_path))
# Rename already existing zip # Rename already existing zip
if os.path.exists(zip_path): if os.path.exists(zip_path):
dst_filepath = add_timestamp(zip_path) dst_filepath = add_timestamp(zip_path)
@ -121,6 +123,7 @@ def pack_project(project_name, destination_dir=None):
with open(temp_docs_json, "w") as stream: with open(temp_docs_json, "w") as stream:
stream.write(data) stream.write(data)
print("Packing files into zip")
# Write all to zip file # Write all to zip file
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zip_stream: with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zip_stream:
# Add metadata file # Add metadata file
@ -138,10 +141,12 @@ def pack_project(project_name, destination_dir=None):
) )
zip_stream.write(filepath, archive_name) zip_stream.write(filepath, archive_name)
print("Cleaning up")
# Cleanup # Cleanup
os.remove(temp_docs_json) os.remove(temp_docs_json)
os.remove(temp_metadata_json) os.remove(temp_metadata_json)
dbcon.uninstall() dbcon.uninstall()
print("*** Packing finished ***")
def unpack_project(path_to_zip, new_root=None): def unpack_project(path_to_zip, new_root=None):
@ -153,12 +158,13 @@ def unpack_project(path_to_zip, new_root=None):
new_root(str): Optional way how to set different root path for unpacked new_root(str): Optional way how to set different root path for unpacked
project. project.
""" """
print("Unpacking project from zip {}".format(path_to_zip))
if not os.path.exists(path_to_zip): if not os.path.exists(path_to_zip):
print("Zip file does not exists: {}".format(path_to_zip)) print("Zip file does not exists: {}".format(path_to_zip))
return return
tmp_dir = tempfile.mkdtemp(prefix="unpack_") tmp_dir = tempfile.mkdtemp(prefix="unpack_")
print("Zip is extracted to: {}".format(tmp_dir)) print("Zip is extracted to temp: {}".format(tmp_dir))
with zipfile.ZipFile(path_to_zip, "r") as zip_stream: with zipfile.ZipFile(path_to_zip, "r") as zip_stream:
zip_stream.extractall(tmp_dir) zip_stream.extractall(tmp_dir)
@ -183,9 +189,9 @@ def unpack_project(path_to_zip, new_root=None):
database.drop_collection(project_name) database.drop_collection(project_name)
print("Removed existing project collection") print("Removed existing project collection")
collection = database[project_name]
# Create new collection with loaded docs
print("Creating project documents ({})".format(len(docs))) print("Creating project documents ({})".format(len(docs)))
# Create new collection with loaded docs
collection = database[project_name]
collection.insert_many(docs) collection.insert_many(docs)
# Skip change of root if is the same as the one stored in metadata # Skip change of root if is the same as the one stored in metadata
@ -217,16 +223,23 @@ def unpack_project(path_to_zip, new_root=None):
src_project_files_dir = os.path.join( src_project_files_dir = os.path.join(
tmp_dir, PROJECT_FILES_DIR, project_name tmp_dir, PROJECT_FILES_DIR, project_name
) )
dst_project_files_dir = os.path.join(root_path, project_name) dst_project_files_dir = os.path.normpath(
os.path.join(root_path, project_name)
)
if os.path.exists(dst_project_files_dir): if os.path.exists(dst_project_files_dir):
new_path = add_timestamp(dst_project_files_dir) new_path = add_timestamp(dst_project_files_dir)
print("Project folder already exists. Renamed \"{}\" -> \"{}\"".format(
dst_project_files_dir, new_path
))
os.rename(dst_project_files_dir, new_path) os.rename(dst_project_files_dir, new_path)
print("Moving {} -> {}".format( print("Moving project files from temp \"{}\" -> \"{}\"".format(
src_project_files_dir, dst_project_files_dir src_project_files_dir, dst_project_files_dir
)) ))
shutil.move(src_project_files_dir, dst_project_files_dir) shutil.move(src_project_files_dir, dst_project_files_dir)
# CLeanup # CLeanup
print("Cleaning up")
shutil.rmtree(tmp_dir) shutil.rmtree(tmp_dir)
dbcon.uninstall() dbcon.uninstall()
print("*** Unpack finished ***")

View file

@ -434,12 +434,12 @@ class PypeCommands:
version_packer = VersionRepacker(directory) version_packer = VersionRepacker(directory)
version_packer.process() version_packer.process()
def pack_project(project_name, dirpath): def pack_project(self, project_name, dirpath):
from openpype.lib.project_backpack import pack_project from openpype.lib.project_backpack import pack_project
pack_project(project_name, dirpath) pack_project(project_name, dirpath)
def unpack_project(zip_filepath, new_root): def unpack_project(self, zip_filepath, new_root):
from openpype.lib.project_backpack import unpack_project from openpype.lib.project_backpack import unpack_project
unpack_project(zip_filepath, new_root) unpack_project(zip_filepath, new_root)