<commit_before>#
# Copyright (c) 2008-2009 rPath, Inc.
#
# This program is distributed under the terms of the Common Public License,
# version 1.0. A copy of this license should have been distributed with this
# source file in a file called LICENSE. If it is not present, the license
# is always available at http://www.rpath.com/permanent/licenses/CPL-1.0.
#
# 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 Common Public License for
# full details.
#

"""
Wrappers around conary.rpmhelper.
"""

import urllib2

from conary import rpmhelper

class SeekableStream(object):
    """
    File like object that can be seeked forward.
    """

    def __init__(self, url):
        self._url = url

        self._fh = urllib2.urlopen(url)
        self._pos = 0

    def read(self, *args):
        """
        Wrapper around urllib's file object's read method that keeps track
        of position.
        """

        buf = self._fh.read(*args)
        self._pos += len(buf)
        return buf

    def seek(self, amount, sense):
        """
        Simple seek implementation that only goes forward.
        @param amount: amount to seek into file.
        @type amount: integer
        @param sense: direction to seek into file (only valid value is 1)
        @type sense: integer
        """

        assert(sense == 1)
        self.read(amount)

    def tell(self):
        """
        Report the current position in the file.
        @return current position in the file
        """

        return self._pos

    def getTotalSize(self):
        """
        Return the size in the header.
        @return content length
        """

        return int(self._fh.headers.getheader('content-length'))

def readHeader(url):
    """
    Read an RPM header (and only the RPM header) from a remotely hosted RPM.
    @param url: url to RPM file.
    @type url: string
    @return conary.rpmhelper.RpmHeader object
    """

    fh = SeekableStream(url)

    # Have to read into the file a bit to get to the begining of the header
    # that we care about.
    fh.read(96)
    rpmhelper.RpmHeader(fh)

    header = rpmhelper.RpmHeader(fh)
    return header<commit_msg>rpmhelper now handles reading into the file for you<commit_after>#
# Copyright (c) 2008-2009 rPath, Inc.
#
# This program is distributed under the terms of the Common Public License,
# version 1.0. A copy of this license should have been distributed with this
# source file in a file called LICENSE. If it is not present, the license
# is always available at http://www.rpath.com/permanent/licenses/CPL-1.0.
#
# 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 Common Public License for
# full details.
#

"""
Wrappers around conary.rpmhelper.
"""

import urllib2

from conary import rpmhelper

class SeekableStream(object):
    """
    File like object that can be seeked forward.
    """

    def __init__(self, url):
        self._url = url

        self._fh = urllib2.urlopen(url)
        self._pos = 0

    def read(self, *args):
        """
        Wrapper around urllib's file object's read method that keeps track
        of position.
        """

        buf = self._fh.read(*args)
        self._pos += len(buf)
        return buf

    def seek(self, amount, sense):
        """
        Simple seek implementation that only goes forward.
        @param amount: amount to seek into file.
        @type amount: integer
        @param sense: direction to seek into file (only valid value is 1)
        @type sense: integer
        """

        assert(sense == 1)
        self.read(amount)

    def tell(self):
        """
        Report the current position in the file.
        @return current position in the file
        """

        return self._pos

    def getTotalSize(self):
        """
        Return the size in the header.
        @return content length
        """

        return int(self._fh.headers.getheader('content-length'))

def readHeader(url):
    """
    Read an RPM header (and only the RPM header) from a remotely hosted RPM.
    @param url: url to RPM file.
    @type url: string
    @return conary.rpmhelper.RpmHeader object
    """

    fh = SeekableStream(url)

    # Have to read into the file a bit to get to the begining of the header
    # that we care about.
    rpmhelper.RpmHeader(fh)

    header = rpmhelper.RpmHeader(fh)
    return header
{'fraction_non_alphanumeric': 0.08408992650237787, 'fraction_numerical': 0.007782101167315175, 'mean_word_length': 2.414760147601476, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6770507', 'n_tokens_mistral': 1357, 'n_tokens_neox': 1275, 'n_words': 528}