Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--list and --productid options added #25

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 64 additions & 28 deletions installinstallmacos.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def replicate_url(full_url,
if attempt_resume:
curl_cmd.extend(['-C', '-'])
curl_cmd.append(full_url)
print("Downloading %s..." % full_url)
print("Downloading %s..." % full_url, file=sys.stderr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm. I know why you want this; I just have a philosophical issue with directing non-error messages to stderr. Convince me this is the right thing to do. Is this a common pattern for UNIX/POSIX tools?

try:
subprocess.check_call(curl_cmd)
except subprocess.CalledProcessError as err:
Expand Down Expand Up @@ -459,9 +459,6 @@ def find_installer_app(mountpoint):

def main():
'''Do the main thing here'''
if os.getuid() != 0:
sys.exit('This command requires root (to install packages), so please '
'run again with sudo or as root.')

parser = argparse.ArgumentParser()
parser.add_argument('--seedprogram', default='',
Expand All @@ -486,6 +483,11 @@ def main():
'less available disk space and is faster.')
parser.add_argument('--ignore-cache', action='store_true',
help='Ignore any previously cached files.')

parser.add_argument('--list', action='store_true',
help='Output available product IDs and OS versions available in plist format and then exit')
parser.add_argument('--productid', metavar='productid',help='Use the specified product id. Skips prompting. Useful with --available-os-only option')

args = parser.parse_args()

if args.catalogurl:
Expand All @@ -504,6 +506,12 @@ def main():
print('Could not find a default catalog url for this OS version.',
file=sys.stderr)
exit(-1)

#listing doesn't require root so we allow it.
if args.list==False and os.getuid() != 0:
sys.exit('This command requires root (to install packages), so please '
'run again with sudo or as root.')


# download sucatalog and look for products that are for macOS installers
catalog = download_and_parse_sucatalog(
Expand All @@ -516,30 +524,58 @@ def main():
file=sys.stderr)
exit(-1)

# display a menu of choices (some seed catalogs have multiple installers)
print('%2s %12s %10s %8s %11s %s'
% ('#', 'ProductID', 'Version', 'Build', 'Post Date', 'Title'))
for index, product_id in enumerate(product_info):
print('%2s %12s %10s %8s %11s %s' % (
index + 1,
product_id,
product_info[product_id]['version'],
product_info[product_id]['BUILD'],
product_info[product_id]['PostDate'].strftime('%Y-%m-%d'),
product_info[product_id]['title']
))

answer = get_input(
'\nChoose a product to download (1-%s): ' % len(product_info))
try:
index = int(answer) - 1
if index < 0:
raise ValueError
product_id = list(product_info.keys())[index]
except (ValueError, IndexError):
print('Exiting.')
exit(0)

#if product id is specified, skip menu or plist. If it is invalid, we will catch it later
if args.productid:
product_id=args.productid
else:
#build up a list for outputing as a plist, or just print as we read in the lines.
available_os={}
# display a menu of choices (some seed catalogs have multiple installers) if not plist output
if args.list==False:
print('%2s %12s %10s %8s %11s %s'
% ('#', 'ProductID', 'Version', 'Build', 'Post Date', 'Title'))
for index, product_id in enumerate(product_info):
version=product_info[product_id]['version']
build=product_info[product_id]['BUILD']
post_date=product_info[product_id]['PostDate'].strftime('%Y-%m-%d')
title=product_info[product_id]['title']
if args.list==True:
available_os[product_id]={'version':version,
'build':build,
'post_date':post_date,
'title':title}
else:
print('%2s %12s %10s %8s %11s %s' % (
index + 1,
product_id,
version,
build,
post_date,
title
))
#when done, output plist if that is what is requested, then exit cleanly
if args.list==True:
print (plistlib.writePlistToString(available_os))
exit(0)

if args.productid:
if args.productid in product_info:
product_id=args.productid
else:
print("product id not found:" + args.productid,file=sys.stderr)
exit(-1)
else:
answer = get_input(
'\nChoose a product to download (1-%s): ' % len(product_info))
try:
index = int(answer) - 1
if index < 0:
raise ValueError
product_id = list(product_info.keys())[index]
except (ValueError, IndexError):
print('Exiting.')
exit(0)

# download all the packages for the selected product
replicate_product(
catalog, product_id, args.workdir, ignore_cache=args.ignore_cache)
Expand Down