-
Notifications
You must be signed in to change notification settings - Fork 1k
Compression plugin #18314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
perseoGI
wants to merge
22
commits into
conan-io:develop2
Choose a base branch
from
perseoGI:pgi/plugin/compression
base: develop2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Compression plugin #18314
Changes from 8 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
4cd2811
Proof of concept
perseoGI 156e036
Simplify
perseoGI c7b5dff
Added tar_compressor
perseoGI be30ba2
Extra simplify
perseoGI 1295d4f
Cache the plugin load
perseoGI cc4d3ad
WIP
perseoGI e245a93
Merge with develop2
perseoGI f094d9a
Moved plugin load to ConfigAPI
perseoGI fee5fab
Pass config_api to remote_manager and local_recipe_index
perseoGI 452f774
Restore previous tar_extract fixing tests
perseoGI c1a7320
Removed compression.py module and minimize diff
perseoGI a240c8c
Remove debug print
perseoGI cf7de74
Applied thread suggestions
perseoGI 557f8e0
Remove created pkglist.json on cache restore after usage
perseoGI f82d764
Remove unused and avoid rechecking FS
perseoGI 549e086
Merge branch 'develop2' into pgi/plugin/compression
perseoGI ac1fc45
Added test to check extract failure and issue #18259 test
perseoGI 181a736
Added config to compression.py interface and renamed parameters
perseoGI 3b32ec2
Fix invokation
perseoGI b52bce2
Rename config for conf
perseoGI 91da7a4
Added ref on test
perseoGI e96bd3c
Merge branch 'develop2' into pgi/plugin/compression
perseoGI File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -90,3 +90,7 @@ def settings_path_user(self): | |||||||
@property | ||||||||
def config_version_path(self): | ||||||||
return os.path.join(self._home, "config_version.json") | ||||||||
|
||||||||
@property | ||||||||
def compression_plugin_path(self): | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Add a docstring explaining that this property returns the path to the
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||
return os.path.join(self._home, _EXTENSIONS_FOLDER, _PLUGINS, "compression.py") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from functools import lru_cache | ||
from conan.internal.cache.home_paths import HomePaths | ||
from conan.internal.loader import load_python_file | ||
from conan.internal.errors import ConanException | ||
|
||
import os | ||
import gzip | ||
import time | ||
import tarfile | ||
from conan.api.output import ConanOutput | ||
from conan.internal.util.files import set_dirty_context_manager | ||
|
||
def tar_extract(src_path, destination_dir, compress_plugin=None): | ||
if compress_plugin: | ||
return compress_plugin.tar_extract(src_path, destination_dir) | ||
|
||
with open(src_path, mode='rb') as file_handler: | ||
the_tar = tarfile.open(fileobj=file_handler) | ||
# NOTE: The errorlevel=2 has been removed because it was failing in Win10, it didn't allow to | ||
# "could not change modification time", with time=0 | ||
# the_tar.errorlevel = 2 # raise exception if any error | ||
the_tar.extraction_filter = (lambda member, path: member) # fully_trusted, avoid Py3.14 break | ||
the_tar.extractall(path=destination_dir) | ||
the_tar.close() | ||
|
||
|
||
def tar_compress(files, name, dest_dir, compresslevel=None, ref=None, recursive=False, compress_plugin=None): | ||
perseoGI marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if compress_plugin: | ||
return compress_plugin.tar_compress(files, name, dest_dir, compresslevel, ref) | ||
|
||
t1 = time.time() | ||
# FIXME, better write to disk sequentially and not keep tgz contents in memory | ||
tgz_path = os.path.join(dest_dir, name) | ||
ConanOutput(scope=str(ref) if ref else "").info(f"Compressing {name}") | ||
with set_dirty_context_manager(tgz_path), open(tgz_path, "wb") as tgz_handle: | ||
tgz = gzopen_without_timestamps(name, fileobj=tgz_handle, compresslevel=compresslevel) | ||
for filename, abs_path in sorted(files.items()): | ||
# recursive is False in case it is a symlink to a folder | ||
tgz.add(abs_path, filename, recursive=recursive) | ||
tgz.close() | ||
|
||
duration = time.time() - t1 | ||
ConanOutput().debug(f"{name} compressed in {duration} time") | ||
return tgz_path | ||
|
||
def gzopen_without_timestamps(name, fileobj, compresslevel=None): | ||
""" !! Method overrided by laso to pass mtime=0 (!=None) to avoid time.time() was | ||
setted in Gzip file causing md5 to change. Not possible using the | ||
previous tarfile open because arguments are not passed to GzipFile constructor | ||
""" | ||
compresslevel = compresslevel if compresslevel is not None else 9 # default Gzip = 9 | ||
fileobj = gzip.GzipFile(name, "w", compresslevel, fileobj, mtime=0) | ||
# Format is forced because in Python3.8, it changed and it generates different tarfiles | ||
# with different checksums, which break hashes of tgzs | ||
# PAX_FORMAT is the default for Py38, lets make it explicit for older Python versions | ||
t = tarfile.TarFile.taropen(name, "w", fileobj, format=tarfile.PAX_FORMAT) | ||
t._extfileobj = False | ||
return t | ||
|
||
|
||
def load_compression_plugin(cache_folder): | ||
if not cache_folder: | ||
return None | ||
compression_plugin_path = HomePaths(cache_folder).compression_plugin_path | ||
if not os.path.exists(compression_plugin_path): | ||
return None | ||
|
||
mod, _ = load_python_file(compression_plugin_path) | ||
if not hasattr(mod, "tar_extract") or not hasattr(mod, "tar_compress"): | ||
raise ConanException("The 'compression.py' plugin does not contain required `tar_extract` or `tar_compress` functions") | ||
return mod | ||
|
||
|
||
""" | ||
Plugin `compression.py` interface: | ||
|
||
def tar_extract(src_path: str, destination_dir: str) -> None | ||
def tar_compress(files: List[str], name: str, dest_dir: str, compresslevel=None, ref: str=None, cache_folder:str, recursive: bool = False) -> str | ||
""" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.