forked from andreafrancia/trash-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.py
77 lines (60 loc) · 1.83 KB
/
files.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
# Copyright (C) 2011 Andrea Francia Trivolzio(PV) Italy
from nose.tools import assert_equals
from trashcli.fs import has_sticky_bit
import os, shutil
def having_file(path):
dirname=os.path.dirname(path)
if dirname != '': make_dirs(dirname)
open(path,'w').close()
assert os.path.isfile(path)
make_empty_file = having_file
def write_file(filename, contents=''):
parent = os.path.dirname(filename)
if not os.path.isdir(parent): os.makedirs(parent)
open(filename, 'w').write(contents)
assert_equals(open(filename).read(), contents)
def require_empty_dir(path):
if os.path.exists(path): shutil.rmtree(path)
make_dirs(path)
assert os.path.isdir(path)
assert_equals([], list(os.listdir(path)))
def having_empty_dir(path):
require_empty_dir(path)
def make_dirs(path):
if not os.path.isdir(path):
os.makedirs(path)
assert os.path.isdir(path)
def make_parent_for(path):
parent = os.path.dirname(path)
make_dirs(parent)
def make_sticky_dir(path):
os.mkdir(path)
set_sticky_bit(path)
def make_unsticky_dir(path):
os.mkdir(path)
unset_sticky_bit(path)
def make_dir_unsticky(path):
assert_is_dir(path)
unset_sticky_bit(path)
def assert_is_dir(path):
assert os.path.isdir(path)
def set_sticky_bit(path):
import stat
os.chmod(path, os.stat(path).st_mode | stat.S_ISVTX)
def unset_sticky_bit(path):
import stat
os.chmod(path, os.stat(path).st_mode & ~ stat.S_ISVTX)
assert not has_sticky_bit(path)
def touch(path):
open(path,'a+').close()
def ensure_non_sticky_dir(path):
import os
assert os.path.isdir(path)
assert not has_sticky_bit(path)
def make_unreadable_file(path):
write_file(path, '')
import os
os.chmod(path, 0)
from nose.tools import assert_raises
with assert_raises(IOError):
open(path).read()