# Path: brigitte/gitserver/server.py
# class GitSession(object):
#     interface.implements(ISession)
# 
#     def __init__(self, user):
#         self.user = user
# 
#     def writeErr(self, proto, message):
#         proto.session.writeExtended(1, ('%s\n' % message).encode('UTF-8'))
# 
#     def execCommand(self, proto, cmd):
#         self.writeErr(proto, 'Welcome to Brigitte.')
# 
#         argv = shlex.split(cmd)
# 
#         repo_url = argv[-1].strip('/')
#         repo = self.get_repo(repo_url)
# 
#         close_connection = True
#         if not repo:
#             self.writeErr(proto, 'Invalid repository: %s' % repo_url)
#         else:
#             self.writeErr(proto, 'Trying to access repository: %s' % repo.slug)
#             if self.validate_git_command(proto, argv, repo):
#                 close_connection = False
#                 self.execute_git_command(proto, argv, repo)
# 
#         if close_connection:
#             proto.loseConnection()
# 
#     def validate_git_command(self, proto, argv, repo):
#         command = argv[0]
# 
#         if command not in (
#             'git-upload-archive',
#             'git-receive-pack',
#             'git-upload-pack'
#         ):
#             self.writeErr(proto, 'Invalid command: %s' % command)
#             return False
#         else:
#             # upload means receive, weird.
#             want_write = not 'upload' in command
#             self.writeErr(proto,
#                 'Attempted access: %s' % ('write' if want_write else 'read'))
# 
#             db_user = User.objects.get(username=self.user.username)
#             try:
#                 access = repo.repositoryuser_set.get(user__username=db_user)
#             except RepositoryUser.DoesNotExist:
#                 self.writeErr(proto,
#                     'No access configuration found for %s' % db_user.username)
#                 return False
# 
#             self.writeErr(proto, 'Access for %s - read: %s write: %s' % (
#                 db_user.username, access.can_read, access.can_write))
# 
#             if (
#                 (want_write and not access.can_write)
#                 or (not want_write and not access.can_read)
#             ):
#                 self.writeErr(proto, 'Access denied!')
#                 return False
# 
#             return True
# 
#     def execute_git_command(self, proto, argv, repo):
#         sh = self.user.shell
#         command = ' '.join(argv[:-1] + ["'%s'" % (repo.path,)])
#         reactor.spawnProcess(proto, sh, (sh, '-c', command))
# 
#     def eofReceived(self):
#         pass
# 
#     def closed(self):
#         pass
# 
#     def get_repo(self, reponame):
#         log.msg('looking up repository %s' % reponame)
# 
#         username, slug = reponame.strip('/').split('/', 1)
#         slug = slug.rsplit('.', 1)[0]
# 
#         try:
#             repo = Repository.objects.get(
#                 user__username=username, slug=slug)
#             return repo
#         except Repository.DoesNotExist:
#             return None
# 
# class GitConchUser(ConchUser):
#     shell = find_git_shell()
# 
#     def __init__(self, username):
#         ConchUser.__init__(self)
#         self.username = username
#         self.channelLookup.update({"session": GitProcessProtocolSession})
# 
#     def logout(self):
#         pass
# 
# class GitServer(SSHFactory):
#     portal = Portal(GitRealm())
#     portal.registerChecker(GitPubKeyChecker())
# 
#     def __init__(self, priv_key):
#         pub_key = '%s.pub' % priv_key
#         self.privateKeys = {'ssh-rsa': Key.fromFile(priv_key)}
#         self.publicKeys = {'ssh-rsa': Key.fromFile(pub_key)}
#         self.primes = {2048: [(transport.DH_GENERATOR, transport.DH_PRIME)]}


from django.conf import settings
from django.core.management.base import BaseCommand
from twisted.conch.interfaces import ISession
from twisted.internet import reactor
from twisted.python import components
from brigitte.gitserver.server import GitSession, GitConchUser, GitServer
# -*- coding: utf-8 -*-




