Пример #1
0
def uncompress(src_file, dest_dir):
    result = get_filetype(src_file)
    if not result[0]:
        return (False, result[1], '')
    filefmt = result[1]

    result = make_dir(dest_dir)
    if not result:
        return (False, '创建解压目录失败', filefmt)

    if filefmt in ('tgz', 'tar'):
        try:
            tar = tarfile.open(src_file)
            names = tar.getnames()
            for name in names:
                tar.extract(name, dest_dir)
            tar.close()
        except Exception as e:
            return (False, e, filefmt)
    elif filefmt == 'zip':
        try:
            zip_file = zipfile.ZipFile(src_file)
            for names in zip_file.namelist():
                zip_file.extract(names, dest_dir)
            zip_file.close()
        except Exception as e:
            return (False, e, filefmt)
    elif filefmt == 'rar':
        try:
            rar = rarfile.RarFile(src_file)
            os.chdir(dest_dir)
            rar.extractall()
            rar.close()
        except Exception as e:
            return (False, e, filefmt)
    elif filefmt == 'gz':
        try:

            f_name = dest_dir + '/' + os.path.basename(src_file)
            # 获取文件的名称,去掉  
            g_file = gzip.GzipFile(src_file)
            # 创建gzip对象  
            open(f_name, "w+").write(g_file.read())
            # gzip对象用read()打开后,写入open()建立的文件中。  
            g_file.close()
            # 关闭gzip对象  

            result = get_filetype(src_file)
            if not result[0]:
                new_filefmt = '未知'
            else:
                new_filefmt = result[1]
            return (True, '解压后的文件格式为:' + new_filefmt, filefmt)
        except Exception as e:
            return (False, e, filefmt)
    else:
        return (False, '文件格式不支持或者不是压缩文件', filefmt)

    return (True, '', filefmt)
Пример #2
0
def uncompress(src_file, dest_dir):
    result = get_filetype(src_file)
    if not result[0] :
        return (False, result[1], '')
    filefmt = result[1]

    result = make_dir(dest_dir)
    if not result :
        return (False, '创建解压目录失败', filefmt)

    if filefmt in ('tgz', 'tar') :
        try :
            tar = tarfile.open(src_file)  
            names = tar.getnames()   
            for name in names:  
                tar.extract(name, dest_dir)  
            tar.close()
        except Exception as e :
            return (False, e, filefmt)
    elif filefmt == 'zip':
        try :
            zip_file = zipfile.ZipFile(src_file)  
            for names in zip_file.namelist():  
                zip_file.extract(names, dest_dir)  
            zip_file.close()  
        except Exception as e :
            return (False, e, filefmt)
    elif filefmt == 'rar' :
        try :
            rar = rarfile.RarFile(src_file)  
            os.chdir(dest_dir)
            rar.extractall()  
            rar.close()  
        except Exception as e :
            return (False, e, filefmt)
    elif filefmt == 'gz' :
        try :

            f_name = dest_dir + '/' + os.path.basename(src_file)
            # 获取文件的名称,去掉  
            g_file = gzip.GzipFile(src_file)  
            # 创建gzip对象  
            open(f_name, "w+").write(g_file.read())  
            # gzip对象用read()打开后,写入open()建立的文件中。  
            g_file.close()  
            # 关闭gzip对象  

            result = get_filetype(src_file)
            if not result[0] :
                new_filefmt = '未知'
            else :
                new_filefmt = result[1]
            return (True, '解压后的文件格式为:' + new_filefmt, filefmt)
        except Exception as e :
            return (False, e, filefmt)
    else :
        return (False, '文件格式不支持或者不是压缩文件', filefmt)

    return (True, '', filefmt)
