forked from joelagnel/ns-3
/
download-pybindgen.py
215 lines (178 loc) · 8.02 KB
/
download-pybindgen.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#! /usr/bin/env python
import sys
from optparse import OptionParser
import os
import shutil
import urllib
from glob import glob
from util import run_command, fatal, CommandError
import constants
from xml.dom import minidom as dom
def get_ns3(ns3_branch):
print """
#
# Get NS-3
#
"""
ns3_dir = os.path.split(ns3_branch)[-1]
ns3_branch_url = constants.NSNAM_CODE_BASE_URL + ns3_branch
if not os.path.exists(ns3_dir):
print "Cloning ns-3 branch"
run_command(['hg', 'clone', ns3_branch_url, ns3_dir])
else:
print "Updating ns-3 branch"
run_command(['hg', '--cwd', ns3_dir, 'pull', '-u'])
return ns3_dir
def get_regression_traces(ns3_dir, regression_branch):
print """
#
# Get the regression traces
#
"""
# ns3_dir is the directory into which we cloned the repo
# regression_branch is the repo in which we will find the traces. Variations like this should work:
# ns-3-dev-ref-traces
# craigdo/ns-3-dev-ref-traces
# craigdo/ns-3-tap-ref-traces
regression_traces_dir = os.path.split(regression_branch)[-1]
regression_branch_url = constants.REGRESSION_TRACES_REPO + regression_branch
print "Synchronizing reference traces using Mercurial."
try:
if not os.path.exists(regression_traces_dir):
run_command(["hg", "clone", regression_branch_url, regression_traces_dir])
else:
run_command(["hg", "-q", "pull", "--cwd", regression_traces_dir, regression_branch_url])
run_command(["hg", "-q", "update", "--cwd", regression_traces_dir])
except OSError: # this exception normally means mercurial is not found
if not os.path.exists(regression_traces_dir_name):
traceball = regression_tbranch + constants.TRACEBALL_SUFFIX
print "Retrieving " + traceball + " from web."
urllib.urlretrieve(constants.REGRESSION_TRACES_URL + traceball, traceball)
run_command(["tar", "-xjf", traceball])
print "Done."
return regression_traces_dir
def get_pybindgen(ns3_dir):
print """
#
# Get PyBindGen
#
"""
if sys.platform in ['cygwin']:
print "Architecture (%s) does not support PyBindGen ... skipping" % (sys.platform,)
raise RuntimeError
# (peek into the ns-3 wscript and extract the required pybindgen version)
ns3_python_wscript = open(os.path.join(ns3_dir, "bindings", "python", "wscript"), "rt")
required_pybindgen_version = None
for line in ns3_python_wscript:
if 'REQUIRED_PYBINDGEN_VERSION' in line:
required_pybindgen_version = eval(line.split('=')[1].strip())
ns3_python_wscript.close()
break
if required_pybindgen_version is None:
fatal("Unable to detect pybindgen required version")
print "Required pybindgen version: ", '.'.join([str(x) for x in required_pybindgen_version])
# work around http_proxy handling bug in bzr
if 'http_proxy' in os.environ and 'https_proxy' not in os.environ:
os.environ['https_proxy'] = os.environ['http_proxy']
if len(required_pybindgen_version) == 4:
rev = "-rrevno:%i" % required_pybindgen_version[3]
else:
rev = "-rtag:%s" % '.'.join([str(x) for x in required_pybindgen_version])
if os.path.exists(constants.LOCAL_PYBINDGEN_PATH):
print "Trying to update pybindgen; this will fail if no network connection is available. Hit Ctrl-C to skip."
try:
run_command(["bzr", "pull", rev, "-d", constants.LOCAL_PYBINDGEN_PATH, constants.PYBINDGEN_BRANCH])
except KeyboardInterrupt:
print "Interrupted; Python bindings will be disabled."
else:
print "Update was successful."
else:
print "Trying to fetch pybindgen; this will fail if no network connection is available. Hit Ctrl-C to skip."
try:
run_command(["bzr", "checkout", rev, constants.PYBINDGEN_BRANCH, constants.LOCAL_PYBINDGEN_PATH])
except KeyboardInterrupt:
print "Interrupted; Python bindings will be disabled."
shutil.rmtree(constants.LOCAL_PYBINDGEN_PATH, True)
return False
print "Fetch was successful."
## generate a fake version.py file in pybindgen it's safer this
## way, since the normal version generation process requires
## bazaar python bindings, which may not be available.
vfile = open(os.path.join(constants.LOCAL_PYBINDGEN_PATH, "pybindgen", "version.py"), "wt")
vfile.write("""
# (fake version generated by ns-3)
__version__ = %r
""" % list(required_pybindgen_version))
vfile.close()
return (constants.LOCAL_PYBINDGEN_PATH, '.'.join([str(x) for x in required_pybindgen_version]))
def get_nsc(ns3_dir):
print """
#
# Get NSC
#
"""
# Skip downloading NSC on OS X due to HFS+ case insensitive issues
# Skip downloading NSC on Cygwin because of fundamental incompatibilities.
if sys.platform in ['darwin', 'cygwin']:
print "Architecture (%s) does not support NSC... skipping" % (sys.platform,)
raise RuntimeError
# (peek into the ns-3 wscript and extract the required nsc version)
internet_stack_wscript = open(os.path.join(ns3_dir, "src", "internet", "wscript"), "rt")
required_nsc_version = None
for line in internet_stack_wscript:
if 'NSC_RELEASE_NAME' in line:
required_nsc_version = eval(line.split('=')[1].strip())
break
internet_stack_wscript.close()
if required_nsc_version is None:
fatal("Unable to detect NSC required version")
print "Required NSC version: ", required_nsc_version
def nsc_clone():
print "Retrieving nsc from " + constants.NSC_REPO
run_command(['hg', 'clone', constants.NSC_REPO, constants.LOCAL_NSC_PATH])
def nsc_update():
print "Pulling nsc updates from " + constants.NSC_REPO
run_command(['hg', '--cwd', constants.LOCAL_NSC_PATH, 'pull', '-u', constants.NSC_REPO])
def nsc_download():
local_file = required_nsc_version + ".tar.bz2"
remote_file = constants.NSC_RELEASE_URL + "/" + local_file
print "Retrieving nsc from " + remote_file
urllib.urlretrieve(remote_file, local_file)
print "Uncompressing " + local_file
run_command(["tar", "-xjf", local_file])
print "Rename %s as %s" % (required_nsc_version, constants.LOCAL_NSC_PATH)
os.rename(required_nsc_version, constants.LOCAL_NSC_PATH)
if not os.path.exists(os.path.join(ns3_dir, '.hg')):
nsc_download()
elif not os.path.exists(constants.LOCAL_NSC_PATH):
nsc_clone()
else:
nsc_update()
return (constants.LOCAL_NSC_PATH, required_nsc_version)
def main():
parser = OptionParser()
parser.add_option("-n", "--ns3-branch", dest="ns3_branch", default="ns-3-dev",
help="Name of the ns-3 repository", metavar="BRANCH_NAME")
parser.add_option("-r", "--regression-branch", dest="regression_branch", default="",
help="Name of the ns-3 regression traces repository", metavar="REGRESSION_BRANCH_NAME")
(options, dummy_args) = parser.parse_args()
# first of all, change to the directory of the script
os.chdir(os.path.abspath(os.path.dirname(__file__)))
# Create the configuration file
config = dom.getDOMImplementation().createDocument(None, "config", None)
# -- download pybindgen --
try:
pybindgen_dir, pybindgen_version = get_pybindgen("/home/joel/repo/ns-3-git/")
except (CommandError, OSError, RuntimeError):
print " *** Did not fetch pybindgen; python bindings will not be available."
else:
pybindgen_config = config.documentElement.appendChild(config.createElement("pybindgen"))
pybindgen_config.setAttribute("dir", pybindgen_dir)
pybindgen_config.setAttribute("version", pybindgen_version)
# write the config to a file
dot_config = open(".config", "wt")
config.writexml(dot_config, addindent=" ", newl="\n")
dot_config.close()
return 0
if __name__ == '__main__':
sys.exit(main())