Ejemplo n.º 1
0
    def create(self, validated_data):
        regiontask = RegionTask.objects.create(**validated_data)
        file_path = regiontask.file.path
        print file_path

        # 需要进入celery进行空间库操作
        # 解压zip文件
        file_dir = os.path.dirname(file_path)
        # r = zipfile.is_zipfile(file_path)
        # if r:
        #     fz = zipfile.ZipFile(file_path, 'r')
        #     for file in fz.namelist():
        #         fz.extract(file,file_dir)
        # else:
        #     print('This is not zip')
        #
        # filename = file_path.split("\\")[-1].split(".zip")[0]
        # unzipfile = os.path.join(file_dir,filename)
        # print unzipfile

        # 解压rar文件
        f = rarfile.is_rarfile(file_path)
        if f:
            fz = rarfile.RarFile(file_path, 'r')
            for file in fz.namelist():
                fz.extract(file, file_dir)
        else:
            print('This is not rar')
        filename = file_path.split("\\")[-1].split(".rar")[0]
        unrarfile = os.path.join(file_dir, filename)
        print unrarfile

        return regiontask
Ejemplo n.º 2
0
 def uncompress(self):
     """解压缩文件"""
     self.uncompressDir = os.path.join(self.tempDir, self.basename)
     if zipfile.is_zipfile(self.inputPath):
         self.logObj.info("压缩文件格式为zip,开始解压...")
         zFile = zipfile.ZipFile(self.inputPath, 'r')
         for f in zFile.namelist():
             zFile.extract(f, self.uncompressDir)
             self.uncompressFile[f] = {'path': os.path.join(self.uncompressDir, f),
                                       'ext': os.path.splitext(f)[1],
                                       'type': f.replace(self.basename, '').replace(os.path.splitext(f)[1], '')
                                       }
     elif tarfile.is_tarfile(self.inputPath):
         self.logObj.info("压缩文件格式为tar,开始解压...")
         tFile = tarfile.open(self.inputPath)
         for f in tFile.getnames():
             tFile.extract(f, self.uncompressDir)
             self.uncompressFile[f] = {'path': os.path.join(self.uncompressDir, f),
                                       'ext': os.path.splitext(f)[1],
                                       'type': f.replace(self.basename, '').replace(os.path.splitext(f)[1], '')
                                       }
     elif rarfile.is_rarfile(self.inputPath):
         self.logObj.info("压缩文件格式为rar,开始解压...")
         rFile = rarfile.RarFile(self.inputPath, mode='r')
         for f in rFile.namelist():
             rFile.extract(f, self.uncompressDir)
             self.uncompressFile[f] = {'path': os.path.join(self.uncompressDir, f),
                                       'ext': os.path.splitext(f)[1],
                                       'type': f.replace(self.basename, '').replace(os.path.splitext(f)[1], '')
                                       }
     else:
         self.logObj.error("无法识别的压缩文件格式!")
         return False
     self.logObj.info("解压缩文件成功.")
Ejemplo n.º 3
0
    def uncompress(self):
        """解压缩文件"""
        self.uncompressDir = os.path.join(self.tempDir, self.basename)
        if zipfile.is_zipfile(self.inputPath):
            print("压缩文件格式为zip,开始解压...")
            zFile = zipfile.ZipFile(self.inputPath, 'r')
            for f in zFile.namelist():
                zFile.extract(f, self.uncompressDir)
                self.uncompressFileList.append(
                    os.path.join(self.uncompressDir, f))

        elif tarfile.is_tarfile(self.inputPath):
            print("压缩文件格式为tar,开始解压...")
            tFile = tarfile.open(self.inputPath)
            for f in tFile.getnames():
                tFile.extract(f, self.uncompressDir)
                self.uncompressFileList.append(
                    os.path.join(self.uncompressDir, f))

        elif rarfile.is_rarfile(self.inputPath):
            print("压缩文件格式为rar,开始解压...")
            rFile = rarfile.RarFile(self.inputPath, mode='r')
            for f in rFile.namelist():
                rFile.extract(f, self.uncompressDir)
                self.uncompressFileList.append(
                    os.path.join(self.uncompressDir, f))
        else:
            print("无法识别的压缩文件格式!")
            return False

        self.classifyUncompressFile()  # 整理解压缩文件

        print("解压缩文件成功.")
