import os
import shutil
import re
import zipfile
import xml.etree.ElementTree as ET
from tempfile import TemporaryDirectory
import psycopg2

conn = psycopg2.connect(
    database='innoter', user='postgres', password='postgres', host='192.168.0.107', port='5432')
cursor = conn.cursor()

dst_dir = r"\\nas1\storage\DG_archive\sat"
path_list = []
cursor.execute(
    """SELECT path, order_id
    FROM geoarchive.dg_orders
    WHERE aero is not TRUE""")
results = cursor.fetchall()

for i, result in enumerate(results):
    
    with zipfile.ZipFile(zip_path) as zf:
#         order_shape = [fnm for fnm in zf.namelist() if re.match(r'.+ORDER_SHAPE.+', fnm, re.I)]
#         if not order_shape:
#         # for fnm in zf.namelist():
#         #     if re.match(r'.+ORDER_SHAPE.+', fnm, re.I) is None:
#             cursor.execute("""UPDATE geoarchive.dg_orders
#                            SET aero = TRUE
#                            WHERE order_id = %s""", [order_id, ],)
#             conn.commit()
#             print(80*'=', order_id, 80*'=')
#             aero_list.append(order_id)
#
# print('\nDone:\n', len(aero_list))
# for i in aero_list:
#     print(i)

zip_path, order_id = result[0], result[1]
    print(i + 1, zip_path)
    dst_filepath = os.path.join(dst_dir, os.path.basename(zip_path))
    shutil.move(zip_path, dst_filepath)
    cursor.execute("""UPDATE geoarchive.dg_orders
        SET path = %s
        WHERE order_id = %s""", [dst_filepath, order_id, ], )
    conn.commit()
    print('Готово...\n')



# 