0

I am using the following code to search through a folder structure and write all package names and versions into a string.

#!/usr/bin/python3

import glob
import json

repo_list = ["core", "multilib", "nonfree", "testing"] 

for i in repo_list:
    print(i)
    for name in glob.glob('../' + i +'/*/spkgbuild'): 
        package_name = ''
        package_version = ''
        temp_json = ''
        package = open(name,'r')
        Lines = package.readlines()
        for line in Lines:
            if(line.strip().startswith('name')):
                package_name = line.strip()[5:]
            if(line.strip().startswith('version')):
                package_version = line.strip()[8:]
            if(package_name != '' and package_version != ''):
                temp_json = {package_name: {"version": package_version}}

From this I actually want to have the following JSON object later:

{
  "core": {
    "package_name": {
      "version": "1.1.1"
    },
    "package_name2": {
      "version": "1.1.1"
    }
  }
}

I'm stuck writing the object now, can you help me there?

2
  • 1
    you know that what you described is not json array but json object as it has keys and curly brackets Commented Jan 31, 2021 at 16:39
  • 1
    change temp_json = {package_name: {"version": package_version}} to temp_json={}; temp_json[package_name] = {"version": package_version} Commented Jan 31, 2021 at 16:40

1 Answer 1

1
import glob
import json

repo_list = ["core", "multilib", "nonfree", "testing"] 

json_ = {}
for i in repo_list:
    print(i)
    for name in glob.glob('../' + i +'/*/spkgbuild'): 
        package_name = ''
        package_version = ''
        package = open(name,'r')
        Lines = package.readlines()
        for line in Lines:
            if(line.strip().startswith('name')):
                package_name = line.strip()[5:]
            if(line.strip().startswith('version')):
                package_version = line.strip()[8:]
            if(package_name != '' and package_version != ''):
                json_[package_name] = {"version": package_version}
                package_name = ""
                package_version = "" 
printl(json)

just insert all data to one dictionary and you have it all together, now i dont know if you have multiple versions and names per file so just in case I am also reseting values

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.