<commit_before>from __future__ import unicode_literals

import json
import os

from cumulusci.core.config import ScratchOrgConfig
from cumulusci.core.config import OrgConfig
from cumulusci.core.exceptions import ScratchOrgException
from django.core.cache import cache
from django.db import models
from django.urls import reverse
from django.utils import timezone


class Org(models.Model):
    name = models.CharField(max_length=255)
    json = models.TextField()
    scratch = models.BooleanField(default=False)
    repo = models.ForeignKey('repository.Repository', related_name='orgs')

    class Meta:
        ordering = ['name', 'repo__owner', 'repo__name']

    def __unicode__(self):
        return '{}: {}'.format(self.repo.name, self.name)

    def get_absolute_url(self):
        return reverse('org_detail', kwargs={'org_id': self.id})

    def get_org_config(self):
        org_config = json.loads(self.json)

        return OrgConfig(org_config, self.name)

    @property
    def lock_id(self):
        if not self.scratch:
            return u'metaci-org-lock-{}'.format(self.id)

    @property
    def is_locked(self):
        if not self.scratch:
            return True if cache.get(self.lock_id) else False

    def lock(self):
        if not self.scratch:
            cache.add(self.lock_id, 'manually locked', timeout=None)

    def unlock(self):
        if not self.scratch:
            cache.delete(self.lock_id)


class ScratchOrgInstance(models.Model):
    org = models.ForeignKey('cumulusci.Org', related_name='instances')
    build = models.ForeignKey('build.Build', related_name='scratch_orgs', null=True, blank=True)
    username = models.CharField(max_length=255)
    sf_org_id = models.CharField(max_length=32)
    deleted = models.BooleanField(default=False)
    delete_error = models.TextField(null=True, blank=True)
    json = models.TextField()
    json_dx = models.TextField()
    time_created = models.DateTimeField(auto_now_add=True)
    time_deleted = models.DateTimeField(null=True, blank=True)

    def __unicode__(self):
        if self.username:
            return self.username
        if self.sf_org_id:
            return self.sf_org_id
        return '{}: {}'.format(self.org, self.id)

    def get_absolute_url(self):
        return reverse('org_instance_detail', kwargs={'org_id': self.org.id, 'instance_id': self.id})

    def get_org_config(self):
        # Write the org json file to the filesystem for Salesforce DX to use
        dx_local_dir = os.path.join(os.path.expanduser('~'), '.local', '.sfdx')
        if not os.path.isdir(dx_local_dir):
            dx_local_dir = os.path.join(os.path.expanduser('~'), '.sfdx')
        filename = os.path.join(dx_local_dir, '{}.json'.format(self.username))
        with open(filename, 'w') as f:
            f.write(self.json_dx)

        org_config = json.loads(self.json)

        return ScratchOrgConfig(org_config, self.org.name)

    def delete_org(self, org_config=None):
        if org_config is None:
            org_config = self.get_org_config()

        try:
            org_config.delete_org()
        except ScratchOrgException as e:
            self.delete_error = e.message
            self.deleted = False
            self.save()
            return

        self.time_deleted = timezone.now()
        self.deleted = True
        self.save()<commit_msg>Remove lines that checked for ~/.local/.sfdx and wrote the sfdx org json file there instead of ~/.sfdx  It looks like this was a legacy support issue for older versions of sfdx which is no longer valid and was causing scratch org logins to fail<commit_after>from __future__ import unicode_literals

import json
import os

from cumulusci.core.config import ScratchOrgConfig
from cumulusci.core.config import OrgConfig
from cumulusci.core.exceptions import ScratchOrgException
from django.core.cache import cache
from django.db import models
from django.urls import reverse
from django.utils import timezone


class Org(models.Model):
    name = models.CharField(max_length=255)
    json = models.TextField()
    scratch = models.BooleanField(default=False)
    repo = models.ForeignKey('repository.Repository', related_name='orgs')

    class Meta:
        ordering = ['name', 'repo__owner', 'repo__name']

    def __unicode__(self):
        return '{}: {}'.format(self.repo.name, self.name)

    def get_absolute_url(self):
        return reverse('org_detail', kwargs={'org_id': self.id})

    def get_org_config(self):
        org_config = json.loads(self.json)

        return OrgConfig(org_config, self.name)

    @property
    def lock_id(self):
        if not self.scratch:
            return u'metaci-org-lock-{}'.format(self.id)

    @property
    def is_locked(self):
        if not self.scratch:
            return True if cache.get(self.lock_id) else False

    def lock(self):
        if not self.scratch:
            cache.add(self.lock_id, 'manually locked', timeout=None)

    def unlock(self):
        if not self.scratch:
            cache.delete(self.lock_id)


class ScratchOrgInstance(models.Model):
    org = models.ForeignKey('cumulusci.Org', related_name='instances')
    build = models.ForeignKey('build.Build', related_name='scratch_orgs', null=True, blank=True)
    username = models.CharField(max_length=255)
    sf_org_id = models.CharField(max_length=32)
    deleted = models.BooleanField(default=False)
    delete_error = models.TextField(null=True, blank=True)
    json = models.TextField()
    json_dx = models.TextField()
    time_created = models.DateTimeField(auto_now_add=True)
    time_deleted = models.DateTimeField(null=True, blank=True)

    def __unicode__(self):
        if self.username:
            return self.username
        if self.sf_org_id:
            return self.sf_org_id
        return '{}: {}'.format(self.org, self.id)

    def get_absolute_url(self):
        return reverse('org_instance_detail', kwargs={'org_id': self.org.id, 'instance_id': self.id})

    def get_org_config(self):
        dx_local_dir = os.path.join(os.path.expanduser('~'), '.sfdx')
        filename = os.path.join(dx_local_dir, '{}.json'.format(self.username))
        with open(filename, 'w') as f:
            f.write(self.json_dx)

        org_config = json.loads(self.json)

        return ScratchOrgConfig(org_config, self.org.name)

    def delete_org(self, org_config=None):
        if org_config is None:
            org_config = self.get_org_config()

        try:
            org_config.delete_org()
        except ScratchOrgException as e:
            self.delete_error = e.message
            self.deleted = False
            self.save()
            return

        self.time_deleted = timezone.now()
        self.deleted = True
        self.save()
{'fraction_non_alphanumeric': 0.10903703703703704, 'fraction_numerical': 0.0023703703703703703, 'mean_word_length': 2.964180857310628, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1176422', 'n_tokens_mistral': 2117, 'n_tokens_neox': 2067, 'n_words': 462}