コード例 #1
0
def test_replace_filelocking():
    mocked_re = MagicMock()
    mocked_fcntl = MagicMock()

    mocked_open = MagicMock()
    path = 'tests/fixtures/config.conf'

    with patch('pyolite.repo.open', mocked_open, create=True):
        manager = mocked_open.return_value.__enter__.return_value

        # asserts file locking has been put in place before reading
        manager.read = lambda: ([
            mocked_fcntl.flock.assert_called_once_with(manager, mocked_fcntl.
                                                       LOCK_EX),
            mocked_fcntl.reset_mock()
        ])

        with patch.multiple('pyolite.repo', re=mocked_re, fcntl=mocked_fcntl):
            repo = Repo(path)

            mocked_fcntl.reset_mock()
            repo.replace('pattern', 'string')

            # asserts lock has been removed after operating on file
            mocked_fcntl.flock.assert_called_once_with(manager,
                                                       mocked_fcntl.LOCK_UN)
コード例 #2
0
ファイル: test_repo.py プロジェクト: PressLabs/pyolite
def test_replace_filelocking():
    mocked_re = MagicMock()
    mocked_fcntl = MagicMock()

    mocked_open = MagicMock()
    path = 'tests/fixtures/config.conf'

    with patch('pyolite.repo.open', mocked_open, create=True):
        manager = mocked_open.return_value.__enter__.return_value

        # asserts file locking has been put in place before reading
        manager.read = lambda: ([
            mocked_fcntl.flock.assert_called_once_with(
                manager, mocked_fcntl.LOCK_EX
            ),
            mocked_fcntl.reset_mock()
        ])

        with patch.multiple('pyolite.repo', re=mocked_re,
                            fcntl=mocked_fcntl):
            repo = Repo(path)

            mocked_fcntl.reset_mock()
            repo.replace('pattern', 'string')

            # asserts lock has been removed after operating on file
            mocked_fcntl.flock.assert_called_once_with(manager,
                                                       mocked_fcntl.LOCK_UN)
コード例 #3
0
def test_it_should_replace_a_given_string_in_repo_conf():
    mocked_re = MagicMock()
    path = 'tests/fixtures/config.conf'

    mocked_re.sub.return_value = 'another_text'

    with patch.multiple('pyolite.repo', re=mocked_re):
        repo = Repo(path)
        repo.replace('pattern', 'string')

        with open('tests/fixtures/config.conf') as f:
            assert f.read() == 'another_text'

        mocked_re.sub.assert_called_once_with('pattern', 'string',
                                              'another_text')
コード例 #4
0
ファイル: test_repo.py プロジェクト: PressLabs/pyolite
def test_it_should_replace_a_given_string_in_repo_conf():
    mocked_re = MagicMock()
    path = 'tests/fixtures/config.conf'

    mocked_re.sub.return_value = 'another_text'

    with patch.multiple('pyolite.repo', re=mocked_re):
        repo = Repo(path)
        repo.replace('pattern', 'string')

        with open('tests/fixtures/config.conf') as f:
            assert f.read() == 'another_text'

        mocked_re.sub.assert_called_once_with('pattern', 'string',
                                              'another_text')
