Coverage for .tox/coverage/lib/python3.11/site-packages/wuttamess/util.py: 100%
25 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-08 07:01 -0500
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-08 07:01 -0500
1# -*- coding: utf-8; -*-
2################################################################################
3#
4# WuttaMess -- Fabric Automation Helpers
5# Copyright © 2024-2025 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"""
24Misc. Utilities
25"""
27from pathlib import Path
28from typing import Mapping
29from typing_extensions import Any
31from mako.template import Template
34def exists(c, path):
35 """
36 Returns ``True`` if given path exists on the host, otherwise ``False``.
37 """
38 return not c.run(f"test -e {path}", warn=True).failed
41def get_home_path(c, user=None):
42 """
43 Get the path to user's home folder on target machine.
45 :param c: Fabric connection.
47 :param user: Username whose home folder you want. If not
48 specified, the username for the current connection is assumed.
50 :returns: Home folder path as string.
51 """
52 user = user or c.user
53 home = c.run(f"getent passwd {user} | cut -d: -f6").stdout.strip()
54 home = home.rstrip("/")
55 return home
58def is_symlink(c, path):
59 """
60 Check if the given path is a symlink.
62 :param c: Fabric connection.
64 :param path: Path to check, on target machine.
66 :returns: ``True`` if path is a symlink, else ``False``.
67 """
68 # nb. this function is derived from one copied from fabric v1
69 cmd = f'test -L "$(echo {path})"'
70 result = c.run(cmd, warn=True)
71 return not result.failed
74def mako_renderer(c, env=None): # pylint: disable=unused-argument
75 """
76 This returns a *function* suitable for use as a ``fabsync`` file
77 renderer. The function assumes the file is a Mako template.
79 :param c: Fabric connection.
81 :param env: Environment dictionary to be used as Mako template
82 context.
84 Typical usage is something like::
86 from fabric import task
87 from wuttamess import sync, util
89 root = sync.make_root('files')
90 env = {}
92 @task
93 def foo(c):
95 # define possible renderers for fabsync
96 renderers = {'mako': util.mako_renderer(c, env)}
98 sync.check_isync(c, root, 'etc/postfix', renderers=renderers)
99 """
100 env = env or {}
102 def render( # pylint: disable=redefined-builtin,unused-argument
103 path: Path, vars: Mapping[str, Any], **kwargs
104 ) -> bytes:
105 return Template(filename=str(path)).render(**env)
107 return render
110def set_timezone(c, timezone):
111 """
112 Set the system timezone.
114 :param c: Fabric connection.
116 :param timezone: Standard timezone name,
117 e.g. ``'America/Chicago'``.
118 """
119 c.run(f"bash -c 'echo {timezone} > /etc/timezone'")
121 if is_symlink(c, "/etc/localtime"):
122 c.run(f"ln -sf /usr/share/zoneinfo/{timezone} /etc/localtime")
123 else:
124 c.run(f"cp /usr/share/zoneinfo/{timezone} /etc/localtime")