<commit_before>#!/usr/bin/python -Es
# -*- coding: utf-8 -*-
# Authors: <NAME> <<EMAIL>>
#          <NAME> <<EMAIL>>
#
# Copyright (C) 2013 Red Hat, Inc.
# Copyright (C) 2015 Universitat Politècnica de Catalunya.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import argparse
import gettext
import hashlib
import json
import os
import os.path
import shutil
import sys
import urllib2
import subprocess

if os.geteuid() == 0:
    default_template_dir = "/var/lib/libvirt/templates"
    default_image_dir = "/var/lib/libvirt/images"
else:
    default_template_dir = os.environ['HOME'] + "/.local/share/libvirt/templates"
    default_image_dir = os.environ['HOME'] + "/.local/share/libvirt/images"

debug = False
verbose = False

import importlib
def dynamic_source_loader(name):
    name = name[0].upper() + name[1:]
    modname = "libvirt_sandbox.image.sources." + name + "Source"
    mod = importlib.import_module(modname)
    classname = name + "Source"
    classimpl = getattr(mod, classname)
    return classimpl()

gettext.bindtextdomain("libvirt-sandbox", "/usr/share/locale")
gettext.textdomain("libvirt-sandbox")
try:
    gettext.install("libvirt-sandbox",
                    localedir="/usr/share/locale",
                    unicode=False,
                    codeset = 'utf-8')
except IOError:
    import __builtin__
    __builtin__.__dict__['_'] = unicode


def debug(msg):
    sys.stderr.write(msg)

def info(msg):
    sys.stdout.write(msg)

def download(args):
    try:
        dynamic_source_loader(args.source).download_template(templatename=args.template,
                                                             templatedir=args.template_dir,
                                                             registry=args.registry,
                                                             username=args.username,
                                                             password=args.password)
    except IOError,e:
        print "Source %s cannot be found in given path" %args.source
    except Exception,e:
        print "Download Error %s" % str(e)

def delete(args):
    try:
        dynamic_source_loader(args.source).delete_template(templatename=args.template,
                                                           templatedir=args.template_dir)
    except Exception,e:
        print "Delete Error %s", str(e)

def create(args):
    try:
        dynamic_source_loader(args.source).create_template(templatename=args.template,
                                                           templatedir=args.template_dir,
                                                           connect=args.connect,
                                                           format=args.format)
    except Exception,e:
        print "Create Error %s" % str(e)

def requires_template(parser):
    parser.add_argument("template",
                        help=_("name of the template"))

def requires_source(parser):
    parser.add_argument("-s","--source",
                        default="docker",
                        help=_("name of the template"))

def requires_connect(parser):
    parser.add_argument("-c","--connect",
                        help=_("Connect string for libvirt"))

def requires_auth_conn(parser):
    parser.add_argument("-r","--registry",
                        help=_("Url of the custom registry"))
    parser.add_argument("-u","--username",
                        help=_("Username for the custom registry"))
    parser.add_argument("-p","--password",
                        help=_("Password for the custom registry"))

def requires_template_dir(parser):
    global default_template_dir
    parser.add_argument("-t","--template-dir",
                        default=default_template_dir,
                        help=_("Template directory for saving templates"))

def gen_download_args(subparser):
    parser = subparser.add_parser("download",
                                   help=_("Download template data"))
    requires_template(parser)
    requires_source(parser)
    requires_auth_conn(parser)
    requires_template_dir(parser)
    parser.set_defaults(func=download)

def gen_delete_args(subparser):
    parser = subparser.add_parser("delete",
                                   help=_("Delete template data"))
    requires_template(parser)
    requires_source(parser)
    requires_template_dir(parser)
    parser.set_defaults(func=delete)

def gen_create_args(subparser):
    parser = subparser.add_parser("create",
                                   help=_("Create image from template data"))
    requires_template(parser)
    requires_source(parser)
    requires_connect(parser)
    requires_template_dir(parser)
    parser.add_argument("-f","--format",
                        default="qcow2",
                        help=_("format format for image"))
    parser.set_defaults(func=create)

def main():
    parser = argparse.ArgumentParser(description='Sandbox Container Image Tool')

    subparser = parser.add_subparsers(help=_("commands"))
    gen_download_args(subparser)
    gen_delete_args(subparser)
    gen_create_args(subparser)

    try:
        args = parser.parse_args()
        args.func(args)
        sys.exit(0)
    except KeyboardInterrupt, e:
        sys.exit(0)
    except ValueError, e:
        for line in e:
            for l in line:
                sys.stderr.write("%s: %s\n" % (sys.argv[0], l))<commit_msg>Image: Add run args<commit_after>#!/usr/bin/python -Es
# -*- coding: utf-8 -*-
# Authors: <NAME> <<EMAIL>>
#          <NAME> <<EMAIL>>
#
# Copyright (C) 2013 Red Hat, Inc.
# Copyright (C) 2015 Universitat Politècnica de Catalunya.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import argparse
import gettext
import hashlib
import json
import os
import os.path
import shutil
import sys
import urllib2
import subprocess

