Coverage for .tox/coverage/lib/python3.11/site-packages/wuttamess/apt.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-11-21 07:00 -0600

1# -*- coding: utf-8; -*- 

2################################################################################ 

3# 

4# WuttaMess -- Fabric Automation Helpers 

5# Copyright © 2024 Lance Edgar 

6# 

7# This file is part of Wutta Framework. 

8# 

9# Wutta Framework is free software: you can redistribute it and/or modify it 

10# under the terms of the GNU General Public License as published by the Free 

11# Software Foundation, either version 3 of the License, or (at your option) any 

12# later version. 

13# 

14# Wutta Framework is distributed in the hope that it will be useful, but 

15# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 

16# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 

17# more details. 

18# 

19# You should have received a copy of the GNU General Public License along with 

20# Wutta Framework. If not, see <http://www.gnu.org/licenses/>. 

21# 

22################################################################################ 

23""" 

24APT package management 

25""" 

26 

27 

28def dist_upgrade(c, frontend='noninteractive'): 

29 """ 

30 Run a full dist-upgrade for APT. Essentially this runs: 

31 

32 .. code-block:: sh 

33 

34 apt update 

35 apt dist-upgrade 

36 """ 

37 update(c) 

38 upgrade(c, dist_upgrade=True, frontend=frontend) 

39 

40 

41def install(c, *packages, **kwargs): 

42 """ 

43 Install some package(s) via APT. Essentially this runs: 

44 

45 .. code-block:: sh 

46 

47 apt install PKG [PKG ...] 

48 """ 

49 frontend = kwargs.pop('frontend', 'noninteractive') 

50 packages = ' '.join(packages) 

51 return c.run(f'DEBIAN_FRONTEND={frontend} apt-get --assume-yes install {packages}') 

52 

53 

54def is_installed(c, package): 

55 """ 

56 Check if the given APT package is installed. 

57 

58 :param c: Fabric connection. 

59 

60 :param package: Name of package to be checked. 

61 

62 :returns: ``True`` if package is installed, else ``False``. 

63 """ 

64 return c.run(f'dpkg-query -s {package}', warn=True).ok 

65 

66 

67def update(c): 

68 """ 

69 Update the APT package lists. Essentially this runs: 

70 

71 .. code-block:: sh 

72 

73 apt update 

74 """ 

75 c.run('apt-get update') 

76 

77 

78def upgrade(c, dist_upgrade=False, frontend='noninteractive'): 

79 """ 

80 Upgrade packages via APT. Essentially this runs: 

81 

82 .. code-block:: sh 

83 

84 apt upgrade 

85 

86 # ..or.. 

87 

88 apt dist-upgrade 

89 """ 

90 options = '' 

91 if frontend == 'noninteractive': 

92 options = '--option Dpkg::Options::="--force-confdef" --option Dpkg::Options::="--force-confold"' 

93 upgrade = 'dist-upgrade' if dist_upgrade else 'upgrade' 

94 c.run(f'DEBIAN_FRONTEND={frontend} apt-get --assume-yes {options} {upgrade}')