pathaction 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pathaction/__init__.py ADDED
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env python
2
+ #
3
+ # Copyright (C) 2021-2024 James Cherti
4
+ # URL: https://github.com/jamescherti/pathaction
5
+ #
6
+ # This program is free software: you can redistribute it and/or modify it under
7
+ # the terms of the GNU General Public License as published by the Free Software
8
+ # Foundation, either version 3 of the License, or (at your option) any later
9
+ # version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful, but WITHOUT
12
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
+ # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
+ # details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License along with
17
+ # this program. If not, see <https://www.gnu.org/licenses/>.
18
+ #
19
+ """The entry-point of pathaction."""
20
+
21
+ from .pathactioncli import PathActionCli
22
+
23
+
24
+ def command_line_interface():
25
+ """Command line interface."""
26
+ PathActionCli() # pragma: no cover
27
+
28
+
29
+ if __name__ == '__main__':
30
+ command_line_interface()
@@ -0,0 +1,132 @@
1
+ #!/usr/bin/env python
2
+ #
3
+ # Copyright (C) 2021-2024 James Cherti
4
+ # URL: https://github.com/jamescherti/pathaction
5
+ #
6
+ # This program is free software: you can redistribute it and/or modify it under
7
+ # the terms of the GNU General Public License as published by the Free Software
8
+ # Foundation, either version 3 of the License, or (at your option) any later
9
+ # version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful, but WITHOUT
12
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
+ # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
+ # details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License along with
17
+ # this program. If not, see <https://www.gnu.org/licenses/>.
18
+ #
19
+ """Manage permissions to allow execution only from specific paths."""
20
+
21
+ from pathlib import Path
22
+ from pprint import pformat
23
+ from typing import Dict, List, Set, Union
24
+
25
+ import yaml
26
+
27
+
28
+ class AllowedPaths:
29
+ def __init__(self):
30
+ self._temporarily_allowed = set()
31
+ self._permanently_allowed = set()
32
+
33
+ def reset(self):
34
+ self._temporarily_allowed = set()
35
+ self._permanently_allowed = set()
36
+
37
+ def add(self, path: Union[str, Path], permanent: bool):
38
+ """Add a path to the list of allowed paths.
39
+
40
+ Args:
41
+ path: The path to be added.
42
+ permanent: True to add the path permanently.
43
+ """
44
+ path = Path(path).resolve()
45
+ if permanent:
46
+ self._permanently_allowed.add(path)
47
+ self._temporarily_allowed.discard(path)
48
+ else:
49
+ self._temporarily_allowed.add(path)
50
+ self._permanently_allowed.discard(path)
51
+
52
+ def remove(self, path: Union[str, Path]):
53
+ """Remove a path from the list of allowed paths.
54
+
55
+ Args:
56
+ path: The path to be removed.
57
+ """
58
+ path = Path(path).resolve()
59
+ self._permanently_allowed.discard(path)
60
+ self._temporarily_allowed.discard(path)
61
+
62
+ def get_all(self) -> Set[Path]:
63
+ """Return all paths (permanent and temporary)"""
64
+ return set(self._temporarily_allowed | self._permanently_allowed)
65
+
66
+ def __iter__(self):
67
+ return iter(self.get_all())
68
+
69
+ def is_allowed(self, path: Union[str, Path]) -> bool:
70
+ """Check if a path is allowed, including subdirectories and files.
71
+
72
+ Args:
73
+ path: The path to be checked.
74
+
75
+ Returns: True if the path is allowed, False otherwise.
76
+ """
77
+ rpath = Path(path).resolve()
78
+ return any(rpath.is_relative_to(allowed_path) # type: ignore
79
+ for allowed_path in self)
80
+
81
+ def load_from_yaml(self, path: Union[str, Path]):
82
+ """Load the list of allowed paths from a YAML file.
83
+
84
+ Args:
85
+ path: The path to the YAML file containing allowed paths.
86
+ """
87
+ with open(path, "r", encoding="utf-8") as fhandler:
88
+ self.load_yaml_from_string(fhandler)
89
+
90
+ def load_yaml_from_string(self, stream):
91
+ """Load from a string that contains YAML data."""
92
+ content = yaml.safe_load(stream)
93
+ self._permanently_allowed = \
94
+ set(map(Path, content["permanently_allowed"]))
95
+
96
+ def save_to_yaml(self, path: Union[str, Path]):
97
+ """Save the list of allowed paths to a YAML file.
98
+
99
+ Args:
100
+ path: The path to the YAML file where allowed paths will be saved.
101
+ """
102
+ file_path = Path(path)
103
+ with open(file_path, "w", encoding="utf-8") as fhandler:
104
+ yaml.dump(self._gen_saveable_data(),
105
+ fhandler,
106
+ default_flow_style=False)
107
+
108
+ def dump_to_yaml(self) -> str:
109
+ """Dump the list of allowed paths to a YAML string.
110
+
111
+ Returns: The YAML representation of allowed paths.
112
+ """
113
+ return str(yaml.dump(self._gen_saveable_data(),
114
+ default_flow_style=False))
115
+
116
+ def _gen_saveable_data(self) -> Dict[str, List[str]]:
117
+ return {
118
+ "permanently_allowed": [str(path)
119
+ for path in self._permanently_allowed],
120
+ }
121
+
122
+ def __repr__(self) -> str:
123
+ """Provide a string representation of the object.
124
+
125
+ Returns: str: A string representation of the object.
126
+ """
127
+ return (
128
+ "Temporary:\n"
129
+ + pformat([str(path) for path in self._temporarily_allowed])
130
+ + "\n\nPermanent:\n"
131
+ + pformat([str(path) for path in self._permanently_allowed])
132
+ )
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env python
2
+ #
3
+ # Copyright (C) 2021-2024 James Cherti
4
+ # URL: https://github.com/jamescherti/pathaction
5
+ #
6
+ # This program is free software: you can redistribute it and/or modify it under
7
+ # the terms of the GNU General Public License as published by the Free Software
8
+ # Foundation, either version 3 of the License, or (at your option) any later
9
+ # version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful, but WITHOUT
12
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
+ # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
+ # details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License along with
17
+ # this program. If not, see <https://www.gnu.org/licenses/>.
18
+ #
19
+ """PathAction exceptions."""
20
+
21
+
22
+ class PathActionError(Exception):
23
+ """Error with one of PathAction classes and methods."""