Пример #3
0
    def import_upload(self,
                      file,
                      name,
                      yaml_tpye='main',
                      file_type='tasks',
                      describe=''):

        result = upload_file(file)
        if not result:
            self.logger.error('为用户' + self.username +
                              '上传文件方式导入ansible yaml数据失败,上传文件失败,原因:' +
                              result[1])
            return (False, '上传文件失败,' + result[1])
        else:
            this_path = result[1]

        if yaml_tpye == 'roles':
            roles_path = '/dev/shm/lykops/ansible/yaml/' + random_str(
                ranlen=16)
            result = uncompress(this_path, roles_path)
            if not result[0]:
                self.logger.error(
                    '为用户' + self.username +
                    '上传文件方式导入ansible yaml数据失败,原因:不是一个压缩文件。注:系统要求导入类型为roles时,文件格式必须是zip格式的压缩文件、目录或者文件名等其中之一'
                )
                return (False, '上传文件不是一个格式为zip格式的压缩文件')

            result = self.read2file_api.roles(roles_path,
                                              preserve=True,
                                              together=True,
                                              name=name,
                                              describe=describe)
            if result[0]:
                self.logger.info('为用户' + self.username + '从服务器本地路径为' +
                                 this_path + '导入ansible yaml数据成功')
                return (True, '对yaml文件进行语法验证等处理工作成功')
            else:
                self.logger.error(
                    '为用户' + self.username + '从服务器本地路径为' + this_path +
                    '导入ansible yaml数据失败,对yaml文件进行语法验证等处理工作时失败,原因:' + result[1])
                return (False, '对yaml文件进行语法验证等处理工作时失败,原因:' + result[1])
        elif yaml_tpye in ('main', 'full_roles'):
            result = get_filetype(this_path)
            if not result[0]:
                self.logger.error(
                    '为用户' + self.username +
                    '上传文件方式导入ansible yaml数据失败,原因:指定的路径为文件,但不是文本文件。注:系统要求导入类型为main或者full_roles时,如果路径是目录或者是压缩文件解压后的目录,必须带有main.yaml;如果是文件,必须是一个格式为zip格式的压缩文件或者是文本文件'
                )
                return (
                    False,
                    '指定的路径为文件,但不是文本文件。注:系统要求导入类型为main或者full_roles时,如果路径是目录或者是压缩文件解压后的目录,必须带有main.yaml;如果是文件,必须是一个格式为zip格式的压缩文件或者是文本文件'
                )

            if result[1] == 'txt':
                pass
            elif result[1] == 'zip':
                dest_dir = '/dev/shm/lykops/ansible/yaml/' + random_str(
                    ranlen=16)
                result = uncompress(this_path, dest_dir)
                if not result[0]:
                    self.logger.error(
                        '为用户' + self.username +
                        '上传文件方式导入ansible yaml数据失败,原因:路径为' + this_path +
                        '不是一个格式为zip的压缩文件。注:系统要求导入类型为main或者full_roles时,如果路径是目录或者是压缩文件解压后的目录,必须带有main.yaml;如果是文件,必须是一个格式为zip格式的压缩文件或者是文本文件'
                    )
                    return (False, '路径为' + this_path + '不是一个格式为zip的压缩文件')

                this_path = dest_dir + '/main.yaml'
                if not os.path.exists(this_path):
                    self.logger.error(
                        '为用户' + self.username +
                        '上传文件方式导入ansible yaml数据失败,原因:压缩文件解压后发现无法找到mai.yamln。注:系统要求导入类型为main或者full_roles时,如果路径是目录或者是压缩文件解压后的目录,必须带有main.yaml;如果是文件,必须是一个格式为zip格式的压缩文件或者是文本文件'
                    )
                    return (False, '压缩文件解压后发现无法找到main.yaml')
            else:
                self.logger.error(
                    '为用户' + self.username +
                    '上传文件方式导入ansible yaml数据失败,原因:暂时只支持txt或者zip格式文件。注:系统要求导入类型为main或者full_roles时,如果路径是目录或者是压缩文件解压后的目录,必须带有main.yaml;如果是文件,必须是一个格式为zip格式的压缩文件或者是文本文件'
                )
                return (False, '暂时只支持txt或者zip格式文件')

            result = self.read2file_api.main(this_path,
                                             preserve=True,
                                             together=True,
                                             name=name,
                                             describe=describe)
            if result[0]:
                self.logger.info('为用户' + self.username +
                                 '上传文件方式导入ansible yaml数据成功')
                return (True, '对yaml文件进行语法验证等处理工作成功')
            else:
                self.logger.error(
                    '为用户' + self.username +
                    '上传文件方式导入ansible yaml数据失败,对yaml文件进行语法验证等处理工作时失败,原因:' +
                    result[1])
                return (False, '对yaml文件进行语法验证等处理工作时失败,原因:' + result[1])
        else:
            result = get_filetype(this_path)
            if not result[0]:
                self.logger.error(
                    '为用户' + self.username +
                    '上传文件方式导入ansible yaml数据失败,原因:不是文本文件。注:系统要求导入类型为include时,暂时只支持文本格式文件'
                )
                return (False,
                        '指定的路径为文件,但不是文本文件。注:系统要求导入类型为include时,暂时只支持文本格式文件')

            if result[1] == 'txt':
                result = self.read2file_api.include(this_path,
                                                    file_type=file_type,
                                                    preserve=True,
                                                    name=name,
                                                    describe=describe)
                if result[0]:
                    self.logger.info('为用户' + self.username +
                                     '上传文件方式导入ansible yaml数据成功')
                    return (True, '对yaml文件进行语法验证等处理工作成功')
                else:
                    self.logger.error(
                        '为用户' + self.username +
                        '上传文件方式导入ansible yaml数据失败,对yaml文件进行语法验证等处理工作时失败,原因:' +
                        result[1])
                    return (False, '对yaml文件进行语法验证等处理工作时失败,原因:' + result[1])
            else:
                self.logger.error(
                    '为用户' + self.username +
                    '上传文件方式导入ansible yaml数据失败,原因:不是txt格式文件。注:系统要求导入类型为include时,暂时只支持文本格式文件'
                )
                return (False, '暂时只支持文本格式文件')
