| 1 | """ |
| 2 | Helper functions for producing tar files from tables containing |
| 3 | a product column. |
| 4 | |
| 5 | Everything in this module expects the product interface, i.e., tables |
| 6 | must at least contain accref, owner, embargo, and accsize fields. |
| 7 | """ |
| 8 | |
| 9 | #c Copyright 2008-2025, the GAVO project <[email protected]> |
| 10 | #c |
| 11 | #c This program is free software, covered by the GNU GPL. See the |
| 12 | #c COPYING file in the source distribution. |
| 13 | |
| 14 | |
| 15 | # XXX TODO: this should eventually become a renderer on the product core, |
| 16 | # redirected to from the current TarResponse. |
| 17 | |
| 18 | import functoolsscip-python python python-stdlib 3.11 functools/__init__: |
| 19 | import ioscip-python python python-stdlib 3.11 io/__init__: |
| 20 | import osscip-python python python-stdlib 3.11 os/__init__: |
| 21 | import tarfilescip-python python python-stdlib 3.11 tarfile/__init__: |
| 22 | import timescip-python python python-stdlib 3.11 time/__init__: |
| 23 | |
| 24 | from gavoscip-python python gavodachs 2.13 gavo/__init__: import basescip-python python gavodachs 2.13 `gavo.base`/__init__: |
| 25 | from gavoscip-python python gavodachs 2.13 gavo/__init__: import svcsscip-python python gavodachs 2.13 `gavo.svcs`/__init__: |
| 26 | from gavo.protocolsscip-python python gavodachs 2.13 `gavo.protocols`/__init__: import productsscip-python python gavodachs 2.13 `gavo.protocols.products`/__init__: |
| 27 | from gavo.svcsscip-python python gavodachs 2.13 `gavo.svcs`/__init__: import streamingscip-python python gavodachs 2.13 `gavo.svcs.streaming`/__init__: |
| 28 | |
| 29 | |
| 30 | MSscip-python python gavodachs 2.13 `gavo.web.producttar`/MS. = basescip-python python gavodachs 2.13 `gavo.base`/__init__:.makeStructscip-python python gavodachs 2.13 `gavo.base.structure`/makeStruct(). |
| 31 | |
| 32 | |
| 33 | class UniqueNameGeneratorscip-python python gavodachs 2.13 `gavo.web.producttar`/UniqueNameGenerator#(objectscip-python python python-stdlib 3.11 builtins/object#): |
| 34 | """A factory to build unique names from possibly ambiguous ones. |
| 35 | |
| 36 | If the lower case of a name is not known to an instance, it just returns |
| 37 | that name. Otherwise, it disambiguates by adding characters in front |
| 38 | of the extension. |
| 39 | """ |
| 40 | def __init__scip-python python gavodachs 2.13 `gavo.web.producttar`/UniqueNameGenerator#__init__().(selfscip-python python gavodachs 2.13 `gavo.web.producttar`/UniqueNameGenerator#__init__().(self)): |
| 41 | selfscip-python python gavodachs 2.13 `gavo.web.producttar`/UniqueNameGenerator#__init__().(self).knownNamesscip-python python gavodachs 2.13 `gavo.web.producttar`/UniqueNameGenerator#knownNames. = setscip-python python python-stdlib 3.11 builtins/set#() |
| 42 | |
| 43 | def _buildNamesscip-python python gavodachs 2.13 `gavo.web.producttar`/UniqueNameGenerator#_buildNames().(selfscip-python python gavodachs 2.13 `gavo.web.producttar`/UniqueNameGenerator#_buildNames().(self), baseNamescip-python python gavodachs 2.13 `gavo.web.producttar`/UniqueNameGenerator#_buildNames().(baseName)): |
| 44 | base, ext = osscip-python python python-stdlib 3.11 os/__init__:.pathscip-python python python-stdlib 3.11 os/path..splitextscip-python python python-stdlib 3.11 ntpath/join().(baseNamescip-python python gavodachs 2.13 `gavo.web.producttar`/UniqueNameGenerator#_buildNames().(baseName)) |
| 45 | yield "dc_data/%s%s"%(base, ext) |
| 46 | i = 1 |
| 47 | while True: |
| 48 | yield "dc_data/%s-%03d%s"%(base, i, ext) |
| 49 | i += 1 |
| 50 | |
| 51 | def makeNamescip-python python gavodachs 2.13 `gavo.web.producttar`/UniqueNameGenerator#makeName().(selfscip-python python gavodachs 2.13 `gavo.web.producttar`/UniqueNameGenerator#makeName().(self), baseNamescip-python python gavodachs 2.13 `gavo.web.producttar`/UniqueNameGenerator#makeName().(baseName)): |
| 52 | for name in selfscip-python python gavodachs 2.13 `gavo.web.producttar`/UniqueNameGenerator#makeName().(self)._buildNamesscip-python python gavodachs 2.13 `gavo.web.producttar`/UniqueNameGenerator#_buildNames().(baseNamescip-python python gavodachs 2.13 `gavo.web.producttar`/UniqueNameGenerator#makeName().(baseName)): |
| 53 | if name.lowerscip-python python python-stdlib 3.11 builtins/str#lower().() not in selfscip-python python gavodachs 2.13 `gavo.web.producttar`/UniqueNameGenerator#makeName().(self).knownNamesscip-python python gavodachs 2.13 `gavo.web.producttar`/UniqueNameGenerator#knownNames.: |
| 54 | selfscip-python python gavodachs 2.13 `gavo.web.producttar`/UniqueNameGenerator#makeName().(self).knownNamesscip-python python gavodachs 2.13 `gavo.web.producttar`/UniqueNameGenerator#knownNames..addscip-python python python-stdlib 3.11 builtins/set#add().(name.lowerscip-python python python-stdlib 3.11 builtins/str#lower().()) |
| 55 | return strscip-python python python-stdlib 3.11 builtins/str#(name) |
| 56 | |
| 57 | |
| 58 | class ProductTarMakerscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#(objectscip-python python python-stdlib 3.11 builtins/object#): |
| 59 | """A factory for tar files. |
| 60 | |
| 61 | You probably don't want to instantiate it directly but instead get a copy |
| 62 | through the getProductMaker function below. |
| 63 | |
| 64 | The main entry point to this class is deliverProductTar. |
| 65 | """ |
| 66 | def __init__scip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#__init__().(selfscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#__init__().(self)): |
| 67 | selfscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#__init__().(self).rdscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#rd. = basescip-python python gavodachs 2.13 `gavo.base`/__init__:.cachesscip-python python gavodachs 2.13 `gavo.base.caches`/__init__:.getRD("__system__/products") |
| 68 | selfscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#__init__().(self).corescip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#core. = selfscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#__init__().(self).rdscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#rd..getById("forTar") |
| 69 | |
| 70 | def _getEmbargoedFilescip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_getEmbargoedFile().(selfscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_getEmbargoedFile().(self), namescip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_getEmbargoedFile().(name)): |
| 71 | stuff = ioscip-python python python-stdlib 3.11 io/__init__:.BytesIOscip-python python python-stdlib 3.11 io/BytesIO#(b"This file is embargoed. Sorry.\n") |
| 72 | b = tarfilescip-python python python-stdlib 3.11 tarfile/__init__:.TarInfoscip-python python python-stdlib 3.11 tarfile/TarInfo#(namescip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_getEmbargoedFile().(name)) |
| 73 | b.sizescip-python python python-stdlib 3.11 tarfile/TarInfo#size. = len(stuff.getvaluescip-python python python-stdlib 3.11 io/BytesIO#getvalue().()) |
| 74 | b.mtimescip-python python python-stdlib 3.11 tarfile/TarInfo#mtime. = timescip-python python python-stdlib 3.11 time/__init__:.timescip-python python python-stdlib 3.11 time/time().() |
| 75 | return b, stuff |
| 76 | |
| 77 | def _getTarInfoFromProductscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_getTarInfoFromProduct().(selfscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_getTarInfoFromProduct().(self), prodscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_getTarInfoFromProduct().(prod), namescip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_getTarInfoFromProduct().(name)): |
| 78 | """returns a tar info from a general products.PlainProduct instance |
| 79 | prod. |
| 80 | |
| 81 | This is relatively inefficient for data that's actually on disk, |
| 82 | so you should only use it when data is being computed on the fly. |
| 83 | """ |
| 84 | assert not isinstance(prodscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_getTarInfoFromProduct().(prod), productsscip-python python gavodachs 2.13 `gavo.protocols.products`/__init__:.UnauthorizedProductscip-python python gavodachs 2.13 `gavo.protocols.products`/UnauthorizedProduct#) |
| 85 | data = b"".joinscip-python python python-stdlib 3.11 builtins/bytes#join().(prodscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_getTarInfoFromProduct().(prod).iterData()) |
| 86 | b = tarfilescip-python python python-stdlib 3.11 tarfile/__init__:.TarInfoscip-python python python-stdlib 3.11 tarfile/TarInfo#(namescip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_getTarInfoFromProduct().(name)) |
| 87 | b.sizescip-python python python-stdlib 3.11 tarfile/TarInfo#size. = len(data) |
| 88 | b.mtimescip-python python python-stdlib 3.11 tarfile/TarInfo#mtime. = timescip-python python python-stdlib 3.11 time/__init__:.timescip-python python python-stdlib 3.11 time/time().() |
| 89 | return b, ioscip-python python python-stdlib 3.11 io/__init__:.BytesIOscip-python python python-stdlib 3.11 io/BytesIO#(data) |
| 90 | |
| 91 | def _getDestNamescip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_getDestName().(selfscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_getDestName().(self), productsTablescip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_getDestName().(productsTable)): |
| 92 | """returns a filename for a tar with the stuff in productsTable. |
| 93 | |
| 94 | For now, we just distinguish overflowed and non-overflowed tars. |
| 95 | """ |
| 96 | qs = basescip-python python gavodachs 2.13 `gavo.base`/__init__:.getMetaTextscip-python python gavodachs 2.13 `gavo.base.meta`/getMetaText().(productsTablescip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_getDestName().(productsTable), "_queryStatus") |
| 97 | if qs=="OVERFLOW": |
| 98 | return "truncated_data.tar" |
| 99 | else: |
| 100 | return "data.tar" |
| 101 | |
| 102 | def _productsToTarscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_productsToTar().(selfscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_productsToTar().(self), productListscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_productsToTar().(productList), destinationscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_productsToTar().(destination)): |
| 103 | """actually writes the tar. |
| 104 | """ |
| 105 | nameGen = UniqueNameGeneratorscip-python python gavodachs 2.13 `gavo.web.producttar`/UniqueNameGenerator#() |
| 106 | outputTar = tarfilescip-python python python-stdlib 3.11 tarfile/__init__:.TarFilescip-python python python-stdlib 3.11 tarfile/TarFile#.openscip-python python python-stdlib 3.11 tarfile/TarFile#open().("data.tar", "w|", destinationscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_productsToTar().(destination)) |
| 107 | for prodRec in productListscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_productsToTar().(productList): |
| 108 | src = prodRec |
| 109 | if isinstance(src, productsscip-python python gavodachs 2.13 `gavo.protocols.products`/__init__:.NonExistingProductscip-python python gavodachs 2.13 `gavo.protocols.products`/NonExistingProduct#): |
| 110 | continue # just skip files that somehow don't exist any more |
| 111 | |
| 112 | elif isinstance(src, productsscip-python python gavodachs 2.13 `gavo.protocols.products`/__init__:.UnauthorizedProductscip-python python gavodachs 2.13 `gavo.protocols.products`/UnauthorizedProduct#): |
| 113 | outputTar.addfilescip-python python python-stdlib 3.11 tarfile/TarFile#addfile().(*selfscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_productsToTar().(self)._getEmbargoedFilescip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_getEmbargoedFile().( |
| 114 | nameGen.makeNamescip-python python gavodachs 2.13 `gavo.web.producttar`/UniqueNameGenerator#makeName().(src.namescip-python python gavodachs 2.13 `gavo.protocols.products`/FileProduct#name.))) |
| 115 | |
| 116 | elif isinstance(src, productsscip-python python gavodachs 2.13 `gavo.protocols.products`/__init__:.FileProductscip-python python gavodachs 2.13 `gavo.protocols.products`/FileProduct#): |
| 117 | # actual file in the file system |
| 118 | outputTar.addscip-python python python-stdlib 3.11 tarfile/TarFile#add().(strscip-python python python-stdlib 3.11 builtins/str#(src.rAccrefscip-python python gavodachs 2.13 `gavo.protocols.products`/ProductBase#rAccref..localpath), |
| 119 | nameGen.makeNamescip-python python gavodachs 2.13 `gavo.web.producttar`/UniqueNameGenerator#makeName().(src.namescip-python python gavodachs 2.13 `gavo.protocols.products`/FileProduct#name.)) |
| 120 | |
| 121 | else: # anything else is read from the src |
| 122 | outputTar.addfilescip-python python python-stdlib 3.11 tarfile/TarFile#addfile().(*selfscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_productsToTar().(self)._getTarInfoFromProductscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_getTarInfoFromProduct().(src, |
| 123 | nameGen.makeNamescip-python python gavodachs 2.13 `gavo.web.producttar`/UniqueNameGenerator#makeName().(src.name))) |
| 124 | outputTar.closescip-python python python-stdlib 3.11 tarfile/TarFile#close().() |
| 125 | return b"" # finish off request if necessary. |
| 126 | |
| 127 | def _streamOutTarscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_streamOutTar().(selfscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_streamOutTar().(self), productDatascip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_streamOutTar().(productData), requestscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_streamOutTar().(request), queryMetascip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_streamOutTar().(queryMeta), destNamescip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_streamOutTar().(destName)): |
| 128 | requestscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_streamOutTar().(request).setHeader('content-disposition', |
| 129 | 'attachment; filename=%s'%destNamescip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_streamOutTar().(destName)) |
| 130 | requestscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_streamOutTar().(request).setHeader("content-type", "application/x-tar") |
| 131 | |
| 132 | def writeTarscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#writeTar().(destscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#writeTar().(dest)): |
| 133 | selfscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_streamOutTar().(self)._productsToTarscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_productsToTar().(productDatascip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_streamOutTar().(productData), destscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#writeTar().(dest)) |
| 134 | |
| 135 | return streamingscip-python python gavodachs 2.13 `gavo.svcs.streaming`/__init__:.streamOutscip-python python gavodachs 2.13 `gavo.svcs.streaming`/streamOut().(writeTarscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#writeTar()., requestscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_streamOutTar().(request), queryMetascip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_streamOutTar().(queryMeta)) |
| 136 | |
| 137 | def deliverProductTarscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#deliverProductTar().(selfscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#deliverProductTar().(self), coreResultscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#deliverProductTar().(coreResult), requestscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#deliverProductTar().(request), queryMetascip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#deliverProductTar().(queryMeta)): |
| 138 | """causes a tar containing all accrefs mentioned in coreResult |
| 139 | to be streamed out via request. |
| 140 | """ |
| 141 | table = coreResultscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#deliverProductTar().(coreResult).getPrimaryTable() |
| 142 | productColumns = productsscip-python python gavodachs 2.13 `gavo.protocols.products`/__init__:.getProductColumnsscip-python python gavodachs 2.13 `gavo.protocols.products`/getProductColumns().(table.tableDef.columns) |
| 143 | if not productColumns: |
| 144 | raise basescip-python python gavodachs 2.13 `gavo.base`/__init__:.ValidationErrorscip-python python gavodachs 2.13 `gavo.base.structure`/Structure#("This query does not select any" |
| 145 | " columns with access references", "_OUTPUT") |
| 146 | |
| 147 | accrefs = [] |
| 148 | for row in table: |
| 149 | for col in productColumns: |
| 150 | accrefs.appendscip-python python python-stdlib 3.11 builtins/list#append().(row[col.name]) |
| 151 | |
| 152 | inputTable = svcsscip-python python gavodachs 2.13 `gavo.svcs`/__init__:.CoreArgsscip-python python gavodachs 2.13 `gavo.svcs.inputdef`/CoreArgs#.fromRawArgsscip-python python gavodachs 2.13 `gavo.svcs.inputdef`/CoreArgs#fromRawArgs().( |
| 153 | selfscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#deliverProductTar().(self).rdscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#rd..getById("forTar").inputTable, |
| 154 | {"accref": accrefs}) |
| 155 | |
| 156 | prods = selfscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#deliverProductTar().(self).corescip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#core..run(selfscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#deliverProductTar().(self).rdscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#rd..getById("getTar"), inputTable, queryMetascip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#deliverProductTar().(queryMeta)) |
| 157 | return selfscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#deliverProductTar().(self)._streamOutTarscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_streamOutTar().(prods, requestscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#deliverProductTar().(request), queryMetascip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#deliverProductTar().(queryMeta), |
| 158 | selfscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#deliverProductTar().(self)._getDestNamescip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#_getDestName().(table)) |
| 159 | |
| 160 | |
| 161 | @functoolsscip-python python python-stdlib 3.11 functools/__init__:.lru_cachescip-python python python-stdlib 3.11 functools/lru_cache().(1) |
| 162 | def getTarMakerscip-python python gavodachs 2.13 `gavo.web.producttar`/getTarMaker().(): |
| 163 | return ProductTarMakerscip-python python gavodachs 2.13 `gavo.web.producttar`/ProductTarMaker#() |