I have a lengthy shell script, where I am trying to convert into a python script but I am struck in a line.
cat $projects_list | while read line
do
list=`find . -name "*.json" -exec grep -l "project_name.*\"$line\"" {} \; | grep -vw project`
Wat actually I need is
- Parse all the folders expect "project" from current folder and get the files with .json extension.
- Parse each json file and search for the value whose key is "project_name"
- Store the the json file path whose value is found in the json file
Project_list contains a list of values which is to be matched with the key 'project name'
And below is the json format
{
"artifactory_space_MiB": 1,
"enovia": {
"name": "",
"product": "",
"release": "",
"release_notes_url": "http://example.com/ld+Release"
},
"goldmine": {
"build_type_list": [
"product",
"prodspec"
]
},
"goldmine_monitor_flag": false,
"image_list": [
{
"image": "framework-image",
"machine": "qemux86-64",
"name": "qemu"
}
],
"item_status": "inactive",
"jira": {
"key": "cdtest",
"version": "1.0"
},
"product_name": "cdtest",
"product_release": "1.0",
"project_name": "cdtest-1.0",
"testlink": {
"proj_name": "__TESTLINK_PROJ_NAME__"
},
"warios_repo_list": [],
"zone_tracking": {
"inactivation": {
"deletion_time": "2018-02-16_1516",
"inactivation_time": "2018-02-16_1516"
},
"previous_values": {
"artifactory_space_MiB": 1
}
}
}
I have tried below code using python..
with open(projects_list, 'r') as f:
for line in f:
# Ignore "project" files... only interested in downstream product files.
json_list = subprocess.check_output(["find . -name '*.json' " , "-exec grep -l 'project_name.*\"" + line + "\"' {} \; " , "| grep -vw project"], shell=True)