Repository navigation
Expand file tree
/
Copy pathfolder.py
More file actions
101 lines (89 loc) · 2.91 KB
/
Copy pathfolder.py
File metadata and controls
101 lines (89 loc) · 2.91 KB
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
"""
Wrapper around Folders
"""
import errno
import os
import platform
import shutil
from core.osutils.command import run
from core.osutils.file import File
from core.osutils.process import Process
class Folder(object):
@staticmethod
def create(folder):
if not os.path.exists(folder):
try:
os.makedirs(folder)
except:
raise
@staticmethod
def cleanup(folder, force=True):
if os.path.exists(folder):
try:
shutil.rmtree(folder, False)
except:
if os.path.exists(folder):
if 'Windows' in platform.platform():
# File is locked by some process
print "Failed to delete {0}.".format(folder)
if force:
print "Kill processes associated with this folder."
Process.kill_by_handle(folder)
run('rm -rf ' + folder)
if Folder.exists(folder):
shutil.rmtree(folder)
else:
run('rm -rf ' + folder)
@staticmethod
def exists(path):
if os.path.isdir(path):
return True
else:
return False
@staticmethod
def is_empty(path):
if not os.listdir(path):
return True
else:
return False
@staticmethod
def get_current_folder():
current_folder = os.getcwd()
print "Current dir: " + current_folder
return current_folder
@staticmethod
def navigate_to(folder, relative_from_current_folder=True):
new_folder = folder
if relative_from_current_folder:
new_folder = os.path.join(Folder.get_current_folder(), folder).replace("\"", "")
print "Navigate to: " + new_folder
os.chdir(new_folder)
@staticmethod
def copy(src, dst, only_files=False):
"""
Copy src folder in the dst folder.
:param only_files: If it's set to True - only the files from src folder are copied to dst folder.
If it's set to False - src folder with all files in it are copied.
"""
if only_files is True:
files = os.listdir(src)
for f in files:
f_path = os.path.join(src, f)
File.copy(f_path, dst)
else:
try:
shutil.copytree(src, dst, symlinks=True)
except OSError as exc: # python >2.5
if exc.errno == errno.ENOTDIR:
shutil.copy(src, dst)
else:
raise
@staticmethod
def move(src, dst):
try:
shutil.move(src, dst)
except OSError as exc: # python >2.5
if exc.errno == errno.ENOTDIR:
shutil.move(src, dst)
else:
raise