# Path: packages/config.py

# 
# Path: packages/email.py
# def send_new_pacakge_email(package):
#     if _cfg("smtp-host") == "":
#         return
#     smtp = smtplib.SMTP(_cfg("smtp-host"), _cfgi("smtp-port"))
#     smtp.ehlo()
#     smtp.starttls()
#     smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
#     with open("emails/new-package") as f:
#         message = MIMEText(html.parser.HTMLParser().unescape(\
#                 pystache.render(f.read(), { 'url': _cfg("protocol") + "://" + _cfg("domain") + url_for("html.package", name=package.name, repo=package.repo) })))
#     targets = [u.email for u in User.query.filter(User.admin == True)]
#     message['Subject'] = "New package pending approval"
#     message['From'] = _cfg("smtp-user")
#     message['To'] = ';'.join(targets)
#     smtp.sendmail(_cfg("smtp-user"), targets, message.as_string())
#     smtp.quit()


from flask import Blueprint, render_template, abort, request, redirect, session, url_for
from flask.ext.login import current_user, login_user
from sqlalchemy import desc
from shutil import move
from datetime import datetime
from packages.objects import *
from packages.common import *
from packages.config import _cfg
from packages.kpack import PackageInfo
from packages.email import send_new_pacakge_email
import os
import binascii
import zipfile
import urllib
import tempfile
        package.name = info.name
        package.repo = info.repo
        package.approved = False
    package.version = '{0}.{1}.{2}'.format(info.version[0], info.version[1], info.version[2])
    package.description = info.description
    package.author = info.author
    package.maintainer = info.maintainer
    package.infourl = info.infourl
    package.copyright = info.copyright
    package.capabilities = ' '.join(info.capabilities)
    package.contents = None
    package.dependencies = list()
    for dep in info.dependencies:
        try:
            repo = dep.split('/')[0]
            name = dep.split('/')[1]
            db_dep = Package.query.filter(Package.repo == repo).filter(Package.name == name).first()
            if not db_dep:
                raise Exception()
            package.dependencies.append(db_dep)
            print('appended ' + db_dep.name)
        except:
            return { 'success': False, 'error': '{0} is not a known dependency. Did you upload it first?'.format(dep) }, 400
    storage_dir = os.path.join(_cfg("storage"), package.repo)
    if not os.path.exists(storage_dir):
        os.makedirs(storage_dir)
    final_path = os.path.join(storage_dir, "{0}-{1}.pkg".format(package.name, package.version))
    move(path, final_path)
    if not existing:
        db.add(package)

# Path: packages/config.py

# 
# Path: packages/email.py
# def send_new_pacakge_email(package):
#     if _cfg("smtp-host") == "":
#         return
#     smtp = smtplib.SMTP(_cfg("smtp-host"), _cfgi("smtp-port"))
#     smtp.ehlo()
#     smtp.starttls()
#     smtp.login(_cfg("smtp-user"), _cfg("smtp-password"))
#     with open("emails/new-package") as f:
#         message = MIMEText(html.parser.HTMLParser().unescape(\
#                 pystache.render(f.read(), { 'url': _cfg("protocol") + "://" + _cfg("domain") + url_for("html.package", name=package.name, repo=package.repo) })))
#     targets = [u.email for u in User.query.filter(User.admin == True)]
#     message['Subject'] = "New package pending approval"
#     message['From'] = _cfg("smtp-user")
#     message['To'] = ';'.join(targets)
#     smtp.sendmail(_cfg("smtp-user"), targets, message.as_string())
#     smtp.quit()


# Path: packages/blueprints/api.py
from flask import Blueprint, render_template, abort, request, redirect, session, url_for
from flask.ext.login import current_user, login_user
from sqlalchemy import desc
from shutil import move
from datetime import datetime
from packages.objects import *
from packages.common import *
from packages.config import _cfg
from packages.kpack import PackageInfo
from packages.email import send_new_pacakge_email
import os
import binascii
import zipfile
import urllib
import tempfile

        package.name = info.name
        package.repo = info.repo
        package.approved = False
    package.version = '{0}.{1}.{2}'.format(info.version[0], info.version[1], info.version[2])
    package.description = info.description
    package.author = info.author
    package.maintainer = info.maintainer
    package.infourl = info.infourl
    package.copyright = info.copyright
    package.capabilities = ' '.join(info.capabilities)
    package.contents = None
    package.dependencies = list()
    for dep in info.dependencies:
        try:
            repo = dep.split('/')[0]
            name = dep.split('/')[1]
            db_dep = Package.query.filter(Package.repo == repo).filter(Package.name == name).first()
            if not db_dep:
                raise Exception()
            package.dependencies.append(db_dep)
            print('appended ' + db_dep.name)
        except:
            return { 'success': False, 'error': '{0} is not a known dependency. Did you upload it first?'.format(dep) }, 400
    storage_dir = os.path.join(_cfg("storage"), package.repo)
    if not os.path.exists(storage_dir):
        os.makedirs(storage_dir)
    final_path = os.path.join(storage_dir, "{0}-{1}.pkg".format(package.name, package.version))
    move(path, final_path)
    if not existing:
        db.add(package)

        send_new_pacakge_email(package)