#! /usr/bin/env python
import arc
import sys
import os

def retrieve(uc, endpoints):
    # The ComputingServiceRetriever needs the UserConfig to know which credentials
    # to use in case of HTTPS connections
    retriever = arc.ComputingServiceRetriever(uc, endpoints)
    # the constructor of the ComputingServiceRetriever returns immediately
    sys.stdout.write('\n')
    sys.stdout.write("ComputingServiceRetriever created with the following endpoints:\n")
    for endpoint in endpoints:
        sys.stdout.write("- %s\n"%endpoint.str())
    # here we want to wait until all the results arrive
    sys.stdout.write("Waiting for the results...\n")
    retriever.wait()
    return retriever

def example():
    # Creating a UserConfig object with the user's proxy
    # and the path of the trusted CA certificates
    uc = arc.UserConfig()
    uc.ProxyPath("/tmp/x509up_u%s" % os.getuid())
    uc.CACertificatesDirectory("/etc/grid-security/certificates")

    # Query two registries (index servers) for Computing Services
    registries = [
        # for the index1, we specify that it is an EGIIS service
        arc.Endpoint("index1.nordugrid.org:2135/Mds-Vo-name=NorduGrid,o=grid", arc.Endpoint.REGISTRY, "org.nordugrid.ldapegiis"),
        # for the arc-emi.grid.upjs.sk, we don't specify the type (the InterfaceName)
        # we let the system to try all possibilities
        arc.Endpoint("arc-emi.grid.upjs.sk/O=Grid/Mds-Vo-Name=ARC-EMI", arc.Endpoint.REGISTRY)
    ]

    retriever = retrieve(uc, registries)

    # The retriever acts as a list containing all the discovered ComputingServices:
    sys.stdout.write("Discovered ComputingServices: %s\n"%(", ".join([service.Name for service in retriever])))

    # Get all the ExecutionTargets on these ComputingServices
    targets = retriever.GetExecutionTargets()
    sys.stdout.write("Number of ExecutionTargets on these ComputingServices: %d\n"%len(targets))

    # Query the local infosys (COMPUTINGINFO) of computing elements
    computing_elements = [
        # for piff, we specify that we want to query the LDAP GLUE2 tree
        arc.Endpoint("piff.hep.lu.se", arc.Endpoint.COMPUTINGINFO, "org.nordugrid.ldapglue2"),
        # for pgs03, we don't specify the interface, we let the system try all possibilities
        arc.Endpoint("pgs03.grid.upjs.sk", arc.Endpoint.COMPUTINGINFO)
    ]

    retriever2 = retrieve(uc, computing_elements)

    # Get all the ExecutionTargets on these ComputingServices
    targets2 = retriever2.GetExecutionTargets()

    sys.stdout.write("The discovered ExecutionTargets:\n")
    for target in targets2:
        sys.stdout.write("%s\n"%str(target))


    # Query both registries and computing elements at the same time:
    endpoints = [
        arc.Endpoint("arc-emi.grid.upjs.sk/O=Grid/Mds-Vo-Name=ARC-EMI", arc.Endpoint.REGISTRY),
        arc.Endpoint("piff.hep.lu.se", arc.Endpoint.COMPUTINGINFO, "org.nordugrid.ldapglue2")
    ]

    retriever3 = retrieve(uc, endpoints)

    sys.stdout.write("Discovered ComputingServices: %s\n"%(", ".join([service.Name for service in retriever3])))


# wait for all the background threads to finish before we destroy the objects they may use
import atexit
@atexit.register
def wait_exit():
    arc.ThreadInitializer().waitExit()

# arc.Logger.getRootLogger().addDestination(arc.LogStream(sys.stderr))
# arc.Logger.getRootLogger().setThreshold(arc.DEBUG)

# run the example
example()

