Passed
Push — master ( 416358...d9c5dd )
by Max
01:22
created

mutmut_helper.backup_files()   A

Complexity

Conditions 3

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
import os.path
2
import subprocess
3
4
5
def untracked_files():
6
    for file_ in (
7
        subprocess.run(["hg", "status", "--unknown"], capture_output=True)
8
        .stdout.decode()
9
        .split("\n")
10
    ):
11
        if file_:
12
            yield file_[2:]
13
14
15
def backup_files(file_list):
16
    for file_ in file_list:
17
        fn, ext = os.path.splitext(file_)
18
        if ext == ".bak":
19
            yield fn
20
21
22
def cache_files(file_list):
23
    for file_ in file_list:
24
        dirname, fn = os.path.split(file_)
25
        cache_dir = os.path.join(dirname, "__pycache__")
26
        prefix = fn + os.extsep
27
        try:
28
            cache_files = os.listdir(cache_dir)
29
        except FileNotFoundError:
30
            continue
31
        for cache_file in cache_files:
32
            if cache_file.startswith(prefix):
33
                yield os.path.join(cache_dir, cache_file)
34
35
36
for cache_file in cache_files(backup_files(untracked_files())):
37
    os.remove(cache_file)
38