Пример #1
0
 def test_extract_package_name(self):
     sample_package_archives_dir = os.path.join(os.path.dirname(__file__), 'sample_package_archives')
     for archive_file_name in os.listdir(sample_package_archives_dir):
         if not archive_file_name.startswith('.'):
             with open(os.path.join(sample_package_archives_dir, archive_file_name)) as f:
                 name = r_packages.get_package_name_from_archive_file(archive_file_name, f)
                 self.assertTrue(archive_file_name.startswith(name),'Extracted %s from %s' % (name, archive_file_name))
Пример #2
0
 def test_extract_package_name(self):
     sample_package_archives_dir = os.path.join(os.path.dirname(__file__),
                                                'sample_package_archives')
     for archive_file_name in os.listdir(sample_package_archives_dir):
         if not archive_file_name.startswith('.'):
             with open(
                     os.path.join(sample_package_archives_dir,
                                  archive_file_name)) as f:
                 name = r_packages.get_package_name_from_archive_file(
                     archive_file_name, f)
                 self.assertTrue(
                     archive_file_name.startswith(name),
                     'Extracted %s from %s' % (name, archive_file_name))
Пример #3
0
def packages(request):
    add_package_action = 'add_package'
    add_package_field_name = 'add_package_name'
    delete_package_action_prefix = 'delete_package_'
    upload_package_action = 'upload_package'
    upload_package_field_name = 'package_file'

    if request.method == 'POST':
        try:
            # raise errors.Error(packagelib.get_packages_path())
            # add package stanza
            if add_package_action in request.POST:
                package_name = request.POST[add_package_field_name]
                packagelib.get_package_description_lines(package_name)
                packagelib.add(request.service, package_name)
            # upload package and add package stanza
            elif upload_package_action in request.POST:
                if upload_package_field_name in request.FILES:
                    archive_file = request.FILES[upload_package_field_name]
                    package_name = packagelib.get_package_name_from_archive_file(archive_file.name, archive_file)
                    target_archive_path = os.path.join(packagelib.get_packages_path(),
                                                       packagelib.get_local_package_filename(package_name))
                    archive_file.seek(0)
                    data = archive_file.read()
                    with open(target_archive_path, 'w') as f:
                        f.write(data)
                    packagelib.add(request.service, package_name)
                else:
                    raise r_errors.Error('File missing')
            # delete package stanza
            else:
                for key in request.POST:
                    if key.startswith(delete_package_action_prefix):
                        package_name = key[len(delete_package_action_prefix):]
                        packagelib.remove(request.service, package_name)
        except r_errors.Error as e:
            return HttpResponseRedirect('./?add_error=%s' % str(e))
        except Exception as e:
            return HttpResponseRedirect('./?add_fatal_error=%s' % str(e))
        return HttpResponseRedirect('./')

    # scan for package stanzas
    r_packages = []
    for stanza, package_name in packagelib.iter_stanzas(request.service):
        r_packages.append({
            'name': package_name,
            'is_removable': stanza.access['removable'] == '1',
            'owner': stanza.access['owner'],
            'state': packagelib.get_package_state(package_name),
        })

    return {
        'packages': r_packages,
        'app_label': request.service.apps[app_id].label,
        'request': request,
        'can_add': packagelib.can_add(request.service),
        'add_package_field_name': add_package_field_name,
        'add_package_action': add_package_action,
        'delete_package_action_prefix': delete_package_action_prefix,
        'add_error': request.GET.get('add_error', ''),
        'add_fatal_error': request.GET.get('add_fatal_error', ''),
        'not_installed': packagelib.metadata_package_not_installed,
        'upload_package_action': upload_package_action,
        "upload_package_field_name": upload_package_field_name
    }
Пример #4
0
def packages(request):
    add_package_action = 'add_package'
    add_package_field_name = 'add_package_name'
    delete_package_action_prefix = 'delete_package_'
    upload_package_action = 'upload_package'
    upload_package_field_name = 'package_file'

    if request.method == 'POST':
        try:
            # raise errors.Error(packagelib.get_packages_path())
            # add package stanza
            if add_package_action in request.POST:
                package_name = request.POST[add_package_field_name]
                packagelib.get_package_description_lines(package_name)
                packagelib.add(request.service, package_name)
            # upload package and add package stanza
            elif upload_package_action in request.POST:
                if upload_package_field_name in request.FILES:
                    archive_file = request.FILES[upload_package_field_name]
                    package_name = packagelib.get_package_name_from_archive_file(
                        archive_file.name, archive_file)
                    target_archive_path = os.path.join(
                        packagelib.get_packages_path(),
                        packagelib.get_local_package_filename(package_name))
                    archive_file.seek(0)
                    data = archive_file.read()
                    with open(target_archive_path, 'w') as f:
                        f.write(data)
                    packagelib.add(request.service, package_name)
                else:
                    raise r_errors.Error('File missing')
            # delete package stanza
            else:
                for key in request.POST:
                    if key.startswith(delete_package_action_prefix):
                        package_name = key[len(delete_package_action_prefix):]
                        packagelib.remove(request.service, package_name)
        except r_errors.Error as e:
            return HttpResponseRedirect('./?add_error=%s' % str(e))
        except Exception as e:
            return HttpResponseRedirect('./?add_fatal_error=%s' % str(e))
        return HttpResponseRedirect('./')

    # scan for package stanzas
    r_packages = []
    for stanza, package_name in packagelib.iter_stanzas(request.service):
        r_packages.append({
            'name': package_name,
            'is_removable': stanza.access['removable'] == '1',
            'owner': stanza.access['owner'],
            'state': packagelib.get_package_state(package_name),
        })

    return {
        'packages': r_packages,
        'app_label': request.service.apps[app_id].label,
        'request': request,
        'can_add': packagelib.can_add(request.service),
        'add_package_field_name': add_package_field_name,
        'add_package_action': add_package_action,
        'delete_package_action_prefix': delete_package_action_prefix,
        'add_error': request.GET.get('add_error', ''),
        'add_fatal_error': request.GET.get('add_fatal_error', ''),
        'not_installed': packagelib.metadata_package_not_installed,
        'upload_package_action': upload_package_action,
        "upload_package_field_name": upload_package_field_name
    }