Failed Conditions
Pull Request — master (#1511)
by Abdeali
01:34
created

coalib.get_config_directory()   B

Complexity

Conditions 5

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 5
dl 0
loc 10
rs 8.5454
1
import os
2
from pyprint.ConsolePrinter import ConsolePrinter
3
4
from coalib.parsing import Globbing
5
from coalib.output.printers.LogPrinter import LogPrinter
6
from coalib.settings.Section import Section
7
from coalib.settings.ConfigurationGathering import get_config_directory
8
9
10
def main(log_printer=None, section: Section=None):
11
    start_path = get_config_directory(section)
12
    log_printer = log_printer or LogPrinter(ConsolePrinter())
13
14
    if start_path is None:
15
        log_printer.err("Can only delete .orig files if .coafile is found")
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:
28
            not_deleted += 1
29
            log_printer.warn("Couldn't delete... " + os.path.relpath(ofile))
30
31
    if not_deleted:
32
        log_printer.warn(str(not_deleted) + " .orig backup files could not be"
33
                         " deleted, possibly because you lack the permission"
34
                         " to do so. coala may not be able to create"
35
                         " backup files when patches are applied.")
36
    return 0
37