[((9, 16, 9, 60), 'arc.ComputingServiceRetriever', 'arc.ComputingServiceRetriever', ({(9, 46, 9, 48): 'uc', (9, 50, 9, 59): 'endpoints'}, {}), '(uc, endpoints)', False, 'import arc\n'), ((11, 4, 11, 26), 'sys.stdout.write', 'sys.stdout.write', ({(11, 21, 11, 25): '"""\n"""'}, {}), "('\\n')", False, 'import sys\n'), ((12, 4, 12, 89), 'sys.stdout.write', 'sys.stdout.write', ({(12, 21, 12, 88): '"""ComputingServiceRetriever created with the following endpoints:\n"""'}, {}), "(\n    'ComputingServiceRetriever created with the following endpoints:\\n')", False, 'import sys\n'), ((16, 4, 16, 52), 'sys.stdout.write', 'sys.stdout.write', ({(16, 21, 16, 51): '"""Waiting for the results...\n"""'}, {}), "('Waiting for the results...\\n')", False, 'import sys\n'), ((23, 9, 23, 25), 'arc.UserConfig', 'arc.UserConfig', ({}, {}), '()', False, 'import arc\n'), ((58, 4, 58, 58), 'sys.stdout.write', 'sys.stdout.write', ({(58, 21, 58, 57): '"""The discovered ExecutionTargets:\n"""'}, {}), "('The discovered ExecutionTargets:\\n')", False, 'import sys\n'), ((30, 8, 30, 128), 'arc.Endpoint', 'arc.Endpoint', ({(30, 21, 30, 77): '"""index1.nordugrid.org:2135/Mds-Vo-name=NorduGrid,o=grid"""', (30, 79, 30, 100): 'arc.Endpoint.REGISTRY', (30, 102, 30, 127): '"""org.nordugrid.ldapegiis"""'}, {}), "('index1.nordugrid.org:2135/Mds-Vo-name=NorduGrid,o=grid', arc.\n    Endpoint.REGISTRY, 'org.nordugrid.ldapegiis')", False, 'import arc\n'), ((33, 8, 33, 94), 'arc.Endpoint', 'arc.Endpoint', ({(33, 21, 33, 70): '"""arc-emi.grid.upjs.sk/O=Grid/Mds-Vo-Name=ARC-EMI"""', (33, 72, 33, 93): 'arc.Endpoint.REGISTRY'}, {}), "('arc-emi.grid.upjs.sk/O=Grid/Mds-Vo-Name=ARC-EMI', arc.\n    Endpoint.REGISTRY)", False, 'import arc\n'), ((48, 8, 48, 93), 'arc.Endpoint', 'arc.Endpoint', ({(48, 21, 48, 37): '"""piff.hep.lu.se"""', (48, 39, 48, 65): 'arc.Endpoint.COMPUTINGINFO', (48, 67, 48, 92): '"""org.nordugrid.ldapglue2"""'}, {}), "('piff.hep.lu.se', arc.Endpoint.COMPUTINGINFO,\n    'org.nordugrid.ldapglue2')", False, 'import arc\n'), ((50, 8, 50, 70), 'arc.Endpoint', 'arc.Endpoint', ({(50, 21, 50, 41): '"""pgs03.grid.upjs.sk"""', (50, 43, 50, 69): 'arc.Endpoint.COMPUTINGINFO'}, {}), "('pgs03.grid.upjs.sk', arc.Endpoint.COMPUTINGINFO)", False, 'import arc\n'), ((65, 8, 65, 94), 'arc.Endpoint', 'arc.Endpoint', ({(65, 21, 65, 70): '"""arc-emi.grid.upjs.sk/O=Grid/Mds-Vo-Name=ARC-EMI"""', (65, 72, 65, 93): 'arc.Endpoint.REGISTRY'}, {}), "('arc-emi.grid.upjs.sk/O=Grid/Mds-Vo-Name=ARC-EMI', arc.\n    Endpoint.REGISTRY)", False, 'import arc\n'), ((66, 8, 66, 93), 'arc.Endpoint', 'arc.Endpoint', ({(66, 21, 66, 37): '"""piff.hep.lu.se"""', (66, 39, 66, 65): 'arc.Endpoint.COMPUTINGINFO', (66, 67, 66, 92): '"""org.nordugrid.ldapglue2"""'}, {}), "('piff.hep.lu.se', arc.Endpoint.COMPUTINGINFO,\n    'org.nordugrid.ldapglue2')", False, 'import arc\n'), ((24, 37, 24, 48), 'os.getuid', 'os.getuid', ({}, {}), '()', False, 'import os\n'), ((78, 4, 78, 27), 'arc.ThreadInitializer', 'arc.ThreadInitializer', ({}, {}), '()', False, 'import arc\n')]