-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystemManager.py
More file actions
44 lines (37 loc) · 1.45 KB
/
Copy pathFileSystemManager.py
File metadata and controls
44 lines (37 loc) · 1.45 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
#!/usr/bin/env python3
import os
import sys
import shutil
import logging
import coloredlogs
coloredlogs.install(level="INFO", stream=sys.stdout)
class FileSystemManager:
@staticmethod
def prepare_target(target_path: str) -> None:
"""Prepare target path by deleting existing content"""
if os.path.exists(target_path):
logging.info("PURGE %s", target_path)
shutil.rmtree(target_path)
os.makedirs(target_path, exist_ok=True)
@staticmethod
def write_file(path: str, content: str) -> None:
"""Create missing folders and write file"""
dir = os.path.dirname(path)
if not os.path.exists(dir):
os.makedirs(dir, exist_ok=True)
with open(path, "w") as file:
file.write(content)
@staticmethod
def copy_file(source_path: str, target_path: str) -> None:
"""Create missing folders and copy file to new path"""
target_dir = os.path.dirname(target_path)
if not os.path.exists(target_dir):
os.makedirs(target_dir, exist_ok=True)
shutil.copyfile(source_path, target_path)
@staticmethod
def copy_directory(source_path: str, target_path: str) -> None:
"""Create missing folders and copy directory to new path"""
target_dir = os.path.dirname(target_path)
if not os.path.exists(target_dir):
os.makedirs(target_dir, exist_ok=True)
shutil.copytree(source_path, target_path)