Search in sources :

Example 1 with Attribute

use of org.haiku.pkg.model.Attribute in project haikudepotserver by haiku.

the class AttributeWriter method write.

private void write(int indent, AttributeContext context, Attribute attribute) throws IOException {
    Preconditions.checkNotNull(context);
    Preconditions.checkNotNull(attribute);
    Preconditions.checkState(indent >= 0);
    for (int i = 0; i < indent; i++) {
        write(' ');
    }
    write(attribute.getAttributeId().getName());
    write(" : ");
    write(attribute.getAttributeType().name());
    write(" : ");
    try {
        switch(attribute.getAttributeType()) {
            case RAW:
                ByteSource byteSource = (ByteSource) attribute.getValue(context);
                write(String.format("%d bytes", byteSource.size()));
                if (byteSource instanceof RawHeapAttribute.HeapByteSource) {
                    HeapCoordinates coordinates = ((RawHeapAttribute.HeapByteSource) byteSource).getHeapCoordinates();
                    write(String.format(" {off:%d, len:%d}", coordinates.getOffset(), coordinates.getLength()));
                }
                break;
            case INT:
                write(attribute.getValue(context).toString());
                break;
            case STRING:
                write(attribute.getValue(context).toString());
                break;
            default:
                write("???");
                break;
        }
    } catch (HpkException e) {
        throw new IOException("unable to process an attribute '" + attribute.toString() + "'", e);
    }
    write("\n");
    if (attribute.hasChildAttributes()) {
        for (Attribute childAttribute : attribute.getChildAttributes()) {
            write(indent + 2, context, childAttribute);
        }
    }
}
Also used : HpkException(org.haiku.pkg.HpkException) Attribute(org.haiku.pkg.model.Attribute) RawHeapAttribute(org.haiku.pkg.model.RawHeapAttribute) ByteSource(com.google.common.io.ByteSource) IOException(java.io.IOException) HeapCoordinates(org.haiku.pkg.heap.HeapCoordinates)

Example 2 with Attribute

use of org.haiku.pkg.model.Attribute in project haikudepotserver by haiku.

the class HpkgFileExtractorAttributeTest method testReadFile.

@Test
public void testReadFile() throws Exception {
    File hpkgFile = prepareTestFile(RESOURCE_TEST);
    try (HpkgFileExtractor hpkgFileExtractor = new HpkgFileExtractor(hpkgFile)) {
        AttributeContext tocContext = hpkgFileExtractor.getTocContext();
        List<Attribute> tocAttributes = toList(hpkgFileExtractor.getTocIterator());
        IntAttribute mTimeAttribute = (IntAttribute) tocAttributes.get(0).getChildAttributes().get(2);
        Assertions.assertThat(mTimeAttribute.getAttributeId()).isEqualTo(AttributeId.FILE_MTIME);
        Assertions.assertThat(mTimeAttribute.getAttributeType()).isEqualTo(AttributeType.INT);
        Assertions.assertThat(((Number) mTimeAttribute.getValue(tocContext)).longValue()).isEqualTo(1551679116L);
        AttributeContext packageAttributeContext = hpkgFileExtractor.getPackageAttributeContext();
        List<Attribute> packageAttributes = toList(hpkgFileExtractor.getPackageAttributesIterator());
        Attribute summaryAttribute = packageAttributes.get(1);
        Assertions.assertThat(summaryAttribute.getAttributeId()).isEqualTo(AttributeId.PACKAGE_SUMMARY);
        Assertions.assertThat(summaryAttribute.getAttributeType()).isEqualTo(AttributeType.STRING);
        Assertions.assertThat(summaryAttribute.getValue(packageAttributeContext)).isEqualTo("An application to display Haiku usage tips");
        // Pull out the actual binary to check.  The expected data results were obtained
        // from a Haiku host with the package installed.
        Attribute tipsterDirectoryEntry = findByDirectoryEntries(tocAttributes, tocContext, List.of("apps", "Tipster"));
        Attribute binaryData = tipsterDirectoryEntry.getChildAttribute(AttributeId.DATA);
        ByteSource binaryDataByteSource = (ByteSource) binaryData.getValue(tocContext);
        Assertions.assertThat(binaryDataByteSource.size()).isEqualTo(153840L);
        HashCode hashCode = binaryDataByteSource.hash(Hashing.md5());
        Assertions.assertThat(hashCode.toString().toLowerCase(Locale.ROOT)).isEqualTo("13b16cd7d035ddda09a744c49a8ebdf2");
        Optional<StringAttribute> iconAttributeOptional = tipsterDirectoryEntry.getChildAttributes(AttributeId.FILE_ATTRIBUTE).stream().map(a -> (StringAttribute) a).filter(a -> a.getValue(tocContext).equals("BEOS:ICON")).findFirst();
        Assertions.assertThat(iconAttributeOptional.isPresent()).isTrue();
        Attribute iconAttribute = iconAttributeOptional.get();
        Attribute iconBinaryData = iconAttribute.getChildAttribute(AttributeId.DATA);
        ByteSource iconDataByteSource = (ByteSource) iconBinaryData.getValue(tocContext);
        byte[] iconBytes = iconDataByteSource.read();
        assertIsHvif(iconBytes);
    }
}
Also used : StringAttribute(org.haiku.pkg.model.StringAttribute) HashCode(com.google.common.hash.HashCode) Assertions(org.fest.assertions.Assertions) Hashing(com.google.common.hash.Hashing) AttributeId(org.haiku.pkg.model.AttributeId) File(java.io.File) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) IntAttribute(org.haiku.pkg.model.IntAttribute) List(java.util.List) AttributeType(org.haiku.pkg.model.AttributeType) Locale(java.util.Locale) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) ByteSource(com.google.common.io.ByteSource) Attribute(org.haiku.pkg.model.Attribute) IntAttribute(org.haiku.pkg.model.IntAttribute) StringAttribute(org.haiku.pkg.model.StringAttribute) IntAttribute(org.haiku.pkg.model.IntAttribute) Attribute(org.haiku.pkg.model.Attribute) StringAttribute(org.haiku.pkg.model.StringAttribute) HashCode(com.google.common.hash.HashCode) ByteSource(com.google.common.io.ByteSource) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 3 with Attribute

