<commit_before># coding=utf-8

"""
DCRM - Darwin Cydia Repository Manager
Copyright (C) 2017  <NAME> <<EMAIL>>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from __future__ import unicode_literals

import os
import re
import uuid
import shutil
import hashlib

from debian.debian_support import NativeVersion
from debian.deb822 import PkgRelation
from django.urls import reverse
from django.db import models
from django.core import urlresolvers
from django.core.validators import URLValidator
from django.core.validators import validate_slug
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ValidationError
from django_rq import job
from django.conf import settings

from preferences import preferences

from WEIPDCRM.models.os_version import OSVersion
from WEIPDCRM.models.device_type import DeviceType
from WEIPDCRM.models.section import Section
from WEIPDCRM.models.debian_package import DebianPackage

from WEIPDCRM.tools import mkdir_p


@job("high")
def write_to_package_job(control, path, callback_version_id):
    # copy to temporary
    """
    This job will be called when any field in .deb file control part
    has been edited.
    
    :param control: New Control Dict
    :type control: dict
    :param path: Original Package Path
    :type path: str
    :param callback_version_id: Callback Version ID, for callback query
    :type callback_version_id: int
    """
    abs_path = os.path.join(settings.MEDIA_ROOT, path)
    temp_path = os.path.join(settings.TEMP_ROOT, str(uuid.uuid1()) + '.deb')
    shutil.copyfile(abs_path, temp_path)
    # read new package
    temp_package = DebianPackage(temp_path)
    temp_package.control = control
    # save new package
    temp_package.save()
    t_version = Version.objects.get(id=callback_version_id)
    t_version.write_callback(temp_package.path)


def validate_reversed_domain(value):
    """
    Apple's identifier.
    """
    pattern = re.compile(r"^[0-9A-Za-z.+\-]{2,}$")
    if not pattern.match(value):
        raise ValidationError(
            _("We recommend using a reverse-domain name style string (i.e., com.domainname.appname).")
        )


def validate_version(value):
    """
    Debian standard version format.
    """
    try:
        NativeVersion(value)
    except ValueError as e:
        raise ValidationError(
            _("Invalid version number.")
        )


def validate_name(value):
    """
    Value-Detail based names.
    """
    pattern = re.compile(r"[^<>]")
    if not pattern.match(value):
        raise ValidationError(
            _("Name cannot contain < or >.")
        )


def validate_relations(value):
    """
    Package Lists
    """
    relations = PkgRelation.parse_relations(value)
    for relation in relations:
        for rel in relation:
            if len(rel) == 3:
                raise ValidationError(
                    _("Cannot parse package relationship \"%s\"") % rel.get("name", "untitled")
                )


bugs_validator = URLValidator(
    schemes=["bts-type", "debbugs"],
    message=_("Enter a valid url of the bug tracking system."),
    code="invalid"
)


def validate_bugs(value):
    """
    Inherits from a Built-in URLValidator
    """
    return bugs_validator(value)


class Version(models.Model):
    """
    DCRM Base Model: Version
    This model manages all versions generated from .deb files.
    
    All database fields corresponding to the original control
    part in deb files will be prefixed by 'c_' except foreign keys.
    """
    class Meta(object):
        verbose_name = _("Version")
        verbose_name_plural = _("Versions")
    
    @staticmethod
    def get_model_fields(self):
        """
        Access the fields of Version model via _meta table
        :return: An array of fields in Version class
        """
        return self._meta.fields
    
    # Base Property
    id = models.AutoField(
        primary_key=True,
        editable=False
    )
    enabled = models.BooleanField(
        verbose_name=_("Enabled"),
        default=False,
        db_index=True
    )  # OK
    created_at = models.DateTimeField(
        verbose_name=_("Created At"),
        auto_now_add=True
    )  # OK
    
    def __unicode__(self):
        return self.c_package + ' (' + self.c_version + ')'
    
    def get_external_storage_link(self):
        """
        This getter method for storage_link property generates outer
        link for actual downloads.
        
        :return: External Storage Link
         :rtype: str
        """
        file_path = os.path.relpath(self.storage.name, settings.MEDIA_ROOT)
        slash_index = file_path.find("/")
        if slash_index > 0:
            file_path = file_path[slash_index + 1:]
        return unicode(preferences.Setting.resources_alias) + file_path
    <commit_msg>Fix Relative Path.<commit_after># coding=utf-8

"""
DCRM - Darwin Cydia Repository Manager
Copyright (C) 2017  <NAME> <<EMAIL>>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from __future__ import unicode_literals

