a little nicer wrt pep8
[sfa.git] / clientbin / sfiAddLinks.py
1 #!/usr/bin/env python3
2
3 import sys
4
5 from sfa.util.sfalogging import logger
6 from sfa.client.sfi_commands import Commands
7 from sfa.rspecs.rspec import RSpec
8 from sfa.rspecs.version_manager import VersionManager
9
10 logger.enable_console()
11 command = Commands(usage="%prog [options] node1 node2...",
12                    description="Add links to the RSpec. " +
13                    "This command reads in an RSpec and outputs a modified " +
14                    "RSpec. Use this to add links to your slivers")
15 command.add_linkfile_option()
16 command.prep()
17
18 if not command.opts.linkfile:
19     print("Missing link list -- exiting")
20     command.parser.print_help()
21     sys.exit(1)
22
23 if command.opts.infile:
24     infile = open(command.opts.infile)
25 else:
26     infile = sys.stdin
27 if command.opts.outfile:
28     outfile = open(command.opts.outfile, "w")
29 else:
30     outfile = sys.stdout
31 ad_rspec = RSpec(infile)
32 links = open(command.opts.linkfile).read().split('\n')
33 link_tuples = [tuple(x.split()) for x in links]
34
35 version_manager = VersionManager()
36 try:
37     type = ad_rspec.version.type
38     version_num = ad_rspec.version.version
39     request_version = version_manager._get_version(
40         type, version_num, 'request')
41     request_rspec = RSpec(version=request_version)
42     request_rspec.version.merge(ad_rspec)
43     request_rspec.version.add_link_requests(link_tuples)
44 except:
45     logger.log_exc("sfiAddLinks FAILED with links %s" % links)
46     sys.exit(1)
47 print(request_rspec.toxml(), file=outfile)
48 sys.exit(0)