use of org.haiku.pkg.model.Attribute in project haikudepotserver by haiku.

the class HpkrFileExtractorAttributeTest method testReadFile.

@Test
public void testReadFile() throws Exception {
    File hpkrFile = prepareTestFile(RESOURCE_TEST);
    try (HpkrFileExtractor hpkrFileExtractor = new HpkrFileExtractor(hpkrFile)) {
        AttributeIterator attributeIterator = hpkrFileExtractor.getPackageAttributesIterator();
        AttributeContext attributeContext = hpkrFileExtractor.getAttributeContext();
        Optional<Attribute> ncursesSourceAttributeOptional = tryFindAttributesForPackage(attributeIterator, "ncurses_source");
        Assertions.assertThat(ncursesSourceAttributeOptional.isPresent()).isTrue();
        Attribute ncursesSourceAttribute = ncursesSourceAttributeOptional.get();
        // now the analysis phase.
        assertThat(ncursesSourceAttribute).isNotNull();
        assertThat(ncursesSourceAttribute.getChildAttribute(AttributeId.PACKAGE_NAME).getValue(attributeContext)).isEqualTo("ncurses_source");
        assertThat(ncursesSourceAttribute.getChildAttribute(AttributeId.PACKAGE_ARCHITECTURE).getValue(attributeContext)).isEqualTo(new BigInteger("3"));
        assertThat(ncursesSourceAttribute.getChildAttribute(AttributeId.PACKAGE_URL).getValue(attributeContext)).isEqualTo("http://www.gnu.org/software/ncurses/ncurses.html");
        assertThat(ncursesSourceAttribute.getChildAttribute(AttributeId.PACKAGE_SOURCE_URL).getValue(attributeContext)).isEqualTo("Download <http://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.9.tar.gz>");
        assertThat(ncursesSourceAttribute.getChildAttribute(AttributeId.PACKAGE_CHECKSUM).getValue(attributeContext)).isEqualTo("6a25c52890e7d335247bd96965b5cac2f04dafc1de8d12ad73346ed79f3f4215");
        // check the version which is a sub-tree of attributes.
        Attribute majorVersionAttribute = ncursesSourceAttribute.getChildAttribute(AttributeId.PACKAGE_VERSION_MAJOR);
        assertThat(majorVersionAttribute.getValue(attributeContext)).isEqualTo("5");
        assertThat(majorVersionAttribute.getChildAttribute(AttributeId.PACKAGE_VERSION_MINOR).getValue(attributeContext)).isEqualTo("9");
        assertThat(majorVersionAttribute.getChildAttribute(AttributeId.PACKAGE_VERSION_REVISION).getValue(attributeContext)).isEqualTo(new BigInteger("10"));
    }
}
Also used : Attribute(org.haiku.pkg.model.Attribute) BigInteger(java.math.BigInteger) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 4 with Attribute