class Command(BaseCommand):
    help = 'Starts the GitServer for brigitte.'

    def handle(self, *args, **options):
        components.registerAdapter(GitSession, GitConchUser, ISession)
        reactor.listenTCP(settings.BRIGITTE_SSH_PORT,

# Path: brigitte/gitserver/server.py
# class GitSession(object):
#     interface.implements(ISession)
# 
#     def __init__(self, user):
#         self.user = user
# 
#     def writeErr(self, proto, message):
#         proto.session.writeExtended(1, ('%s\n' % message).encode('UTF-8'))
# 
#     def execCommand(self, proto, cmd):
#         self.writeErr(proto, 'Welcome to Brigitte.')
# 
#         argv = shlex.split(cmd)
# 
#         repo_url = argv[-1].strip('/')
#         repo = self.get_repo(repo_url)
# 
#         close_connection = True
#         if not repo:
#             self.writeErr(proto, 'Invalid repository: %s' % repo_url)
#         else:
#             self.writeErr(proto, 'Trying to access repository: %s' % repo.slug)
#             if self.validate_git_command(proto, argv, repo):
#                 close_connection = False
#                 self.execute_git_command(proto, argv, repo)
# 
#         if close_connection:
#             proto.loseConnection()
# 
#     def validate_git_command(self, proto, argv, repo):
#         command = argv[0]
# 
#         if command not in (
#             'git-upload-archive',
#             'git-receive-pack',
#             'git-upload-pack'
#         ):
#             self.writeErr(proto, 'Invalid command: %s' % command)
#             return False
#         else:
#             # upload means receive, weird.
#             want_write = not 'upload' in command
#             self.writeErr(proto,
#                 'Attempted access: %s' % ('write' if want_write else 'read'))
# 
#             db_user = User.objects.get(username=self.user.username)
#             try:
#                 access = repo.repositoryuser_set.get(user__username=db_user)
#             except RepositoryUser.DoesNotExist:
#                 self.writeErr(proto,
#                     'No access configuration found for %s' % db_user.username)
#                 return False
# 
#             self.writeErr(proto, 'Access for %s - read: %s write: %s' % (
#                 db_user.username, access.can_read, access.can_write))
# 
#             if (
#                 (want_write and not access.can_write)
#                 or (not want_write and not access.can_read)
#             ):
#                 self.writeErr(proto, 'Access denied!')
#                 return False
# 
#             return True
# 
#     def execute_git_command(self, proto, argv, repo):
#         sh = self.user.shell
#         command = ' '.join(argv[:-1] + ["'%s'" % (repo.path,)])
#         reactor.spawnProcess(proto, sh, (sh, '-c', command))
# 
#     def eofReceived(self):
#         pass
# 
#     def closed(self):
#         pass
# 
#     def get_repo(self, reponame):
#         log.msg('looking up repository %s' % reponame)
# 
#         username, slug = reponame.strip('/').split('/', 1)
#         slug = slug.rsplit('.', 1)[0]
# 
#         try:
#             repo = Repository.objects.get(
#                 user__username=username, slug=slug)
#             return repo
#         except Repository.DoesNotExist:
#             return None
# 
# class GitConchUser(ConchUser):
#     shell = find_git_shell()
# 
#     def __init__(self, username):
#         ConchUser.__init__(self)
#         self.username = username
#         self.channelLookup.update({"session": GitProcessProtocolSession})
# 
#     def logout(self):
#         pass
# 
# class GitServer(SSHFactory):
#     portal = Portal(GitRealm())
#     portal.registerChecker(GitPubKeyChecker())
# 
#     def __init__(self, priv_key):
#         pub_key = '%s.pub' % priv_key
#         self.privateKeys = {'ssh-rsa': Key.fromFile(priv_key)}
#         self.publicKeys = {'ssh-rsa': Key.fromFile(pub_key)}
#         self.primes = {2048: [(transport.DH_GENERATOR, transport.DH_PRIME)]}


# Path: brigitte/gitserver/management/commands/run_gitserver.py
from django.conf import settings
from django.core.management.base import BaseCommand
from twisted.conch.interfaces import ISession
from twisted.internet import reactor
from twisted.python import components
from brigitte.gitserver.server import GitSession, GitConchUser, GitServer

# -*- coding: utf-8 -*-




class Command(BaseCommand):
    help = 'Starts the GitServer for brigitte.'

    def handle(self, *args, **options):
        components.registerAdapter(GitSession, GitConchUser, ISession)
        reactor.listenTCP(settings.BRIGITTE_SSH_PORT,

            GitServer(settings.BRIGITTE_SSH_KEY_PATH))