1
|
1 #!/usr/bin/python
|
|
2 import re
|
|
3 import os
|
|
4 import time
|
|
5 import dateutil
|
|
6 import dateutil.parser as parser2
|
|
7 import datetime
|
|
8 import calendar
|
|
9
|
|
10 def parse_date(adate):
|
|
11 """
|
|
12 Convert human-entered time into linux integer timestamp
|
|
13
|
|
14 @param adate string Human entered date to parse into linux time
|
|
15
|
|
16 @return integer Linux time equivalent or 0 if no date supplied
|
|
17 """
|
|
18 adate = adate.strip()
|
|
19 if adate > '':
|
|
20 adateP = parser2.parse(adate, fuzzy=True)
|
|
21 #dateP2 = time.mktime(adateP.timetuple())
|
|
22 # This handles UTC & daylight savings exactly
|
|
23 return calendar.timegm(adateP.timetuple())
|
|
24 return 0
|
|
25
|
|
26
|
|
27 def get_unix_time(vtime, voffset=0):
|
|
28 return float(vtime) - int(voffset)/100*60*60
|
|
29
|
|
30
|
|
31 def natural_sort_key(s, _nsre=re.compile('([0-9]+)')):
|
|
32 return [int(text) if text.isdigit() else text.lower()
|
|
33 for text in re.split(_nsre, s)]
|
|
34
|
|
35
|
|
36 class date_matcher(object):
|
|
37 """
|
|
38 Enables cycling through a list of versions and picking the one that matches
|
|
39 or immediately preceeds a given date. As soon as an item is found, subsequent
|
|
40 calls to date_matcher return false (because of the self.found flag)
|
|
41 """
|
|
42 def __init__(self, unix_time):
|
|
43 """
|
|
44 @param adate linux date/time
|
|
45 """
|
|
46 self.found = False
|
|
47 self.unix_time = unix_time
|
|
48
|
|
49 def __iter__(self):
|
|
50 return self
|
|
51
|
|
52 def next(self, unix_datetime):
|
|
53 select = False
|
|
54 if (self.found == False) and (self.unix_time > 0) and (unix_datetime <= self.unix_time):
|
|
55 self.found = True
|
|
56 select = True
|
|
57 return select
|
|
58
|
|
59
|
|
60 def dateISOFormat(atimestamp):
|
|
61 return datetime.datetime.isoformat(datetime.datetime.fromtimestamp(atimestamp))
|
|
62
|
|
63 def lightDate(unixtime):
|
|
64 return datetime.datetime.utcfromtimestamp(float(unixtime)).strftime('%Y-%m-%d %H:%M')
|
|
65
|
|
66 def move_files(source_path, destination_path, file_paths):
|
|
67 """
|
|
68 MOVE FILES TO CACHE FOLDER (RATHER THAN COPYING THEM) FOR GREATER EFFICIENCY.
|
|
69 Since a number of data source systems have hidden / temporary files in their
|
|
70 data folder structure, a list of file_paths is required to select only that
|
|
71 content that should be copied over. Note: this will leave skeleton of folders; only files are moved.
|
|
72
|
|
73 Note: Tried using os.renames() but it errors when attempting to remove folders
|
|
74 from git archive that aren't empty due to files that are not to be copied.
|
|
75
|
|
76
|
|
77 @param source_path string Absolute folder path to move data files from
|
|
78 @param destination_path string Absolute folder path to move data files to
|
|
79 @param file_paths string List of files and their relative paths from source_path root
|
|
80 """
|
|
81 for file_name in file_paths:
|
|
82 if len(file_name):
|
|
83 print "(" + file_name + ")"
|
|
84 v_path = os.path.dirname(os.path.join(destination_path, file_name))
|
|
85 if not os.path.isdir(v_path):
|
|
86 os.makedirs(v_path)
|
|
87 os.rename(os.path.join(source_path, file_name), os.path.join(destination_path, file_name) )
|
|
88
|