Fixed merge.

This commit is contained in:
antux18 2024-07-23 17:27:10 +02:00
parent 687b777912
commit 5ba63ad50e

54
ecg.py
View File

@ -10,13 +10,11 @@
import subprocess import subprocess
import json import json
# import yaml
import argparse import argparse
import tempfile import tempfile
import os import os
import requests import requests
import zipfile import zipfile
import io
import tarfile import tarfile
import pathlib import pathlib
import logging import logging
@ -59,7 +57,7 @@ def download_file(url, dest):
Returns Returns
------- -------
str str
The hash of the downloaded file. Hash of the downloaded file.
""" """
req = requests.get(url) req = requests.get(url)
file = open(dest, "wb") file = open(dest, "wb")
@ -68,7 +66,7 @@ def download_file(url, dest):
hash_process = subprocess.run(f"sha256sum {file.name} | cut -d ' ' -f 1 | tr -d '\n'", capture_output=True, shell=True) hash_process = subprocess.run(f"sha256sum {file.name} | cut -d ' ' -f 1 | tr -d '\n'", capture_output=True, shell=True)
return hash_process.stdout.decode("utf-8") return hash_process.stdout.decode("utf-8")
def download_sources(config, arthashlog_path, tmp_dir, use_cache): def download_sources(config, arthashlog_path, dl_dir, use_cache):
""" """
Downloads the source of the artifact in 'config'. Downloads the source of the artifact in 'config'.
@ -80,20 +78,20 @@ def download_sources(config, arthashlog_path, tmp_dir, use_cache):
arthashlog_path: str arthashlog_path: str
Path to the artifact hash log file. Path to the artifact hash log file.
cachedir_path: str dl_dir: str
Path to the cache directory. Path to the directory where to download the artifact.
use_cache: bool use_cache: bool
Indicates whether the artifact should be cached or not. Indicates whether the cache should be used or not.
Returns Returns
------- -------
temp_dir: tempfile.TemporaryDirectory temp_dir: str
The directory where the artifact is downloaded to. Path to the directory where the artifact is downloaded to.
""" """
url = config["artifact_url"] url = config["artifact_url"]
artifact_name = trim(url) artifact_name = trim(url)
artifact_dir = os.path.join(tmp_dir, artifact_name) artifact_dir = os.path.join(dl_dir, artifact_name)
# Checking if artifact in cache. Not downloading if it is: # Checking if artifact in cache. Not downloading if it is:
if not os.path.exists(artifact_dir) or not use_cache: if not os.path.exists(artifact_dir) or not use_cache:
logging.info(f"Downloading artifact from {url}") logging.info(f"Downloading artifact from {url}")
@ -128,7 +126,7 @@ def buildstatus_saver(output, buildstatus_path, config_path):
Parameters Parameters
---------- ----------
output: str output: str
The output of Docker. Output of Docker.
buildstatus_path: str buildstatus_path: str
Path to the build status file. Path to the build status file.
@ -161,7 +159,6 @@ def buildstatus_saver(output, buildstatus_path, config_path):
now = datetime.datetime.now() now = datetime.datetime.now()
timestamp = str(datetime.datetime.timestamp(now)) timestamp = str(datetime.datetime.timestamp(now))
buildstatus_file.write(f"{artifact_name},{timestamp},{error_cat}\n") buildstatus_file.write(f"{artifact_name},{timestamp},{error_cat}\n")
print(unknown_error)
if unknown_error: if unknown_error:
now = datetime.datetime.now() now = datetime.datetime.now()
timestamp = str(datetime.datetime.timestamp(now)) timestamp = str(datetime.datetime.timestamp(now))
@ -177,8 +174,8 @@ def build_image(config, src_dir):
config: dict config: dict
Parsed config file. Parsed config file.
src_dir: tempfile.TemporaryDirectory src_dir: str
The directory where the artifact is stored. Path to the directory where the artifact is stored.
Returns Returns
------- -------
@ -209,8 +206,8 @@ def check_env(config, src_dir, pkglist_path):
config: dict config: dict
Parsed config file. Parsed config file.
src_dir: tempfile.TemporaryDirectory src_dir: str
The directory where the artifact is stored. Path to the directory where the artifact is stored.
pkglist_path: str pkglist_path: str
Path to the package list file. Path to the package list file.
@ -290,9 +287,8 @@ def main():
pkglist_path = "" # Package list being generated pkglist_path = "" # Package list being generated
buildstatus_path = "" # Summary of the build process of the image buildstatus_path = "" # Summary of the build process of the image
arthashlog_path = "" # Log of the hash of the downloaded artifact arthashlog_path = "" # Log of the hash of the downloaded artifact
cachedir_path = "cache" # Artifact cache directory cache_dir = "" # Artifact cache directory, when using one. 'None' value indicates no cache.
use_cache = False
use_cache = False # Indicates whether cache should be enabled or not
# Command line arguments parsing: # Command line arguments parsing:
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
@ -342,9 +338,7 @@ def main():
log_path = args.log_path log_path = args.log_path
buildstatus_path = args.build_summary buildstatus_path = args.build_summary
arthashlog_path = args.artifact_hash arthashlog_path = args.artifact_hash
if args.cache_dir != None: cache_dir = args.cache_dir
use_cache = True
cachedir_path = args.cache_dir
# Setting up the log: will be displayed both on stdout and to the specified # Setting up the log: will be displayed both on stdout and to the specified
# file: # file:
@ -363,19 +357,25 @@ def main():
# print(config) # print(config)
config_file.close() config_file.close()
tmp_dir = tempfile.TemporaryDirectory() dl_dir = None
artifact_dir = download_sources(config, arthashlog_path, tmp_dir.name, use_cache) # If not using cache, creates a temporary directory:
if cache_dir == None:
tmp_dir = tempfile.TemporaryDirectory()
dl_dir = tmp_dir.name
else:
use_cache = True
dl_dir = cache_dir
artifact_dir = download_sources(config, arthashlog_path, dl_dir, use_cache)
return_code, build_output = build_image(config, artifact_dir) return_code, build_output = build_image(config, artifact_dir)
if return_code == 0: if return_code == 0:
check_env(config, artifact_dir, pkglist_path) check_env(config, artifact_dir, pkglist_path)
remove_image(config) remove_image(config)
# Creates file if not already:
pathlib.Path(buildstatus_path).touch() pathlib.Path(buildstatus_path).touch()
else: else:
# Creates file if not already:
pathlib.Path(pkglist_path).touch() pathlib.Path(pkglist_path).touch()
buildstatus_saver(build_output, buildstatus_path, config_path) buildstatus_saver(build_output, buildstatus_path, config_path)
if not use_cache:
os.system(f"rm -rf {os.path.join(cachedir_path, trim(config['artifact_url']))}")
if __name__ == "__main__": if __name__ == "__main__":
main() main()