コード例 #5
0
ファイル: users.py プロジェクト: quietshu/pyolite
class ListUsers(object):
  def __init__(self, repository):
    self.repository_model = repository
    self.repo = Repo(Path(repository.path,
                          "conf/repos/%s.conf" % repository.name))

  def with_user(func):
    def decorated(self, string_user, *args, **kwargs):
      try:
        user = User.get(string_user, self.repository_model.path,
                        self.repository_model.git)
      except ValueError:
        user = User(self.repository_model.path, self.repository_model.git,
                    string_user)

      return func(self, user, *args, **kwargs)
    return decorated

  @with_user
  def add(self, user, permission):
    if user.name in self.repo.users:
      raise ValueError('User %s already exists. Please check '
                       'example/repository.py in order to see how you can '
                       'delete or change permissions' % user.name)

    if set(map(lambda permission: permission.upper(), permission)) - \
       ACCEPTED_PERMISSIONS != set([]):
      raise ValueError('Invalid permissions. They must be from %s' %
                       ACCEPTED_PERMISSIONS)

    self.repo.write("    %s     =    %s\n" % (permission, user.name))

    commit_message = 'User %s added to repo %s with permissions: %s' %\
                     (user, self.repository_model.name, permission)
    self.repository_model.git.commit(['conf'], commit_message)

    user.repos.append(self.repo)
    return user

  @with_user
  def edit(self, user, permission):
    pattern = r'(\s*)([RW+DC]*)(\s*)=(\s*)%s' % user.name
    string = r"\n    %s    =    %s" % (permission, user.name)

    self.repo.replace(pattern, string)

    self.repository_model.git.commit(['conf'],
                         "User %s has %s permission for repository %s" %
                         (user.name, permission, self.repository_model.name))
    return user

  @with_user
  def remove(self, user):
    pattern = r'(\s*)([RW+DC]*)(\s*)=(\s*)%s' % user.name
    self.repo.replace(pattern, "")

    self.repository_model.git.commit(['conf'],
                         "Deleted user %s from repository %s" %
                         (user.name, self.repository_model.name))

  def __iter__(self):
    for user in self._user:
      yield user

  def __getitem__(self, item):
    return self._users[item]

  def __setitem__(self, item, value):
    self._users[item] = value

  def __add__(self, items):
    for item in items:
      self.append(item)

  def __str__(self):
    return "['%s']" % ', '.join(self.repo.users)
コード例 #6
0
ファイル: users.py プロジェクト: PressLabs/pyolite
class ListUsers(object):
    def __init__(self, repository):
        self.repository_model = repository
        self.repo = Repo(Path(repository.path,
                              "conf/repos/%s.conf" % repository.name))

    @with_user
    def add(self, user, permission):
        if user.name in self.repo.users:
            raise ValueError('User %s already exists. Please check '
                             'example/repository.py in order to see how you can '
                             'delete or change permissions' % user.name)

        if set(map(lambda permission: permission.upper(), permission)) - \
                ACCEPTED_PERMISSIONS != set([]):
            raise ValueError('Invalid permissions. They must be from %s' %
                             ACCEPTED_PERMISSIONS)

        self.repo.write("    %s     =    %s\n" % (permission, user.name))

        commit_message = 'User %s added to repo %s with permissions: %s' % \
                         (user, self.repository_model.name, permission)
        self.repository_model.git.commit(['conf'], commit_message)

        user.repos.append(self.repo)
        return user

    @with_user
    def edit(self, user, permission):
        pattern = USER_PATTERN % user.name
        string = r"\n    %s    =    %s" % (permission, user.name)

        self.repo.replace(pattern, string)

        self.repository_model.git.commit(['conf'],
                                         "User %s has %s permission for repository %s" %
                                         (user.name, permission,
                                          self.repository_model.name))
        return user

    @with_user
    def remove(self, user):
        pattern = USER_PATTERN % user.name
        self.repo.replace(pattern, "")

        self.repository_model.git.commit(['conf'],
                                         "Deleted user %s from repository %s" %
                                         (user.name,
                                          self.repository_model.name))

    @with_user
    def get_or_create(self, user):
        return user

    def set(self, users=None, overwrite_config=False):
        users_serialized = "repo {}\n".format(self.repository_model.name)
        if isinstance(users, dict):
            users = users.iteritems()

        if users:
            for user, permission in users:
                if not hasattr(user, 'name'):
                    user = self.get_or_create(user)

                users_serialized += "    %s     =    %s\n" % (permission,
                                                              user.name)
        config = "" if overwrite_config else self.repository_model.get_config()
        self.repo.overwrite(users_serialized + config)

        users = ", ".join((user for user, permission in users))
        commit_message = "Initialized repository %s with users: %s" % (
            self.repository_model.name, users
        )
        self.repository_model.git.commit(['conf'], commit_message)

    def __iter__(self):
        for user in self._users:
            yield user

    def __getitem__(self, item):
        return self._users[item]

    def __setitem__(self, item, value):
        self._users[item] = value

    def __add__(self, items):
        for item in items:
            self.append(item)

    def __str__(self):
        return "['%s']" % ', '.join(self.repo.users)
