Log to stderr when running in non-interactive mode

So we can use stdout for data, e.g. printing product metadata in JSON.
This commit is contained in:
Jakub Jirutka 2023-11-21 21:57:40 +01:00
parent dfc6e2feb8
commit 36e51f43f7
2 changed files with 31 additions and 32 deletions

View file

@ -222,7 +222,7 @@ class Utils:
# Header drawing method # Header drawing method
def head(self, text = None, width = 55): def head(self, text = None, width = 55):
if not self.interactive: if not self.interactive:
print(text) print(text, file=sys.stderr)
return return
if text == None: if text == None:
text = self.name text = self.name
@ -241,6 +241,12 @@ class Utils:
print("#"*width) print("#"*width)
print("") print("")
def info(self, text):
if self.interactive:
print(text)
else:
print(text, file=sys.stderr)
def resize(self, width, height): def resize(self, width, height):
print('\033[8;{};{}t'.format(height, width)) print('\033[8;{};{}t'.format(height, width))

View file

@ -106,7 +106,7 @@ class gibMacOS:
message += "Please ensure you have a working internet connection." message += "Please ensure you have a working internet connection."
raise ProgramError(message, title="Catalog Data Error") raise ProgramError(message, title="Catalog Data Error")
self.u.head("Parsing Data") self.u.head("Parsing Data")
print("Scanning products after catalog download...\n") self.u.info("Scanning products after catalog download...\n")
self.mac_prods = self.get_dict_for_prods(self.get_installers()) self.mac_prods = self.get_dict_for_prods(self.get_installers())
def set_catalog(self, catalog): def set_catalog(self, catalog):
@ -151,40 +151,40 @@ class gibMacOS:
url = self.build_url(catalog=self.current_catalog, version=self.current_macos) url = self.build_url(catalog=self.current_catalog, version=self.current_macos)
self.u.head("Downloading Catalog") self.u.head("Downloading Catalog")
if local: if local:
print("Checking locally for {}".format(self.plist)) self.u.info("Checking locally for {}".format(self.plist))
cwd = os.getcwd() cwd = os.getcwd()
os.chdir(os.path.dirname(os.path.realpath(__file__))) os.chdir(os.path.dirname(os.path.realpath(__file__)))
if os.path.exists(os.path.join(os.path.dirname(os.path.realpath(__file__)), self.scripts, self.plist)): if os.path.exists(os.path.join(os.path.dirname(os.path.realpath(__file__)), self.scripts, self.plist)):
print(" - Found - loading...") self.u.info(" - Found - loading...")
try: try:
with open(os.path.join(os.getcwd(), self.scripts, self.plist), "rb") as f: with open(os.path.join(os.getcwd(), self.scripts, self.plist), "rb") as f:
self.catalog_data = plist.load(f) self.catalog_data = plist.load(f)
os.chdir(cwd) os.chdir(cwd)
return True return True
except: except:
print(" - Error loading - downloading instead...\n") self.u.info(" - Error loading - downloading instead...\n")
os.chdir(cwd) os.chdir(cwd)
else: else:
print(" - Not found - downloading instead...\n") self.u.info(" - Not found - downloading instead...\n")
print("Currently downloading {} catalog from:\n\n{}\n".format(self.current_catalog, url)) self.u.info("Currently downloading {} catalog from:\n\n{}\n".format(self.current_catalog, url))
try: try:
b = self.d.get_bytes(url) b = self.d.get_bytes(url, self.interactive)
print("") self.u.info("")
self.catalog_data = plist.loads(b) self.catalog_data = plist.loads(b)
except: except:
print("Error downloading!") self.u.info("Error downloading!")
return False return False
try: try:
# Assume it's valid data - dump it to a local file # Assume it's valid data - dump it to a local file
if local or self.force_local: if local or self.force_local:
print(" - Saving to {}...".format(self.plist)) self.u.info(" - Saving to {}...".format(self.plist))
cwd = os.getcwd() cwd = os.getcwd()
os.chdir(os.path.dirname(os.path.realpath(__file__))) os.chdir(os.path.dirname(os.path.realpath(__file__)))
with open(os.path.join(os.getcwd(), self.scripts, self.plist), "wb") as f: with open(os.path.join(os.getcwd(), self.scripts, self.plist), "wb") as f:
plist.dump(self.catalog_data, f) plist.dump(self.catalog_data, f)
os.chdir(cwd) os.chdir(cwd)
except: except:
print(" - Error saving!") self.u.info(" - Error saving!")
return False return False
return True return True
@ -274,7 +274,7 @@ class gibMacOS:
# Attempt to get the build/version/name/device-ids info from the dist # Attempt to get the build/version/name/device-ids info from the dist
prodd["build"],v,n,prodd["device_ids"] = self.get_build_version(plist_dict.get("Products",{}).get(prod,{}).get("Distributions",{})) prodd["build"],v,n,prodd["device_ids"] = self.get_build_version(plist_dict.get("Products",{}).get(prod,{}).get("Distributions",{}))
prodd["title"] = smd.get("localization",{}).get("English",{}).get("title",n) prodd["title"] = smd.get("localization",{}).get("English",{}).get("title",n)
print(" -->{}. {} ({}){}".format( self.u.info(" -->{}. {} ({}){}".format(
str(len(prod_list)+1).rjust(3), str(len(prod_list)+1).rjust(3),
prodd["title"], prodd["title"],
prodd["build"], prodd["build"],
@ -317,8 +317,7 @@ class gibMacOS:
if os.path.exists(os.path.join(os.getcwd(), self.saves, self.current_catalog, name)): if os.path.exists(os.path.join(os.getcwd(), self.saves, self.current_catalog, name)):
while True: while True:
self.u.head("Already Exists") self.u.head("Already Exists")
print("It looks like you've already downloaded {}".format(name)) self.u.info("It looks like you've already downloaded {}\n".format(name))
print("")
if not self.interactive: if not self.interactive:
return return
menu = self.u.grab("Redownload? (y/n): ") menu = self.u.grab("Redownload? (y/n): ")
@ -336,13 +335,11 @@ class gibMacOS:
c += 1 c += 1
self.u.head("Downloading File {} of {}".format(c, len(dl_list))) self.u.head("Downloading File {} of {}".format(c, len(dl_list)))
if len(done): if len(done):
print("\n".join(["{} --> {}".format(y["name"], "Succeeded" if y["status"] else "Failed") for y in done])) self.u.info("\n".join(["{} --> {}".format(y["name"], "Succeeded" if y["status"] else "Failed") for y in done]))
print("") self.u.info("")
if dmg: if dmg:
print("NOTE: Only Downloading DMG Files") self.u.info("NOTE: Only Downloading DMG Files\n")
print("") self.u.info("Downloading {} for {}...\n".format(os.path.basename(x), name))
print("Downloading {} for {}...".format(os.path.basename(x), name))
print("")
try: try:
self.d.stream_to_file(x, os.path.join(os.getcwd(), self.saves, self.current_catalog, name, os.path.basename(x))) self.d.stream_to_file(x, os.path.join(os.getcwd(), self.saves, self.current_catalog, name, os.path.basename(x)))
done.append({"name":os.path.basename(x), "status":True}) done.append({"name":os.path.basename(x), "status":True})
@ -351,23 +348,19 @@ class gibMacOS:
succeeded = [x for x in done if x["status"]] succeeded = [x for x in done if x["status"]]
failed = [x for x in done if not x["status"]] failed = [x for x in done if not x["status"]]
self.u.head("Downloaded {} of {}".format(len(succeeded), len(dl_list))) self.u.head("Downloaded {} of {}".format(len(succeeded), len(dl_list)))
print("Succeeded:") self.u.info("Succeeded:")
if len(succeeded): if len(succeeded):
for x in succeeded: for x in succeeded:
print(" {}".format(x["name"])) self.u.info(" {}".format(x["name"]))
else: else:
print(" None") self.u.info(" None")
print("") self.u.info("\nFailed:")
print("Failed:")
if len(failed): if len(failed):
for x in failed: for x in failed:
print(" {}".format(x["name"])) self.u.info(" {}".format(x["name"]))
else: else:
print(" None") self.u.info(" None")
print("") self.u.info("\nFiles saved to:\n {}\n".format(os.path.join(os.getcwd(), self.saves, self.current_catalog, name)))
print("Files saved to:")
print(" {}".format(os.path.join(os.getcwd(), self.saves, self.current_catalog, name)))
print("")
if self.interactive: if self.interactive:
self.u.grab("Press [enter] to return...") self.u.grab("Press [enter] to return...")
elif len(failed): elif len(failed):