|
1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
|
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
|
3
|
|
|
# this work for additional information regarding copyright ownership. |
|
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
|
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
|
6
|
|
|
# the License. You may obtain a copy of the License at |
|
7
|
|
|
# |
|
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
9
|
|
|
# |
|
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
|
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13
|
|
|
# See the License for the specific language governing permissions and |
|
14
|
|
|
# limitations under the License. |
|
15
|
|
|
|
|
16
|
|
|
import os |
|
17
|
|
|
import glob |
|
18
|
|
|
import shutil |
|
19
|
|
|
|
|
20
|
|
|
__all__ = [ |
|
21
|
|
|
'get_full_file_list', |
|
22
|
|
|
'get_dirs_in_path', |
|
23
|
|
|
'copy_files', |
|
24
|
|
|
'remove_file' |
|
25
|
|
|
] |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
def get_full_file_list(file_path_glob): |
|
29
|
|
|
""" |
|
30
|
|
|
Return a list of absolute file paths to all the files in the provided file |
|
31
|
|
|
path glob. |
|
32
|
|
|
|
|
33
|
|
|
:type file_path_glob: ``str`` |
|
34
|
|
|
""" |
|
35
|
|
|
file_list = [] |
|
36
|
|
|
|
|
37
|
|
|
for file_name in glob.glob(file_path_glob): |
|
38
|
|
|
full_file_path = os.path.abspath(file_name) |
|
39
|
|
|
file_list.append(full_file_path) |
|
40
|
|
|
|
|
41
|
|
|
return file_list |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
def get_dirs_in_path(file_path): |
|
45
|
|
|
""" |
|
46
|
|
|
Retrieve full paths to the directories in the provided file path. |
|
47
|
|
|
|
|
48
|
|
|
:param file_path: Parent directory file path. |
|
49
|
|
|
:type file_path: ``str`` |
|
50
|
|
|
|
|
51
|
|
|
:rtype: ``list`` |
|
52
|
|
|
""" |
|
53
|
|
|
names = os.listdir(file_path) |
|
54
|
|
|
|
|
55
|
|
|
result = [] |
|
56
|
|
|
for name in names: |
|
57
|
|
|
full_path = os.path.join(file_path, name) |
|
58
|
|
|
|
|
59
|
|
|
if not os.path.isdir(full_path): |
|
60
|
|
|
continue |
|
61
|
|
|
|
|
62
|
|
|
result.append(full_path) |
|
63
|
|
|
return result |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
def copy_files(file_paths, destination, ignore_errors=True): |
|
67
|
|
|
""" |
|
68
|
|
|
Copy files to the provided destination. |
|
69
|
|
|
|
|
70
|
|
|
:type file_paths: ``list`` |
|
71
|
|
|
:type destination: ``str`` |
|
72
|
|
|
|
|
73
|
|
|
:param ignore_errors: True to ignore errors if a source or destination doesnt'e exist. |
|
74
|
|
|
:type ignore_errors: ``bool`` |
|
75
|
|
|
""" |
|
76
|
|
|
|
|
77
|
|
|
for file_path in file_paths: |
|
78
|
|
|
try: |
|
79
|
|
|
shutil.copy(src=file_path, dst=destination) |
|
80
|
|
|
except IOError as e: |
|
81
|
|
|
if not ignore_errors: |
|
82
|
|
|
raise e |
|
83
|
|
|
|
|
84
|
|
|
return True |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
def remove_file(file_path, ignore_errors=True): |
|
88
|
|
|
try: |
|
89
|
|
|
os.remove(file_path) |
|
90
|
|
|
except Exception as e: |
|
91
|
|
|
if not ignore_errors: |
|
92
|
|
|
raise e |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
def remove_dir(dir_path, ignore_errors=True): |
|
96
|
|
|
""" |
|
97
|
|
|
Recursively remove a directory. |
|
98
|
|
|
|
|
99
|
|
|
:param dir_path: Directory to be removed. |
|
100
|
|
|
:type dir_path: ``str`` |
|
101
|
|
|
|
|
102
|
|
|
:param ignore_errors: True to ignore errors during removal. |
|
103
|
|
|
:type ignore_errors: ``bool`` |
|
104
|
|
|
""" |
|
105
|
|
|
try: |
|
106
|
|
|
shutil.rmtree(dir_path, ignore_errors) |
|
107
|
|
|
except Exception as e: |
|
108
|
|
|
if not ignore_errors: |
|
109
|
|
|
raise e |
|
110
|
|
|
|