Ejemplo n.º 1
0
def package(repo, name):
    p = Package.query.filter(Package.name == name).filter(Package.repo == repo).first()
    if not p:
        abort(404)
    if p.contents == None:
        packagePath = os.path.join(_cfg("storage"), p.repo, "{0}-{1}.pkg".format(p.name, p.version))
        packageDict = PackageInfo.get_package_contents(packagePath)
        p.contents = json.dumps(packageDict)
        db.commit()
    packageContents = json.loads(p.contents)
    if p.downloads == None:
        p.downloads = 0
    return render_template("package.html", package=p, packageContents = packageContents)
Ejemplo n.º 2
0
def package(repo, name):
    p = Package.query.filter(Package.name == name).filter(Package.repo == repo).first()
    if not p:
        abort(404)
    if p.contents == None:
        packagePath = os.path.join(_cfg("storage"), p.repo, "{0}-{1}.pkg".format(p.name, p.version))
        packageDict = PackageInfo.get_package_contents(packagePath)
        p.contents = json.dumps(packageDict)
        db.commit()
    packageContents = json.loads(p.contents)
    if p.downloads == None:
        p.downloads = 0
    return render_template("package.html", package=p, packageContents = packageContents)
Ejemplo n.º 3
0
def upload_package():
    package_file = request.files.get('package')
    if not package_file:
        return { 'success': False, 'error': 'You must include a package file.' }
    f, path = tempfile.mkstemp()
    package_file.save(path)
    info = None
    try:
        info = PackageInfo.read_package(path)
        if info.repo == None or info.name == None or info.version == None:
            return { 'success': False, 'error': 'This is not a valid KnightOS package.' }, 400
        if not info.repo in ['core', 'extra', 'community', 'ports', 'nonfree']:
            return { 'success': False, 'error': '{0} is not an acceptable package repository.'.format(info.repo) }, 400
        if '/' in info.name:
            return { 'success': False, 'error': '{0} is not an acceptable package name.'.format(info.name) }, 400
    except:
        return { 'success': False, 'error': 'This is not a valid KnightOS package.' }, 400
    package = Package()
    existing = Package.query.filter(Package.name == info.name).first()
    if existing:
        if existing.repo != info.repo:
            return { 'success': False, 'error': 'This name conflicts with {0}/{1}.'.format(existing.repo, existing.name) }, 403
        if existing.user.username != current_user.username and not current_user.admin:
            return { 'success': False, 'error': 'You do not have permission to update {0}/{1}.'.format(existing.repo, existing.name) }, 403
        package = existing
        package.updated = datetime.now()
    else:
        package.user = current_user
        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)
    db.commit()
    return { 'success': True, 'url': '/{0}/{1}'.format(package.repo, package.name) }, 200
Ejemplo n.º 4
0
def upload_package():
    package_file = request.files.get('package')
    if not package_file:
        return {'success': False, 'error': 'You must include a package file.'}
    f, path = tempfile.mkstemp()
    package_file.save(path)
    info = None
    try:
        info = PackageInfo.read_package(path)
        if info.repo == None or info.name == None or info.version == None:
            return {
                'success': False,
                'error': 'This is not a valid KnightOS package.'
            }, 400
        if not info.repo in ['core', 'extra', 'community', 'ports', 'nonfree']:
            return {
                'success':
                False,
                'error':
                '{0} is not an acceptable package repository.'.format(
                    info.repo)
            }, 400
        if '/' in info.name:
            return {
                'success': False,
                'error':
                '{0} is not an acceptable package name.'.format(info.name)
            }, 400
    except:
        return {
            'success': False,
            'error': 'This is not a valid KnightOS package.'
        }, 400
    package = Package()
    existing = Package.query.filter(Package.name == info.name).first()
    if existing:
        if existing.repo != info.repo:
            return {
                'success':
                False,
                'error':
                'This name conflicts with {0}/{1}.'.format(
                    existing.repo, existing.name)
            }, 403
        if existing.user.username != current_user.username and not current_user.admin:
            return {
                'success':
                False,
                'error':
                'You do not have permission to update {0}/{1}.'.format(
                    existing.repo, existing.name)
            }, 403
        package = existing
        package.updated = datetime.now()
    else:
        package.user = current_user
        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)
    db.commit()
    return {
        'success': True,
        'url': '/{0}/{1}'.format(package.repo, package.name)
    }, 200