import os
import re
import uuid
import shutil
import hashlib

from debian.debian_support import NativeVersion
from debian.deb822 import PkgRelation
from django.urls import reverse
from django.db import models
from django.core import urlresolvers
from django.core.validators import URLValidator
from django.core.validators import validate_slug
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ValidationError
from django_rq import job
from django.conf import settings

from preferences import preferences

from WEIPDCRM.models.os_version import OSVersion
from WEIPDCRM.models.device_type import DeviceType
from WEIPDCRM.models.section import Section
from WEIPDCRM.models.debian_package import DebianPackage

from WEIPDCRM.tools import mkdir_p


@job("high")
def write_to_package_job(control, path, callback_version_id):
    # copy to temporary
    """
    This job will be called when any field in .deb file control part
    has been edited.
    
    :param control: New Control Dict
    :type control: dict
    :param path: Original Package Path
    :type path: str
    :param callback_version_id: Callback Version ID, for callback query
    :type callback_version_id: int
    """
    abs_path = os.path.join(settings.MEDIA_ROOT, path)
    temp_path = os.path.join(settings.TEMP_ROOT, str(uuid.uuid1()) + '.deb')
    shutil.copyfile(abs_path, temp_path)
    # read new package
    temp_package = DebianPackage(temp_path)
    temp_package.control = control
    # save new package
    temp_package.save()
    t_version = Version.objects.get(id=callback_version_id)
    t_version.write_callback(temp_package.path)


def validate_reversed_domain(value):
    """
    Apple's identifier.
    """
    pattern = re.compile(r"^[0-9A-Za-z.+\-]{2,}$")
    if not pattern.match(value):
        raise ValidationError(
            _("We recommend using a reverse-domain name style string (i.e., com.domainname.appname).")
        )


def validate_version(value):
    """
    Debian standard version format.
    """
    try:
        NativeVersion(value)
    except ValueError as e:
        raise ValidationError(
            _("Invalid version number.")
        )


def validate_name(value):
    """
    Value-Detail based names.
    """
    pattern = re.compile(r"[^<>]")
    if not pattern.match(value):
        raise ValidationError(
            _("Name cannot contain < or >.")
        )


def validate_relations(value):
    """
    Package Lists
    """
    relations = PkgRelation.parse_relations(value)
    for relation in relations:
        for rel in relation:
            if len(rel) == 3:
                raise ValidationError(
                    _("Cannot parse package relationship \"%s\"") % rel.get("name", "untitled")
                )


bugs_validator = URLValidator(
    schemes=["bts-type", "debbugs"],
    message=_("Enter a valid url of the bug tracking system."),
    code="invalid"
)


def validate_bugs(value):
    """
    Inherits from a Built-in URLValidator
    """
    return bugs_validator(value)


class Version(models.Model):
    """
    DCRM Base Model: Version
    This model manages all versions generated from .deb files.
    
    All database fields corresponding to the original control
    part in deb files will be prefixed by 'c_' except foreign keys.
    """
    class Meta(object):
        verbose_name = _("Version")
        verbose_name_plural = _("Versions")
    
    @staticmethod
    def get_model_fields(self):
        """
        Access the fields of Version model via _meta table
        :return: An array of fields in Version class
        """
        return self._meta.fields
    
    # Base Property
    id = models.AutoField(
        primary_key=True,
        editable=False
    )
    enabled = models.BooleanField(
        verbose_name=_("Enabled"),
        default=False,
        db_index=True
    )  # OK
    created_at = models.DateTimeField(
        verbose_name=_("Created At"),
        auto_now_add=True
    )  # OK
    
    def __unicode__(self):
        return self.c_package + ' (' + self.c_version + ')'
    
    def get_external_storage_link(self):
        """
        This getter method for storage_link property generates outer
        link for actual downloads.
        
        :return: External Storage Link
         :rtype: str
        """
        file_path = os.path.relpath(self.storage.name, settings.MEDIA_ROOT)
        ext_path = os.path.join(unicode(preferences.Setting.resources_alias), file_path)
        return ext_path
    
{'fraction_non_alphanumeric': 0.08440603162927547, 'fraction_numerical': 0.002758367046708349, 'mean_word_length': 2.9581513828238717, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1329330', 'n_tokens_mistral': 3150, 'n_tokens_neox': 3053, 'n_words': 996}