<commit_before>#!/usr/bin/python

#
# Copyright (c) 2006 rPath, Inc.
#
# All rights reserved
#

import xenmac, xenip
import os
import copy
import sys

class KernelMissing(Exception):
    def __str__(self):
        return "No suitable kernel found"

class UnrecognizedImage(Exception):
    def __str__(self):
        return self.msg
    def __init__(self, msg = 'Unrecognized Image'):
        self.msg = msg

class XenCfg(object):
    def __init__(self, imgPath, cfg = {}):
        self.cfg = copy.deepcopy(cfg)
        self.cfg.setdefault('memory', 64)

        if 'kernel' not in self.cfg:
            self.cfg.setdefault('bootloader', '/usr/bin/pygrub')

        # genMac has an effect that is global to the entire system.
        if 'vif' not in self.cfg or not self.cfg['vif'] \
               or self.cfg['vif'] == ['']:
            mac = xenmac.genMac()
            self.ip = xenip.genIP()
            self.cfg['vif'] = [ 'ip=%s, mac=%s' % (self.ip, mac) ]

        self.cfg.setdefault('name', 'slave%s' % \
                            self.cfg['vif'][0].split(':')[-1])

        p = os.popen('file -k %s' % imgPath)
        data = p.read()
        p.close()
        if 'filesystem' in data:
            suffix = '1'
        elif 'boot sector' in data:
            suffix = ''
        else:
            suffix = '1'
            #raise UnrecognizedImage('%s is not a recognized image' % imgPath)
        self.cfg.setdefault('disk', ['tap:aio:%s,xvda%s,w' % (imgPath, suffix)])

    def write(self, f = sys.stdout):
        for key, val in self.cfg.iteritems():
            if isinstance(val, list):
                f.write("%s = %s\n" % (key, str([str(x) for x in val])))
            else:
                f.write("%s = \"%s\"\n" % (key, val))

if __name__ == '__main__':
    def usage(out = sys.stderr):
        print >> out, "usage: %s /path/to/image" % os.path.basename(sys.argv[0])
        sys.exit(1)

    if len(sys.argv) == 1:
        usage()

    imgFile = sys.argv[1]

    import tempfile
    fd, fn = tempfile.mkstemp()
    os.close(fd)
    f = open(fn, 'w')
    try:
        cfg = XenCfg(imgFile)
        cfg.write(f)
    finally:
        f.close()
    print fn
<commit_msg>add support for additional local storage<commit_after>#!/usr/bin/python

#
# Copyright (c) 2006 rPath, Inc.
#
# All rights reserved
#

import xenmac, xenip
import os
import copy
import sys

class KernelMissing(Exception):
    def __str__(self):
        return "No suitable kernel found"

class UnrecognizedImage(Exception):
    def __str__(self):
        return self.msg
    def __init__(self, msg = 'Unrecognized Image'):
        self.msg = msg

class XenCfg(object):
    def __init__(self, imgPath, cfg = {}, extraDisk = None):
        self.cfg = copy.deepcopy(cfg)
        self.cfg.setdefault('memory', 64)

        if 'kernel' not in self.cfg:
            self.cfg.setdefault('bootloader', '/usr/bin/pygrub')

        # genMac has an effect that is global to the entire system.
        if 'vif' not in self.cfg or not self.cfg['vif'] \
               or self.cfg['vif'] == ['']:
            mac = xenmac.genMac()
            self.ip = xenip.genIP()
            self.cfg['vif'] = [ 'ip=%s, mac=%s' % (self.ip, mac) ]

        self.cfg.setdefault('name', 'slave%s' % \
                            self.cfg['vif'][0].split(':')[-1])

        p = os.popen('file -k %s' % imgPath)
        data = p.read()
        p.close()
        if 'filesystem' in data:
            suffix = '1'
        elif 'boot sector' in data:
            suffix = ''
        else:
            suffix = '1'
            #raise UnrecognizedImage('%s is not a recognized image' % imgPath)
        disks = self.cfg.setdefault('disk', ['tap:aio:%s,xvda%s,w' % (imgPath, suffix)])
        if extraDisk:
            disks.append('phy:%s,xvda2,w')

    def write(self, f = sys.stdout):
        for key, val in self.cfg.iteritems():
            if isinstance(val, list):
                f.write("%s = %s\n" % (key, str([str(x) for x in val])))
            else:
                f.write("%s = \"%s\"\n" % (key, val))

if __name__ == '__main__':
    def usage(out = sys.stderr):
        print >> out, "usage: %s /path/to/image" % os.path.basename(sys.argv[0])
        sys.exit(1)

    if len(sys.argv) == 1:
        usage()

    imgFile = sys.argv[1]

    import tempfile
    fd, fn = tempfile.mkstemp()
    os.close(fd)
    f = open(fn, 'w')
    try:
        cfg = XenCfg(imgFile)
        cfg.write(f)
    finally:
        f.close()
    print fn

{'fraction_non_alphanumeric': 0.15495775900400177, 'fraction_numerical': 0.006447309915518008, 'mean_word_length': 1.9501639344262296, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6569724', 'n_tokens_mistral': 1561, 'n_tokens_neox': 1508, 'n_words': 383}