Commit bb414682 authored by Renán Sosa Guillen's avatar Renán Sosa Guillen

L2A products population

parent 5adbe140
...@@ -5,30 +5,35 @@ ...@@ -5,30 +5,35 @@
Usage: Usage:
$ cd GeoInt_SIDT $ cd GeoInt_SIDT
$ python manage.py populate_products_l2a [path_where_files_are_located] $ python manage.py populate_products_l2a
Example:
$ cd GeoInt_SIDT
$ python manage.py populate_products_l2a /home/geointdev/sidt-env/sidt-zip/L2A/
""" """
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from catalog.models import Product_l1c, Product_l2a from catalog.models import Product_l1c, Product_l2a
import os import os, sys, paramiko
SSH_ADDRESS = "192.168.1.59"
SSH_USERNAME = "geoint"
SSH_COMMAND = "cd NAS/sentinelImages/L2A/; pwd"
class Command(BaseCommand): ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
def add_arguments(self, parser):
## positional arguments
parser.add_argument('file_path', nargs=1, type=str)
class Command(BaseCommand):
def handle(self, *args, **options): def handle(self, *args, **options):
path = options['file_path'][0] ssh_stdin = ssh_stdout = ssh_stderr = None
file_list = os.listdir(path)
file_list.sort() try:
ssh.connect(SSH_ADDRESS, username=SSH_USERNAME)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(SSH_COMMAND)
path = ssh_stdout.read().replace("\n", '')
sftp = ssh.open_sftp() ## Creates SFTPClient() object
sftp.chdir(path)
file_list = sftp.listdir()
for file in file_list: for file in file_list:
file_name = file.replace(".zip", '') file_name = file.replace(".zip", '')
...@@ -37,20 +42,30 @@ class Command(BaseCommand): ...@@ -37,20 +42,30 @@ class Command(BaseCommand):
PRODUCT_EXISTS = Product_l2a.objects.filter(identifier=file_name).exists() PRODUCT_EXISTS = Product_l2a.objects.filter(identifier=file_name).exists()
if not PRODUCT_EXISTS: if not PRODUCT_EXISTS:
prod_l1c = Product_l1c.objects.filter(identifier=file_name)[0] prod_l1c = Product_l1c.objects.filter(identifier=file_name.replace("L2A", "L1C"))[0]
product_l2a = Product_l2a( product_l2a = Product_l2a(
prod_l1c = prod_l1c, prod_l1c = prod_l1c,
identifier = prod_l1c.identifier, identifier = file_name,
file_path = path file_path = path
) )
product_l2a.save() product_l2a.save()
print prod_l1c.identifier print "NEW product: " + file_name
else:
"Existe"
# else:
# print "Product " + file_name + " is ALREADY in DB."
except: except:
print "Error con archivo " + file_name print "Error con archivo " + file_name
except Exception as e:
sys.stderr.write("SSH connection error: {0}".format(e))
if ssh_stdout:
sys.stdout.write(ssh_stdout.read())
if ssh_stderr:
sys.stderr.write(ssh_stderr.read())
sftp.close()
ssh.close()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment