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