Skip to content

Commit

Permalink
S/B Final
Browse files Browse the repository at this point in the history
  • Loading branch information
NorthernMan54 committed May 25, 2023
1 parent 036a465 commit 106d54b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 25 deletions.
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ jobs:
- name: Generate rpi-image-repo.json
id: generate_rpi-image-repo
run: |
sudo bash -c 'shasum -a 256 work/Homebridge/export-image/Raspbian-Homebridge.img > deploy/image_Raspbian-Homebridge.sha256'
sudo RPI_IMAGER_NAME="homebridge" RPI_IMAGER_DESCRIPTION="Official Homebridge Raspberry Pi Image" RPI_IMAGER_ICON="https://user-images.githubusercontent.com/3979615/116509191-3c35f880-a906-11eb-9a7f-7cad7c2aa641.png" ./make_rpi-imager-snipplet.py -u"https://github.com/homebridge/homebridge-raspbian-image/releases/download/${{ steps.get_version.outputs.VERSION }}/Raspbian-Homebridge-${{ steps.get_version.outputs.VERSION }}.zip"
- name: Create Release
id: create_release
Expand Down
92 changes: 68 additions & 24 deletions make_rpi-imager-snipplet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,52 @@
# Credits for the go to https://github.com/guysoft/CustomPiOS/blob/devel/src/make_rpi-imager-snipplet.py


def calculate_sha256(data):
sha256_hash = hashlib.sha256()
sha256_hash.update(data)
return sha256_hash.hexdigest()


def calculate_sha256_zip(zip_file_path):
# Calculate the SHA256 hash of the zip file
with open(zip_file_path, 'rb') as file:
sha256_hash = hashlib.sha256()
while True:
# Read the file in chunks
chunk = file.read(4096)
if not chunk:
break
sha256_hash.update(chunk)

zip_hash = sha256_hash.hexdigest()

# Open the zip file in binary mode
with zipfile.ZipFile(zip_file_path, 'r') as zip_obj:
# Get the list of file names in the zip
file_names = zip_obj.namelist()

if file_names:
# Get the first file name in the zip
first_file_name = file_names[0]

# Calculate the SHA256 hash of the first file's data within the zip
sha256_hash = hashlib.sha256()
with zip_obj.open(first_file_name) as first_file:
while True:
# Read the first file's data in chunks
chunk = first_file.read(4096)
if not chunk:
break
sha256_hash.update(chunk)

first_file_hash = sha256_hash.hexdigest()

return zip_hash, first_file_hash

# Return None if the zip file is empty
return zip_hash, None


def handle_arg(key, optional=False):
if optional and key not in os.environ.keys():
return
Expand All @@ -21,57 +67,55 @@ def handle_arg(key, optional=False):


if __name__ == "__main__":
parser = argparse.ArgumentParser(add_help=True, description='Create a json snipplet from an image to be used with the make_rpi-imager_list.py and eventually published in a repo')
parser.add_argument('workspace_suffix', nargs='?', default="default", type=str, help='Suffix of workspace folder')
parser.add_argument('-u', '--rpi_imager_url', type=str, default="MISSING_URL", help='url to the uploaded image url')

parser = argparse.ArgumentParser(
add_help=True, description='Create a json snipplet from an image to be used with the make_rpi-imager_list.py and eventually published in a repo')
parser.add_argument('workspace_suffix', nargs='?', default="default",
type=str, help='Suffix of workspace folder')
parser.add_argument('-u', '--rpi_imager_url', type=str,
default="MISSING_URL", help='url to the uploaded image url')

args = parser.parse_args()

workspace_path = os.path.join(os.getcwd(), "deploy")
if args.workspace_suffix != "" and args.workspace_suffix != "default":
workspace_path += "-" + args.workspace_suffix

name = handle_arg("RPI_IMAGER_NAME")
description = handle_arg("RPI_IMAGER_DESCRIPTION")
url = args.rpi_imager_url
icon = handle_arg("RPI_IMAGER_ICON")
website = handle_arg("RPI_IMAGER_WEBSITE", True)
release_date = date.today().strftime("%Y-%m-%d")
print("workspace_path " + workspace_path);
zip_local = glob.glob(os.path.join(workspace_path,"*.zip"))[0]

zip_local = glob.glob(os.path.join(workspace_path, "*.zip"))[0]

if url == "MISSING_URL":
url = os.path.basename(zip_local)


output_path = os.path.join(workspace_path, "rpi-image-repo.json")

json_out = {"name": name,
"description": description,
"url": url,
"icon": icon,
"release_date": release_date,
}

if website is not None:
json_out["website"] = website

img_sha256_path = glob.glob(os.path.join(workspace_path,"*.sha256"))[0]
json_out["extract_sha256"] = None
with open(img_sha256_path, 'r') as f:
json_out["extract_sha256"] = f.read().split()[0]

json_out["extract_size"] = None
with zipfile.ZipFile(zip_local) as zipfile:
json_out["extract_size"] = zipfile.filelist[0].file_size
with zipfile.ZipFile(zip_local) as zipSize:
json_out["extract_size"] = zipSize.filelist[0].file_size

json_out["image_download_size"] = os.stat(zip_local).st_size

json_out["extract_sha256"] = None
json_out["image_download_sha256"] = None
with open(zip_local,"rb") as f:
json_out["image_download_sha256"] = hashlib.sha256(f.read()).hexdigest()
json_out["image_download_sha256"], json_out["extract_sha256"] = calculate_sha256_zip(
zip_local)

with open(output_path, "w") as w:
json.dump(json_out, w, indent=2)

print("Done generating rpi-imager json snipplet")

0 comments on commit 106d54b

Please sign in to comment.