-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from NucciTheBoss/nuccitheboss/feat/gres-editor
- Loading branch information
Showing
10 changed files
with
752 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -18,7 +18,7 @@ build-backend = "poetry.core.masonry.api" | |
|
||
[tool.poetry] | ||
name = "slurmutils" | ||
version = "0.9.0" | ||
version = "0.10.0" | ||
description = "Utilities and APIs for interfacing with the Slurm workload manager." | ||
repository = "https://github.com/charmed-hpc/slurmutils" | ||
authors = ["Jason C. Nucciarone <[email protected]>"] | ||
|
This file contains 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 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,82 @@ | ||
# Copyright 2024 Canonical Ltd. | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License version 3 as published by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""Edit gres.conf files.""" | ||
|
||
__all__ = ["dump", "dumps", "load", "loads", "edit"] | ||
|
||
import logging | ||
import os | ||
from contextlib import contextmanager | ||
from pathlib import Path | ||
from typing import Optional, Union | ||
|
||
from ..models import GRESConfig | ||
from .editor import dumper, loader, set_file_permissions | ||
|
||
_logger = logging.getLogger("slurmutils") | ||
|
||
|
||
@loader | ||
def load(file: Union[str, os.PathLike]) -> GRESConfig: | ||
"""Load `gres.conf` data model from gres.conf file.""" | ||
return loads(Path(file).read_text()) | ||
|
||
|
||
def loads(content: str) -> GRESConfig: | ||
"""Load `gres.conf` data model from string.""" | ||
return GRESConfig.from_str(content) | ||
|
||
|
||
@dumper | ||
def dump( | ||
config: GRESConfig, | ||
file: Union[str, os.PathLike], | ||
mode: int = 0o644, | ||
user: Optional[Union[str, int]] = None, | ||
group: Optional[Union[str, int]] = None, | ||
) -> None: | ||
"""Dump `gres.conf` data model into gres.conf file.""" | ||
Path(file).write_text(dumps(config)) | ||
set_file_permissions(file, mode, user, group) | ||
|
||
|
||
def dumps(config: GRESConfig) -> str: | ||
"""Dump `gres.conf` data model into a string.""" | ||
return str(config) | ||
|
||
|
||
@contextmanager | ||
def edit( | ||
file: Union[str, os.PathLike], | ||
mode: int = 0o644, | ||
user: Optional[Union[str, int]] = None, | ||
group: Optional[Union[str, int]] = None, | ||
) -> GRESConfig: | ||
"""Edit a gres.conf file. | ||
Args: | ||
file: gres.conf file to edit. An empty config will be created if it does not exist. | ||
mode: Access mode to apply to the gres.conf file. (Default: rw-r--r--) | ||
user: User to set as owner of the gres.conf file. (Default: $USER) | ||
group: Group to set as owner of the gres.conf file. (Default: None) | ||
""" | ||
if not os.path.exists(file): | ||
_logger.warning("file %s not found. creating new empty gres.conf configuration", file) | ||
config = GRESConfig() | ||
else: | ||
config = load(file) | ||
|
||
yield config | ||
dump(config, file, mode, user, group) |
This file contains 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.