if os.geteuid() == 0:
    default_template_dir = "/var/lib/libvirt/templates"
    default_image_dir = "/var/lib/libvirt/images"
else:
    default_template_dir = os.environ['HOME'] + "/.local/share/libvirt/templates"
    default_image_dir = os.environ['HOME'] + "/.local/share/libvirt/images"

debug = False
verbose = False

import importlib
def dynamic_source_loader(name):
    name = name[0].upper() + name[1:]
    modname = "libvirt_sandbox.image.sources." + name + "Source"
    mod = importlib.import_module(modname)
    classname = name + "Source"
    classimpl = getattr(mod, classname)
    return classimpl()

gettext.bindtextdomain("libvirt-sandbox", "/usr/share/locale")
gettext.textdomain("libvirt-sandbox")
try:
    gettext.install("libvirt-sandbox",
                    localedir="/usr/share/locale",
                    unicode=False,
                    codeset = 'utf-8')
except IOError:
    import __builtin__
    __builtin__.__dict__['_'] = unicode


def debug(msg):
    sys.stderr.write(msg)

def info(msg):
    sys.stdout.write(msg)

def download(args):
    try:
        dynamic_source_loader(args.source).download_template(templatename=args.template,
                                                             templatedir=args.template_dir,
                                                             registry=args.registry,
                                                             username=args.username,
                                                             password=args.password)
    except IOError,e:
        print "Source %s cannot be found in given path" %args.source
    except Exception,e:
        print "Download Error %s" % str(e)

def delete(args):
    try:
        dynamic_source_loader(args.source).delete_template(templatename=args.template,
                                                           templatedir=args.template_dir)
    except Exception,e:
        print "Delete Error %s", str(e)

def create(args):
    try:
        dynamic_source_loader(args.source).create_template(templatename=args.template,
                                                           templatedir=args.template_dir,
                                                           connect=args.connect,
                                                           format=args.format)
    except Exception,e:
        print "Create Error %s" % str(e)

def requires_template(parser):
    parser.add_argument("template",
                        help=_("name of the template"))

def requires_name(parser):
    parser.add_argument("-n","--name",
                        help=_("Name of the running sandbox"))

def requires_source(parser):
    parser.add_argument("-s","--source",
                        default="docker",
                        help=_("name of the template"))

def requires_connect(parser):
    parser.add_argument("-c","--connect",
                        help=_("Connect string for libvirt"))

def requires_auth_conn(parser):
    parser.add_argument("-r","--registry",
                        help=_("Url of the custom registry"))
    parser.add_argument("-u","--username",
                        help=_("Username for the custom registry"))
    parser.add_argument("-p","--password",
                        help=_("Password for the custom registry"))

def requires_template_dir(parser):
    global default_template_dir
    parser.add_argument("-t","--template-dir",
                        default=default_template_dir,
                        help=_("Template directory for saving templates"))

def requires_image_dir(parser):
    global default_image_dir
    parser.add_argument("-I","--image-dir",
                        default=default_image_dir,
                        help=_("Image directory for saving images"))

def gen_download_args(subparser):
    parser = subparser.add_parser("download",
                                   help=_("Download template data"))
    requires_template(parser)
    requires_source(parser)
    requires_auth_conn(parser)
    requires_template_dir(parser)
    parser.set_defaults(func=download)

def gen_delete_args(subparser):
    parser = subparser.add_parser("delete",
                                   help=_("Delete template data"))
    requires_template(parser)
    requires_source(parser)
    requires_template_dir(parser)
    parser.set_defaults(func=delete)

def gen_create_args(subparser):
    parser = subparser.add_parser("create",
                                   help=_("Create image from template data"))
    requires_template(parser)
    requires_source(parser)
    requires_connect(parser)
    requires_template_dir(parser)
    parser.add_argument("-f","--format",
                        default="qcow2",
                        help=_("format format for image"))
    parser.set_defaults(func=create)

def gen_run_args(subparser):
    parser = subparser.add_parser("run",
                                  help=_("Run an already built image"))
    requires_name(parser)
    requires_template(parser)
    requires_source(parser)
    requires_connect(parser)
    requires_template_dir(parser)
    requires_image_dir(parser)
    parser.add_argument("args",
                        nargs=argparse.REMAINDER,
                        help=_("command arguments to run"))
    parser.set_defaults(func=run)

def main():
    parser = argparse.ArgumentParser(description='Sandbox Container Image Tool')

    subparser = parser.add_subparsers(help=_("commands"))
    gen_download_args(subparser)
    gen_delete_args(subparser)
    gen_create_args(subparser)
    gen_run_args(subparser)

    try:
        args = parser.parse_args()
        args.func(args)
        sys.exit(0)
    except KeyboardInterrupt, e:
        sys.exit(0)
    except ValueError, e:
        for line in e:
            for l in line:
                sys.stderr.write("%s: %s\n" % (sys.argv[0], l))
{'fraction_non_alphanumeric': 0.10203135861589557, 'fraction_numerical': 0.00417085039005175, 'mean_word_length': 2.239429572179134, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 55, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3717113', 'n_tokens_mistral': 3475, 'n_tokens_neox': 3432, 'n_words': 842}