Ejemplo n.º 4
0
def append_mapnums():
    conn = psycopg2.connect(dbname=u"mmanageV8.0",
                            user=u"postgres",
                            password=u"Lantucx2018",
                            host=u"localhost",
                            port=u"5432")
    cur = conn.cursor()
    SQL = "SELECT name,file FROM taskpackages_taskpackage"
    cur.execute(SQL)
    name_file = cur.fetchall()
    print name_file
    file_path = ''

    file_dir = os.path.dirname(file_path)         # 解压后文件保存路径
    file_name = os.path.basename(file_path)       # 文件名

    z = zipfile.is_zipfile(file_path)
    r = rarfile.is_rarfile(file_path)
    if z:
        # -ibck: 后台运行; -o+:覆盖已存在文件
        rar_command = '"C:\Program Files\WinRAR\WinRAR.exe" x %s %s -ibck -o+' % (file_path, file_dir)
        rar_command = rar_command.encode('gbk')
        os.system(rar_command)
        print rar_command.decode('gbk')
        print u"解压成功"
    elif r:
        fz = rarfile.RarFile(file_path, 'r')
        for file in fz.namelist():
            fz.extract(file, file_dir)
        print u"解压成功"
    else:
        print(u'This is not zip or rar')
        return False


    taskpath =u"E:\\RGSManager\\celery_app\\测试融合\\Source"
    path = os.path.join(taskpath, u"接图表.gdb", u"DLG_50000", u"GBmaprange")
    # 查找任务包名字和图号
    cursor = arcpy.da.SearchCursor(path, ["new_jbmapn"])
    taskpackage_name = "任务包1号"
    try:

        for row in cursor:
            SQL = "SELECT mapnums from taskpackages_taskpackage where name='{0}'".format(taskpackage_name)  # 按任务包名字查询原mapnums
            cur.execute(SQL)
            mapnums = cur.fetchall()
            old_mapnum = mapnums[0][0]      # 提取原有mapnums

            if old_mapnum is None:
                SQL = "UPDATE taskpackages_taskpackage set mapnums='{0}' where name='{1}'".format(row[0], taskpackage_name)
            else:
                SQL = "UPDATE taskpackages_taskpackage set mapnums='{0}' where name='{1}'".format(old_mapnum +","+ row[0], taskpackage_name)
            print SQL                       # 添加mapnums
            cur.execute(SQL)
            conn.commit()
        conn.close()
    except Exception as e:
        # e = str(e).decode('gbk')
        print [e, ]
Ejemplo n.º 5
0
 def unpackRar(self, comicFile, dirPath):
     if rarfile.is_rarfile(comicFile):
         rar = rarfile.RarFile(comicFile)
         os.mkdir(dirPath)
         rar.extractall(dirPath)
     else:
         comicError(comicFile, "rarErr")
         return
