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

kivy / python-for-android / 5976260269

25 Aug 2023 01:05PM UTC coverage: 58.986% (+0.3%) from 58.676%
5976260269

push

github

web-flow
Standardise `ensure_dir` and `rmdir` (#2871)

* Standardise ensure_dir and rmdir

* Standardise ensure_dir and rmdir

* Add libmysqlclient to broken list

* Libtorrent failing to be rebuilt

* Add boost to broken recipes list

940 of 2241 branches covered (0.0%)

Branch coverage included in aggregate %.

73 of 113 new or added lines in 21 files covered. (64.6%)

3 existing lines in 3 files now uncovered.

4715 of 7346 relevant lines covered (64.18%)

2.56 hits per line

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

93.18
/pythonforandroid/util.py
1
import contextlib
4✔
2
from fnmatch import fnmatch
4✔
3
import logging
4✔
4
from os.path import exists, join
4✔
5
from os import getcwd, chdir, makedirs, walk
4✔
6
from platform import uname
4✔
7
from shutil import rmtree
4✔
8
from tempfile import mkdtemp
4✔
9

10
from pythonforandroid.logger import (logger, Err_Fore, error, info)
4✔
11

12
LOGGER = logging.getLogger("p4a.util")
4✔
13

14
build_platform = "{system}-{machine}".format(
4✔
15
    system=uname().system, machine=uname().machine
16
).lower()
17
"""the build platform in the format `system-machine`. We use
2✔
18
this string to define the right build system when compiling some recipes or
19
to get the right path for clang compiler"""
20

21

22
@contextlib.contextmanager
4✔
23
def current_directory(new_dir):
4✔
24
    cur_dir = getcwd()
4✔
25
    logger.info(''.join((Err_Fore.CYAN, '-> directory context ', new_dir,
4✔
26
                         Err_Fore.RESET)))
27
    chdir(new_dir)
4✔
28
    yield
4✔
29
    logger.info(''.join((Err_Fore.CYAN, '<- directory context ', cur_dir,
4✔
30
                         Err_Fore.RESET)))
31
    chdir(cur_dir)
4✔
32

33

34
@contextlib.contextmanager
4✔
35
def temp_directory():
4✔
36
    temp_dir = mkdtemp()
4✔
37
    try:
4✔
38
        logger.debug(''.join((Err_Fore.CYAN, ' + temp directory used ',
4✔
39
                              temp_dir, Err_Fore.RESET)))
40
        yield temp_dir
4✔
41
    finally:
42
        rmtree(temp_dir)
4✔
43
        logger.debug(''.join((Err_Fore.CYAN, ' - temp directory deleted ',
4✔
44
                              temp_dir, Err_Fore.RESET)))
45

46

47
def walk_valid_filens(base_dir, invalid_dir_names, invalid_file_patterns):
4✔
48
    """Recursively walks all the files and directories in ``dirn``,
49
    ignoring directories that match any pattern in ``invalid_dirns``
50
    and files that patch any pattern in ``invalid_filens``.
51

52
    ``invalid_dirns`` and ``invalid_filens`` should both be lists of
53
    strings to match. ``invalid_dir_patterns`` expects a list of
54
    invalid directory names, while ``invalid_file_patterns`` expects a
55
    list of glob patterns compared against the full filepath.
56

57
    File and directory paths are evaluated as full paths relative to ``dirn``.
58

59
    """
60

61
    for dirn, subdirs, filens in walk(base_dir):
4✔
62

63
        # Remove invalid subdirs so that they will not be walked
64
        for i in reversed(range(len(subdirs))):
4✔
65
            subdir = subdirs[i]
4✔
66
            if subdir in invalid_dir_names:
4✔
67
                subdirs.pop(i)
4✔
68

69
        for filen in filens:
4✔
70
            for pattern in invalid_file_patterns:
4✔
71
                if fnmatch(filen, pattern):
4✔
72
                    break
4✔
73
            else:
74
                yield join(dirn, filen)
4✔
75

76

77
def load_source(module, filename):
4✔
78
    # Python 3.5+
79
    import importlib.util
4✔
80
    if hasattr(importlib.util, 'module_from_spec'):
4!
81
        spec = importlib.util.spec_from_file_location(module, filename)
4✔
82
        mod = importlib.util.module_from_spec(spec)
4✔
83
        spec.loader.exec_module(mod)
4✔
84
        return mod
4✔
85
    else:
86
        # Python 3.3 and 3.4:
87
        from importlib.machinery import SourceFileLoader
×
88
        return SourceFileLoader(module, filename).load_module()
×
89

90

91
class BuildInterruptingException(Exception):
4✔
92
    def __init__(self, message, instructions=None):
4✔
93
        super().__init__(message, instructions)
4✔
94
        self.message = message
4✔
95
        self.instructions = instructions
4✔
96

97

98
def handle_build_exception(exception):
4✔
99
    """
100
    Handles a raised BuildInterruptingException by printing its error
101
    message and associated instructions, if any, then exiting.
102
    """
103
    error('Build failed: {}'.format(exception.message))
4✔
104
    if exception.instructions is not None:
4!
105
        info('Instructions: {}'.format(exception.instructions))
4✔
106
    exit(1)
4✔
107

108

109
def rmdir(dn, ignore_errors=False):
4✔
110
    if not exists(dn):
4!
NEW
111
        return
×
112
    LOGGER.debug("Remove directory and subdirectory {}".format(dn))
4✔
113
    rmtree(dn, ignore_errors)
4✔
114

115

116
def ensure_dir(dn):
4✔
117
    if exists(dn):
4✔
118
        return
4✔
119
    LOGGER.debug("Create directory {0}".format(dn))
4✔
120
    makedirs(dn)
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

© 2025 Coveralls, Inc