use of org.haiku.pkg.model.Attribute in project haikudepotserver by haiku.

the class PkgFactoryTest method testCreatePackage.

@Test
public void testCreatePackage() {
    Attribute attribute = createTestPackageAttributes();
    PkgFactory pkgFactory = new PkgFactory();
    // it is ok that this is empty because the factory should not need to reference the heap again; its all inline
    // and we are not testing the heap here.
    AttributeContext attributeContext = new AttributeContext();
    Pkg pkg = pkgFactory.createPackage(attributeContext, attribute);
    // now do some checks.
    assertThat(pkg.getArchitecture()).isEqualTo(PkgArchitecture.X86);
    assertThat(pkg.getName()).isEqualTo("testpkg");
    assertThat(pkg.getVendor()).isEqualTo("Test Vendor");
    assertThat(pkg.getSummary()).isEqualTo("This is a test package summary");
    assertThat(pkg.getDescription()).isEqualTo("This is a test package description");
    assertThat(pkg.getHomePageUrl().getUrl()).isEqualTo("http://www.haiku-os.org");
    assertThat(pkg.getHomePageUrl().getUrlType()).isEqualTo(PkgUrlType.HOMEPAGE);
    assertThat(pkg.getVersion().getMajor()).isEqualTo("6");
    assertThat(pkg.getVersion().getMinor()).isEqualTo("32");
    assertThat(pkg.getVersion().getMicro()).isEqualTo("9");
    assertThat(pkg.getVersion().getPreRelease()).isEqualTo("beta");
    assertThat(pkg.getVersion().getRevision()).isEqualTo(8);
    assertThat(pkg.getCopyrights().stream().anyMatch(x -> x.equals("Some copyright A"))).isTrue();
    assertThat(pkg.getLicenses().stream().anyMatch(x -> x.equals("Some license A"))).isTrue();
    assertThat(pkg.getLicenses().stream().anyMatch(x -> x.equals("Some license B"))).isTrue();
}
Also used : IntAttribute(org.haiku.pkg.model.IntAttribute) StringInlineAttribute(org.haiku.pkg.model.StringInlineAttribute) Attribute(org.haiku.pkg.model.Attribute) Pkg(org.haiku.pkg.model.Pkg) Test(org.junit.jupiter.api.Test)

Example 5 with Attribute

use of org.haiku.pkg.model.Attribute in project haikudepotserver by haiku.

the class PkgImportServiceImpl method populateIconFromPayload.

private void populateIconFromPayload(ObjectContext objectContext, PkgVersion persistedPkgVersion, HpkgFileExtractor hpkgFileExtractor) {
    AttributeContext context = hpkgFileExtractor.getTocContext();
    List<Attribute> iconAttrs = HpkgHelper.findIconAttributesFromExecutableDirEntries(context, hpkgFileExtractor.getToc());
    switch(iconAttrs.size()) {
        case 0:
            LOGGER.info("package [{}] version [{}] has no icons", persistedPkgVersion.getPkg(), persistedPkgVersion);
            break;
        case 1:
            populateIconFromPayload(objectContext, persistedPkgVersion, context, Iterables.getFirst(iconAttrs, null));
            break;
        default:
            LOGGER.info("package [{}] version [{}] has {} icons --> ambiguous so will not load any", persistedPkgVersion.getPkg(), persistedPkgVersion, iconAttrs.size());
            break;
    }
}
Also used : AttributeContext(org.haiku.pkg.AttributeContext) Attribute(org.haiku.pkg.model.Attribute)

Aggregations

Attribute (org.haiku.pkg.model.Attribute)7 ByteSource (com.google.common.io.ByteSource)4 Test (org.junit.jupiter.api.Test)4 File (java.io.File)3 IOException (java.io.IOException)2 AttributeContext (org.haiku.pkg.AttributeContext)2 IntAttribute (org.haiku.pkg.model.IntAttribute)2 Preconditions (com.google.common.base.Preconditions)1 HashCode (com.google.common.hash.HashCode)1 Hashing (com.google.common.hash.Hashing)1 InputStream (java.io.InputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Locale (java.util.Locale)1 Optional (java.util.Optional)1 Assertions (org.fest.assertions.Assertions)1 BadPkgIconException (org.haiku.haikudepotserver.pkg.model.BadPkgIconException)1 HpkException (org.haiku.pkg.HpkException)1