-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
105 lines (92 loc) · 2.87 KB
/
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import re
__author__="langiewi_m"
__date__ ="$Jun 25, 2010 10:21:13 AM$"
import os
import os.path
from time import time
from time import sleep
from commands import getstatusoutput
#TODO: ta funkcja ma byc prywatna
def ned_walk(filename, dir, elements):
if dir.find("lost+found") != -1:
return
if len(elements) == 0:
fullfilename = os.path.join(dir, filename)
print "running: sudo touch \"" + fullfilename + "\""
run_command("sudo touch \"" + fullfilename + "\"", log=False)
def noemptydirs(dir, filename):
print "START noemptydirs(" + dir + ", " + filename + ")"
starttime = time()
os.path.walk(dir, ned_walk, filename)
endtime = time()
print "END noemptydirs(" + dir + ", " + filename + ") (time: ", endtime - starttime, ")"
class Log:
NONE, LITTLE, MUCH = range(3)
def run_command(cmd, log=Log.MUCH, pretend=False):
if log == Log.MUCH:
print " START COMMAND:",
starttime = time()
if log != Log.NONE:
print cmd
if not pretend:
os.system(cmd)
if log == Log.MUCH:
endtime = time()
print " END COMMAND: ", cmd, " (time: ", endtime - starttime, ")"
def try_command_out(command, trials=20, pause=5):
status = 0
output = ""
for i in range(trials):
status, output = getstatusoutput(command)
if status == 0:
break
sleep(pause)
print "Warning: non 0 status when executing command:", command
print "status:" , status
print "output:"
print output
print
print "trying again..."
if status != 0:
raise Exception("Error trying command: " + command + "\nstatus: " + str(status) + "\noutput:\n" + output)
return output
def make_path(path):
"""
make whole path if not exists
"""
try:
os.makedirs(path)
except os.error: #TODO: lapac tylko wyjatek typu File exists
pass
def time_str(sec):
h = int(sec / 3600)
sec -= h * 3600
m = int(sec / 60)
sec -= m * 60
s = int(sec)
sec -= s
s100 = int(sec*100)
result = str(s) + " seconds " + str(s100) + " miliseconds "
if m > 0:
result = str(m) + " minutes " + result
if h > 0:
result = str(h) + " hours " + result
return result
def walk_exclude_walk(data, dir, fnames):
"""
TODO: ta funkcja ma byc prywatna
"""
print "walk_execute_walk: dir:", dir
walk, data, exclude = data
for f in fnames[:]:
ff = os.path.join(dir,f)
print "re.search(exclude, \"" + ff + "\")"
if re.search(exclude, ff):
print "remove:", f
fnames.remove(f)
walk(data, dir, fnames)
def walk_exclude(topdir, walk, data, exclude):
print "walk_execute: topdir:", topdir, "data:", data, "exclude:", exclude
os.path.walk(topdir, walk_exclude_walk, (walk, data, exclude))
if __name__ == "__main__":
raise Exception