Completed
Pull Request — master (#2077)
by Lasse
02:11
created

ApplyPatchAction.apply()   F

Complexity

Conditions 9

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
c 1
b 0
f 0
dl 0
loc 35
rs 3
1
import shutil
2
from os.path import isfile
3
from os import remove
4
5
from coalib.results.Diff import ConflictError
6
from coalib.results.result_actions.ResultAction import ResultAction
7
8
9
class ApplyPatchAction(ResultAction):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ResultAction does not seem to be defined.
Loading history...
10
11
    SUCCESS_MESSAGE = "Patch applied successfully."
12
13
    @staticmethod
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable staticmethod does not seem to be defined.
Loading history...
14
    def is_applicable(result, original_file_dict, file_diff_dict):
15
        if not result.diffs:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable result does not seem to be defined.
Loading history...
16
            return False
17
18
        try:
19
            for filename in result.diffs:
20
                if filename in file_diff_dict:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable filename does not seem to be defined.
Loading history...
21
                    result.diffs[filename].__add__(
22
                        file_diff_dict[filename])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable file_diff_dict does not seem to be defined.
Loading history...
23
24
            return True
25
        except ConflictError:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable ConflictError does not seem to be defined.
Loading history...
26
            return False
27
28
    def apply(self,
29
              result,
30
              original_file_dict,
31
              file_diff_dict,
32
              no_orig: bool=False):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable bool does not seem to be defined.
Loading history...
33
        """
34
        Apply the patch automatically.
35
36
        :param no_orig: Whether or not to create .orig backup files
37
        """
38
        for filename in result.diffs:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable result does not seem to be defined.
Loading history...
39
            diff = file_diff_dict[filename]
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable file_diff_dict does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable filename does not seem to be defined.
Loading history...
40
            pre_patch_filename = (diff.rename
41
                                  if diff.rename is not False
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable diff does not seem to be defined.
Loading history...
42
                                  else filename)
43
            if filename in file_diff_dict:
44
                file_diff_dict[filename] += result.diffs[filename]
45
            else:
46
                file_diff_dict[filename] = result.diffs[filename]
47
48
            # Backup file, backups are deleted on startup -> don't override
49
            if not no_orig and not isfile(pre_patch_filename + ".orig"):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable pre_patch_filename does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable no_orig does not seem to be defined.
Loading history...
50
                shutil.copy2(pre_patch_filename, pre_patch_filename + ".orig")
51
52
            diff = file_diff_dict[filename]
53
            if diff.delete:
54
                remove(pre_patch_filename)
55
            else:
56
                new_filename = (diff.rename
57
                                if diff.rename is not False
58
                                else filename)
59
                with open(new_filename, mode='w', encoding='utf-8') as file:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable new_filename does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable file does not seem to be defined.
Loading history...
60
                    file.writelines(diff.modified)
61
62
        return file_diff_dict
63