forked from BrettMayson/Lir
/
fs.py
executable file
·88 lines (75 loc) · 2.31 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
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3.4
import os
import shutil
import subprocess
DEBUG = False
def debug(*text):
if DEBUG:
print("[DEBUG]",text)
def system(cmd):
with open(os.devnull, 'wb') as devnull:
subprocess.check_call(cmd, stdout=devnull, stderr=subprocess.STDOUT)
def expand_path(path):
#expand home directory
path = os.path.expanduser(path)
if not path.startswith("/"):
path = os.getcwd() + "/" + path
#resolve relative paths
path = os.path.realpath(path)
return path
def move(src,dst):
import lang
src = expand_path(src)
dst = expand_path(dst)
shutil.move(src,dst)
debug(lang.get("files","move"),src,dst)
return True
def copy(src,dst):
import lang
src = expand_path(src)
dst = expand_path(dst)
if os.path.isdir(src):
files = [f for f in os.listdir(src) if os.path.isfile(os.path.join(src, f))]
dirs = [f for f in os.listdir(src) if os.path.isdir(os.path.join(src, f))]
for f in files:
if not f.endswith("~"):
shutil.copy(os.path.join(src,f),os.path.join(dst,f))
debug(lang.get("files","copy"),os.path.join(src,f),os.path.join(dst,f))
for d in dirs:
if copy(os.path.join(src,f),os.path.join(dst,f)) == False:
return False
return True
elif os.path.isfile(src):
shutil.copy(src,dst)
debug(lang.get("files","copy"),src,dst)
return True
else:
return False
def delete(path):
import lang
path = expand_path(path)
if os.path.exists(path):
if os.path.isdir(path):
shutil.rmtree(path)
debug(lang.get("files","delete_dir"),path)
return True
elif os.path.isfile(path):
os.remove(path)
debug(lang.get("files","delete_file"),path)
return True
else:
return False
else:
return False
def create(path):
path = expand_path(path)
if not os.path.exists(path):
open(path, 'a').close()
def create_directory(directory):
import lang
directory = expand_path(directory)
if not os.path.exists(directory):
debug(lang.get("files","create_dir"),directory)
os.makedirs(directory)
def home():
return expand_path("~")