Coverage for .tox/coverage/lib/python3.13/site-packages/wuttamess/util.py: 100%
24 statements
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-20 15:16 -0500
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-20 15:16 -0500
1# -*- coding: utf-8; -*-
2################################################################################
3#
4# WuttaMess -- Fabric Automation Helpers
5# Copyright © 2024-2026 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, Any
30from mako.template import Template
33def exists(c, path):
34 """
35 Returns ``True`` if given path exists on the host, otherwise ``False``.
36 """
37 return not c.run(f"test -e {path}", warn=True).failed
40def get_home_path(c, user=None):
41 """
42 Get the path to user's home folder on target machine.
44 :param c: Fabric connection.
46 :param user: Username whose home folder you want. If not
47 specified, the username for the current connection is assumed.
49 :returns: Home folder path as string.
50 """
51 user = user or c.user
52 home = c.run(f"getent passwd {user} | cut -d: -f6").stdout.strip()
53 home = home.rstrip("/")
54 return home
57def is_symlink(c, path):
58 """
59 Check if the given path is a symlink.
61 :param c: Fabric connection.
63 :param path: Path to check, on target machine.
65 :returns: ``True`` if path is a symlink, else ``False``.
66 """
67 # nb. this function is derived from one copied from fabric v1
68 cmd = f'test -L "$(echo {path})"'
69 result = c.run(cmd, warn=True)
70 return not result.failed
73def mako_renderer(c, env=None): # pylint: disable=unused-argument
74 """
75 This returns a *function* suitable for use as a ``fabsync`` file
76 renderer. The function assumes the file is a Mako template.
78 :param c: Fabric connection.
80 :param env: Environment dictionary to be used as Mako template
81 context.
83 Typical usage is something like::
85 from fabric import task
86 from wuttamess import sync, util
88 root = sync.make_root('files')
89 env = {}
91 @task
92 def foo(c):
94 # define possible renderers for fabsync
95 renderers = {'mako': util.mako_renderer(c, env)}
97 sync.check_isync(c, root, 'etc/postfix', renderers=renderers)
98 """
99 env = env or {}
101 def render( # pylint: disable=redefined-builtin,unused-argument
102 path: Path, vars: Mapping[str, Any], **kwargs
103 ) -> bytes:
104 return Template(filename=str(path)).render(**env)
106 return render
109def set_timezone(c, timezone):
110 """
111 Set the system timezone.
113 :param c: Fabric connection.
115 :param timezone: Standard timezone name,
116 e.g. ``'America/Chicago'``.
117 """
118 c.run(f"bash -c 'echo {timezone} > /etc/timezone'")
120 if is_symlink(c, "/etc/localtime"):
121 c.run(f"ln -sf /usr/share/zoneinfo/{timezone} /etc/localtime")
122 else:
123 c.run(f"cp /usr/share/zoneinfo/{timezone} /etc/localtime")