diff --git a/.gitmodules b/.gitmodules index 1c3b404..d79419f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,9 @@ [submodule "oss-internship-2020/sapi_lodepng/lodepng"] path = oss-internship-2020/lodepng/lodepng url = https://github.com/lvandeve/lodepng +[submodule "oss-internship-2020/jsonnet/jsonnet"] + path = oss-internship-2020/jsonnet/jsonnet + url = https://github.com/google/jsonnet.git [submodule "oss-internship-2020/openjpeg/openjpeg"] path = oss-internship-2020/openjpeg/openjpeg url = https://github.com/uclouvain/openjpeg.git diff --git a/oss-internship-2020/jsonnet/CMakeLists.txt b/oss-internship-2020/jsonnet/CMakeLists.txt new file mode 100644 index 0000000..c7759b5 --- /dev/null +++ b/oss-internship-2020/jsonnet/CMakeLists.txt @@ -0,0 +1,58 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http:#www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +cmake_minimum_required(VERSION 3.10) + +project(jsonnet-sapi C CXX) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED True) + +add_subdirectory(jsonnet) + +add_subdirectory(examples) + +set(SAPI_ROOT "../.." CACHE PATH "Path to the Sandboxed API source tree") +set(SAPI_ENABLE_EXAMPLES OFF CACHE BOOL "") +set(SAPI_ENABLE_TESTS OFF CACHE BOOL "") + +add_subdirectory("${SAPI_ROOT}" + "${CMAKE_BINARY_DIR}/sandboxed-api-build" + EXCLUDE_FROM_ALL) + +add_sapi_library(jsonnet_sapi + FUNCTIONS c_jsonnet_evaluate_snippet + c_jsonnet_make + c_jsonnet_destroy + c_read_input + c_write_output_file + c_jsonnet_realloc + c_jsonnet_evaluate_snippet_multi + c_write_multi_output_files + c_write_output_stream + c_jsonnet_evaluate_snippet_stream + c_jsonnet_fmt_snippet + INPUTS jsonnet_helper.h + LIBRARY jsonnet_helper + LIBRARY_NAME Jsonnet + NAMESPACE "" +) + +target_include_directories(jsonnet_sapi INTERFACE + "${PROJECT_BINARY_DIR}" +) + +target_link_libraries(jsonnet_sapi PUBLIC jsonnet_helper) + +add_subdirectory(tests) diff --git a/oss-internship-2020/jsonnet/README.md b/oss-internship-2020/jsonnet/README.md new file mode 100644 index 0000000..43e46db --- /dev/null +++ b/oss-internship-2020/jsonnet/README.md @@ -0,0 +1,84 @@ +# Jsonnet Sandboxed API + +This library provides sandboxed version of the +[Jsonnet](https://github.com/google/jsonnet) library. + +## Examples + +The `examples/` directory contains code to produce three command-line tools -- +`jsonnet_sandboxed`, `jsonnet_yaml_stream_sandboxed` and +`jsonnet_multiple_files_sandboxed` to evaluate jsonnet code. The first one +enables the user to evaluate jsonnet code held in one file and writing to one +output file. The second evaluates one jsonnet file into one file, which can be +interepreted as YAML stream. The third one is for evaluating one jsonnet file +into multiple output files. All three tools are based on what can be found +[here](https://github.com/google/jsonnet/blob/master/cmd/jsonnet.cpp). + +Apart from these, there is also a file producing `jsonnet_formatter_sandboxed` +executable. It is based on a tool found from +[here](https://github.com/google/jsonnet/blob/master/cmd/jsonnetfmt.cpp). It is +a jsonnet code formatter -- it changes poorly written jsonnet files into their +canonical form. + +## Build + +To build these examples, after cloning the whole Sandbox API project, you also +need to run + +``` +git submodule update --init --recursive +``` +anywhere in the project tree in order to clone the `jsonnet` submodule. +Then in the `sandboxed-api/oss-internship-2020/jsonnet` run + +``` +mkdir build && cd build +cmake -G Ninja +ninja +``` + +To run `jsonnet_sandboxed` (or `jsonnet_yaml_stream_sandboxed` or +`jsonnet_formatter_sandboxed` in a similar way): + +``` +cd examples +./jsonnet_sandboxed \ + absolute/path/to/the/input_file.jsonnet \ + absolute/path/to/the/output_file +``` + +To run `jsonnet_mutiple_files_sandboxed`: + +``` +cd examples +./jsonnet_mutiple_files_sandboxed \ + absolute/path/to/the/input_file.jsonnet \ + absolute/path/to/the/output_directory +``` + +All three tools support evaluating one input file (possibly relying on multiple +other files, e.x. by jsonnet `import` command; the files must be held in the +same directory as input file) into one or more output files. Example jsonnet +codes to evaluate in a one-in-one-out manner can be found +[here](https://github.com/google/jsonnet/tree/master/examples). Example code +producing multiple output files or YAML stream files can be found in the +`examples/jsonnet_codes` directory (along with some other examples copied with +minimal changes from the library files), in files called +`multiple_files_example.jsonnet` and `yaml_stream_example.jsonnet`, +respectively. In the `examples/jsonnet_codes_expected_output` directory one can +found outputs the mentioned above files' evaluation should produce. + +The formatter reads one input file and produces one output file as a result. +Example code for this tool can also be found in `examples/jsonnet_codes` +directory, in a file called `formatter_example.jsonnet`. + +## Testing + +A few tests prepared with a use of +[Google Test](https://github.com/google/googletest) framework can be found in +the `tests/` directory. To run them type: + +``` +cd tests +./tests +``` diff --git a/oss-internship-2020/jsonnet/examples/CMakeLists.txt b/oss-internship-2020/jsonnet/examples/CMakeLists.txt new file mode 100644 index 0000000..eafe2d0 --- /dev/null +++ b/oss-internship-2020/jsonnet/examples/CMakeLists.txt @@ -0,0 +1,75 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/gen_files") +file(COPY "${PROJECT_SOURCE_DIR}/jsonnet/cmd/jsonnet.cpp" DESTINATION "${PROJECT_BINARY_DIR}/gen_files/") +file(COPY "${PROJECT_SOURCE_DIR}/jsonnet.patch" DESTINATION "${PROJECT_BINARY_DIR}/gen_files/") + +add_custom_command( + OUTPUT ${PROJECT_BINARY_DIR}/gen_files/write_helper.cc + COMMAND cd ${PROJECT_BINARY_DIR}/gen_files && patch < ${PROJECT_SOURCE_DIR}/jsonnet.patch > /dev/null + COMMAND mv ${PROJECT_BINARY_DIR}/gen_files/jsonnet.cpp ${PROJECT_BINARY_DIR}/gen_files/write_helper.cc +) + +list(APPEND JSONNET_SAPI_HEADERS + ${PROJECT_SOURCE_DIR} + ${PROJECT_SOURCE_DIR}/headers + ${PROJECT_BINARY_DIR}/gen_files +) + +add_library(jsonnet_helper STATIC + ${PROJECT_SOURCE_DIR}/jsonnet_helper.cc + ${PROJECT_SOURCE_DIR}/jsonnet_helper.h + ${PROJECT_SOURCE_DIR}/jsonnet/cmd/utils.h + ${PROJECT_SOURCE_DIR}/jsonnet/cmd/utils.cpp + ${PROJECT_BINARY_DIR}/gen_files/write_helper.cc +) + +target_include_directories(jsonnet_helper PUBLIC + ${JSONNET_SAPI_HEADERS} +) + +target_link_libraries(jsonnet_helper + libjsonnet_for_binaries +) + +foreach(exe base multiple_files yaml_stream formatter) + add_executable(jsonnet_${exe}_sandboxed + jsonnet_${exe}_example.cc + ) + + target_link_libraries(jsonnet_${exe}_sandboxed PRIVATE + libjsonnet + jsonnet_helper + jsonnet_sapi + sandbox2::file_base + sandbox2::fileops + sapi::sapi + ) + + target_include_directories(jsonnet_${exe}_sandboxed PUBLIC + ${JSONNET_SAPI_HEADERS} + ) +endforeach() + +add_executable(jsonnet_base_transacted + jsonnet_base_transaction.cc +) + +target_link_libraries(jsonnet_base_transacted PRIVATE + libjsonnet + jsonnet_helper + jsonnet_sapi + sapi::sapi +) diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_base_example.cc b/oss-internship-2020/jsonnet/examples/jsonnet_base_example.cc new file mode 100644 index 0000000..519f803 --- /dev/null +++ b/oss-internship-2020/jsonnet/examples/jsonnet_base_example.cc @@ -0,0 +1,95 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "jsonnet_base_sandbox.h" // NOLINT(build/include) +#include "sandboxed_api/sandbox2/util/fileops.h" +#include "sandboxed_api/sandbox2/util/path.h" + +absl::Status JsonnetMain(std::string in_file, std::string out_file) { + using sandbox2::file::JoinPath; + using sandbox2::file_util::fileops::Basename; + + // Initialize sandbox. + JsonnetBaseSandbox sandbox(in_file, out_file); + SAPI_RETURN_IF_ERROR(sandbox.Init()) + + JsonnetApi api(&sandbox); + + // Initialize library's main structure. + SAPI_ASSIGN_OR_RETURN(JsonnetVm * jsonnet_vm, api.c_jsonnet_make()); + sapi::v::RemotePtr vm_pointer(jsonnet_vm); + + // Read input file. + std::string in_file_in_sandboxee(JoinPath("/input", Basename(in_file))); + sapi::v::ConstCStr in_file_var(in_file_in_sandboxee.c_str()); + SAPI_ASSIGN_OR_RETURN(char* input, + api.c_read_input(false, in_file_var.PtrBefore())); + + // Process jsonnet data. + sapi::v::RemotePtr input_pointer(input); + sapi::v::Int error; + SAPI_ASSIGN_OR_RETURN(char* output, api.c_jsonnet_evaluate_snippet( + &vm_pointer, in_file_var.PtrBefore(), + &input_pointer, error.PtrAfter())); + CHECK(!error.GetValue()) << "Jsonnet code evaluation failed: " + << error.GetValue() << "\n" + << "Make sure all files used by your jsonnet file " + "are in the same directory as your file."; + + // Write data to file. + std::string out_file_in_sandboxee(JoinPath("/output", Basename(out_file))); + sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str()); + sapi::v::RemotePtr output_pointer(output); + + SAPI_ASSIGN_OR_RETURN( + bool success, + api.c_write_output_file(&output_pointer, out_file_var.PtrBefore())); + CHECK(success) << "Writing to output file failed: " << success; + + // Clean up. + SAPI_ASSIGN_OR_RETURN(char* result, + api.c_jsonnet_realloc(&vm_pointer, &output_pointer, 0)); + SAPI_RETURN_IF_ERROR(api.c_jsonnet_destroy(&vm_pointer)); + SAPI_RETURN_IF_ERROR(api.c_free_input(&input_pointer)); + + return absl::OkStatus(); +} + +int main(int argc, char* argv[]) { + using sandbox2::file_util::fileops::Basename; + + google::InitGoogleLogging(argv[0]); + gflags::ParseCommandLineFlags(&argc, &argv, true); + + if (!(argc == 3)) { + std::cerr << "Usage:\n" + << Basename(argv[0]) + << " /absolute/path/to/INPUT.jsonnet /absolute/path/to/OUTPUT\n"; + return EXIT_FAILURE; + } + + std::string in_file(argv[1]); + std::string out_file(argv[2]); + + absl::Status status = JsonnetMain(in_file, out_file); + if (!status.ok()) { + LOG(ERROR) << "Failed: " << status.ToString(); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_base_transaction.cc b/oss-internship-2020/jsonnet/examples/jsonnet_base_transaction.cc new file mode 100644 index 0000000..2d199b5 --- /dev/null +++ b/oss-internship-2020/jsonnet/examples/jsonnet_base_transaction.cc @@ -0,0 +1,88 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "jsonnet_base_transaction.h" // NOLINT(build/include) + +#include "sandboxed_api/sandbox2/util/fileops.h" +#include "sandboxed_api/sandbox2/util/path.h" + +absl::Status JsonnetTransaction::Main() { + using sandbox2::file::JoinPath; + using sandbox2::file_util::fileops::Basename; + + JsonnetApi api(sandbox()); + + // Initialize library's main structure. + SAPI_ASSIGN_OR_RETURN(JsonnetVm * jsonnet_vm, api.c_jsonnet_make()); + sapi::v::RemotePtr vm_pointer(jsonnet_vm); + + // Read input file. + std::string in_file_in_sandboxee(JoinPath("/input", Basename(in_file))); + sapi::v::ConstCStr in_file_var(in_file_in_sandboxee.c_str()); + SAPI_ASSIGN_OR_RETURN(char* input, + api.c_read_input(false, in_file_var.PtrBefore())); + + // Process jsonnet data. + sapi::v::RemotePtr input_pointer(input); + sapi::v::Int error; + SAPI_ASSIGN_OR_RETURN(char* output, api.c_jsonnet_evaluate_snippet( + &vm_pointer, in_file_var.PtrBefore(), + &input_pointer, error.PtrAfter())); + TRANSACTION_FAIL_IF_NOT(error.GetValue() == 0, + "Jsonnet code evaluation failed."); + + // Write data to file. + std::string out_file_in_sandboxee(JoinPath("/output", Basename(out_file))); + sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str()); + sapi::v::RemotePtr output_pointer(output); + SAPI_ASSIGN_OR_RETURN( + bool success, + api.c_write_output_file(&output_pointer, out_file_var.PtrBefore())); + TRANSACTION_FAIL_IF_NOT(success, "Writing to output file failed."); + + // Clean up. + SAPI_ASSIGN_OR_RETURN(char* result, + api.c_jsonnet_realloc(&vm_pointer, &output_pointer, 0)); + + SAPI_RETURN_IF_ERROR(api.c_jsonnet_destroy(&vm_pointer)); + SAPI_RETURN_IF_ERROR(api.c_free_input(&input_pointer)); + + return absl::OkStatus(); +} + +int main(int argc, char* argv[]) { + using sandbox2::file_util::fileops::Basename; + + google::InitGoogleLogging(argv[0]); + gflags::ParseCommandLineFlags(&argc, &argv, true); + + if (!(argc == 3)) { + std::cerr << "Usage:\n" + << Basename(argv[0]) + << " /absolute/path/to/INPUT.jsonnet /absolute/path/to/OUTPUT\n"; + return EXIT_FAILURE; + } + + std::string in_file(argv[1]); + std::string out_file(argv[2]); + + JsonnetTransaction jsonnet_transaction(in_file, out_file); + + auto result = jsonnet_transaction.Run(); + + LOG(INFO) << "Transaction result: " << result.message(); + CHECK(result.ok()); + + return EXIT_SUCCESS; +} diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes/arith.jsonnet b/oss-internship-2020/jsonnet/examples/jsonnet_codes/arith.jsonnet new file mode 100644 index 0000000..c967b95 --- /dev/null +++ b/oss-internship-2020/jsonnet/examples/jsonnet_codes/arith.jsonnet @@ -0,0 +1,50 @@ +/* +Copyright 2015 Google Inc. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +{ + concat_array: [1, 2, 3] + [4], + concat_string: '123' + 4, + equality1: 1 == '1', + equality2: [{}, { x: 3 - 1 }] + == [{}, { x: 2 }], + ex1: 1 + 2 * 3 / (4 + 5), + // Bitwise operations first cast to int. + ex2: self.ex1 | 3, + // Modulo operator. + ex3: self.ex1 % 2, + // Boolean logic + ex4: (4 > 3) && (1 <= 3) || false, + // Mixing objects together + obj: { a: 1, b: 2 } + { b: 3, c: 4 }, + // Test if a field is in an object + obj_member: 'foo' in { foo: 1 }, + // String formatting + str1: 'The value of self.ex2 is ' + + self.ex2 + '.', + str2: 'The value of self.ex2 is %g.' + % self.ex2, + str3: 'ex1=%0.2f, ex2=%0.2f' + % [self.ex1, self.ex2], + // By passing self, we allow ex1 and ex2 to + // be extracted internally. + str4: 'ex1=%(ex1)0.2f, ex2=%(ex2)0.2f' + % self, + // Do textual templating of entire files: + str5: ||| + ex1=%(ex1)0.2f + ex2=%(ex2)0.2f + ||| % self, +} diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes/formatter_example.jsonnet b/oss-internship-2020/jsonnet/examples/jsonnet_codes/formatter_example.jsonnet new file mode 100644 index 0000000..29ef188 --- /dev/null +++ b/oss-internship-2020/jsonnet/examples/jsonnet_codes/formatter_example.jsonnet @@ -0,0 +1,32 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// This is a poorly written jsonnet file. Given to the formatter executable will be changed into a canonical jsonnet file form. +local b = import "somefile.libsonnet"; # comment +local a = import "differentfile.libsonnet"; // another comment in different style + +local SomeStuff = {bar: "foo"}; + + local funtion_to_do_addition(x,y)=x+y; + + { +"this": ((3)) , +"that that": +funtion_to_do_addition(4,2), +arrArr: [[ + 1, 2, 5 + ], + 3, 10, 19 + ] +} + SomeStuff diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes/imports.jsonnet b/oss-internship-2020/jsonnet/examples/jsonnet_codes/imports.jsonnet new file mode 100644 index 0000000..7b26dd2 --- /dev/null +++ b/oss-internship-2020/jsonnet/examples/jsonnet_codes/imports.jsonnet @@ -0,0 +1,30 @@ +/* +Copyright 2015 Google Inc. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +local martinis = import 'martinis.libsonnet'; + +{ + 'Vodka Martini': martinis['Vodka Martini'], + Manhattan: { + ingredients: [ + { kind: 'Rye', qty: 2.5 }, + { kind: 'Sweet Red Vermouth', qty: 1 }, + { kind: 'Angostura', qty: 'dash' }, + ], + garnish: importstr 'garnish.txt', + served: 'Straight Up', + }, +} diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes/multiple_files_example.jsonnet b/oss-internship-2020/jsonnet/examples/jsonnet_codes/multiple_files_example.jsonnet new file mode 100644 index 0000000..4856eee --- /dev/null +++ b/oss-internship-2020/jsonnet/examples/jsonnet_codes/multiple_files_example.jsonnet @@ -0,0 +1,26 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// This is a jsonnet code which evaluates to mutliple output files. +{ + "first_file.json": { + name: 'This is the first file created by the multiple-files example code.', + caption: 'The other one\'s name is -> ' + $["second_file.json"].name, + }, + "second_file.json": { + name: 'And that is the other one.', + caption: 'If it was the first one, variable name would hold what\'s in variable.', + first_name: $["first_file.json"].name, + }, +} diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes/negroni.jsonnet b/oss-internship-2020/jsonnet/examples/jsonnet_codes/negroni.jsonnet new file mode 100644 index 0000000..4f42b27 --- /dev/null +++ b/oss-internship-2020/jsonnet/examples/jsonnet_codes/negroni.jsonnet @@ -0,0 +1,29 @@ +/* +Copyright 2015 Google Inc. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +local utils = import 'utils.libsonnet'; +{ + Negroni: { + // Divide 3oz among the 3 ingredients. + ingredients: utils.equal_parts(3, [ + 'Farmers Gin', + 'Sweet Red Vermouth', + 'Campari', + ]), + garnish: 'Orange Peel', + served: 'On The Rocks', + }, +} diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes/utils.libsonnet b/oss-internship-2020/jsonnet/examples/jsonnet_codes/utils.libsonnet new file mode 100644 index 0000000..7fd1fcc --- /dev/null +++ b/oss-internship-2020/jsonnet/examples/jsonnet_codes/utils.libsonnet @@ -0,0 +1,26 @@ +/* +Copyright 2015 Google Inc. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +{ + equal_parts(size, ingredients):: + // Define a function-scoped variable. + local qty = size / std.length(ingredients); + // Return an array. + [ + { kind: i, qty: qty } + for i in ingredients + ], +} diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes/yaml_stream_example.jsonnet b/oss-internship-2020/jsonnet/examples/jsonnet_codes/yaml_stream_example.jsonnet new file mode 100644 index 0000000..ca7520a --- /dev/null +++ b/oss-internship-2020/jsonnet/examples/jsonnet_codes/yaml_stream_example.jsonnet @@ -0,0 +1,26 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// This is a jsonnet code which evaluates to json file, which can be interpreted as YAML stream. +local + first_object = { + name: 'First object\'s name.', + age: 'Just created!', + }, + second_object = { + name: 'Hi, my name is .', + sibling: first_object.name + }; + +[first_object, second_object] diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/arith.golden b/oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/arith.golden new file mode 100644 index 0000000..a23901c --- /dev/null +++ b/oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/arith.golden @@ -0,0 +1,26 @@ +{ + "concat_array": [ + 1, + 2, + 3, + 4 + ], + "concat_string": "1234", + "equality1": false, + "equality2": true, + "ex1": 1.6666666666666665, + "ex2": 3, + "ex3": 1.6666666666666665, + "ex4": true, + "obj": { + "a": 1, + "b": 3, + "c": 4 + }, + "obj_member": true, + "str1": "The value of self.ex2 is 3.", + "str2": "The value of self.ex2 is 3.", + "str3": "ex1=1.67, ex2=3.00", + "str4": "ex1=1.67, ex2=3.00", + "str5": "ex1=1.67\nex2=3.00\n" +} diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/first_file.json b/oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/first_file.json new file mode 100644 index 0000000..eb28fde --- /dev/null +++ b/oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/first_file.json @@ -0,0 +1,4 @@ +{ + "caption": "The other one's name is -> And that is the other one.", + "name": "This is the first file created by the multiple-files example code." +} diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/negroni.golden b/oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/negroni.golden new file mode 100644 index 0000000..b25b2fc --- /dev/null +++ b/oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/negroni.golden @@ -0,0 +1,20 @@ +{ + "Negroni": { + "garnish": "Orange Peel", + "ingredients": [ + { + "kind": "Farmers Gin", + "qty": 1 + }, + { + "kind": "Sweet Red Vermouth", + "qty": 1 + }, + { + "kind": "Campari", + "qty": 1 + } + ], + "served": "On The Rocks" + } +} diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/second_file.json b/oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/second_file.json new file mode 100644 index 0000000..5afb469 --- /dev/null +++ b/oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/second_file.json @@ -0,0 +1,5 @@ +{ + "caption": "If it was the first one, variable name would hold what's in variable.", + "first_name": "This is the first file created by the multiple-files example code.", + "name": "And that is the other one." +} diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/yaml_stream_example.yaml b/oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/yaml_stream_example.yaml new file mode 100644 index 0000000..231a5d0 --- /dev/null +++ b/oss-internship-2020/jsonnet/examples/jsonnet_codes_expected_output/yaml_stream_example.yaml @@ -0,0 +1,11 @@ +--- +{ + "age": "Just created!", + "name": "First object's name." +} +--- +{ + "name": "Hi, my name is .", + "sibling": "First object's name." +} +... diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_formatter_example.cc b/oss-internship-2020/jsonnet/examples/jsonnet_formatter_example.cc new file mode 100644 index 0000000..f236e16 --- /dev/null +++ b/oss-internship-2020/jsonnet/examples/jsonnet_formatter_example.cc @@ -0,0 +1,128 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include +#include + +#include "jsonnet_sapi.sapi.h" // NOLINT(build/include) +#include "sandboxed_api/util/flag.h" +#include "sandboxed_api/sandbox2/util/fileops.h" +#include "sandboxed_api/sandbox2/util/path.h" + +class JsonnetSapiSandbox : public JsonnetSandbox { + public: + explicit JsonnetSapiSandbox(std::string in_file, std::string out_file) + : in_file_(std::move(in_file)), out_file_(std::move(out_file)) {} + + // We need only the input file here, not the whole input directory + std::unique_ptr ModifyPolicy( + sandbox2::PolicyBuilder*) override { + return sandbox2::PolicyBuilder() + .AllowStaticStartup() + .AllowOpen() + .AllowRead() + .AllowWrite() + .AllowStat() + .AllowSystemMalloc() + .AllowExit() + .AllowSyscalls({ + __NR_futex, + __NR_close, + }) + .AddDirectoryAt(dirname(&out_file_[0]), "/output", /*is_ro=*/false) + .AddFile(in_file_, true) + .BuildOrDie(); + } + + private: + std::string in_file_; + std::string out_file_; +}; + +absl::Status JsonnetMain(std::string in_file, std::string out_file) { + using sandbox2::file::JoinPath; + using sandbox2::file_util::fileops::Basename; + + // Initialize sandbox. + JsonnetSapiSandbox sandbox(in_file, out_file); + SAPI_RETURN_IF_ERROR(sandbox.Init()) + + JsonnetApi api(&sandbox); + + // Initialize library's main structure. + SAPI_ASSIGN_OR_RETURN(JsonnetVm * jsonnet_vm, api.c_jsonnet_make()); + sapi::v::RemotePtr vm_pointer(jsonnet_vm); + + // Read input file. + std::string in_file_in_sandboxee(JoinPath("/input", Basename(in_file))); + sapi::v::ConstCStr in_file_var(in_file_in_sandboxee.c_str()); + SAPI_ASSIGN_OR_RETURN(char* input, + api.c_read_input(false, in_file_var.PtrBefore())); + + // Process jsonnet data. + sapi::v::RemotePtr input_pointer(input); + sapi::v::Int error; + SAPI_ASSIGN_OR_RETURN(char* output, api.c_jsonnet_fmt_snippet( + &vm_pointer, in_file_var.PtrBefore(), + &input_pointer, error.PtrAfter())); + + CHECK(!error.GetValue()) << "Jsonnet code evaluation failed: " + << error.GetValue() << "\n"; + + // Write data to file. + std::string out_file_in_sandboxee(JoinPath("/output", Basename(out_file))); + sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str()); + sapi::v::RemotePtr output_pointer(output); + + SAPI_ASSIGN_OR_RETURN( + bool success, + api.c_write_output_file(&output_pointer, out_file_var.PtrBefore())); + CHECK(success) << "Writing to output file failed: " << success; + + // Clean up. + SAPI_ASSIGN_OR_RETURN(char* result, + api.c_jsonnet_realloc(&vm_pointer, &output_pointer, 0)); + SAPI_RETURN_IF_ERROR(api.c_jsonnet_destroy(&vm_pointer)); + SAPI_RETURN_IF_ERROR(api.c_free_input(&input_pointer)); + + return absl::OkStatus(); +} + +int main(int argc, char* argv[]) { + using sandbox2::file_util::fileops::Basename; + + google::InitGoogleLogging(argv[0]); + gflags::ParseCommandLineFlags(&argc, &argv, true); + + if (!(argc == 3)) { + std::cerr << "Usage:\n" + << Basename(argv[0]) + << " /absolute/path/to/INPUT.jsonnet /absolute/path/to/OUTPUT\n"; + return EXIT_FAILURE; + } + + std::string in_file(argv[1]); + std::string out_file(argv[2]); + + absl::Status status = JsonnetMain(in_file, out_file); + if (!status.ok()) { + LOG(ERROR) << "Failed: " << status.ToString(); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_multiple_files_example.cc b/oss-internship-2020/jsonnet/examples/jsonnet_multiple_files_example.cc new file mode 100644 index 0000000..c26f9b3 --- /dev/null +++ b/oss-internship-2020/jsonnet/examples/jsonnet_multiple_files_example.cc @@ -0,0 +1,132 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include +#include + +#include "jsonnet_sapi.sapi.h" // NOLINT(build/include) +#include "sandboxed_api/util/flag.h" +#include "sandboxed_api/sandbox2/util/fileops.h" +#include "sandboxed_api/sandbox2/util/path.h" + +class JsonnetSapiSandbox : public JsonnetSandbox { + public: + explicit JsonnetSapiSandbox(std::string in_file, std::string out_directory) + : in_file_(std::move(in_file)), + out_directory_(std::move(out_directory)) {} + + // We need a slightly different policy than the default one + std::unique_ptr ModifyPolicy( + sandbox2::PolicyBuilder*) override { + return sandbox2::PolicyBuilder() + .AllowStaticStartup() + .AllowOpen() + .AllowRead() + .AllowWrite() + .AllowStat() + .AllowSystemMalloc() + .AllowExit() + .AllowSyscalls({ + __NR_futex, + __NR_close, + }) + .AddDirectoryAt(sandbox2::file::CleanPath(&out_directory_[0]), + "/output", + /*is_ro=*/false) + .AddDirectoryAt(dirname(&in_file_[0]), "/input", true) + .BuildOrDie(); + } + + private: + std::string in_file_; + std::string out_directory_; +}; + +absl::Status JsonnetMain(std::string in_file, std::string out_file) { + using sandbox2::file::JoinPath; + using sandbox2::file_util::fileops::Basename; + + // Initialize sandbox. + JsonnetSapiSandbox sandbox(in_file, out_file); + SAPI_RETURN_IF_ERROR(sandbox.Init()) + + JsonnetApi api(&sandbox); + + // Initialize library's main structure. + SAPI_ASSIGN_OR_RETURN(JsonnetVm * jsonnet_vm, api.c_jsonnet_make()); + sapi::v::RemotePtr vm_pointer(jsonnet_vm); + + // Read input file. + std::string in_file_in_sandboxee(JoinPath("/input", Basename(in_file))); + sapi::v::ConstCStr in_file_var(in_file_in_sandboxee.c_str()); + SAPI_ASSIGN_OR_RETURN(char* input, + api.c_read_input(false, in_file_var.PtrBefore())); + + // Process jsonnet data. + sapi::v::RemotePtr input_pointer(input); + sapi::v::Int error; + SAPI_ASSIGN_OR_RETURN(char* output, api.c_jsonnet_evaluate_snippet_multi( + &vm_pointer, in_file_var.PtrBefore(), + &input_pointer, error.PtrAfter())); + CHECK(!error.GetValue()) << "Jsonnet code evaluation failed: " + << error.GetValue() << "\n" + << "Make sure all files used by your jsonnet file " + "are in the same directory as your file."; + + // Write data to file. + std::string out_file_in_sandboxee(JoinPath("/output", Basename(out_file))); + sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str()); + sapi::v::RemotePtr output_pointer(output); + + SAPI_ASSIGN_OR_RETURN( + bool success, + api.c_write_output_file(&output_pointer, out_file_var.PtrBefore())); + CHECK(success) << "Writing to output file failed: " << success; + + // Clean up. + SAPI_ASSIGN_OR_RETURN(char* result, + api.c_jsonnet_realloc(&vm_pointer, &output_pointer, 0)); + SAPI_RETURN_IF_ERROR(api.c_jsonnet_destroy(&vm_pointer)); + SAPI_RETURN_IF_ERROR(api.c_free_input(&input_pointer)); + + return absl::OkStatus(); +} + +int main(int argc, char* argv[]) { + using sandbox2::file_util::fileops::Basename; + + google::InitGoogleLogging(argv[0]); + gflags::ParseCommandLineFlags(&argc, &argv, true); + + if (!(argc == 3)) { + std::cerr << "Usage:\n" + << Basename(argv[0]) + << " /absolute/path/to/INPUT.jsonnet /absolute/path/to/OUTPUT\n"; + return EXIT_FAILURE; + } + + std::string in_file(argv[1]); + std::string out_file(argv[2]); + + absl::Status status = JsonnetMain(in_file, out_file); + if (!status.ok()) { + LOG(ERROR) << "Failed: " << status.ToString(); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/oss-internship-2020/jsonnet/examples/jsonnet_yaml_stream_example.cc b/oss-internship-2020/jsonnet/examples/jsonnet_yaml_stream_example.cc new file mode 100644 index 0000000..7a92809 --- /dev/null +++ b/oss-internship-2020/jsonnet/examples/jsonnet_yaml_stream_example.cc @@ -0,0 +1,95 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "jsonnet_base_sandbox.h" // NOLINT(build/include) +#include "sandboxed_api/sandbox2/util/fileops.h" +#include "sandboxed_api/sandbox2/util/path.h" + +absl::Status JsonnetMain(std::string in_file, std::string out_file) { + using sandbox2::file::JoinPath; + using sandbox2::file_util::fileops::Basename; + + // Initialize sandbox. + JsonnetBaseSandbox sandbox(in_file, out_file); + SAPI_RETURN_IF_ERROR(sandbox.Init()) + + JsonnetApi api(&sandbox); + + // Initialize library's main structure. + SAPI_ASSIGN_OR_RETURN(JsonnetVm * jsonnet_vm, api.c_jsonnet_make()); + sapi::v::RemotePtr vm_pointer(jsonnet_vm); + + // Read input file. + std::string in_file_in_sandboxee(JoinPath("/input", Basename(in_file))); + sapi::v::ConstCStr in_file_var(in_file_in_sandboxee.c_str()); + SAPI_ASSIGN_OR_RETURN(char* input, + api.c_read_input(false, in_file_var.PtrBefore())); + + // Process jsonnet data. + sapi::v::RemotePtr input_pointer(input); + sapi::v::Int error; + SAPI_ASSIGN_OR_RETURN(char* output, api.c_jsonnet_evaluate_snippet_stream( + &vm_pointer, in_file_var.PtrBefore(), + &input_pointer, error.PtrAfter())); + CHECK(!error.GetValue()) + << "Jsonnet code evaluation failed: " << error.GetValue() << "\n" + << "Make sure all files used by your jsonnet file are in the same " + "directory as your file."; + + // Write data to file. + std::string out_file_in_sandboxee(JoinPath("/output", Basename(out_file))); + sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str()); + sapi::v::RemotePtr output_pointer(output); + + SAPI_ASSIGN_OR_RETURN( + bool success, + api.c_write_output_file(&output_pointer, out_file_var.PtrBefore())); + CHECK(success) << "Writing to output file failed: " << success; + + // Clean up. + SAPI_ASSIGN_OR_RETURN(char* result, + api.c_jsonnet_realloc(&vm_pointer, &output_pointer, 0)); + SAPI_RETURN_IF_ERROR(api.c_jsonnet_destroy(&vm_pointer)); + SAPI_RETURN_IF_ERROR(api.c_free_input(&input_pointer)); + + return absl::OkStatus(); +} + +int main(int argc, char* argv[]) { + using sandbox2::file_util::fileops::Basename; + + google::InitGoogleLogging(argv[0]); + gflags::ParseCommandLineFlags(&argc, &argv, true); + + if (!(argc == 3)) { + std::cerr << "Usage:\n" + << Basename(argv[0]) + << " /absolute/path/to/INPUT.jsonnet /absolute/path/to/OUTPUT\n"; + return EXIT_FAILURE; + } + + std::string in_file(argv[1]); + std::string out_file(argv[2]); + + absl::Status status = JsonnetMain(in_file, out_file); + if (!status.ok()) { + LOG(ERROR) << "Failed: " << status.ToString(); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/oss-internship-2020/jsonnet/headers/jsonnet_base_sandbox.h b/oss-internship-2020/jsonnet/headers/jsonnet_base_sandbox.h new file mode 100644 index 0000000..4d2eb1e --- /dev/null +++ b/oss-internship-2020/jsonnet/headers/jsonnet_base_sandbox.h @@ -0,0 +1,55 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef JSONNET_BASE_SANDBOX_H_ +#define JSONNET_BASE_SANDBOX_H_ + +#include +#include + +#include "jsonnet_sapi.sapi.h" // NOLINT(build/include) +#include "sandboxed_api/util/flag.h" +#include "sandboxed_api/transaction.h" +#include "sandboxed_api/vars.h" + +class JsonnetBaseSandbox : public JsonnetSandbox { + public: + explicit JsonnetBaseSandbox(std::string in_file, std::string out_file) + : in_file_(in_file), out_file_(out_file) {} + + std::unique_ptr ModifyPolicy( + sandbox2::PolicyBuilder *) override { + return sandbox2::PolicyBuilder() + .AllowStaticStartup() + .AllowOpen() + .AllowRead() + .AllowWrite() + .AllowStat() + .AllowSystemMalloc() + .AllowExit() + .AllowSyscalls({ + __NR_futex, + __NR_close, + }) + .AddDirectoryAt(dirname(&out_file_[0]), "/output", /*is_ro=*/false) + .AddDirectoryAt(dirname(&in_file_[0]), "/input", true) + .BuildOrDie(); + } + + private: + std::string in_file_; + std::string out_file_; +}; + +#endif // JSONNET_BASE_SANDBOX_H_ diff --git a/oss-internship-2020/jsonnet/headers/jsonnet_base_transaction.h b/oss-internship-2020/jsonnet/headers/jsonnet_base_transaction.h new file mode 100644 index 0000000..7208ae3 --- /dev/null +++ b/oss-internship-2020/jsonnet/headers/jsonnet_base_transaction.h @@ -0,0 +1,38 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef JSNONNET_BASE_TRANSACTION_H_ +#define JSNONNET_BASE_TRANSACTION_H_ + +#include "jsonnet_base_sandbox.h" // NOLINT(build/include) + +class JsonnetTransaction : public sapi::Transaction { + public: + JsonnetTransaction(std::string in_file, std::string out_file) + : sapi::Transaction( + std::make_unique(in_file, out_file)), + in_file_(in_file), + out_file_(out_file) { + sapi::Transaction::set_retry_count(0); // Try once, no retries + sapi::Transaction::SetTimeLimit(0); // Infinite time limit + } + + private: + std::string in_file_; + std::string out_file_; + + absl::Status Main() override; +}; + +#endif // JSNONNET_BASE_TRANSACTION_H_ diff --git a/oss-internship-2020/jsonnet/headers/jsonnet_tests.h b/oss-internship-2020/jsonnet/headers/jsonnet_tests.h new file mode 100644 index 0000000..cd58b65 --- /dev/null +++ b/oss-internship-2020/jsonnet/headers/jsonnet_tests.h @@ -0,0 +1,57 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef JSONNET_TESTS_H_ +#define JSONNET_TESTS_H_ + +#include + +#include +#include +#include +#include +#include +#include + +#include "jsonnet_base_sandbox.h" // NOLINT(build/include) +#include "jsonnet_sapi.sapi.h" // NOLINT(build/include) +#include "gtest/gtest.h" +#include "sandboxed_api/util/flag.h" +#include "sandboxed_api/sandbox2/util/path.h" +#include "sandboxed_api/util/status_matchers.h" + +class JsonnetTestHelper { + protected: + enum Evaluation { kBase, kMultipleFiles, kYamlStream }; + + void TestSetUp(); + void TestTearDown(); + + void ReadInput(const char* filename); + void EvaluateJsonnetCode(Evaluation type, bool expected_correct); + void WriteOutput(const char* filename_or_directory, Evaluation type); + std::string ReadOutput(const char* filename); + + std::unique_ptr sandbox_; + std::unique_ptr api_; + std::unique_ptr input_; + std::unique_ptr output_; + std::unique_ptr vm_; + + std::string input_filename_in_sandboxee_; + bool jsonnet_vm_was_used_; + bool input_was_read_; +}; + +#endif // JSONNET_TESTS_H_ diff --git a/oss-internship-2020/jsonnet/jsonnet b/oss-internship-2020/jsonnet/jsonnet new file mode 160000 index 0000000..3e25595 --- /dev/null +++ b/oss-internship-2020/jsonnet/jsonnet @@ -0,0 +1 @@ +Subproject commit 3e25595d5c4acd32a1c3951a57471986b90d3bad diff --git a/oss-internship-2020/jsonnet/jsonnet.patch b/oss-internship-2020/jsonnet/jsonnet.patch new file mode 100644 index 0000000..57f1812 --- /dev/null +++ b/oss-internship-2020/jsonnet/jsonnet.patch @@ -0,0 +1,664 @@ +--- jsonnet.cpp 2020-09-09 12:15:33.687539042 +0000 ++++ write_helper.cpp 2020-09-25 15:38:37.317147682 +0000 +@@ -14,559 +14,125 @@ + limitations under the License. + */ + +-#include +-#include +-#include ++// We need two functions defined in jsonnet.cpp file, used for writing output ++// (multiple files and yaml streams) -- with minor changes (e.x. return type). + +-#include + #include + #include +-#include + #include +-#include +-#include + #include + +-#include "utils.h" ++#include "jsonnet_helper.h" // NOLINT(build/include) + +-extern "C" { +-#include +-} +- +-#ifdef _WIN32 +-const char PATH_SEP = ';'; +-#else +-const char PATH_SEP = ':'; +-#endif +- +-void version(std::ostream &o) +-{ +- o << "Jsonnet commandline interpreter " << jsonnet_version() << std::endl; +-} +- +-void usage(std::ostream &o) +-{ +- version(o); +- o << "\n"; +- o << "jsonnet {