Completed
Pull Request — master (#1829)
by Zatreanu
02:30 queued 59s
created

coalib.main()   B

Complexity

Conditions 5

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 5
dl 0
loc 27
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
10
11
def main(log_printer=None, section: Section=None):
12
    start_path = get_config_directory(section)
13
    log_printer = log_printer or LogPrinter(ConsolePrinter())
14
15
    if start_path is None:
16
        return 255
17
18
    orig_files = Globbing.glob(os.path.abspath(
19
        os.path.join(start_path, '**', '*.orig')))
20
21
    not_deleted = 0
22
    for ofile in orig_files:
23
        log_printer.info("Deleting old backup file... "
24
                         + os.path.relpath(ofile))
25
        try:
26
            os.remove(ofile)
27
        except OSError as oserror:
28
            not_deleted += 1
29
            log_printer.warn("Couldn't delete {}. {}".format(
30
                os.path.relpath(ofile), oserror.strerror))
31
32
    if not_deleted:
33
        log_printer.warn(str(not_deleted) + " .orig backup files could not be"
34
                         " deleted, possibly because you lack the permission"
35
                         " to do so. coala may not be able to create"
36
                         " backup files when patches are applied.")
37
    return 0
38