<commit_before>""" A collection of command-line tools for building encoded update.xml
files.
"""

#Requires Python 2.6 (for format string notation)

import os
import re
import md5

_version_in_name = re.compile("(\S*)[-](\d+\.\d+\.*\d*)\S*")

def _get_name(filename):
    match = _version_in_name.search(filename)
    if match is None:
        raise ValueError, "Could not find name in filename: {0}".format(filename)
    return match.group(1)

def _get_version(filename):
    match = _version_in_name.search(filename)
    if match is None:
        raise ValueError, "Could not find version in filename: {0}".format(filename)
    return match.group(2)

def _get_checksum(filename):
    base, ext = os.path.splitext(filename)
    data = open(base).read()
    return md5.new(data).hexdigest()


name_re = re.compile('name:\s*(.*)\n')
version_re = re.compile('version:\s*(.*)\n')
checksum_re = re.compile('checksum:\s*(.*)\n')
html_re = re.compile('\nhtml:')

codedict = {'name':{'re':name_re,
                    'get':_get_name},
            'version': {'re':version_re,
                        'get':_get_version},
            'checksum': {'re':checksum_re,
                         'get':_get_checksum}
            }

class InfoFile(object):
    """Representation of an .info file

    Important methods: 
    
    @classmethod
    from_info_file(filename)

      construct an InfoFile object from a filename --- simple parser

      name: %filename% (if not present extracted from .info filename)
      version: %filename% (if not present it is extracted from name of file)
      checksum: md5hash (if not present it is computed from the basefile)
      html: (everything else in the file from the next line to the end)
    
    get_xml()
      return a list of xml elements for this file       
    """

    # The name of the update_file
    name = ""
    version = None
    checksum = None

    # A multi-line HTML document describing the changes between
    # this version and the previous version
    description = ""

    @classmethod
    def from_info_file(cls, filename):
        str = open(filename).read()        
        obj = cls()
        for attr in ['name', 'version', 'checksum']:
            funcdict = codedict[attr]
            match = funcdict['re'].search(str)
            if match is None:
                value = funcdict['get'](filename)
            else:
                value = match.group(1)
            setattr(obj, attr, value)
            
        match = html_re.search(str)
        beg, end = match.span()
        start = str.find('\n', end)
        obj.description = str[start:]
        return obj

    def get_xml(self):
        xml_elements = []
        for attr in ['name', 'version', 'checksum', 'description']:
            value = getattr(self, attr)
            str = "{spaces}<{attr}>{value}</{attr}>"
            xml_elements.append(str.format(spaces=" "*4,attr=attr, value=value))
        return xml_elements

_xmlheader = """<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- DO NOT EDIT MANUALLY -->
<!-- Automatically generated file using enthought.util.updates -->
"""

def files2xml(filenames):
    """ Given a list of filenames, extracts the app version and log
    information from accompanying files produces an output xml string.

    There are no constraints or restrictions on the names or extensions
    of the input files.  They just need to be accompanied by a sidecar
    file named similarly, but with a ".info" extension, that can be 
    loaded by the InfoFile class.

    If there is no .info file for a filename or an error occurs while constructing it
    a warning message is printed. 
    """
    xmlparts = [_xmlheader]
    for file in filenames:
        info_file_name = "{0}.info".format(file)
        if not os.path.exists(info_file_name):
            print "Warning: {0} was not found.".format(info_file_name)
            continue
        try:
            info = InfoFile.from_info_file(info_file_name)
            xml_list = info.get_xml()
        except:
            print "Warning: Failure in extracting XML for {0}".format(info_file_name)
            continue
        xmlparts.append('<update_file>') <commit_msg>Improve error message.<commit_after>""" A collection of command-line tools for building encoded update.xml
files.
"""

#Requires Python 2.6 (for format string notation)

import os
import re
import md5

_version_in_name = re.compile("(\S*)[-](\d+\.\d+\.*\d*)\S*")

def _get_name(filename):
    match = _version_in_name.search(filename)
    if match is None:
        raise ValueError, "Could not find name in filename: {0}".format(filename)
    return match.group(1)

def _get_version(filename):
    match = _version_in_name.search(filename)
    if match is None:
        raise ValueError, "Could not find version in filename: {0}".format(filename)
    return match.group(2)

def _get_checksum(filename):
    base, ext = os.path.splitext(filename)
    data = open(base).read()
    return md5.new(data).hexdigest()


name_re = re.compile('name:\s*(.*)\n')
version_re = re.compile('version:\s*(.*)\n')
checksum_re = re.compile('checksum:\s*(.*)\n')
html_re = re.compile('\nhtml:')

codedict = {'name':{'re':name_re,
                    'get':_get_name},
            'version': {'re':version_re,
                        'get':_get_version},
            'checksum': {'re':checksum_re,
                         'get':_get_checksum}
            }

class InfoFile(object):
    """Representation of an .info file

    Important methods: 
    
    @classmethod
    from_info_file(filename)

      construct an InfoFile object from a filename --- simple parser

      name: %filename% (if not present extracted from .info filename)
      version: %filename% (if not present it is extracted from name of file)
      checksum: md5hash (if not present it is computed from the basefile)
      html: (everything else in the file from the next line to the end)
    
    get_xml()
      return a list of xml elements for this file       
    """

    # The name of the update_file
    name = ""
    version = None
    checksum = None

    # A multi-line HTML document describing the changes between
    # this version and the previous version
    description = ""

    @classmethod
    def from_info_file(cls, filename):
        str = open(filename).read()        
        obj = cls()
        for attr in ['name', 'version', 'checksum']:
            funcdict = codedict[attr]
            match = funcdict['re'].search(str)
            if match is None:
                value = funcdict['get'](filename)
            else:
                value = match.group(1)
            setattr(obj, attr, value)
            
        match = html_re.search(str)
        beg, end = match.span()
        start = str.find('\n', end)
        obj.description = str[start:]
        return obj

    def get_xml(self):
        xml_elements = []
        for attr in ['name', 'version', 'checksum', 'description']:
            value = getattr(self, attr)
            str = "{spaces}<{attr}>{value}</{attr}>"
            xml_elements.append(str.format(spaces=" "*4,attr=attr, value=value))
        return xml_elements

_xmlheader = """<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- DO NOT EDIT MANUALLY -->
<!-- Automatically generated file using enthought.util.updates -->
"""

def files2xml(filenames):
    """ Given a list of filenames, extracts the app version and log
    information from accompanying files produces an output xml string.

    There are no constraints or restrictions on the names or extensions
    of the input files.  They just need to be accompanied by a sidecar
    file named similarly, but with a ".info" extension, that can be 
    loaded by the InfoFile class.

    If there is no .info file for a filename or an error occurs while constructing it
    a warning message is printed. 
    """
    xmlparts = [_xmlheader]
    for file in filenames:
        info_file_name = "{0}.info".format(file)
        if not os.path.exists(info_file_name):
            print "Warning: {0} was not found.".format(info_file_name)
            continue
        try:
            info = InfoFile.from_info_file(info_file_name)
            xml_list = info.get_xml()
        except:
            print "Warning: Failure in creating XML for {0}".format(info_file_name)
            continue
        xmlparts.append('<update_file>') 
{'fraction_non_alphanumeric': 0.11493018259935553, 'fraction_numerical': 0.005251223296336078, 'mean_word_length': 2.4599504541701074, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 2, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 32}, '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': '4204538', 'n_tokens_mistral': 2487, 'n_tokens_neox': 2414, 'n_words': 766}