Пример #4
0
def uncompress(src_file, dest_dir):
    result = get_filetype(src_file)
    if not result[0]:
        return (False, result[1], '')
    filefmt = result[1]

    if filefmt not in ('zip'):
        return (False, '本系统暂时只支持解压zip格式的文件')

    result = make_dir(dest_dir)
    if not result:
        return (False, '创建解压目录失败', filefmt)

    if filefmt in ('tgz', 'tar'):
        try:
            tar = tarfile.open(src_file)
            names = tar.getnames()
            for name in names:
                tar.extract(name, dest_dir)
            tar.close()
        except Exception as e:
            return (False, e, filefmt)
    elif filefmt == 'zip':
        try:
            zip_file = zipfile.ZipFile(src_file)
            for names in zip_file.namelist():
                zip_file.extract(names, dest_dir)
            zip_file.close()
        except Exception as e:
            return (False, e, filefmt)
    elif filefmt == 'rar':
        try:
            rar = rarfile.RarFile(src_file)
            os.chdir(dest_dir)
            rar.extractall()
            rar.close()
        except Exception as e:
            if str(e) == "Unrar not installed? (rarfile.UNRAR_TOOL='unrar')":
                return (False, '请在服务器上安装unrar,yum install -y unrar', filefmt)

            return (False, e, filefmt)
    elif filefmt == 'gz':
        try:
            f_name = dest_dir + '/' + os.path.basename(src_file)
            # 获取文件的名称,去掉
            g_file = gzip.GzipFile(src_file)
            # 创建gzip对象
            open(f_name, "w+").write(g_file.read())
            # gzip对象用read()打开后,写入open()建立的文件中。
            g_file.close()
            # 关闭gzip对象

            result = get_filetype(src_file)
            if not result[0]:
                new_filefmt = '未知'
            else:
                new_filefmt = result[1]
            return (True, '解压后的文件格式为:' + new_filefmt, filefmt)
            '''
            suffix = os.path.splitext(src_file)[-1]
            if suffix :
                f_name = src_file.replace(suffix, "") 
            else :
                f_name = src_file + '_' + random_str()
            #获取文件的名称,去掉  
            g_file = gzip.GzipFile(src_file)  
            #创建gzip对象  
            open(f_name, "w+").write(g_file.read())  
            #gzip对象用read()打开后,写入open()建立的文件中。  
            g_file.close()  
            #关闭gzip对象  
            
            result = get_filetype(src_file)
            if not result[0] :
                return (False, result[1], '')
            new_filefmt = result[1]
            return (True, '解压后的文件格式为:' + new_filefmt, filefmt)
            result = uncompress(f_name, dest_dir)
            if not result[0] :
                new_filefmt = result[2]
                if new_filefmt not in ('gz','tar','tgz','zip' ,'rar') :
                    return (True, '解压后的文件格式为:' + new_filefmt, filefmt)
                else :
                    return (False, '解压后的文件格式不支持或者不是压缩文件', filefmt)
            '''

        except Exception as e:
            return (False, e, filefmt)
    else:
        return (False, '文件格式不支持或者不是压缩文件', filefmt)

    return (True, '', filefmt)