<commit_before>from django.db.models.signals import pre_delete, pre_save
from django.dispatch import receiver
from ipynbsrv.wui.models import Container
from ipynbsrv.wui.signals.signals import *
from ipynbsrv.wui.tools import Docker

d = Docker()

""
@receiver(container_commited)
def commited(sender, image, ct_id, name, **kwargs):
    print("Received container_commited signal.")
    d.commitContainer(ct_id, name, 'latest')
    c_name = name + ":latest"
    cont = d.images_name(name)
    image.img_id = cont[0]['Id']

""
@receiver(container_created)
def created(sender, container, image, **kwargs):
    print("Received container_created signal.")
    cont = d.createContainer(image, container.name, 'True', container.image.ports)
    id = cont['Id']
    id = str(id)
    container.ct_id = id


""
@receiver(container_deleted)
def delete(sender, container, **kwargs):
    print("Received container_deleted signal.")
    containers = d.containersall()
    tmp = False
    for cont in containers:
	if cont['Id'] == container.ct_id:
	    tmp=True
    if tmp:
	d.delContainer(container.ct_id)
    else:
	print("Container allready deleted")	


""
@receiver(container_started)
def started(sender, container, **kwargs):
    print("Received container_started signal.")
    containers = d.containersall()
    tmp = False
    for cont in containers:
	if cont['Id'] == container.ct_id:
	    tmp=True
    if tmp:
    	d.startContainer(container.ct_id, container.image.ports)
    else:
	raise Exception("Container doesnt exist")

    tmp = False
    container.ports = ""
    containers = d.containers()
    for cont in containers:
	if cont['Id'] == container.ct_id:
	    for port in cont['Ports']:
		if 'PublicPort' in port:
			container.ports += str(port['PublicPort']) + "-"


""
@receiver(container_stopped)
def stopped(sender, container, **kwargs):
    print("Received container_stopped signal.")
    containers = d.containersall()
    tmp = False
    for cont in containers:
	if cont['Id'] == container.ct_id:
	    tmp=True
    if tmp:
    	d.stopContainer(container.ct_id)
	container.ports = ''
    else:
	raise Exception("Container doesnt exist")

""
@receiver(container_restarted)
def restarted(sender, container, **kwargs):
    print("Received container_stopped signal.")
    containers = d.containersall()
    tmp = False
    for cont in containers:
	if cont['Id'] == container.ct_id:
	    tmp=True
    if tmp:
    	d.restartContainer(container.ct_id)
    else:
	raise Exception("Container doesnt exist")


#
# Bridges
#
""
@receiver(pre_delete, sender=Container)
def pre_delete(sender, instance, **kwargs):
    print("Received container_pre_delete signal from container.")
    containers = d.containers()
    tmp = False
    for cont in containers:
	if cont['Id'] == instance.ct_id:
	    tmp=True
    if tmp:
	container_stopped.send(sender='', container=instance)
    container_deleted.send(sender='', container=instance)
    if instance.is_clone:
	image_deleted.send(sender='', id=instance.image.img_id)

""
@receiver(pre_save, sender=Container)
def pre_save(sender, instance, **kwargs):
    print("Received pre_save signal from container.")
    if instance.status == True:
	container_started.send(sender='',container=instance)
    else:
	container_stopped.send(sender='',container=instance)<commit_msg>Included port forwarding and provided the ssh port to the user #12<commit_after>from django.db.models.signals import pre_delete, pre_save
from django.dispatch import receiver
from ipynbsrv.wui.models import Container
from ipynbsrv.wui.signals.signals import *
from ipynbsrv.wui.tools import Docker
import re

d = Docker()

""
@receiver(container_commited)
def commited(sender, image, ct_id, name, **kwargs):
    print("Received container_commited signal.")
    d.commitContainer(ct_id, name, 'latest')
    c_name = name + ":latest"
    cont = d.images_name(name)
    image.img_id = cont[0]['Id']

""
@receiver(container_created)
def created(sender, container, image, **kwargs):
    print("Received container_created signal.")
    cont = d.createContainer(image, container.name, 'True', container.image.ports)
    id = cont['Id']
    id = str(id)
    container.ct_id = id


""
@receiver(container_deleted)
def delete(sender, container, **kwargs):
    print("Received container_deleted signal.")
    containers = d.containersall()
    tmp = False
    for cont in containers:
	if cont['Id'] == container.ct_id:
	    tmp=True
    if tmp:
	d.delContainer(container.ct_id)
    else:
	print("Container allready deleted")	


""
@receiver(container_started)
def started(sender, container, **kwargs):
    print("Received container_started signal.")
    containers = d.containersall()
    tmp = False
    for cont in containers:
	if cont['Id'] == container.ct_id:
	    tmp=True
    if tmp:
    	d.startContainer(container.ct_id, container.image.ports)
    else:
	raise Exception("Container doesnt exist")

    tmp = False
    container.ports = ""
    containers = d.containers()
    for cont in containers:
	if cont['Id'] == container.ct_id:
	    for port in cont['Ports']:
		if 'PublicPort' in port:
			container.ports += str(port['PublicPort']) + "-"
			if port['PrivatePort'] == 22:
				container.description += "\n SSH-Port = " + str(port['PublicPort'])


""
@receiver(container_stopped)
def stopped(sender, container, **kwargs):
    print("Received container_stopped signal.")
    containers = d.containersall()
    tmp = False
    for cont in containers:
	if cont['Id'] == container.ct_id:
	    tmp=True
    if tmp:
    	d.stopContainer(container.ct_id)
	container.ports = ''
	container.description = re.sub(r'SSH-Port = [0-9]*',"",container.description)
    else:
	raise Exception("Container doesnt exist")

""
@receiver(container_restarted)
def restarted(sender, container, **kwargs):
    print("Received container_restarted signal.")
#    containers = d.containersall()
#    tmp = False
#    for cont in containers:
#	if cont['Id'] == container.ct_id:
#	    tmp=True
#    if tmp:
#    	d.restartContainer(container.ct_id)
#    else:
#	raise Exception("Container doesnt exist")
    container_stopped.send(sender=sender, container=container)
    container_started.send(sender=sender, container=container)


#
# Bridges
#
""
@receiver(pre_delete, sender=Container)
def pre_delete(sender, instance, **kwargs):
    print("Received container_pre_delete signal from container.")
    containers = d.containers()
    tmp = False
    for cont in containers:
	if cont['Id'] == instance.ct_id:
	    tmp=True
    if tmp:
	container_stopped.send(sender='', container=instance)
    container_deleted.send(sender='', container=instance)
    if instance.is_clone:
	image_deleted.send(sender='', id=instance.image.img_id)

""
@receiver(pre_save, sender=Container)
def pre_save(sender, instance, **kwargs):
    print("Received pre_save signal from container.")
    if instance.status == True:
	container_started.send(sender='',container=instance)
    else:
	container_stopped.send(sender='',container=instance)
{'fraction_non_alphanumeric': 0.1295396053760366, 'fraction_numerical': 0.001143837575064341, 'mean_word_length': 4.303260045489007, 'pattern_counts': {'":': 2, '<': 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': '7234664', 'n_tokens_mistral': 2229, 'n_tokens_neox': 2190, 'n_words': 522}