Ejemplo n.º 6
0
def download_movie_sub(movie_object):
    movie_search_keyword = movie_object.movie_search_keyword
    movie_dir = movie_object.dir
    is_downloaded = False  # 字幕是否已经下载

    log.info("------------------------------------------")
    log.info(movie_search_keyword+u" 字幕下载......")
    # 字幕搜索页,得到一堆字幕信息页
    try:
        r = requests.get(base_url+'/search?q='+movie_search_keyword)
        html = etree.HTML(r.text)
        sub_info_details = html.xpath("//td[@class='first']/a/@href")        # 得到字幕信息页
    except Exception :
        log.info(movie_search_keyword+u" 未找到字幕")
        return

    if len(sub_info_details) == 0:
        log.info(movie_search_keyword+u" 未找到字幕")
        return

    # 遍历字幕信息页
    for i in range(len(sub_info_details)):
        # 访问字幕信息页,得到字幕下载页面
        log.info(u"\n字幕信息页:"+base_url+sub_info_details[i])
        try:
            r = requests.get(base_url + sub_info_details[i])
        except Exception:
            log.info(base_url+sub_info_details[i]+u" 请求字幕信息页失败")
            continue

        try:
            html = etree.HTML(r.text)
            sub_download_page_short_path = html.xpath("//a[@id='down1']/@href")     # 字幕下载短地址
            sub_download_page_url = "http:"+sub_download_page_short_path[0]
        except Exception:
            log.info(base_url+sub_info_details[i]+u" 获取字幕文件下载地址失败")
            continue

        log.info(u"字幕下载页:"+sub_download_page_url)
        try:
            r = requests.get(sub_download_page_url)
        except Exception:
            log.info(u"请求字幕下载页失败")
            continue

        try:
            html = etree.HTML(r.text)
            sub_download_url=html.xpath("//div[@class='down clearfix']/ul/li/a/@href")  # 字幕下载地址
            sub_download_url = download_page_base_url+sub_download_url[0]
        except Exception:
            log.info(u"获取字幕下载地址失败")
            continue

        log.info("字幕下载地址:"+sub_download_url)

        try:
            sub_file_name = wget.download(sub_download_url, movie_dir)
        except Exception as e:
            log.info(u"\n字幕下载失败! "+str(e))
            continue

        # 下载成功记录为True
        is_downloaded = True

        # 判断是否UrlDecode成功
        is_url_decode_succeed = True
        sub_file_name_url_decode = urllib.parse.unquote(sub_file_name)

        if sub_file_name != sub_file_name_url_decode:
            # 判断urldecode的文件名是否存在,如果不存在进行修改,如果存在则删除当前文件,跳出循环
            if not os.path.exists(sub_file_name_url_decode):
                try:
                    os.rename(sub_file_name, sub_file_name_url_decode)    # 字幕文件UrlDecode
                    is_url_decode_succeed = True
                except Exception as e:
                    log.info(sub_file_name+" UrlDecode失败。"+str(e))
                    is_url_decode_succeed = False
            else:
                try:
                    log.info("字幕文件已存在,删除多余文件:"+sub_file_name_url_decode)
                    os.remove(sub_file_name)
                except Exception as e:
                    log.info(str(e))
                #字幕文件已经存在,跳出循环。这里没有判断是否是压缩包文件,因为压缩包每次运行完成后会删除,不存在重复的可能
                continue

        # 文件名称urldecode
        if is_url_decode_succeed is True:
            sub_file_name = sub_file_name_url_decode

        log.info("\n字幕文件:"+sub_file_name)

        name, ext = os.path.splitext(sub_file_name)
        is_zip = zipfile.is_zipfile(sub_file_name)
        is_rar = rarfile.is_rarfile(sub_file_name)
        if is_zip:
            try:
                log.info("解压ZIP:"+sub_file_name)
                un_zip(sub_file_name, movie_dir)
                os.remove(sub_file_name)        # 删除zip文件
            except Exception as e:
                log.info(u"解压ZIP失败。"+str(e))
        elif is_rar:
            try:
                log.info(u"解压RAR:"+sub_file_name)
                un_rar(sub_file_name, movie_dir)
                os.remove(sub_file_name)
            except Exception as e:
                log.info(u"解压RAR失败。"+str(e))

    # 如果有下载成功的记录,将已经下载过的电影记录到db中
    if is_downloaded:
        with open(db, 'a+', encoding='utf-8') as db_file:
            db_file.write(movie_object.movie_search_keyword+'\n')
 def test_no_exists_israrfile(self):
     another_file = os.path.join(TESTS_DIR, 'foo.rar')
     self.assertFalse(is_rarfile(another_file))
 def test_no_rar_israrfile(self):
     another_file = os.path.join(TESTS_DIR, '__init__.py')
     self.assertFalse(is_rarfile(another_file))
 def test_rar_israrfile(self):
     rar_filename = os.path.join(TESTS_DIR, 'test_rar.rar')
     self.assertTrue(is_rarfile(rar_filename))
Ejemplo n.º 10
0
 def _rar(self, path):
     '''return list of file names in rar'''
     if not rarfile.is_rarfile(path):
         return None
     r = rarfile.RarFile(path)
     return r.namelist()
