#!/usr/bin/env python
#
# Created as part of the StratusLab project (http://stratuslab.eu),
# co-funded by the European Commission under the Grant Agreement
# INFSO-RI-261552."
#
# Copyright (c) 2014, Centre National de la Recherche Scientifique (CNRS)
#
# 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.
#



import sys
from optparse import OptionParser
sys.path.append('/var/lib/stratuslab/python')

import stratuslab.Util as Util
from stratuslab.marketplace.Util import Util as MarketplaceUtil
from stratuslab.Util import etree_from_text

from stratuslab import Defaults
from stratuslab.Exceptions import ValidationException

# initialize console logging
import stratuslab.api.LogUtil as LogUtil

LogUtil.get_console_logger()

# define some vars

ENVVAR_ENDPOINT = 'STRATUSLAB_MARKETPLACE_ENDPOINT'
#OPTION_STRING = '--marketplace-endpoint'

ENDPOINT = Defaults.marketplaceEndpoint
ENDPOINT_MKP = ENDPOINT + "/marketplace/metadata"

DCTERMS = "http://purl.org/dc/terms/"
RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
SLTERMS = "http://mp.stratuslab.eu/slterms#"
SLREQ = "http://mp.stratuslab.eu/slreq#"


def MainProgram():
    """
    Show specific image on a given MarketPlace using mechanize
    """

    parser = OptionParser()

    parser.usage = '%prog [options] image-id ...'
    parser.description = 'Show a specific image on MarketPlace.'
    parser.add_option("-i", "--image",
                  action="store", type="string", dest="image")

    (options, args) = parser.parse_args()

    if not options.image:
        parser.error('Please specify the machine image to start')

    ID = options.image

    try:
        import mechanize
    except:
        sys.exit("mechanize not found ! Please install it with pip! Exiting...")

    br = mechanize.Browser()
    response = br.open(ENDPOINT_MKP)
    content = response.read()


    # checking marketplace endpoint URL
    url_is_ok = Util.checkUrlExists(ENDPOINT_MKP, 30)
    if url_is_ok is True and ID is not False:
        xml = Util.etree_from_text(content)

        desc_nodes = xml.iter("{" + RDF + "}Description")
        desc = {}

        for desc_node in desc_nodes:
            desc["identifier"] = desc_node.find('{' + DCTERMS + '}identifier').text
            if (ID == desc["identifier"]):

                desc["checksum"] = {}
                desc["checksum"]["val"] = []
                desc["checksum"]["algo"] = []

                desc["description"] = desc_node.find('{' + DCTERMS + '}description').text
                desc["creator"] = desc_node.find('{' + DCTERMS + '}creator').text
                desc["created"] = desc_node.find('{' + DCTERMS + '}created').text
                desc["valid"] = desc_node.find('{' + DCTERMS + '}valid').text
                desc["os"] = desc_node.find('{' + SLTERMS + '}os').text
                desc["os-version"] = desc_node.find('{' + SLTERMS + '}os-version').text
                desc["os-arch"] = desc_node.find('{' + SLTERMS + '}os-arch').text
                desc["version"] = desc_node.find('{' + SLTERMS + '}version').text
                desc["compression"] = desc_node.find('{' + DCTERMS + '}compression').text
                desc["location"] = desc_node.find('{' + SLTERMS + '}location').text
                desc["location"] = desc_node.find('{' + SLTERMS + '}location').text
                desc["format"] = desc_node.find('{' + DCTERMS + '}format').text
                desc["publisher"] = desc_node.find('{' + DCTERMS + '}publisher').text
                desc["hypervisor"] = desc_node.find('{' + SLTERMS + '}hypervisor').text
                for check in desc_node.findall('{' + SLREQ + '}checksum'):
                    desc["checksum"]["algo"].append(check.find('{' + SLREQ + '}algorithm').text)
                    desc["checksum"]["val"].append(check.find('{' + SLREQ + '}value').text)
                for endorsement in desc_node.findall('{' + SLREQ + '}endorsement'):
                    for endorser in endorsement.findall('{' + SLREQ + '}endorser'):
                        desc["email"] = endorser.find('{' + SLREQ + '}email').text

                # cast in str for None object (otherwise, I should use try/Except)
                print "Description: " + str(desc["description"])
                print "ID: " + str(desc["identifier"])
                print "Creator: " + str(desc["creator"])
                print "Created at: " + str(desc["created"].replace("Z","").split('T'))
                print "Validity: " + str(desc["valid"].replace("Z","").split('T'))
                print "OS: " + str(desc["os"]), str(desc["os-version"]), \
                "| Arch: " + str(desc["os-arch"])
                print "Version: " + str(desc["version"])
                print "Compression: " + str(desc["compression"])
                print "Location: " + str(desc["location"])
                print "Format: " + str(desc["format"])
                print "Publisher: " + str(desc["publisher"])
                print "Hypervisor: " + str(desc["hypervisor"])
                print "Endorser: " + str(desc["email"])
                for i in range(len(desc["checksum"]["algo"])):
                    print str(desc["checksum"]["algo"][i]), str(desc["checksum"]["val"][i])
                print "####\n"



if __name__ == '__main__':
    try:
        MainProgram()
    except KeyboardInterrupt:
        print '\n\nExecution interrupted by the user... goodbye!'
