|
| 1 | +# Copyright (c) 2022 , IRIS-HEP |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted provided that the following conditions are met: |
| 6 | +# |
| 7 | +# * Redistributions of source code must retain the above copyright notice, this |
| 8 | +# list of conditions and the following disclaimer. |
| 9 | +# |
| 10 | +# * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | +# this list of conditions and the following disclaimer in the documentation |
| 12 | +# and/or other materials provided with the distribution. |
| 13 | +# |
| 14 | +# * Neither the name of the copyright holder nor the names of its |
| 15 | +# contributors may be used to endorse or promote products derived from |
| 16 | +# this software without specific prior written permission. |
| 17 | +# |
| 18 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 22 | +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 | +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 25 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 | +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | +# |
| 29 | + |
| 30 | +# |
| 31 | +# Redistribution and use in source and binary forms, with or without |
| 32 | +# modification, are permitted provided that the following conditions are met: |
| 33 | +# |
| 34 | +from abc import ABC, abstractmethod |
| 35 | +import os |
| 36 | +import zipfile |
| 37 | +from collections import namedtuple |
| 38 | +from tempfile import TemporaryDirectory |
| 39 | + |
| 40 | +GeneratedFileResult = namedtuple('GeneratedFileResult', 'hash output_dir') |
| 41 | + |
| 42 | + |
| 43 | +class GenerateCodeException(BaseException): |
| 44 | + """Custom exception for top level code generation exceptions""" |
| 45 | + |
| 46 | + def __init__(self, message: str): |
| 47 | + BaseException.__init__(self, message) |
| 48 | + |
| 49 | + |
| 50 | +class CodeGenerator(ABC): |
| 51 | + def zipdir(self, path: str, zip_handle: zipfile.ZipFile) -> None: |
| 52 | + """Given a `path` to a directory, zip up its contents into a zip file. |
| 53 | +
|
| 54 | + Arguments: |
| 55 | + path Path to a local directory. The contents will be put into the zip file |
| 56 | + zip_handle The zip file handle to write into. |
| 57 | + """ |
| 58 | + for root, _, files in os.walk(path): |
| 59 | + for file in files: |
| 60 | + zip_handle.write(os.path.join(root, file), file) |
| 61 | + |
| 62 | + @abstractmethod |
| 63 | + def generate_code(self, query, cache_path: str): |
| 64 | + pass |
| 65 | + |
| 66 | + def translate_query_to_zip(self, query: str) -> bytes: |
| 67 | + """Translate a text ast into a zip file as a memory stream |
| 68 | +
|
| 69 | + Arguments: |
| 70 | + code Text `qastle` version of the input ast generated by func_adl |
| 71 | +
|
| 72 | + Returns |
| 73 | + bytes Data that if written as a binary output would be a zip file. |
| 74 | + """ |
| 75 | + |
| 76 | + # Generate the python code |
| 77 | + with TemporaryDirectory() as tempdir: |
| 78 | + r = self.generate_code(query, tempdir) |
| 79 | + |
| 80 | + # Zip up everything in the directory - we are going to ship it as back as part |
| 81 | + # of the message. |
| 82 | + z_filename = os.path.join(str(tempdir), 'joined.zip') |
| 83 | + zip_h = zipfile.ZipFile(z_filename, 'w', zipfile.ZIP_DEFLATED) |
| 84 | + self.zipdir(r.output_dir, zip_h) |
| 85 | + zip_h.close() |
| 86 | + |
| 87 | + with open(z_filename, 'rb') as b_in: |
| 88 | + return b_in.read() |
0 commit comments