Skip to content

Instantly share code, notes, and snippets.

@Fusion86
Last active December 13, 2017 09:15
Show Gist options
  • Save Fusion86/dadb6d2a440541e04bf35ecf4f0f4df1 to your computer and use it in GitHub Desktop.
Save Fusion86/dadb6d2a440541e04bf35ecf4f0f4df1 to your computer and use it in GitHub Desktop.
Generate database.json containing Cydia package info for use in a javascript frontend
#!/usr/bin/env python3
"""
Generate database.json
"""
__author__ = "Fusion86"
__version__ = "0.1.1"
__license__ = "MIT"
from os import listdir
from os.path import join
import re
import subprocess
import json
import time
CONTROL_REQUIRED_FIELDS = ["name", "version", "package", "architecture", "author", "section"]
OUTPUT_FILE_NAME = "database.json"
PACKAGE_DIRECTORY = "deb"
def parse_deb_control(path):
"""
Parse Debian Package Control file.
Returns None if a required_field is missing.
"""
control = {"path": path}
result = subprocess.run(["dpkg-deb", "-I", path, "Control"], stdout=subprocess.PIPE, universal_newlines=True)
for line in result.stdout.splitlines():
arr = re.split(r":?\s", line) # 0 = key, 1 = value
control[arr[0].lower()] = arr[1]
for required_field in CONTROL_REQUIRED_FIELDS:
if required_field not in control:
print(f"{required_field.title()} not found in {path}")
return None
return control
def main():
""" Main entry point of the app """
packages = []
for filename in listdir(PACKAGE_DIRECTORY):
filepath = (join(PACKAGE_DIRECTORY, filename))
control = parse_deb_control(filepath)
if control != None:
packages.append(control)
print(f"Added {control['name']} {control['version']} to the list ({control['package']})")
with open(OUTPUT_FILE_NAME, "w", encoding="utf8") as outfile:
data = {"version": int(time.time()), "packages": packages}
print(f"Saving {OUTPUT_FILE_NAME} (version: {data['version']})")
json.dump(data, outfile)
if __name__ == "__main__":
main()
@Fusion86
Copy link
Author

- www
    - deb
        - package1.deb
        - package2.deb
    - index.html
    - database.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment