Completed
Pull Request — master (#1937)
by Lasse
01:58
created

coalib.main()   B

Complexity

Conditions 5

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 5
dl 0
loc 28
rs 8.0894
1
import os
2
3
from pyprint.ConsolePrinter import ConsolePrinter
4
5
from coalib.output.printers.LogPrinter import LogPrinter
6
from coalib.parsing import Globbing
7
from coalib.settings.ConfigurationGathering import get_config_directory
8
from coalib.settings.Section import Section
9
from coalib.parsing.Globbing import glob_escape
10
11
12
def main(log_printer=None, section: Section=None):
13
    start_path = get_config_directory(section)
14
    log_printer = log_printer or LogPrinter(ConsolePrinter())
15
16
    if start_path is None:
17
        return 255
18
19
    # start_path may have unintended glob characters
20
    orig_files = Globbing.glob(os.path.join(
21
        glob_escape(start_path), '**', '*.orig'))
22
23
    not_deleted = 0
24
    for ofile in orig_files:
25
        log_printer.info("Deleting old backup file... "
26
                         + os.path.relpath(ofile))
27
        try:
28
            os.remove(ofile)
29
        except OSError as oserror:
30
            not_deleted += 1
31
            log_printer.warn("Couldn't delete {}. {}".format(
32
                os.path.relpath(ofile), oserror.strerror))
33
34
    if not_deleted:
35
        log_printer.warn(str(not_deleted) + " .orig backup files could not be"
36
                         " deleted, possibly because you lack the permission"
37
                         " to do so. coala may not be able to create"
38
                         " backup files when patches are applied.")
39
    return 0
40