github.com/whamcloud/lemur@v0.0.0-20190827193804-4655df8a52af/packaging/ci/lambda/GitPullS3/pygit2/utils.py (about)

     1  # -*- coding: utf-8 -*-
     2  #
     3  # Copyright 2010-2015 The pygit2 contributors
     4  #
     5  # This file is free software; you can redistribute it and/or modify
     6  # it under the terms of the GNU General Public License, version 2,
     7  # as published by the Free Software Foundation.
     8  #
     9  # In addition to the permissions in the GNU General Public License,
    10  # the authors give you unlimited permission to link the compiled
    11  # version of this file into combinations with other programs,
    12  # and to distribute those combinations without any restriction
    13  # coming from the use of this file.  (The General Public License
    14  # restrictions do apply in other respects; for example, they cover
    15  # modification of the file, and distribution when not linked into
    16  # a combined executable.)
    17  #
    18  # This file is distributed in the hope that it will be useful, but
    19  # WITHOUT ANY WARRANTY; without even the implied warranty of
    20  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    21  # General Public License for more details.
    22  #
    23  # You should have received a copy of the GNU General Public License
    24  # along with this program; see the file COPYING.  If not, write to
    25  # the Free Software Foundation, 51 Franklin Street, Fifth Floor,
    26  # Boston, MA 02110-1301, USA.
    27  
    28  # Import from the future
    29  from __future__ import absolute_import
    30  
    31  # Import from the Standard Library
    32  from sys import version_info
    33  
    34  # Import from pygit2
    35  from .ffi import ffi
    36  
    37  
    38  if version_info[0] < 3:
    39      from .py2 import is_string, to_bytes, to_str
    40  else:
    41      from .py3 import is_string, to_bytes, to_str
    42  
    43  
    44  
    45  def strarray_to_strings(arr):
    46      l = [None] * arr.count
    47      for i in range(arr.count):
    48          l[i] = ffi.string(arr.strings[i]).decode()
    49  
    50      return l
    51  
    52  
    53  class StrArray(object):
    54      """A git_strarray wrapper
    55  
    56      Use this in order to get a git_strarray* to pass to libgit2 out of a
    57      list of strings. This has a context manager, which you should use, e.g.
    58  
    59          with StrArray(list_of_strings) as arr:
    60              C.git_function_that_takes_strarray(arr)
    61      """
    62  
    63      def __init__(self, l):
    64          # Allow passing in None as lg2 typically considers them the same as empty
    65          if l is None:
    66              self.array = ffi.NULL
    67              return
    68  
    69          if not isinstance(l, list):
    70              raise TypeError("Value must be a list")
    71  
    72          strings = [None] * len(l)
    73          for i in range(len(l)):
    74              if not is_string(l[i]):
    75                  raise TypeError("Value must be a string")
    76  
    77              strings[i] = ffi.new('char []', to_bytes(l[i]))
    78  
    79          self._arr = ffi.new('char *[]', strings)
    80          self._strings = strings
    81          self.array = ffi.new('git_strarray *', [self._arr, len(strings)])
    82  
    83      def __enter__(self):
    84          return self.array
    85  
    86      def __exit__(self, type, value, traceback):
    87          pass
    88  
    89  
    90  class GenericIterator(object):
    91      """Helper to easily implement an iterator.
    92  
    93      The constructor gets a container which must implement __len__ and
    94      __getitem__
    95      """
    96  
    97      def __init__(self, container):
    98          self.container = container
    99          self.length = len(container)
   100          self.idx = 0
   101  
   102      def next(self):
   103          return self.__next__()
   104  
   105      def __next__(self):
   106          idx = self.idx
   107          if idx >= self.length:
   108              raise StopIteration
   109  
   110          self.idx += 1
   111          return self.container[idx]