0

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)
Puneeth Kumar
  • 182
  • 2
  • 14
  • 1
    Are you "stuck" writing it yourself? If so, please edit your question to include what you have done so far – OneCricketeer Apr 23 '18 at 13:37
  • For starters - glob for the JSON files https://stackoverflow.com/questions/2225564/get-a-filtered-list-of-files-in-a-directory And secondly, replace grep with an actual JSON parser – OneCricketeer Apr 23 '18 at 13:40

1 Answers1

0

If you are simply trying to execute all the Unix commands from Python script that you have executed from shell script, does not make any sense. You can not even utilize the same python script in Windows as it simply executing Unix commands. While you plan to convert a shell script to Python script first generate a algorithm from what shell script is doing and implement that algorithm in Python.

Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17