<commit_before>import os
import subprocess
from datetime import datetime, timedelta
from typing import List

import dns.query
import dns.zone
from celery import shared_task
from django.utils import timezone

from desecapi import models


class ReplicationException(Exception):

    def __init__(self, message, **kwargs):
        super().__init__(message)
        for k, v in kwargs.items():
            self.__setattr__(k, v)


class GitRepositoryException(ReplicationException):
    pass


class UnsupportedZoneNameException(ReplicationException):
    pass


class Repository:
    # TODO replication performance could potentially(*) be further improved by allowing to run multiple AXFR in
    #  parallel, and then use a file lock to synchronize git file system actions
    #  (*) but only if the signing server can sign multiple requests in parallel

    _config = {
        'user.email': '<EMAIL>',
        'user.name': 'deSEC API',
    }

    def __init__(self, path):
        self.path = path

    def _git(self, *args):
        cmd = ['/usr/bin/git'] + list(args)
        print('>>> ' + str(cmd))

        with subprocess.Popen(
                cmd,
                bufsize=0,
                cwd=self.path,
                stderr=subprocess.PIPE,
                stdout=subprocess.PIPE,
                env={'HOME': '/'},  # Celery does not adjust $HOME when dropping privleges
        ) as p:
            rcode = p.wait()
            stderr = p.stderr.read()
            stdout = p.stdout.read()
            try:
                stderr, stdout = stderr.decode(), stdout.decode()
            except UnicodeDecodeError:<commit_msg>fix(gitzones): avoid deadlock when waiting for git diff<commit_after>import os
import subprocess
from datetime import datetime, timedelta
from typing import List

import dns.query
import dns.zone
from celery import shared_task
from django.utils import timezone

from desecapi import models


class ReplicationException(Exception):

    def __init__(self, message, **kwargs):
        super().__init__(message)
        for k, v in kwargs.items():
            self.__setattr__(k, v)


class GitRepositoryException(ReplicationException):
    pass


class UnsupportedZoneNameException(ReplicationException):
    pass


class Repository:
    # TODO replication performance could potentially(*) be further improved by allowing to run multiple AXFR in
    #  parallel, and then use a file lock to synchronize git file system actions
    #  (*) but only if the signing server can sign multiple requests in parallel

    _config = {
        'user.email': '<EMAIL>',
        'user.name': 'deSEC API',
    }

    def __init__(self, path):
        self.path = path

    def _git(self, *args):
        cmd = ['/usr/bin/git'] + list(args)
        print('>>> ' + str(cmd))

        with subprocess.Popen(
                cmd,
                bufsize=0,
                cwd=self.path,
                stderr=subprocess.PIPE,
                stdout=subprocess.PIPE,
                env={'HOME': '/'},  # Celery does not adjust $HOME when dropping privleges
        ) as p:
            try:
                stdout, stderr = p.communicate(input=None, timeout=60)
                rcode = p.returncode
                stderr, stdout = stderr.decode(), stdout.decode()
            except subprocess.TimeoutExpired:
                p.kill()
                raise
            except UnicodeDecodeError:
{'fraction_non_alphanumeric': 0.09021675454012888, 'fraction_numerical': 0.0011716461628588166, 'mean_word_length': 2.271072796934866, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3366469', 'n_tokens_mistral': 953, 'n_tokens_neox': 902, 'n_words': 292}