-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtils.py
More file actions
executable file
·95 lines (76 loc) · 2.19 KB
/
Utils.py
File metadata and controls
executable file
·95 lines (76 loc) · 2.19 KB
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
88
89
90
91
92
#!/usr/bin/python
import re,os,time,datetime,subprocess,sys
import os.path
import platform
from shutil import copyfile
import sqlite3
import time,random
class DateString:
def __init__(self):
self.yesterday = str(datetime.date.fromtimestamp(time.time() - (60*60*24) ).strftime("%Y-%m-%d"))
self.today = str(datetime.date.fromtimestamp(time.time()).strftime("%Y-%m-%d"))
self.tomorrow = str(datetime.date.fromtimestamp(time.time() + (60*60*24) ).strftime("%Y-%m-%d"))
self.now = str(time.strftime('%X %x %Z'))
class Files:
def __init__(self):
self.dir = ''
self.data = []
self.file_exists = 0
def mkdir(self):
if not os.path.isdir(self.dir):
if 'Win' in platform.system():
subprocess.call(["md", self.dir], shell=True)
else:
subprocess.call(["mkdir", self.dir])
def write_file(self,filename,list):
f = open(filename,'w')
for line in list:
f.write(line + '\n')
f.close()
def write_file_append(self,filename,list):
f = open(filename,'a')
for line in list:
f.write(line)
f.close()
def write_log(self,logfile,logentry):
f = open(logfile,'a')
reportDate = str(time.strftime("%x - %X"))
f.write(reportDate + " :" + logentry)
f.close()
def read_file(self,filename):
self.data = []
self.file_exists = 1
# Testing if file exists.
if os.path.isfile(filename):
try:
f = open(filename,'r')
except IOError:
print "Failed opening ", filename
sys.exit(2)
for line in f:
line = line.strip()
self.data.append(line)
f.close()
else:
# Set the file_exists flag in case caller cares.
self.file_exists = 0
def copy_file(self,src, dest):
try:
copyfile(src, dest)
except IOError:
print "Failed file copy ", src,dest
sys.exit(2)
def stat_file(self,fname):
blocksize = 4096
hash_sha = hashlib.sha256()
f = open(fname, "rb")
buf = f.read(blocksize)
while 1:
hash_sha.update(buf)
buf = f.read(blocksize)
if not buf:
break
checksum = hash_sha.hexdigest()
filestat = os.stat(fname)
filesize = filestat[6]
return checksum,filesize