1
+ import yaml
2
+ import logging
3
+ import os
4
+ import subprocess
5
+
6
+ def init_logger ():
7
+ log_format = "[%(filename)s %(lineno)d %(levelname)s] %(message)s "
8
+ date_format = '%Y-%m-%d %H:%M:%S %a '
9
+ logging .basicConfig (level = logging .INFO ,
10
+ format = log_format ,
11
+ datefmt = date_format ,
12
+ )
13
+
14
+ class CheckOut :
15
+ def __init__ (self , rtt_repo , rtt_branch ):
16
+ self .root = os .getcwd ()
17
+ self .rtt_repo = rtt_repo
18
+ self .rtt_branch = rtt_branch
19
+
20
+ def __exclude_file (self , file_path ):
21
+ dir_number = file_path .split ('/' )
22
+ ignore_path = file_path
23
+
24
+ # gets the file path depth.
25
+ for i in dir_number :
26
+ # current directory.
27
+ dir_name = os .path .dirname (ignore_path )
28
+ ignore_path = dir_name
29
+ # judge the ignore file exists in the current directory.
30
+ ignore_file_path = os .path .join (dir_name , ".ignore_format.yml" )
31
+ if not os .path .exists (ignore_file_path ):
32
+ continue
33
+ try :
34
+ with open (ignore_file_path ) as f :
35
+ ignore_config = yaml .safe_load (f .read ())
36
+ file_ignore = ignore_config .get ("file_path" , [])
37
+ dir_ignore = ignore_config .get ("dir_path" , [])
38
+ except Exception as e :
39
+ logging .error (e )
40
+ continue
41
+ logging .debug ("ignore file path: {}" .format (ignore_file_path ))
42
+ logging .debug ("file_ignore: {}" .format (file_ignore ))
43
+ logging .debug ("dir_ignore: {}" .format (dir_ignore ))
44
+ try :
45
+ # judge file_path in the ignore file.
46
+ for file in file_ignore :
47
+ if file is not None :
48
+ file_real_path = os .path .join (dir_name , file )
49
+ if file_real_path == file_path :
50
+ logging .info ("ignore file path: {}" .format (file_real_path ))
51
+ return 0
52
+
53
+ file_dir_path = os .path .dirname (file_path )
54
+ for _dir in dir_ignore :
55
+ if _dir is not None :
56
+ dir_real_path = os .path .join (dir_name , _dir )
57
+ if file_dir_path .startswith (dir_real_path ):
58
+ logging .info ("ignore dir path: {}" .format (dir_real_path ))
59
+ return 0
60
+ except Exception as e :
61
+ logging .error (e )
62
+ continue
63
+
64
+ return 1
65
+
66
+ # def get_new_file(self):
67
+ # file_list = list()
68
+ # try:
69
+ # os.system('git remote add rtt_repo {}'.format(self.rtt_repo))
70
+ # os.system('git fetch rtt_repo')
71
+ # os.system('git merge rtt_repo/{}'.format(self.rtt_branch))
72
+ # os.system('git reset rtt_repo/{} --soft'.format(self.rtt_branch))
73
+ # os.system('git status > git.txt')
74
+ # except Exception as e:
75
+ # logging.error(e)
76
+ # return None
77
+ # try:
78
+ # with open('git.txt', 'r') as f:
79
+ # file_lines = f.readlines()
80
+ # except Exception as e:
81
+ # logging.error(e)
82
+ # return None
83
+ # file_path = ''
84
+ # for line in file_lines:
85
+ # if 'new file' in line:
86
+ # file_path = line.split('new file:')[1].strip()
87
+ # logging.info('new file -> {}'.format(file_path))
88
+ # elif 'deleted' in line:
89
+ # logging.info('deleted file -> {}'.format(line.split('deleted:')[1].strip()))
90
+ # elif 'modified' in line:
91
+ # file_path = line.split('modified:')[1].strip()
92
+ # logging.info('modified file -> {}'.format(file_path))
93
+ # else:
94
+ # continue
95
+
96
+ # result = self.__exclude_file(file_path)
97
+ # if result != 0:
98
+ # file_list.append(file_path)
99
+
100
+ # return file_list
101
+
102
+ def get_new_file (self ):
103
+ result = subprocess .run (['git' , 'diff' , '--name-only' , 'HEAD' , 'origin/master' , '--diff-filter=ACMR' , '--no-renames' , '--full-index' ], stdout = subprocess .PIPE )
104
+ file_list = result .stdout .decode ().strip ().split ('\n ' )
105
+ for line in file_list :
106
+ logging .info ("modified file -> {}" .format (line ))
107
+ result = self .__exclude_file (line )
108
+ if result != 0 :
109
+ file_list .append (line )
110
+
111
+ return file_list
0 commit comments