Ejemplo n.º 11
0
def testInput(rfName, pfName):
    # test rfName is rar and pfName is file
    if rarfile.is_rarfile(rfName) and os.path.isfile(pfName):
        return True
    else:
        print '[-] file invalid'
def append_mapnums():
    conn = psycopg2.connect(dbname=u"mmanageV8.0",
                            user=u"postgres",
                            password=u"Lantucx2018",
                            host=u"localhost",
                            port=u"5432")
    cur = conn.cursor()
    SQL = "SELECT name,file FROM taskpackages_taskpackage"
    cur.execute(SQL)
    name_file = cur.fetchall()
    num = 0
    error_list = []
    base_path = u"E:\\RGSManager\\media\\"
    while True:
        if num >= 2:
            break
        database_path = name_file[num][1]
        file_path = base_path + database_path
        file_dir = os.path.dirname(file_path)  # 解压后文件保存路径
        file_name = name_file[num][0]  # 文件名
        num += 1

        z = zipfile.is_zipfile(file_path)
        r = rarfile.is_rarfile(file_path)
        if z:
            # -ibck: 后台运行; -o+:覆盖已存在文件
            rar_command = '"C:\Program Files\WinRAR\WinRAR.exe" x %s %s -ibck -o+' % (
                file_path, file_dir)
            rar_command = rar_command.encode('gbk')
            os.system(rar_command)
            # print rar_command.decode('gbk')
            # print u"解压成功"
        elif r:
            fz = rarfile.RarFile(file_path, 'r')
            for file in fz.namelist():
                fz.extract(file, file_dir)
            # print u"解压成功"
        else:
            print(u'This is not zip or rar')
            return False

        taskpath = file_dir + "\\" + file_name
        path = os.path.join(taskpath, u"Source", u"接图表.gdb", u"DLG_50000",
                            u"GBmaprange")

        # 查找任务包名字和图号
        try:
            cursor = arcpy.da.SearchCursor(path, ["new_jbmapn"])
        except Exception as e:
            error_list.append(file_name)
            print "任务包 '{0}' 接图表无法找到".format(file_name)
            continue
        taskpackage_name = file_name
        for row in cursor:
            SQL = "SELECT mapnums from taskpackages_taskpackage where name='{0}'".format(
                taskpackage_name)  # 按任务包名字查询原mapnums
            cur.execute(SQL)
            mapnums = cur.fetchall()
            old_mapnum = mapnums[0][0]  # 提取原有mapnums
            if old_mapnum is None:
                SQL = "UPDATE taskpackages_taskpackage set mapnums='{0}' where name='{1}'".format(
                    row[0], taskpackage_name)
            else:
                SQL = "UPDATE taskpackages_taskpackage set mapnums='{0}' where name='{1}'".format(
                    old_mapnum + "," + row[0], taskpackage_name)
            # print SQL                       # 添加mapnums
            cur.execute(SQL)
            conn.commit()
            print "任务包 {0} 属性添加成功".format(file_name)

    conn.close()
    print "以下任务包接图表文件查找失败"
    print error_list
Ejemplo n.º 13
0
 def test_no_exists_israrfile(self):
     another_file = os.path.join(TESTS_DIR, 'foo.rar')
     self.assertFalse(is_rarfile(another_file))
Ejemplo n.º 14
0
 def test_no_rar_israrfile(self):
     another_file = os.path.join(TESTS_DIR, '__init__.py')
     self.assertFalse(is_rarfile(another_file))
Ejemplo n.º 15
0
 def test_rar_israrfile(self):
     rar_filename = os.path.join(TESTS_DIR, 'test_rar.rar')
     self.assertTrue(is_rarfile(rar_filename))
