Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 91 lines (70 sloc) 2.217 kb
cd34197 @alonswartz added cli: hub-list-appliances
alonswartz authored
1 #!/usr/bin/python
1b86502 @alonswartz added copyright / GPL
alonswartz authored
2 #
3 # Copyright (c) 2011 Alon Swartz <alon@turnkeylinux.org>
4 #
5 # This file is part of HubTools.
6 #
7 # HubTools is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the
9 # Free Software Foundation; either version 3 of the License, or (at your
10 # option) any later version.
11 #
cd34197 @alonswartz added cli: hub-list-appliances
alonswartz authored
12 """
13 List appliances
14
4948a28 @alonswartz added custom formatting support to hub-list-* commands
alonswartz authored
15 By default uses a built-in format, unless a user-specified format is specified.
16 Format variables:
17
18 %name Appliance code name
19 %version Appliance version code
20 %description Appliance descriptive label
21 %preseeds Appliance supported/required preseeding arguments
22
23 Examples:
24
25 hub-list-appliances
26 hub-list-appliances "name=%name preseeds=%preseeds"
27
cd34197 @alonswartz added cli: hub-list-appliances
alonswartz authored
28 Environment variables:
4948a28 @alonswartz added custom formatting support to hub-list-* commands
alonswartz authored
29
30 HUB_APIKEY Displayed in your Hub account's user profile
cd34197 @alonswartz added cli: hub-list-appliances
alonswartz authored
31 """
32 import os
33 import sys
34 import getopt
35
36 from hub import Hub
4948a28 @alonswartz added custom formatting support to hub-list-* commands
alonswartz authored
37 from hub.formatter import Formatter, fmt_appliance_header, fmt_appliance
cd34197 @alonswartz added cli: hub-list-appliances
alonswartz authored
38
39 def fatal(e):
40 print >> sys.stderr, "error: " + str(e)
41 sys.exit(1)
42
43 def usage(e=None):
44 if e:
45 print >> sys.stderr, "error: " + str(e)
46
47 print >> sys.stderr, "Syntax: %s" % (sys.argv[0])
48 print >> sys.stderr, __doc__
49
50 sys.exit(1)
51
52 def main():
53 try:
54 opts, args = getopt.gnu_getopt(sys.argv[1:], "h", ["help"])
55 except getopt.GetoptError, e:
56 usage(e)
57
58 refresh = False
59 for opt, val in opts:
60 if opt in ('-h', '--help'):
61 usage()
62
63 apikey = os.getenv('HUB_APIKEY', None)
64 if not apikey:
65 fatal("HUB_APIKEY not specified in environment")
66
4948a28 @alonswartz added custom formatting support to hub-list-* commands
alonswartz authored
67 if args:
68 if len(args) != 1:
69 usage("incorrect number of arguments")
cd34197 @alonswartz added cli: hub-list-appliances
alonswartz authored
70
4948a28 @alonswartz added custom formatting support to hub-list-* commands
alonswartz authored
71 format = args[0]
72 else:
73 format = None
74
75 hub = Hub(apikey)
cd34197 @alonswartz added cli: hub-list-appliances
alonswartz authored
76 appliances = hub.appliances.get()
77 appliances = sorted(appliances, key=lambda appliance: appliance.name)
78
4948a28 @alonswartz added custom formatting support to hub-list-* commands
alonswartz authored
79 if format:
80 format = Formatter(format)
81 for appliance in appliances:
82 print format(appliance)
83 else:
84 print fmt_appliance_header()
85 for appliance in appliances:
86 print fmt_appliance(appliance)
cd34197 @alonswartz added cli: hub-list-appliances
alonswartz authored
87
88 if __name__ == "__main__":
89 main()
90
Something went wrong with that request. Please try again.