1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 """ History file management related functionalities. """
21
22
23 import sys
24 import os
25 from os.path import join
26
27
29 """ To manage EC history files. """
30
31 weak_num = ""
32 branch_name = ""
33 file_dict = {}
34
35 - def __init__(self, arg1, arg2, arg3):
36 """ Constructor. """
37 self.path = str(arg1)
38 self.weak_num = str(arg2)
39 self.branch_name = str(arg3)
40
42 """Find the new path where the history file will be updated based on the week number and branch.
43
44 This will normally the same path as used to copy from net drive to local drive.
45 But for new branch / week number, the path will be different. """
46 branch_dir_list = self.branch_name.split(".")
47 for dir_ in branch_dir_list:
48 self.path = os.path.join(self.path, dir_)
49 if(self.path.endswith("\\0")):
50 self.path = self.path[0:-2]
51 return str(self.path)
52
54 """ Finds the path of the history file based on input
55 branch and week number. """
56 branch_dir_list = self.branch_name.split(".")
57 for dir_ in branch_dir_list:
58 if(not os.path.exists(os.path.join(self.path, dir_))):
59 break
60 else:
61 self.path = os.path.join(self.path, dir_)
62 if(self.path.endswith("\\0")):
63 self.path = self.path[0:-2]
64
66 """ Finds the closest history file match to week number. """
67 ret_file_name = None
68 file_names = os.listdir(self.path)
69 file_names_alone = file_names[:]
70
71
72 for name in file_names:
73 if(os.path.isdir(os.path.join(self.path, name))):
74 file_names_alone.remove(name)
75
76 if(len(file_names_alone) > 0):
77 file_names_alone.sort()
78 low_index = 0
79 high_index = len(file_names_alone) - 1
80
81 if(high_index == 0):
82 temp_name = file_names_alone[low_index]
83 if(self.weak_num >= temp_name[0:4]):
84 return temp_name
85 else:
86 return ret_file_name
87
88
89 while(low_index < high_index):
90 mid_index = (low_index + high_index) / 2
91 temp_name = file_names_alone[mid_index]
92 if(temp_name[0:4] < self.weak_num):
93 low_index = mid_index + 1
94 else:
95 high_index = mid_index
96
97 temp_name = file_names_alone[high_index]
98 if( self.weak_num >= temp_name[0:4]):
99 ret_file_name = file_names_alone[high_index]
100 else:
101 if(high_index != 0):
102 ret_file_name = file_names_alone[high_index - 1]
103
104 return str(ret_file_name)
105