Ejemplo n.º 16
0
def createregiontask(regiontask_id, regiontask_filepath):
    service_name = 'mmanage' + str(regiontask_id)
    file_dir = os.path.dirname(regiontask_filepath)         # 解压后文件保存路径
    file_name = os.path.basename(regiontask_filepath)       # 文件名
    z = zipfile.is_zipfile(regiontask_filepath)
    r = rarfile.is_rarfile(regiontask_filepath)
    if z:
        # -ibck: 后台运行; -o+:覆盖已存在文件
        rar_command = '"C:\Program Files\WinRAR\WinRAR.exe" x %s %s -ibck -o+' % (regiontask_filepath, file_dir)
        rar_command = rar_command.encode('gbk')
        os.system(rar_command)
        print rar_command.decode('gbk')
        print u"解压 {0} 成功".format(file_name)
    elif r:
        fz = rarfile.RarFile(regiontask_filepath, 'r')
        for file in fz.namelist():
            fz.extract(file, file_dir)
        print u"解压 {0} 成功".format(file_name)
    else:
        print(u'This is not zip or rar')
        return False

    time_ymdhms = datetime.now().strftime(u"%Y%m%d%H%M%S")
    dir_list = os.listdir(file_dir)

    # 创建空间库
    print u"开始创建空间库"
    for dir in dir_list:
        dir_abspath = os.path.join(file_dir, dir)
        if os.path.isdir(dir_abspath):
            for subdir in os.listdir(dir_abspath):
                if subdir.startswith(u"接图表"):
                    gdbpath = os.path.join(dir_abspath, subdir)
                    datatype = u"mapindex"
                    ARCGIS_create_database(gdbpath, time_ymdhms, datatype)

                elif subdir.startswith(u"RGS"):
                    gdbpath = os.path.join(dir_abspath, subdir)
                    datatype = u"rgs"
                    ARCGIS_create_database(gdbpath, time_ymdhms, datatype)

        if dir.startswith(u"接图表"):
            gdbpath = os.path.join(file_dir, dir)
            datatype = u"mapindex"
            ARCGIS_create_database(gdbpath, time_ymdhms, datatype)
            # print u"创建mapindex数据库成功"
        elif dir.startswith(u"RGS"):
            gdbpath = os.path.join(file_dir, dir)
            datatype = u"rgs"
            ARCGIS_create_database(gdbpath, time_ymdhms, datatype)
            # print u"创建rgs数据库成功"

    print u'创建空间库成功'

    mapindexsde = "mapindex" + time_ymdhms + ".sde"
    rgssde = "rgs" + time_ymdhms + ".sde"
    # mmanage.mxd对应的mapindexsde,要放在当前目录下
    # old_mapindexsde = "mapindex20181137133843.sde"
    old_mapindexsde = "mapindex20181206202724.sde"

    # 注册版本,第一个参数为要注册版本的数据集的名称
    print u'开始注册版本'
    mapindexsde_dataset = os.path.join(os.path.dirname(os.path.abspath(__file__)), mapindexsde,
                                       mapindexsde + u".DLG_50000")
    rgssde_dataset = os.path.join(os.path.dirname(os.path.abspath(__file__)), rgssde, rgssde + u".DLG_K050")
    arcpy.RegisterAsVersioned_management(mapindexsde_dataset, u'EDITS_TO_BASE')
    arcpy.RegisterAsVersioned_management(rgssde_dataset, u'EDITS_TO_BASE')
    print u'注册版本成功'

    # 添加用于标记颜色的status字段
    ARCGIS_add_field(mapindexsde)

    # 发布服务
    is_successfull = ARCGIS_publishService(service_name, old_mapindexsde, mapindexsde)

    if is_successfull:
        # 填充postgresql中对应字段
        Posrgres_change_regiontask(regiontask_id, service_name, mapindexsde, rgssde)
        return True
    else:
        return False
Ejemplo n.º 17
0
def testInput(rfName, pfName):
    # test rfName is rar and pfName is file
    if rarfile.is_rarfile(rfName) and os.path.isfile(pfName):
        return True
    else:
        print '[-] file invalid'
Ejemplo n.º 18
0
from unrar import rarfile
#rarfile.RarFile('')
file = rarfile.RarFile('C:/untitled/download/桭源鑫汇3号私募证券投资基金20190620.rar')
file.extractall(path='C:/untitled/un/')
rarfile.is_rarfile()