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
106
107
108
109
import os
from os import path as osp
import shutil
WC = "*"
REPO_DIR = osp.dirname(osp.dirname(osp.realpath(__file__)))
def extend_path(path):
"""
List files (not directory) matching the given wildcard (only one *
supported)
"""
nb = path.count(WC)
if nb == 0:
if osp.isfile(path):
return [path]
else:
return []
elif nb == 1:
base, wildcard = osp.split(path)
prefix, suffix = wildcard.split(WC)
res = []
for f in os.listdir(base):
if f.startswith(prefix) and f.endswith(suffix) \
and osp.isfile(osp.join(base, f)):
res.append(osp.join(base, f))
return res
else:
raise Exception("multiple wildcards not supported")
def remove_if_exist(path):
"""Only for files, not folders"""
if osp.isfile(path):
os.remove(path)
def clean_folder(path):
if osp.isdir(path):
if osp.islink(path):
os.remove(path)
else:
shutil.rmtree(path)
create_folder(path)
def soft_clean_folder(path):
if osp.isdir(path):
for f in extend_path(osp.join(path, "*")):
remove_if_exist(f)
def reset_to_empty_folder(path):
if folder_exists(path):
if osp.isdir(path):
clean_folder(path)
elif osp.isfile(path):
os.remove(path)
create_folder(path)
else:
raise Exception("I don't know what I'm doing here")
else:
create_folder(path)
def folder_exists(path):
return osp.exists(path)
def create_folder(path):
os.makedirs(path)
def remove_folder(path):
if folder_exists(path):
shutil.rmtree(path)
def tail_lines(filename,linesback=10,returnlist=0):
"""Does what "tail -10 filename" would have done
Parameters:
filename file to read
linesback Number of lines to read from end of file
returnlist Return a list containing the lines instead of a string
"""
avgcharsperline = 75.
with open(filename,'r') as f:
while True:
try:
f.seek(-1 * avgcharsperline * linesback, 2)
except IOError:
f.seek(0)
if f.tell() == 0:
atstart = 1
else:
atstart = 0
lines = f.read().split("\n")
if (len(lines) > (linesback+1)) or atstart:
break
#The lines are bigger than we thought
avgcharsperline = avgcharsperline * 1.3 #Inc avg for retry
if len(lines) > linesback:
start = len(lines) - linesback - 1
else:
start = 0
if returnlist:
return lines[start:len(lines) - 1]
out = ""
for l in lines[start:len(lines) - 1]:
out=out + l + "\n"
return out