<commit_before>#!/usr/bin/python

from distutils.core import setup, Extension, Command
from distutils.command.build import build
from distutils.command.clean import clean
from distutils.command.sdist import sdist
from distutils.dir_util import remove_tree
from distutils.util import get_platform
from distutils.spawn import spawn
import distutils

import sys
import datetime
import os
import os.path
import re
import time

MIN_LIBVIRT = "0.10.2"

pkgcfg = distutils.spawn.find_executable("pkg-config")

if pkgcfg is None:
    raise Exception("pkg-config binary is required to compile libvirt-python")

spawn([pkgcfg,
       "--print-errors",
       "--atleast-version=%s" % MIN_LIBVIRT,
       "libvirt"])

def get_pkgconfig_data(args, mod, required=True):
    """Run pkg-config to and return content associated with it"""
    f = os.popen("%s %s %s" % (pkgcfg, " ".join(args), mod))

    line = f.readline()
    if line is not None:
        line = line.strip()

    if line is None or line == "":
        if required:
            raise Exception("Cannot determine '%s' from libvirt pkg-config file" % " ".join(args))
        else:
            return ""

    return line

ldflags = get_pkgconfig_data(["--libs-only-L"], "libvirt", False)
cflags = get_pkgconfig_data(["--cflags"], "libvirt", False)

c_modules = []
py_modules = []

module = Extension('libvirtmod',
                   sources = ['libvirt-override.c', 'build/libvirt.c', 'typewrappers.c', 'libvirt-utils.c'],
                   libraries = [ "virt" ],
                   include_dirs = [ "." ])
if cflags != "":
    module.extra_compile_args.append(cflags)
if ldflags != "":
    module.extra_link_args.append(ldflags)

c_modules.append(module)
py_modules.append("libvirt")

moduleqemu = Extension('libvirtmod_qemu',
                       sources = ['libvirt-qemu-override.c', 'build/libvirt-qemu.c', 'typewrappers.c', 'libvirt-utils.c'],
                       libraries = [ "virt-qemu" ],
                       include_dirs = [ "." ])
if cflags != "":
    moduleqemu.extra_compile_args.append(cflags)
if ldflags != "":
    moduleqemu.extra_link_args.append(ldflags)

c_modules.append(moduleqemu)
py_modules.append("libvirt_qemu")

modulelxc = Extension('libvirtmod_lxc',
                      sources = ['libvirt-lxc-override.c', 'build/libvirt-lxc.c', 'typewrappers.c', 'libvirt-utils.c'],
                      libraries = [ "virt-lxc" ],
                      include_dirs = [ "." ])
if cflags != "":
    modulelxc.extra_compile_args.append(cflags)
if ldflags != "":
    modulelxc.extra_link_args.append(ldflags)

c_modules.append(modulelxc)
py_modules.append("libvirt_lxc")


class my_build(build):

    def get_api_xml_files(self):
        """Check with pkg-config that libvirt is present and extract
        the API XML file paths we need from it"""

        libvirt_api = get_pkgconfig_data(["--variable", "libvirt_api"], "libvirt")

        offset = libvirt_api.index("-api.xml")
        libvirt_qemu_api = libvirt_api[0:offset] + "-qemu-api.xml"

        offset = libvirt_api.index("-api.xml")
        libvirt_lxc_api = libvirt_api[0:offset] + "-lxc-api.xml"

        return (libvirt_api, libvirt_qemu_api, libvirt_lxc_api)


    def run(self):
        apis = self.get_api_xml_files()

        self.spawn(["./generator.py", "libvirt", apis[0]])
        self.spawn(["./generator.py", "libvirt-qemu", apis[1]])
        self.spawn(["./generator.py", "libvirt-lxc", apis[2]])

        build.run(self)<commit_msg>Don't build LXC module when building less than 1.0.2<commit_after>#!/usr/bin/python

from distutils.core import setup, Extension, Command
from distutils.command.build import build
from distutils.command.clean import clean
from distutils.command.sdist import sdist
from distutils.dir_util import remove_tree
from distutils.util import get_platform
from distutils.spawn import spawn
from distutils.errors import DistutilsExecError
import distutils

