• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

idlesign / uwsgiconf / 15296925549

28 May 2025 09:43AM UTC coverage: 98.081%. Remained the same
15296925549

push

github

idlesign
Add ruff config.

4650 of 4741 relevant lines covered (98.08%)

3.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.3
/src/uwsgiconf/sysinit.py
1
from collections.abc import Callable
4✔
2
from os.path import abspath, basename, dirname
4✔
3
from textwrap import dedent
4✔
4

5
from .config import Configuration, Section
4✔
6
from .utils import Finder, UwsgiRunner
4✔
7

8
TYPE_UPSTART = 'upstart'
4✔
9
TYPE_SYSTEMD = 'systemd'
4✔
10

11
TYPES: list[str] = [
4✔
12
    TYPE_UPSTART,
13
    TYPE_SYSTEMD,
14
]
15

16

17
def get_tpl_systemd(conf: 'Section') -> str:
4✔
18
    """
19

20
    Some Systemd hints:
21

22
        * uwsgiconf sysinit > my.service
23
        * sudo cp my.service /etc/systemd/system/
24

25
        * sudo sh -c "systemctl daemon-reload; systemctl start my.service"
26

27
        * journalctl -fu my.service
28

29
    :param conf: Section object.
30

31
    """
32
    tpl = '''
4✔
33
        # Place into:   /etc/systemd/system/{project}.service
34
        # Setup:        sudo systemctl enable --now $PWD/{project}.service
35
        # Start:        sudo systemctl start {project}.service
36
        # Stop:         sudo systemctl stop {project}.service
37
        # Restart:      sudo systemctl restart {project}.service
38
        # Status:       systemctl status {project}.service
39
        # Journal:      journalctl -fu {project}.service
40

41
        [Unit]
42
        Description={project} uWSGI Service
43
        Wants=network-online.target
44
        After=network-online.target
45

46
        [Service]
47
        Environment="PATH=%(path)s"
48
        ExecStartPre=-/usr/bin/install -d -m 0755 -o %(user)s -g %(group)s %(runtime_dir)s
49
        ExecStart={command}
50
        Restart=on-failure
51
        KillSignal=SIGTERM
52
        Type=notify
53
        StandardError=syslog
54
        NotifyAccess=all
55
        # Bind to priviledged ports.
56
        AmbientCapabilities=CAP_NET_BIND_SERVICE
57

58
        [Install]
59
        WantedBy=multi-user.target
60
    '''
61
    # We do not use 'RuntimeDirectory' systemd directive since we need to chown.
62

63
    uid, gid = conf.main_process.get_owner()
4✔
64

65
    tpl = tpl % {
4✔
66
        'runtime_dir': conf.replace_placeholders('{project_runtime_dir}'),
67
        'path': UwsgiRunner.get_env_path(),
68
        'user':  uid,
69
        'group': gid,
70
    }
71

72
    return tpl
4✔
73

74

75
def get_tpl_upstart(conf: 'Section') -> str:
4✔
76
    """
77

78
    :param conf: Section object.
79

80
    """
81
    tpl = '''
4✔
82
        # Place into: /etc/init/{project}.conf
83
        # Verify:     initctl check-config {project}
84
        # Start:      initctl start {project}
85
        # Stop:       initctl stop {project}
86
        # Restart:    initctl restart {project}
87
        
88
        description "{project} uWSGI Service"
89
        start on runlevel [2345]
90
        stop on runlevel [06]
91
        
92
        respawn
93
        
94
        env PATH=%(path)s
95
        pre-start exec -/usr/bin/install -d -m 0755 -o %(user)s -g %(group)s %(runtime_dir)s
96
        
97
        exec {command}
98
    '''
99

100
    uid, gid = conf.main_process.get_owner()
4✔
101

102
    tpl = tpl % {
4✔
103
        'path': UwsgiRunner.get_env_path(),
104
        'runtime_dir': conf.replace_placeholders('{project_runtime_dir}'),
105
        'user': uid,
106
        'group': gid,
107
    }
108

109
    return tpl
4✔
110

111

112
TEMPLATES: dict[str, Callable] = {
4✔
113
    TYPE_SYSTEMD: get_tpl_systemd,
114
    TYPE_UPSTART: get_tpl_upstart,
115
}
116

117

118
def get_config(
4✔
119
        systype: str,
120
        *,
121
        conf: Configuration | Section,
122
        conf_path: str,
123
        runner: str = None,
124
        project_name: str = None
125
) -> str:
126
    """Returns init system configuration file contents.
127

128
    :param systype: System type alias, e.g. systemd, upstart
129
    :param conf: Configuration/Section object.
130
    :param conf_path: File path to a configuration file or a command producing such a configuration.
131
    :param runner: Runner command to execute conf_path. Defaults to ``uwsgiconf`` runner.
132
    :param project_name: Project name to override.
133

134
    """
135
    runner = runner or f'{Finder.uwsgiconf()} run'
4✔
136
    conf_path = abspath(conf_path)
4✔
137

138
    if isinstance(conf, Configuration):
4✔
139
        conf = conf.sections[0]  # todo Maybe something more intelligent.
×
140

141
    tpl = dedent(TEMPLATES.get(systype)(conf=conf))
4✔
142

143
    formatted = tpl.strip().format(
4✔
144
        project=project_name or conf.project_name or basename(dirname(conf_path)),
145
        command=f'{runner} {conf_path}',
146
    )
147

148
    return formatted
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc