fs.py 5.1 KB
Newer Older
T
tangwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

X
xiexionghang 已提交
15
import os
T
tangwei 已提交
16

X
xiexionghang 已提交
17 18
from paddle.fluid.incubate.fleet.utils.hdfs import HDFSClient

T
tangwei 已提交
19

X
xiexionghang 已提交
20
def is_afs_path(path):
T
tangwei 已提交
21
    """is_afs_path
X
xiexionghang 已提交
22
    """
X
xiexionghang 已提交
23 24 25 26
    if path.startswith("afs") or path.startswith("hdfs"):
        return True
    return False

X
xiexionghang 已提交
27 28

class LocalFSClient(object):
X
xiexionghang 已提交
29 30 31
    """
    Util for local disk file_system io 
    """
T
for mat  
tangwei 已提交
32

X
xiexionghang 已提交
33
    def __init__(self):
X
xiexionghang 已提交
34 35
        """R
        """
X
xiexionghang 已提交
36
        pass
T
for mat  
tangwei 已提交
37

X
xiexionghang 已提交
38
    def write(self, content, path, mode):
X
xiexionghang 已提交
39 40 41 42 43 44 45
        """
        write to file
        Args:
            content(string)
            path(string)
            mode(string): w/a  w:clear_write a:append_write
        """
X
xiexionghang 已提交
46
        temp_dir = os.path.dirname(path)
T
for mat  
tangwei 已提交
47
        if not os.path.exists(temp_dir):
X
xiexionghang 已提交
48 49 50 51 52 53 54
            os.makedirs(temp_dir)
        f = open(path, mode)
        f.write(content)
        f.flush()
        f.close()

    def cp(self, org_path, dest_path):
X
xiexionghang 已提交
55 56
        """R
        """
X
xiexionghang 已提交
57 58 59 60 61 62
        temp_dir = os.path.dirname(dest_path)
        if not os.path.exists(temp_dir):
            os.makedirs(temp_dir)
        return os.system("cp -r " + org_path + " " + dest_path)

    def cat(self, file_path):
X
xiexionghang 已提交
63 64
        """R
        """
X
xiexionghang 已提交
65 66 67 68 69 70
        f = open(file_path)
        content = f.read()
        f.close()
        return content

    def mkdir(self, dir_name):
X
xiexionghang 已提交
71 72 73
        """R
        """
        os.makedirs(dir_name)
X
xiexionghang 已提交
74 75

    def remove(self, path):
X
xiexionghang 已提交
76 77
        """R
        """
X
xiexionghang 已提交
78
        os.system("rm -rf " + path)
T
for mat  
tangwei 已提交
79

X
xiexionghang 已提交
80
    def is_exist(self, path):
X
xiexionghang 已提交
81 82
        """R
        """
X
xiexionghang 已提交
83 84 85 86 87
        if os.system("ls " + path) == 0:
            return True
        return False

    def ls(self, path):
X
xiexionghang 已提交
88 89
        """R
        """
X
xiexionghang 已提交
90 91 92
        files = os.listdir(path)
        return files

X
xiexionghang 已提交
93 94

class FileHandler(object):
X
xiexionghang 已提交
95 96 97
    """
    A Smart file handler. auto judge local/afs by path 
    """
T
for mat  
tangwei 已提交
98

X
xiexionghang 已提交
99
    def __init__(self, config):
X
xiexionghang 已提交
100 101
        """R
        """
X
xiexionghang 已提交
102
        if 'fs_name' in config:
T
for mat  
tangwei 已提交
103
            hadoop_home = "$HADOOP_HOME"
X
xiexionghang 已提交
104
            hdfs_configs = {
T
for mat  
tangwei 已提交
105
                "hadoop.job.ugi": config['fs_ugi'],
X
xiexionghang 已提交
106 107 108 109 110 111
                "fs.default.name": config['fs_name']
            }
            self._hdfs_client = HDFSClient(hadoop_home, hdfs_configs)
        self._local_fs_client = LocalFSClient()

    def is_exist(self, path):
X
xiexionghang 已提交
112 113
        """R
        """
X
xiexionghang 已提交
114 115 116 117 118 119
        if is_afs_path(path):
            return self._hdfs_client.is_exist(path)
        else:
            return self._local_fs_client.is_exist(path)

    def get_file_name(self, path):
X
xiexionghang 已提交
120 121
        """R
        """
X
xiexionghang 已提交
122 123 124 125
        sub_paths = path.split('/')
        return sub_paths[-1]

    def write(self, content, dest_path, mode='w'):
X
xiexionghang 已提交
126 127
        """R
        """
X
xiexionghang 已提交
128 129 130 131 132 133 134 135
        if is_afs_path(dest_path):
            file_name = self.get_file_name(dest_path)
            temp_local_file = "./tmp/" + file_name
            self._local_fs_client.remove(temp_local_file)
            org_content = ""
            if mode.find('a') >= 0:
                org_content = self._hdfs_client.cat(dest_path)
            content = content + org_content
T
tangwei 已提交
136 137 138
            self._local_fs_client.write(
                content, temp_local_file, mode
            )  # fleet hdfs_client only support upload, so write tmp file
X
xiexionghang 已提交
139 140 141 142 143 144 145
            self._hdfs_client.delete(dest_path + ".tmp")
            self._hdfs_client.upload(dest_path + ".tmp", temp_local_file)
            self._hdfs_client.delete(dest_path + ".bak")
            self._hdfs_client.rename(dest_path, dest_path + '.bak')
            self._hdfs_client.rename(dest_path + ".tmp", dest_path)
        else:
            self._local_fs_client.write(content, dest_path, mode)
T
for mat  
tangwei 已提交
146

X
xiexionghang 已提交
147
    def cat(self, path):
X
xiexionghang 已提交
148 149
        """R
        """
X
xiexionghang 已提交
150 151 152 153 154
        if is_afs_path(path):
            hdfs_cat = self._hdfs_client.cat(path)
            return hdfs_cat
        else:
            return self._local_fs_client.cat(path)
T
for mat  
tangwei 已提交
155

X
xiexionghang 已提交
156
    def ls(self, path):
X
xiexionghang 已提交
157 158
        """R
        """
159
        files = []
X
xiexionghang 已提交
160
        if is_afs_path(path):
161
            files = self._hdfs_client.ls(path)
T
tangwei 已提交
162 163
            files = [path + '/' + self.get_file_name(fi)
                     for fi in files]  # absulte path
X
xiexionghang 已提交
164
        else:
165
            files = self._local_fs_client.ls(path)
166
            files = [path + '/' + fi for fi in files]  # absulte path
167
        return files
T
for mat  
tangwei 已提交
168

X
xiexionghang 已提交
169
    def cp(self, org_path, dest_path):
X
xiexionghang 已提交
170 171
        """R
        """
X
xiexionghang 已提交
172 173 174 175 176 177
        org_is_afs = is_afs_path(org_path)
        dest_is_afs = is_afs_path(dest_path)
        if not org_is_afs and not dest_is_afs:
            return self._local_fs_client.cp(org_path, dest_path)
        if not org_is_afs and dest_is_afs:
            return self._hdfs_client.upload(dest_path, org_path)
T
for mat  
tangwei 已提交
178
        if org_is_afs and not dest_is_afs:
X
xiexionghang 已提交
179 180
            return self._hdfs_client.download(org_path, dest_path)
        print("Not Suppor hdfs cp currently")