import sys
import datetime
import os
import os.path
import re
import time

MIN_LIBVIRT = "0.10.2"
MIN_LIBVIRT_LXC = "1.0.2"

pkgcfg = distutils.spawn.find_executable("pkg-config")

if pkgcfg is None:
    raise Exception("pkg-config binary is required to compile libvirt-python")

spawn([pkgcfg,
       "--print-errors",
       "--atleast-version=%s" % MIN_LIBVIRT,
       "libvirt"])

have_libvirt_lxc=True
try:
    spawn([pkgcfg,
           "--atleast-version=%s" % MIN_LIBVIRT_LXC,
         "libvirt"])
except DistutilsExecError,e:
    have_libvirt_lxc=False

def get_pkgconfig_data(args, mod, required=True):
    """Run pkg-config to and return content associated with it"""
    f = os.popen("%s %s %s" % (pkgcfg, " ".join(args), mod))

    line = f.readline()
    if line is not None:
        line = line.strip()

    if line is None or line == "":
        if required:
            raise Exception("Cannot determine '%s' from libvirt pkg-config file" % " ".join(args))
        else:
            return ""

    return line

ldflags = get_pkgconfig_data(["--libs-only-L"], "libvirt", False)
cflags = get_pkgconfig_data(["--cflags"], "libvirt", False)

c_modules = []
py_modules = []

module = Extension('libvirtmod',
                   sources = ['libvirt-override.c', 'build/libvirt.c', 'typewrappers.c', 'libvirt-utils.c'],
                   libraries = [ "virt" ],
                   include_dirs = [ "." ])
if cflags != "":
    module.extra_compile_args.append(cflags)
if ldflags != "":
    module.extra_link_args.append(ldflags)

c_modules.append(module)
py_modules.append("libvirt")

moduleqemu = Extension('libvirtmod_qemu',
                       sources = ['libvirt-qemu-override.c', 'build/libvirt-qemu.c', 'typewrappers.c', 'libvirt-utils.c'],
                       libraries = [ "virt-qemu" ],
                       include_dirs = [ "." ])
if cflags != "":
    moduleqemu.extra_compile_args.append(cflags)
if ldflags != "":
    moduleqemu.extra_link_args.append(ldflags)

c_modules.append(moduleqemu)
py_modules.append("libvirt_qemu")

if have_libvirt_lxc:
    modulelxc = Extension('libvirtmod_lxc',
                          sources = ['libvirt-lxc-override.c', 'build/libvirt-lxc.c', 'typewrappers.c', 'libvirt-utils.c'],
                          libraries = [ "virt-lxc" ],
                          include_dirs = [ "." ])
    if cflags != "":
        modulelxc.extra_compile_args.append(cflags)
    if ldflags != "":
        modulelxc.extra_link_args.append(ldflags)

    c_modules.append(modulelxc)
    py_modules.append("libvirt_lxc")


class my_build(build):

    def get_api_xml_files(self):
        """Check with pkg-config that libvirt is present and extract
        the API XML file paths we need from it"""

        libvirt_api = get_pkgconfig_data(["--variable", "libvirt_api"], "libvirt")

        offset = libvirt_api.index("-api.xml")
        libvirt_qemu_api = libvirt_api[0:offset] + "-qemu-api.xml"

        offset = libvirt_api.index("-api.xml")
        libvirt_lxc_api = libvirt_api[0:offset] + "-lxc-api.xml"

        return (libvirt_api, libvirt_qemu_api, libvirt_lxc_api)


    def run(self):
        apis = self.get_api_xml_files()

        self.spawn(["./generator.py", "libvirt", apis[0]])
        self.spawn(["./generator.py", "libvirt-qemu", apis[1]])
        if have_libvirt_lxc:
            self.spawn(["./generator.py", "libvirt-lxc", apis[2]])

        build.run(self)
{'fraction_non_alphanumeric': 0.1384657236126224, 'fraction_numerical': 0.003264417845484222, 'mean_word_length': 2.9468599033816427, 'pattern_counts': {'":': 14, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, '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': '5718054', 'n_tokens_mistral': 2496, 'n_tokens_neox': 2430, 'n_words': 494}