add some typing stuff and docstrings

This commit is contained in:
Michael Hudson-Doyle 2022-03-17 13:47:39 +13:00
parent 6cc1f50af0
commit 5e6c845b89
1 changed files with 38 additions and 7 deletions

View File

@ -13,7 +13,9 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import abc
import functools import functools
from typing import Optional
import attr import attr
@ -53,8 +55,25 @@ def _is_boot_device_raid(raid):
return any(p.grub_device for p in raid._partitions) return any(p.grub_device for p in raid._partitions)
class MakeBootDevicePlan(abc.ABC):
"""A way of making a device into boot device.
The code for checking if a device can be a boot device attempts to
construct one of these to find out. The code for making a device a
boot device calls apply(). This way we don't have to keep the
implementation of "can this be a boot device" and "how do we make
this a boot device" in sync.
"""
@abc.abstractmethod
def apply(self, manipulator):
pass
@attr.s(auto_attribs=True) @attr.s(auto_attribs=True)
class CreatePartPlan: class CreatePartPlan(MakeBootDevicePlan):
"""Create a partition on the device."""
device: object device: object
offset: int = 0 offset: int = 0
@ -69,26 +88,34 @@ class CreatePartPlan:
@attr.s(auto_attribs=True) @attr.s(auto_attribs=True)
class ResizePlan: class ResizePlan(MakeBootDevicePlan):
"""Resize a partition."""
part: object part: object
size_delta: int = 0 size_delta: int = 0
def apply(self, manipulator): def apply(self, manipulator):
assert not self.part.preserve
self.part.size += self.size_delta self.part.size += self.size_delta
@attr.s(auto_attribs=True) @attr.s(auto_attribs=True)
class SlidePlan: class SlidePlan(MakeBootDevicePlan):
"""Move a collection of partitions by the same amount."""
parts: list parts: list
offset_delta: int = 0 offset_delta: int = 0
def apply(self, manipulator): def apply(self, manipulator):
for part in self.parts: for part in self.parts:
assert not part.preserve
part.offset += self.offset_delta part.offset += self.offset_delta
@attr.s(auto_attribs=True) @attr.s(auto_attribs=True)
class SetAttrPlan: class SetAttrPlan(MakeBootDevicePlan):
"""Set an attribute on an object."""
device: object device: object
attr: str attr: str
val: str val: str
@ -98,7 +125,9 @@ class SetAttrPlan:
@attr.s(auto_attribs=True) @attr.s(auto_attribs=True)
class MountBootEfiPlan: class MountBootEfiPlan(MakeBootDevicePlan):
"""Mount a partition at /boot/efi."""
part: object part: object
def apply(self, manipulator): def apply(self, manipulator):
@ -106,7 +135,9 @@ class MountBootEfiPlan:
@attr.s(auto_attribs=True) @attr.s(auto_attribs=True)
class MultiStepPlan: class MultiStepPlan(MakeBootDevicePlan):
"""Execute several MakeBootDevicePlans in sequence."""
plans: list plans: list
def apply(self, manipulator): def apply(self, manipulator):
@ -114,7 +145,7 @@ class MultiStepPlan:
plan.apply(manipulator) plan.apply(manipulator)
def get_boot_device_plan_bios(device): def get_boot_device_plan_bios(device) -> Optional[MakeBootDevicePlan]:
attr_plan = SetAttrPlan(device, 'grub_device', True) attr_plan = SetAttrPlan(device, 'grub_device', True)
if device.ptable == 'msdos': if device.ptable == 'msdos':
return attr_plan return attr_plan