コード例 #7
0
ファイル: users.py プロジェクト: lmz2932/pyolite
class ListUsers(object):
    def __init__(self, repository):
        self.repository_model = repository
        self.repo = Repo(Path(repository.path,
                              "conf/repos/%s.conf" % repository.name))

    def with_user(func):
        def decorated(self, string_user, *args, **kwargs):
            try:
                user = User.get(string_user, self.repository_model.path,
                                self.repository_model.git)
            except ValueError:
                user = User(self.repository_model.path,
                            self.repository_model.git,
                            string_user)
            return func(self, user, *args, **kwargs)

        return decorated

    @with_user
    def add(self, user, permission):
        if user.name in self.repo.users:
            raise ValueError('User %s already exists. Please check '
                             'example/repository.py in order to see how you can '
                             'delete or change permissions' % user.name)

        if set(map(lambda permission: permission.upper(), permission)) - \
                ACCEPTED_PERMISSIONS != set([]):
            raise ValueError('Invalid permissions. They must be from %s' %
                             ACCEPTED_PERMISSIONS)

        self.repo.write("    %s     =    %s\n" % (permission, user.name))

        commit_message = 'User %s added to repo %s with permissions: %s' % \
                         (user, self.repository_model.name, permission)
        self.repository_model.git.commit(['conf'], commit_message)

        user.repos.append(self.repo)
        return user

    @with_user
    def edit(self, user, permission):
        if user.name not in self.repo.users:
            raise ValueError('User %s not exists in repo %s' %
                             (user.name, self.repository_model.name))

        if set(map(lambda permission: permission.upper(), permission)) - \
                ACCEPTED_PERMISSIONS != set([]):
            raise ValueError('Invalid permissions. They must be from %s' %
                             ACCEPTED_PERMISSIONS)

        pattern = r'(\s*)([RW+DC]*)(\s*)=(\s*)%s' % user.name
        string = r"\n    %s    =    %s" % (permission, user.name)

        self.repo.replace(pattern, string)

        self.repository_model.git.commit(['conf'],
                                         "User %s has %s permission for repository %s" %
                                         (user.name, permission,
                                          self.repository_model.name))
        return user

    @with_user
    def remove(self, user):
        pattern = r'(\s*)([RW+DC]*)(\s*)=(\s*)%s' % user.name
        self.repo.replace(pattern, "")

        self.repository_model.git.commit(['conf'],
                                         "Deleted user %s from repository %s" %
                                         (user.name,
                                          self.repository_model.name))

    @with_user
    def get_or_create(self, user):
        return user

    def set(self, users=None):
        users_serialized = "repo {}\n".format(self.repository_model.name)
        if isinstance(users, dict):
            users = users.iteritems()

        if users:
            for user, permission in users:
                if not hasattr(user, 'name'):
                    user = self.get_or_create(user)

                users_serialized += "    %s     =    %s\n" % (permission,
                                                              user.name)

        self.repo.overwrite(users_serialized)

        users = ", ".join((user for user, permission in users))
        commit_message = "Initialized repository %s with users: %s" % (
            self.repository_model.name, users
        )
        self.repository_model.git.commit(['conf'], commit_message)

    # def __iter__(self):
    #     for user in self._user:
    #         yield user

    # def __getitem__(self, item):
    #     return self._users[item]

    # def __setitem__(self, item, value):
    #     self._users[item] = value

    # def __add__(self, items):
    #     for item in items:
    #         self.append(item)

    def __str__(self):
        return "['%s']" % ', '.join(self.repo.users)