Welcome to mirror list, hosted at ThFree Co, Russian Federation.

utils.py « calm - cygwin.com/git/cygwin-apps/calm.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9f758139cd1cd09d08d9fa898a4e16174de3679a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python3
#
# Copyright (c) 2019 Jon Turney
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

#
# utility functions
#

import filecmp
import logging
import os
import subprocess
from contextlib import contextmanager


#
# touch a file
#
def touch(fn, times=None):
    try:
        with open(fn, 'a'):  # ensure fn exists
            os.utime(fn, times)
    except PermissionError:
        logging.error("couldn't update mtime for %s" % (fn))


#
# ensure a directory exists
#
# for some versions of python, os.makedirs() can still raise FileExistsError
# even when exists_ok=True, if the directory mode is not as expected.
#
def makedirs(name):
    try:
        os.makedirs(name, exist_ok=True)
    except FileExistsError:
        pass


#
# remove any empty subdirectories below a given depth
#
def rmemptysubdirs(path, depth=3):
    for (dirpath, _subdirs, _files) in os.walk(path, topdown=False, followlinks=True):
        # don't do anything while above the given depth
        if len(os.path.relpath(dirpath, path).split(os.sep)) < depth:
            continue

        # check whether the directory is now empty after processing any
        # subdirectories, and if so, remove it
        if len(os.listdir(dirpath)) == 0:
            logging.debug('rmdir %s' % dirpath)
            os.rmdir(dirpath)


#
# a wrapper for open() which:
#
# - atomically changes the file contents (atomic)
# - only touches the mtime if the file contents have changed (move-if-changed)
#
@contextmanager
def open_amifc(filepath, mode='w', cb=None):
    tmppath = filepath + '~'
    while os.path.isfile(tmppath):
        tmppath += '~'

    try:
        with open(tmppath, mode) as file:
            logging.debug('writing %s for move-if-changed' % (tmppath))
            yield file

        changed = not os.path.exists(filepath) or not filecmp.cmp(tmppath, filepath, shallow=False)
        if changed:
            logging.info("writing %s" % (filepath))
            os.rename(tmppath, filepath)
        else:
            logging.debug("unchanged %s" % (filepath))
    finally:
        try:
            os.remove(tmppath)
        except OSError:
            pass

    # notify callback if file was changed or not
    if cb:
        cb(changed)


#
# run a subprocess, logging it's output
#
# N.B. because we use shell=True, args should be a string to be supplied to 'sh
# -c', not a list.
#
def system(args):
    logging.debug(args)
    try:
        output = subprocess.check_output(args, shell=True,
                                         stdin=subprocess.DEVNULL,
                                         stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        for l in e.output.decode().splitlines():
            logging.warning(l)
        logging.warning('%s exited %d' % (args.split()[0], e.returncode))
    else:
        for l in output.decode().splitlines():
            logging.info(l)


#
# This provides a simple wrapper around a function which takes a pathname as
# it's only parameter.  The result is cached as long as the mtime of the
# pathname is unchanged.
#
def mtime_cache(user_function):
    sentinel = object()          # unique object used to signal cache misses
    cache = {}

    def wrapper(key):
        # make sure path is absolute
        key = os.path.abspath(key)

        (result, mtime) = cache.get(key, (sentinel, 0))

        new_mtime = os.path.getmtime(key)

        # cache hit
        if result is not sentinel:
            # cache valid
            if new_mtime == mtime:
                return result
            else:
                logging.debug('%s cache invalidated by mtime change' % key)

        # cache miss
        result = user_function(key)
        cache[key] = (result, new_mtime)
        return result

    return wrapper