This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Utils.py
69 lines (50 loc) · 1.81 KB
/
Utils.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
'''Utils module.'''
import os
import shutil
from tornado.stack_context import StackContext
import Privilege
class FileUtils:
'''Utils.'''
@staticmethod
def copydir(src, dst):
'''Copy directory.
Args:
src (string): Source path.
dst (string): Destination path.
Returns:
None
'''
def _copy_fn(src, dst, follow_symlinks=True):
'''Copytree helper function.
Args:
src (string): Source path.
dst (string): Destination path.
follow_symlinks: Follow symbolic link or not.
Returns:
None
'''
shutil.copy(src, dst, follow_symlinks=False)
with StackContext(Privilege.fileaccess):
shutil.copytree(src, dst, symlinks=True, copy_function=_copy_fn)
@staticmethod
def setperm(path, uid, gid, umask=0o777):
'''Set permission of the file or directory.
Args:
path (string): File or directory path.
uid (int): UID of the files and directories.
gid (int): GID of the files and directories.
umask (int) optional: Umask of the files and directories.
Returns:
None
'''
path_set = set([path])
with StackContext(Privilege.fileaccess):
for root, dirs, files in os.walk(path, followlinks=False):
for name in dirs:
path_set.add(os.path.abspath(os.path.join(root, name)))
for name in files:
path_set.add(os.path.abspath(os.path.join(root, name)))
with StackContext(Privilege.fullaccess):
for path in path_set:
os.chown(path, uid, gid)
os.chmod(path, os.stat(path).st_mode & umask)