<commit_before>#!/usr/bin/env jython

from __future__ import with_statement
import simplejson as json  # JSON module is available from Python 2.6
from kahuna.abstract import AbsPlugin
from kahuna.utils.prettyprint import pprint_templates
from org.jclouds.abiquo.domain.exception import AbiquoException
from org.jclouds.rest import AuthorizationException
from optparse import OptionParser
from time import sleep
from upload.repository import TransientRepository


class TemplatePlugin(AbsPlugin):
    """ Template plugin """

    def list(self, args):
        """ List all available templates """
        try:
            admin = self._context.getAdministrationService()
            enterprise = admin.getCurrentEnterprise()
            templates = enterprise.listTemplates()
            pprint_templates(templates)
        except (AbiquoException, AuthorizationException), ex:
            print "Error: %s" % ex.getMessage()

    def upload(self, args):
        """ Upload a template to a repository """
        parser = OptionParser(usage="template upload <options>")
        parser.add_option('-b', '--bind-address',
                help='The bind address fot the local repository',
                action='store', dest='address', default='localhost')
        parser.add_option('-p', '--port',
                help='The port to open locally for the repository',
                action='store', dest='port', type='int', default=8888)
        parser.add_option('-d', '--disk-file',
                help='The disk file to be uploaded',
                action='store', dest='disk')
        parser.add_option('-c', '--config',
                help='Path to a json configuration file for the template',
                action='store', dest='config')
        (options, args) = parser.parse_args(args)

        if not options.disk or not options.config:
            parser.print_help()
            return

        with open(options.config, "r") as f:
            config = json.loads(f.read())

        repo = TransientRepository()
        try:
            repo.create(options.disk, config)
            repo.start(options.address, options.port)

            # TODO: Once the server is listening, call the API to download
            # the necessary files.
            sleep(30)
        except (AbiquoException, AuthorizationException), ex:
            print "Error: %s" % ex.getMessage()
        finally:
            #repo.destroy()
            repo.stop()


def load():
    """ Loads the current plugin """
    return TemplatePlugin()<commit_msg>Destroy the repository when done<commit_after>#!/usr/bin/env jython

from __future__ import with_statement
import simplejson as json  # JSON module is available from Python 2.6
from kahuna.abstract import AbsPlugin
from kahuna.utils.prettyprint import pprint_templates
from org.jclouds.abiquo.domain.exception import AbiquoException
from org.jclouds.rest import AuthorizationException
from optparse import OptionParser
from time import sleep
from upload.repository import TransientRepository


class TemplatePlugin(AbsPlugin):
    """ Template plugin """

    def list(self, args):
        """ List all available templates """
        try:
            admin = self._context.getAdministrationService()
            enterprise = admin.getCurrentEnterprise()
            templates = enterprise.listTemplates()
            pprint_templates(templates)
        except (AbiquoException, AuthorizationException), ex:
            print "Error: %s" % ex.getMessage()

    def upload(self, args):
        """ Upload a template to a repository """
        parser = OptionParser(usage="template upload <options>")
        parser.add_option('-b', '--bind-address',
                help='The bind address fot the local repository',
                action='store', dest='address', default='localhost')
        parser.add_option('-p', '--port',
                help='The port to open locally for the repository',
                action='store', dest='port', type='int', default=8888)
        parser.add_option('-d', '--disk-file',
                help='The disk file to be uploaded',
                action='store', dest='disk')
        parser.add_option('-c', '--config',
                help='Path to a json configuration file for the template',
                action='store', dest='config')
        (options, args) = parser.parse_args(args)

        if not options.disk or not options.config:
            parser.print_help()
            return

        with open(options.config, "r") as f:
            config = json.loads(f.read())

        repo = TransientRepository()
        try:
            repo.create(options.disk, config)
            repo.start(options.address, options.port)

            # TODO: Once the server is listening, call the API to download
            # the necessary files.
            sleep(30)
        except (AbiquoException, AuthorizationException), ex:
            print "Error: %s" % ex.getMessage()
        finally:
            repo.destroy()


def load():
    """ Loads the current plugin """
    return TemplatePlugin()
{'fraction_non_alphanumeric': 0.10603074497437918, 'fraction_numerical': 0.0031533307055577452, 'mean_word_length': 2.426738690074274, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6508802', 'n_tokens_mistral': 1310, 'n_tokens_neox': 1279, 'n_words': 388}