|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# ----------------------------------------------------------------------------- |
|
3
|
|
|
# Copyright (c) 2016 Continuum Analytics, Inc. |
|
4
|
|
|
# |
|
5
|
|
|
# Licensed under the terms of the MIT License |
|
6
|
|
|
# (see LICENSE.txt for details) |
|
7
|
|
|
# ----------------------------------------------------------------------------- |
|
8
|
|
|
"""File manager.""" |
|
9
|
|
|
|
|
10
|
|
|
# Standard library imports |
|
11
|
|
|
import os |
|
12
|
|
|
|
|
13
|
|
|
# Local imports |
|
14
|
|
|
from ciocheck.config import (ALL_FILES, COMMITED_MODE, DEFAULT_BRANCH, |
|
15
|
|
|
MODIFIED_FILES, MODIFIED_LINES, STAGED_MODE, |
|
16
|
|
|
UNSTAGED_MODE) |
|
17
|
|
|
from ciocheck.utils import filter_files, get_files |
|
18
|
|
|
from ciocheck.vcs import DiffTool |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
class FileManager(object): |
|
22
|
|
|
"""File manager with git support.""" |
|
23
|
|
|
|
|
24
|
|
|
def __init__(self, folders=None, files=None): |
|
25
|
|
|
"""File manager with git support.""" |
|
26
|
|
|
self.folders = folders or [] |
|
27
|
|
|
self.files = files or [] |
|
28
|
|
|
self.paths = self.files + self.folders |
|
29
|
|
|
self.diff_tool = DiffTool(paths=folders) |
|
30
|
|
|
self.cache = {} |
|
31
|
|
|
|
|
32
|
|
|
def get_files(self, |
|
33
|
|
|
branch=DEFAULT_BRANCH, |
|
34
|
|
|
diff_mode=STAGED_MODE, |
|
35
|
|
|
file_mode=MODIFIED_LINES, |
|
36
|
|
|
extensions=()): |
|
37
|
|
|
"""Find files in paths.""" |
|
38
|
|
|
cache_key = (branch, diff_mode, file_mode, tuple(extensions)) |
|
39
|
|
|
if cache_key in self.cache: |
|
40
|
|
|
results = self.cache[cache_key] |
|
41
|
|
|
else: |
|
42
|
|
|
if file_mode == ALL_FILES: |
|
43
|
|
|
results = get_files(paths=self.paths) |
|
44
|
|
|
results = filter_files(results, extensions) |
|
45
|
|
|
elif file_mode == MODIFIED_FILES: |
|
46
|
|
|
results = self.get_modified_files( |
|
47
|
|
|
branch=branch, diff_mode=diff_mode, extensions=extensions) |
|
48
|
|
|
elif file_mode == MODIFIED_LINES: |
|
49
|
|
|
results = self.get_modified_file_lines( |
|
50
|
|
|
branch=branch, diff_mode=diff_mode, extensions=extensions) |
|
51
|
|
|
self.cache[cache_key] = results |
|
52
|
|
|
|
|
53
|
|
|
return results |
|
54
|
|
|
|
|
55
|
|
View Code Duplication |
def get_modified_file_lines(self, |
|
|
|
|
|
|
56
|
|
|
branch=DEFAULT_BRANCH, |
|
57
|
|
|
diff_mode=STAGED_MODE, |
|
58
|
|
|
extensions=()): |
|
59
|
|
|
"""Find modified lines of files in paths.""" |
|
60
|
|
|
cache_key = (branch, diff_mode, tuple(extensions), 'lines') |
|
61
|
|
|
if cache_key in self.cache: |
|
62
|
|
|
results = self.cache[cache_key] |
|
63
|
|
|
else: |
|
64
|
|
|
if diff_mode == COMMITED_MODE: |
|
65
|
|
|
results = self.diff_tool.commited_file_lines(branch=branch) |
|
66
|
|
|
elif diff_mode == STAGED_MODE: |
|
67
|
|
|
results = self.diff_tool.staged_file_lines() |
|
68
|
|
|
elif diff_mode == UNSTAGED_MODE: |
|
69
|
|
|
results = self.diff_tool.unstaged_file_lines() |
|
70
|
|
|
results = filter_files(results, extensions) |
|
71
|
|
|
self.cache[cache_key] = results |
|
72
|
|
|
|
|
73
|
|
|
return results |
|
74
|
|
|
|
|
75
|
|
View Code Duplication |
def get_modified_files(self, |
|
|
|
|
|
|
76
|
|
|
branch=DEFAULT_BRANCH, |
|
77
|
|
|
diff_mode=STAGED_MODE, |
|
78
|
|
|
extensions=()): |
|
79
|
|
|
"""Find modified files in paths.""" |
|
80
|
|
|
cache_key = (branch, diff_mode, tuple(extensions)) |
|
81
|
|
|
if cache_key in self.cache: |
|
82
|
|
|
results = self.cache[cache_key] |
|
83
|
|
|
else: |
|
84
|
|
|
if diff_mode == COMMITED_MODE: |
|
85
|
|
|
results = self.diff_tool.commited_files(branch=branch) |
|
86
|
|
|
elif diff_mode == STAGED_MODE: |
|
87
|
|
|
results = self.diff_tool.staged_files() |
|
88
|
|
|
elif diff_mode == UNSTAGED_MODE: |
|
89
|
|
|
results = self.diff_tool.unstaged_files() |
|
90
|
|
|
results = filter_files(results, extensions) |
|
91
|
|
|
self.cache[cache_key] = results |
|
92
|
|
|
|
|
93
|
|
|
return results |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
def test(): |
|
97
|
|
|
"""Main local test.""" |
|
98
|
|
|
folders = [os.path.dirname(os.path.realpath(__file__))] |
|
99
|
|
|
file_manager = FileManager(folders=folders) |
|
100
|
|
|
files = file_manager.get_modified_file_lines(diff_mode=COMMITED_MODE) |
|
101
|
|
|
print(files) |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
if __name__ == '__main__': |
|
105
|
|
|
test() |
|
106
|
|
|
|