-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfs.py
78 lines (55 loc) · 1.55 KB
/
fs.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
import json
import os
import utils
builtin_open = open
write_indicator = utils.null_context
def open(path, mode='rb'):
make_parent(path)
return builtin_open(path, mode)
def move(path, newpath):
with write_indicator:
destroy(newpath)
os.rename(path, newpath)
def append(path, data):
if data:
with write_indicator:
with open(path, 'ab') as file:
file.write(data)
def write_json(path, obj):
with write_indicator:
with open(path + '.new', 'wt') as file:
json.dump(obj, file)
move(path + '.new', path)
def destroy(path): # removes a file or directory and all descendants
with write_indicator:
if isdir(path):
for file in os.listdir(path):
destroy(path + '/' + file)
os.rmdir(path)
if isfile(path):
os.remove(path)
def free():
_, frsize, _, _, bfree = os.statvfs('.')[:5]
return frsize*bfree
def listdir(): # accept no arguments; only allow listing of /
return os.listdir()
def isdir(path):
return get_mode(path) & 0x4000
def isfile(path):
return get_mode(path) & 0x8000
def get_mode(path):
try:
return os.stat(path)[0]
except:
return 0
def make_parent(path):
parts = path.strip('/').split('/')
path = parts[0]
for part in parts[1:]:
if isfile(path):
with write_indicator:
os.remove(path)
if not isdir(path):
with write_indicator:
os.mkdir(path)
path += '/' + part