Repository navigation
Improve program file handling in the run module
#499
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
158ca36
Add ProgramFiles to model, representing the files of a program
gkreitz ab05d5f
Replace IncludeFile with ProgramFiles
gkreitz a6964fb
Add check for when a path is both a path and a directory in include f…
gkreitz f7be4a8
Use ProgramFiles in run, allowing API users to access the files of a …
gkreitz a10136f
Error if package contains anything but files, directories, and symlinks
gkreitz 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
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,70 @@ | ||
| """In-memory snapshot of the files making up a program (e.g. a submission or validator).""" | ||
|
|
||
| import os | ||
| from dataclasses import dataclass, field | ||
| from pathlib import Path | ||
|
|
||
| from .paths import RelativePath, relpath | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class ProgramFile: | ||
| """A single file of a program. | ||
|
|
||
| `path` is relative to the program's root, e.g. for | ||
| submissions/accepted/hello/src/main.cpp, path is src/main.cpp. | ||
| """ | ||
|
|
||
| path: RelativePath | ||
| data: bytes | ||
| executable: bool | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class ProgramFiles: | ||
| """All files of a program, sorted by path.""" | ||
|
|
||
| files: list[ProgramFile] = field(default_factory=list) | ||
|
|
||
| def merged(self, other: 'ProgramFiles') -> 'ProgramFiles': | ||
| """Files from both self and other; where paths coincide, the file from other wins.""" | ||
| by_path = {file.path: file for file in self.files} | {file.path: file for file in other.files} | ||
| return ProgramFiles(files=sorted(by_path.values(), key=lambda file: file.path)) | ||
|
|
||
| def materialize(self, dest: Path) -> None: | ||
| """Write the files into dest, creating subdirectories as needed. | ||
|
|
||
| Existing files are overwritten. Files are written with mode 0755 if | ||
| executable, 0644 otherwise. | ||
| """ | ||
| for file in self.files: | ||
| target = dest / file.path | ||
| target.parent.mkdir(parents=True, exist_ok=True) | ||
| target.write_bytes(file.data) | ||
| target.chmod(0o755 if file.executable else 0o644) | ||
|
|
||
| def size(self) -> int: | ||
| """Total size of the files, in bytes.""" | ||
| return sum(len(file.data) for file in self.files) | ||
|
|
||
|
|
||
| def load_program_files(path: Path) -> ProgramFiles: | ||
| """Load the files of the program at path. | ||
|
|
||
| path is either a single file (giving a program with that one file), or a | ||
| directory, which is read recursively, following symlinks. A file counts as | ||
| executable if any of its execute bits are set. | ||
| """ | ||
| if path.is_file(): | ||
| return ProgramFiles(files=[_load_file(path, relpath(Path(path.name)))]) | ||
|
|
||
| files = [] | ||
| for root, _, filenames in os.walk(path, followlinks=True): | ||
| for filename in filenames: | ||
| file_path = Path(root) / filename | ||
| files.append(_load_file(file_path, relpath(file_path.relative_to(path)))) | ||
| return ProgramFiles(files=sorted(files, key=lambda file: file.path)) | ||
|
|
||
|
|
||
| def _load_file(path: Path, relative: RelativePath) -> ProgramFile: | ||
| return ProgramFile(path=relative, data=path.read_bytes(), executable=bool(path.stat().st_mode & 0o111)) | ||
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
Oops, something went wrong.
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.