# Path: weechat/common/path.py
# def repo_path_join(*args):
#     """Join multiple paths after settings.REPO_DIR."""
#     return __path_join(settings.REPO_DIR, *args)
# 
# Path: weechat/debian/models.py
# class Repo(models.Model):
#     """A Debian repository."""
#     visible = models.BooleanField(default=True)
#     active = models.BooleanField(default=True)
#     name = models.CharField(max_length=64)
#     version = models.ForeignKey(Version, on_delete=models.CASCADE)
#     url = models.CharField(max_length=512)
#     arch = models.CharField(max_length=128)
#     builder = models.ForeignKey(Builder, on_delete=models.CASCADE)
#     # in hours, 0 = unknown frequency (next build date is not displayed)
#     build_frequency = models.IntegerField(default=24)
#     message = models.CharField(max_length=1024, blank=True)
#     priority = models.IntegerField(default=0)
# 
#     def __str__(self):
#         visible = 'visible' if self.visible else 'hidden'
#         active = 'active' if self.active else 'discontinued'
#         return (f'{self.name} {self.version}, {visible}, {active} '
#                 f'({self.arch}) ({self.priority})')
# 
#     def path_packages_gz(self, arch):
#         """Return path/name to the Packages.gz file of repository."""
#         return repo_path_join(self.name, 'dists',
#                               self.version.codename, 'main',
#                               f'binary-{arch}', 'Packages.gz')
# 
#     def apt_url(self):
#         """
#         Return the URL to use with apt for binary packages, for example:
#         "deb [signed-by=/usr/share/keyrings/weechat-archive-keyring.gpg]
#         https://weechat.org/debian sid main".
#         """
#         return (f'deb [signed-by=/usr/share/keyrings/weechat-archive-'
#                 f'keyring.gpg] {self.url} {self.version.codename} main')
# 
#     def apt_url_src(self):
#         """
#         Return the URL to use with apt for sources packages, for example:
#         "deb-src [signed-by=/usr/share/keyrings/weechat-archive-
#         keyring.gpg] https://weechat.org/debian sid main".
#         """
#         return (
#             f'deb-src [signed-by=/usr/share/keyrings/weechat-'
#             f'archive-keyring.gpg] {self.url} {self.version.codename} main'
#         )
# 
#     class Meta:
#         """Sort Repos by priority."""
#         ordering = ['priority']


from datetime import datetime, timedelta
from django.conf import settings
from django.shortcuts import render
from weechat.common.path import repo_path_join
from weechat.debian.models import Repo
import gzip
import os
import re
import pytz



def get_repository_packages(repository):
    """Get list of packages for a repository."""
    # pylint: disable=too-many-locals
    timezone = pytz.timezone(settings.TIME_ZONE)
    now = datetime.now(tz=timezone)
    repopkgs = []
    for arch in repository.arch.split(','):
        build = {
            'id': f'{repository.name}_{repository.version.version}',
            'repo': repository,
            'arch': arch,
            'date': None,
            'files': [],
        }
        filename = repository.path_packages_gz(arch)
        build['date'] = os.stat(filename).st_mtime
        with gzip.open(filename, 'rb') as _file:
            pkg = {}
            for line in _file.readlines():
                line = line.strip().decode('utf-8')
                if len(line) == 0:
                    if pkg:
                        pkg['repoarch'] = (f'{repository.name}_'
                                           f'{repository.version.codename}')
                        pkg['repo'] = repository
                        pkg['distro'] = repository.name
                        pkg['arch'] = arch

# Path: weechat/common/path.py
# def repo_path_join(*args):
#     """Join multiple paths after settings.REPO_DIR."""
#     return __path_join(settings.REPO_DIR, *args)
# 
# Path: weechat/debian/models.py
# class Repo(models.Model):
#     """A Debian repository."""
#     visible = models.BooleanField(default=True)
#     active = models.BooleanField(default=True)
#     name = models.CharField(max_length=64)
#     version = models.ForeignKey(Version, on_delete=models.CASCADE)
#     url = models.CharField(max_length=512)
#     arch = models.CharField(max_length=128)
#     builder = models.ForeignKey(Builder, on_delete=models.CASCADE)
#     # in hours, 0 = unknown frequency (next build date is not displayed)
#     build_frequency = models.IntegerField(default=24)
#     message = models.CharField(max_length=1024, blank=True)
#     priority = models.IntegerField(default=0)
# 
#     def __str__(self):
#         visible = 'visible' if self.visible else 'hidden'
#         active = 'active' if self.active else 'discontinued'
#         return (f'{self.name} {self.version}, {visible}, {active} '
#                 f'({self.arch}) ({self.priority})')
# 
#     def path_packages_gz(self, arch):
#         """Return path/name to the Packages.gz file of repository."""
#         return repo_path_join(self.name, 'dists',
#                               self.version.codename, 'main',
#                               f'binary-{arch}', 'Packages.gz')
# 
#     def apt_url(self):
#         """
#         Return the URL to use with apt for binary packages, for example:
#         "deb [signed-by=/usr/share/keyrings/weechat-archive-keyring.gpg]
#         https://weechat.org/debian sid main".
#         """
#         return (f'deb [signed-by=/usr/share/keyrings/weechat-archive-'
#                 f'keyring.gpg] {self.url} {self.version.codename} main')
# 
#     def apt_url_src(self):
#         """
#         Return the URL to use with apt for sources packages, for example:
#         "deb-src [signed-by=/usr/share/keyrings/weechat-archive-
#         keyring.gpg] https://weechat.org/debian sid main".
#         """
#         return (
#             f'deb-src [signed-by=/usr/share/keyrings/weechat-'
#             f'archive-keyring.gpg] {self.url} {self.version.codename} main'
#         )
# 
#     class Meta:
#         """Sort Repos by priority."""
#         ordering = ['priority']


# Path: weechat/debian/views.py
from datetime import datetime, timedelta
from django.conf import settings
from django.shortcuts import render
from weechat.common.path import repo_path_join
from weechat.debian.models import Repo
import gzip
import os
import re
import pytz




def get_repository_packages(repository):
    """Get list of packages for a repository."""
    # pylint: disable=too-many-locals
    timezone = pytz.timezone(settings.TIME_ZONE)
    now = datetime.now(tz=timezone)
    repopkgs = []
    for arch in repository.arch.split(','):
        build = {
            'id': f'{repository.name}_{repository.version.version}',
            'repo': repository,
            'arch': arch,
            'date': None,
            'files': [],
        }
        filename = repository.path_packages_gz(arch)
        build['date'] = os.stat(filename).st_mtime
        with gzip.open(filename, 'rb') as _file:
            pkg = {}
            for line in _file.readlines():
                line = line.strip().decode('utf-8')
                if len(line) == 0:
                    if pkg:
                        pkg['repoarch'] = (f'{repository.name}_'
                                           f'{repository.version.codename}')
                        pkg['repo'] = repository
                        pkg['distro'] = repository.name
                        pkg['arch'] = arch

                        pkgfilename = repo_path_join(repository.name,