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