-
Notifications
You must be signed in to change notification settings - Fork 4
/
hub_list_appliances.py
executable file
·90 lines (69 loc) · 2.2 KB
/
hub_list_appliances.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/python3
#
# Copyright (c) 2011 Alon Swartz <[email protected]>
# Copyright (c) 2022 TUrnKey GNU/Linux <[email protected]>
#
# This file is part of HubTools.
#
# HubTools is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
"""
List appliances
By default uses a built-in format, unless a user-specified format is specified.
Format variables:
%name Appliance code name
%version Appliance version code
%description Appliance descriptive label
%preseeds Appliance supported/required preseeding arguments
Examples:
hub-list-appliances
hub-list-appliances "name=%name preseeds=%preseeds"
Environment variables:
HUB_APIKEY Displayed in your Hub account's user profile
"""
import os
import sys
import getopt
from hublib import Hub
from hublib.formatter import Formatter, fmt_appliance_header, fmt_appliance
from hublib.utils import fatal
def usage(e=None):
if e:
print("error: " + str(e), file=sys.stderr)
print("Syntax: %s" % (sys.argv[0]), file=sys.stderr)
print(__doc__, file=sys.stderr)
sys.exit(1)
def main():
try:
opts, args = getopt.gnu_getopt(sys.argv[1:], "h", ["help"])
except getopt.GetoptError as e:
usage(e)
refresh = False
for opt, val in opts:
if opt in ('-h', '--help'):
usage()
apikey = os.getenv('HUB_APIKEY', None)
if not apikey:
fatal("HUB_APIKEY not specified in environment")
if args:
if len(args) != 1:
usage("incorrect number of arguments")
format = args[0]
else:
format = None
hub = Hub(apikey)
appliances = hub.appliances.get()
appliances = sorted(appliances, key=lambda appliance: appliance.name)
if format:
format = Formatter(format)
for appliance in appliances:
print(format(appliance))
else:
print(fmt_appliance_header())
for appliance in appliances:
print(fmt_appliance(appliance))
if __name__ == "__main__":
main()