На моей машине с Linux я пытаюсь проанализировать файл «Results.json».Две строки, которые я хотел найти и получить из них:
- "first_review": true
- "firstReview": true
Если любой изони (или оба) имеют значение true, тогда я хочу напечатать результаты как «fail», и только если оба значения «false», тогда я хочу напечатать результаты как Pass.
Как я могу это сделатьправильно и добивайтесь этого, пожалуйста, предложите.
Вот файл Json (results.json)
{
"scan-dir": "/var/local",
"scan_time_ms": "20394 ms",
"by": "a user",
"project": "local",
"date": "2019-06-30T10:48:07.270Z",
"g_version": "2.2.16",
"q_version": "1.0.112",
"directories_scanned": 25,
"files_scanned": 41,
"packages_found": 3,
"packages": [
{
"name": "flow",
"version": "unknown",
"path": "development_environment/flow",
"source": "__init__.py",
"file_path": "development_environment/flow/__init__.py",
"analyzer": "package-finder",
"license_files": [
{
"file": "development_environment/flow/__init__.py",
"legal": false,
"legal_category": "Other",
"contains_keywords": true,
"blocks": [
{
"text": "# -*- coding: utf-8 -*-\n#\n# ",
"matches": null
},
{
"text": "Licensed",
"matches": "KEYWORD"
},
{
"text": " to the ",
"matches": null
},
{
"text": "Apache Software Foundation (ASF) under one",
"matches": "ACCEPTABLE"
},
{
"text": "\n# or more contributor ",
"matches": null
},
{
"text": "license",
"matches": "KEYWORD"
},
{
"text": " agreements. See the NOTICE file\n# distributed with this work for additional information\n# regarding copyright ownership. The ASF ",
"matches": null
},
{
"text": "licenses",
"matches": "KEYWORD"
},
{
"text": " this file\n# to you under",
"matches": null
},
{
"text": " the Apache License, Version 2.0 (the\n# \"License\"); you may not use this file except in compliance\n# with the License. You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing,\n# software distributed under the License is distributed on an\n# \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n# KIND, either express or implied. See the License for the\n# specific language governing permissions and limitations\n# under the License.",
"matches": "Apache-2.0"
}
],
"license_types": [
"Apache-2.0"
],
"license_approval_flags": {
"approved": true,
"first_review": false,
"needs_legal_approval": false,
"prohibited": false,
"no_license_info": false
},
"affects_package_license_types": true
},
{
"file": "development_environment/flow/base_flow.py",
"legal": false,
"legal_category": "Other",
"contains_keywords": true,
"blocks": [
{
"text": "# -*- coding: utf-8 -*-\n#\n# ",
"matches": null
},
{
"text": "Licensed",
"matches": "KEYWORD"
},
{
"text": " to the ",
"matches": null
},
{
"text": "Apache Software Foundation (ASF) under one",
"matches": "ACCEPTABLE"
},
{
"text": "\n# or more contributor ",
"matches": null
},
{
"text": "license",
"matches": "KEYWORD"
},
{
"text": " agreements. See the NOTICE file\n# distributed with this work for additional information\n# regarding copyright ownership. The ASF ",
"matches": null
},
{
"text": "licenses",
"matches": "KEYWORD"
},
{
"text": " this file\n# to you under",
"matches": null
},
{
"text": " the Apache License, Version 2.0 (the\n# \"License\"); you may not use this file except in compliance\n# with the License. You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing,\n# software distributed under the License is distributed on an\n# \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n# KIND, either express or implied. See the License for the\n# specific language governing permissions and limitations\n# under the License.",
"matches": "Apache-2.0"
},
{
"text": "\n\n\"\"\"Base classes for flow and flowBag.\"\"\"\n\nfrom abc import ABCMeta, abstractmethod\n\n\nclass Baseflow(metaclass=ABCMeta):\n \"\"\"\n Base flow object that both the Simpleflow and flow inherit.\n \"\"\"\n\n @property\n @abstractmethod\n def flow_id(self):\n \"\"\"\n :return: the flow ID\n :rtype: unicode\n \"\"\"\n raise NotImplementedError()\n\n @property\n @abstractmethod\n def task_ids(self):\n \"\"\"\n :return: A list of task IDs that are in this flow\n :rtype: List[unicode]\n \"\"\"\n raise NotImplementedError()\n\n @property\n @abstractmethod\n def full_filepath(self):\n \"\"\"\n :return: The absolute path to the file that contains this flow's definition\n :rtype: unicode\n \"\"\"\n raise NotImplementedError()\n\n @property\n @abstractmethod\n def concurrency(self):\n \"\"\"\n :return: maximum number of tasks that can run simultaneously from this flow\n :rtype: int\n \"\"\"\n raise NotImplementedError()\n\n @abstractmethod\n def is_paused(self):\n \"\"\"\n :return: whether this flow is paused or not\n :rtype: bool\n \"\"\"\n raise NotImplementedError()\n\n @abstractmethod\n def pickle_id(self):\n \"\"\"\n :return: The pickle ID for this flow, if it has one. Otherwise None.\n :rtype: unicode\n \"\"\"\n raise NotImplementedError\n\n\nclass BaseflowBag:\n \"\"\"\n Base object that both the SimpleflowBag and flowBag inherit.\n \"\"\"\n @property\n @abstractmethod\n def flow_ids(self):\n \"\"\"\n :return: a list of flow IDs in this bag\n :rtype: List[unicode]\n \"\"\"\n raise NotImplementedError()\n\n @abstractmethod\n def get_flow(self, flow_id):\n \"\"\"\n :return: whether the task exists in this bag\n :rtype: tzlflow.flow.base_flow.Baseflow\n \"\"\"\n raise NotImplementedError()",
"matches": null
}
],
"license_types": [
"Apache-2.0"
],
"license_approval_flags": {
"approved": true,
"first_review": false,
"needs_legal_approval": false,
"prohibited": false,
"no_license_info": false
},
"affects_package_license_types": true
}
],
"license_types": [
"Apache-2.0"
],
"license_approval_flags": {
"approved": true,
"first_review": false,
"needs_legal_approval": false,
"prohibited": false,
"no_license_info": false,
"reference_only": false
},
"license_approval_status": "Approved",
"package_hash": "c7d6757c6c814a22b44d8299cace3ec1",
"Treat": {
"TreatReviewed": false,
"possiblyRelatedPackages": [],
"searchedName": "flow",
"searchedVersion": "unknown",
"allLicensesMatch": false,
"firstReview": true,
"firstReviewReason": "New package and not exempt"
}
},
{
"name": "testscan-results",
"version": "unknown",
"language": "unknown",
"analyzer": "License verify",
"path": "testscan-results",
"file_path": "testscan-results/License-Report.html",
"license_files": [
{
"file": "testscan-results/License-Report.html",
"hash": "db02a6be21775d25af4cfdb993442a8e",
"legal": true,
"legal_category": "License",
"contains_keywords": true,
"blocks": [
{
"text": "<style>h2 strong{color: red;} pre{padding-left:1em; background-color:cornsilk; border:1px solid black; margin: 0 2em}</style><h1 id=\"",
"matches": null
},
{
"text": "nolicenseswerefoundwhichrequirereview",
"matches": "KEYWORD"
},
{
"text": "\">No ",
"matches": null
},
{
"text": "licenses",
"matches": "KEYWORD"
},
{
"text": " were found which require review.</h1>",
"matches": null
}
],
"license_types": [
"KEYWORD",
"UNKNOWN"
],
"license_approval_flags": {
"approved": false,
"first_review": true,
"needs_legal_approval": false,
"prohibited": false,
"no_license_info": false
},
"affects_package_license_types": true
},
{
"file": "test_scan-results/License-Report.md",
"hash": "b85fd1e259874c81c31253e352068b6f7",
"legal": true,
"legal_category": "License",
"contains_keywords": true,
"blocks": [
{
"text": "# No ",
"matches": null
},
{
"text": "licenses",
"matches": "KEYWORD"
},
{
"text": " were found which require review.",
"matches": null
}
],
"license_types": [
"KEYWORD",
"UNKNOWN"
],
"license_approval_flags": {
"approved": false,
"first_review": true,
"needs_legal_approval": false,
"prohibited": false,
"no_license_info": false
},
"affects_package_license_types": true
}
],
"license_types": [
"KEYWORD",
"UNKNOWN"
],
"license_approval_flags": {
"approved": false,
"first_review": true,
"needs_legal_approval": false,
"prohibited": false,
"no_license_info": false,
"reference_only": false
},
"license_approval_status": "Needs Review",
"package_hash": "11d905fbde2bbd890c5d3e677704185a"
},
{
"name": "unknown",
"version": "unknown",
"language": "unknown",
"analyzer": "License verify",
"path": "development_environment/README.md",
"file_path": "development_environment/README.md",
"license_files": [
{
"file": "development_environment/README.md",
"legal": false,
"legal_category": "Other",
"contains_keywords": false,
"blocks": [
{
"text": "# tzlflow EnV\n\nA development environment for ",
"matches": null
},
{
"text": "Flow deploy upon tzl Cloud utilising shared Database i",
"matches": "ACCEPTABLE"
},
{
"text": ".e.",
"matches": null
},
{
"text": " PostGresql ",
"matches": "PostgreSQL"
},
{
"text": "and Messenging Services i.e. Redis. Intention is that developers can get up and running quickly with working tzlflow environment (precanned to G+EHR software stack).\n\n## Development and Testing\nSetting up a development environment utilising developer free tzl Cloud. \n\n### Pre-requistes\n- Sign up for [tzl Cloud Account](",
"matches": null
},
{
"text": ")\n-- tzl Cloud CLI\n-- kubectl \n- Access to [repo](",
"matches": null
},
{
"text": ")\n- Docker\n- Helm\n\nFollowing two are not pre-req but you may decide to 'bring your own'\n- Postgres DB\n- Redis \nIf using your own following comment instructions in values.yaml to configure. \n\n### Clone repo\n\n- git clone git@",
"matches": null
},
{
"text": ":TzlCo/Tzl-tools\n- cd Tzl-tools\n\n### Update configuration\n\nUnder",
"matches": null
},
{
"text": " postgresql ",
"matches": "PostgreSQL"
},
{
"text": " PostgreSQL ",
"matches": "PostgreSQL"
},
{
"text": "port\n service:\n port: 30406\n\n ##",
"matches": null
},
{
"text": " PostgreSQL ",
"matches": "PostgreSQL"
},
{
"text": "User to create.\n postgresUser: tzl_cloud_xxx\n ##\n ##",
"matches": null
},
{
"text": " PostgreSQL ",
"matches": "PostgreSQL"
},
{
"text": "Password for the new user.\n ## If not set, a random 10 characters password will be used.\n postgresPassword: xxxx\n ##\n ##",
"matches": null
},
{
"text": " PostgreSQL ",
"matches": "PostgreSQL"
},
{
"text": "Database to create.\n postgresDatabase: tzlclouddb\n```\n\nand Redis\n\n```redis:\n ##\n ## Use the redis chart dependency.\n ## Set to false if bringing your own redis.\n enabled: false\n ##\n ## If you are bringing your own redis, you can set the host in redisHost.\n redisHost: xxxx.databases.appdomain.cloud\n ##\n ## Redis password\n ##\n password: xxxxx\n username: admin\n ##\n ## Master configuration\n master:\n #Redis Port - missing from chart.\n port: 31932\n \n```\n\n### flow Git Repo configuration\n\nFollow instructions to generate ssh key for git repo.\n",
"matches": null
},
{
"text": "#deploy-key\nIf you are using a private Git repo, you can set `flows.gitSecret` to the name of a secret you created containing private keys and a `known_hosts` file.\n\nFor example, this will create a secret named `my-git-secret` from your ed25519 key and known_hosts file stored in your home directory: `kubectl create secret generic flow-git-secret --from-file=gitSshKey=g+ehr_flows --from-file=known_hosts=known_hosts --from-file=gitSshKey.pub=g+ehr_flows.pub`\n\n\n\n### Deploy\n\nLog onto tzl Cloud using CLI\n```tzlcloud login -a ",
"matches": null
},
{
"text": " -r us-south -g Tzl-Dev --sso```\n\nDownload the kubeconfig files for your cluster.\n```tzlcloud ks cluster-config --cluster Tzl_dev```\n\nUsing the output from the previous step, set the KUBECONFIG environment variable. The command looks similar to the following example:\n```export KUBECONFIG=/Users/$USER/.tzl/plugins/container-service/clusters/Tzl_dev/kube-config-dal10-Tzl_dev.yml```\n\nUsing Helm deploy tzlflow\n```helm install --namespace \"default\" --name \"tzlflow\" stable/tzlflow -f values.yaml```\n\nTreat access to tzlflow admin\n```export POD_NAME=$(kubectl get pods --namespace default -l \"component=web,app=tzlflow\" -o jsonpath=\"{.items[0].metadata.name}\")\n echo http://127.0.0.1:8080\n kubectl port-forward --namespace default $POD_NAME 8080:8080```\n \n\n\n# TOD0 \n- python script to automate deploy. \n- Steps to integrate with different dependency e.g. postgres or redis.\n- flow script locations.\n- instructions to use github repo as flow location.\n",
"matches": null
}
],
"license_types": [
"PostgreSQL"
],
"license_approval_flags": {
"approved": true,
"first_review": false,
"needs_legal_approval": false,
"prohibited": false,
"no_license_info": false
},
"affects_package_license_types": true
}
],
"license_types": [
"PostgreSQL"
],
"license_approval_flags": {
"approved": true,
"first_review": false,
"needs_legal_approval": false,
"prohibited": false,
"no_license_info": false,
"reference_only": false
},
"license_approval_status": "Approved",
"package_hash": "2776a8d434cd903a7cd441c9dcbdd4ed"
}
],
"Treat_checked": true
}
Вот вещи, которые я пытался, но не мог продвинуться дальше, на каждом изподходы:
Сначала попытался использовать jq и мог бы проанализировать json для путей и получить значения, т.е.'true' или 'false' для требуемых узлов, но не знаю, как перейти к следующему шагу, используя эти значения true и false.
использовал что-то вроде:
cat results.json |jq '.packages[0].license_approval_flags.first_review'
В другой мысли хотел Cat, а затем grep содержимое файла json, для выше двух необходимых полных строковых значений ("first_review": true и "firstReview": true), так что если получить какие-либо результаты как«true», тогда я хотел установить / напечатать «Results» как «Fail», но не смог получить никаких правильных ответов, так как думаю, что кавычки на строки не фильтруются в моем поиске grep.
использовал команду grep, как показано ниже:
grep -o '"[firstReview = true"]\+"' results.json
Затем хотел использовать Python для анализа json, а затем выполнить желаемую работу, но могне продвигаться дальше этой страницы «как анализировать данные из json в python», которая, кажется, подробно описывает что-то, что я мог бы использовать, но у меня еще не было необходимых основ python.
Это, вероятно,не сложно, когда я читаю по интернету, но я пока не могу это сделать.
Как мне правильно это сделать и добиться этого, пожалуйста, предложите.