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

idlesign / makeapp / 15539601990

09 Jun 2025 04:36PM UTC coverage: 90.121% (+0.03%) from 90.087%
15539601990

push

github

idlesign
Add basic linting rules

821 of 911 relevant lines covered (90.12%)

3.6 hits per line

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

73.33
/src/makeapp/utils.py
1
import configparser
4✔
2
import fileinput
4✔
3
import logging
4✔
4
import os
4✔
5
import shutil
4✔
6
import sys
4✔
7
import tempfile
4✔
8
from collections.abc import Generator
4✔
9
from configparser import ConfigParser
4✔
10
from contextlib import contextmanager
4✔
11
from pathlib import Path
4✔
12
from subprocess import PIPE, STDOUT, Popen
4✔
13
from textwrap import indent
4✔
14

15
from .exceptions import CommandError
4✔
16

17
LOG = logging.getLogger(__name__)
4✔
18
PYTHON_VERSION = sys.version_info
4✔
19

20

21
def configure_logging(
4✔
22
        level: int | None = None,
23
        logger: logging.Logger | None = None,
24
        format: str = '%(message)s'
25
):
26
    """Switches on logging at a given level. For a given logger or globally.
27

28
    :param level:
29
    :param logger:
30
    :param format:
31

32
    """
33
    logging.basicConfig(format=format, level=level if level else None)
4✔
34
    logger and logger.setLevel(level or logging.INFO)
4✔
35

36

37
def get_user_dir() -> Path:
4✔
38
    """Returns the user's home directory."""
39
    return Path(os.path.expanduser('~'))
4✔
40

41

42
def read_ini(fpath: Path) -> ConfigParser:
4✔
43
    """Read a .ini file.
44

45
    :param fpath:
46
    """
47
    cfg = configparser.ConfigParser()
×
48
    cfg.read(f'{fpath}')
×
49
    return cfg
×
50

51

52
@contextmanager
4✔
53
def chdir(target_path):
4✔
54
    """Context manager.
55
     
56
    Temporarily switches the current working directory.
57
    
58
    """
59
    curr_dir = os.getcwd()
4✔
60
    os.chdir(target_path)
4✔
61

62
    try:
4✔
63
        yield
4✔
64

65
    finally:
66
        os.chdir(curr_dir)
4✔
67

68

69
@contextmanager
4✔
70
def temp_dir() -> Generator[str, None, None]:
4✔
71
    """Context manager to temporarily create a directory."""
72

73
    dir_tmp = tempfile.mkdtemp(prefix='makeapp_')
×
74

75
    try:
×
76
        yield dir_tmp
×
77

78
    finally:
79
        shutil.rmtree(dir_tmp, ignore_errors=True)
×
80

81

82
def replace_infile(filepath: str, pairs: dict[str, str]):
4✔
83
    """Replaces some term by another in file contents.
84

85
    :param filepath:
86
    :param pairs: search -> replace.
87

88
    """
89
    with fileinput.input(files=filepath, inplace=True) as f:
×
90

91
        for line in f:
×
92

93
            for search, replace in pairs.items():
×
94
                line = line.replace(search, replace)
×
95

96
            sys.stdout.write(line)
×
97

98

99
def check_command(command: str, *, hint: str):
4✔
100
    """Checks whether a command is available.
101
    If not - raises an exception.
102

103
    :param command:
104
    :param hint:
105

106
    """
107
    try:
4✔
108
        run_command(f'type {command}')
4✔
109

110
    except CommandError:
×
111
        raise CommandError(
×
112
            f"Failed to execute '{command}' command. "
113
            f"Check {hint} is installed and available.")
114

115

116
def run_command(command: str, *, err_msg: str = '', env: dict | None = None) -> list[str]:
4✔
117
    """Runs a command in a shell process.
118

119
    Returns a list of strings gathered from a command.
120

121
    :param command:
122

123
    :param err_msg: Message to show on error.
124

125
    :param env: Environment variables to use.
126

127
    :raises: CommandError
128

129
    """
130
    if env:
4✔
131
        env = {**os.environ, **env}
×
132

133
    LOG.debug(f'Run command: {command} ...')
4✔
134

135
    prc = Popen(command, stdout=PIPE, stderr=STDOUT, shell=True, universal_newlines=True, env=env)
4✔
136

137
    out, _ = prc.communicate()
4✔
138
    LOG.debug(indent(out, prefix="    "))
4✔
139

140
    data = [stripped for item in out.splitlines() if (stripped := item.strip())]
4✔
141

142
    if prc.returncode:
4✔
143
        raise CommandError(err_msg or f"Command `{command}` failed: %s" % '\n'.join(data))
×
144

145
    return data
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