-
Notifications
You must be signed in to change notification settings - Fork 5
Rework the modeling of install actions and report failures #25
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
Merged
haboustak
merged 2 commits into
kosma:master
from
haboustak:23-raise-exception-on-missing-files
Sep 27, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| from __future__ import absolute_import | ||
|
|
||
| import os | ||
| import shutil | ||
| import stat | ||
| import click | ||
|
|
||
| from . import _cache | ||
| from . import _paths | ||
|
|
||
| class InstallUnmatchedError(click.ClickException): | ||
| """An exception raised when an artifact was not found""" | ||
|
|
||
| error = '''Source path(s) did not match any files/non-empty directories in the archive: | ||
| archive: {archive} | ||
| project: {project} | ||
| job: {job} | ||
| ref: {ref} | ||
| {unmatched}''' | ||
|
|
||
| def __init__(self, filename, entry, unmatched): | ||
| unmatched_desc = ('{} => {}'.format(src, dst) for src, dst in unmatched.items()) | ||
|
|
||
| message = self.error.format( | ||
| archive=_cache.cache_path(filename), | ||
| unmatched='\n '.join(unmatched_desc), | ||
| **entry) | ||
|
|
||
| super().__init__(message) | ||
|
|
||
| class InstallAction(): | ||
| """Represents a user request to install a file from an artifact archive""" | ||
|
|
||
| S_IRWXUGO = 0o0777 | ||
|
|
||
| def __init__(self, source, destination): | ||
| self.src = source | ||
| self.dest = destination | ||
|
|
||
| if source == '.': | ||
| # "copy all" filter | ||
| self._match = lambda f: True | ||
| self.translate = lambda f: os.path.join(self.dest, f) | ||
| elif source.endswith('/'): | ||
| # 1:1 directory filter | ||
| self._match = lambda f: f.startswith(self.src) | ||
| self.translate = lambda f: os.path.join(self.dest, f[len(self.src):]) | ||
| else: | ||
| # 1:1 file filter | ||
| self._match = lambda f: f == self.src | ||
| self.translate = lambda f: self.dest | ||
|
|
||
| def match(self, filepath): | ||
| """Compare an archive file using the source file pattern | ||
| Arguments: | ||
| filepath The archive filepath to evaluate | ||
|
|
||
| Returns: True if the pattern matched the filepath, otherwise False | ||
| """ | ||
| return self._match(filepath) | ||
|
|
||
| def install(self, archive, member): | ||
| """Perform the install action on a zip archive member | ||
|
|
||
| Parameters: | ||
| archive Archive from which to extract the file | ||
| member ZipInfo identifying the file to install | ||
| """ | ||
| access = None | ||
| filemode_str = "" | ||
|
|
||
| # Translate the archive path to the install destination | ||
| target = self.translate(member.filename) | ||
|
|
||
| # if create_system is Unix (3), external_attr contains filesystem permissions | ||
| if member.create_system == 3: | ||
| filemode = member.external_attr >> 16 | ||
|
|
||
| # Keep only the normal permissions bits; | ||
| # ignore special bits like setuid, setgid, sticky | ||
| access = filemode & InstallAction.S_IRWXUGO | ||
| filemode_str = ' ' + stat.filemode(stat.S_IFMT(filemode) | access) | ||
|
|
||
| click.echo('* install: %s => %s%s' % (member.filename, target, filemode_str)) | ||
|
|
||
| if member.filename.endswith('/'): | ||
| _paths.mkdirs(target) | ||
| else: | ||
| if os.sep in target: | ||
| _paths.mkdirs(os.path.dirname(target)) | ||
| with archive.open(member) as fmember: | ||
| with open(target, 'wb') as ftarget: | ||
| shutil.copyfileobj(fmember, ftarget) | ||
|
|
||
| if access is not None: | ||
| os.chmod(target, access) | ||
|
|
||
| def __str__(self): | ||
| return '{} => {}'.format(self.src, self.dest) |
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
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.