-
Notifications
You must be signed in to change notification settings - Fork 2
/
dist.py
executable file
·182 lines (155 loc) · 6.47 KB
/
dist.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
#! /usr/bin/env python
import sys
from optparse import OptionParser
import os
import tarfile
from xml.dom import minidom as dom
from cStringIO import StringIO
import time
def tar_add_tree(tar, srcdir, tgtdir, exclude_dir_cb, exclude_file_cb):
"""
Add a source tree to an archive while changing name and excluding
directories/files via callbacks.
"""
assert len(os.path.sep) == 1
for (dirpath, dirnames, filenames) in os.walk(srcdir):
assert dirpath.startswith(srcdir)
reldirpath = dirpath[len(srcdir)+1:]
# directories to exclude
while True:
for i, dirname in enumerate(dirnames):
if os.path.islink (os.path.join(dirpath,dirname)):
# Add, but do not follow, symbolic links
srcpath = os.path.join(dirpath, dirname)
tgtpath = os.path.join(tgtdir, reldirpath, dirname)
tar.add(srcpath, tgtpath)
if exclude_dir_cb(reldirpath, dirname):
break
else:
break
del dirnames[i]
for filename in filenames:
if exclude_file_cb(reldirpath, filename):
continue
srcpath = os.path.join(dirpath, filename)
tgtpath = os.path.join(tgtdir, reldirpath, filename)
#print srcpath, "=>", tgtpath
tar.add(srcpath, tgtpath)
def main():
parser = OptionParser()
(options, dummy_args) = parser.parse_args()
# first of all, change to the directory of the script
os.chdir(os.path.dirname(__file__))
try:
dot_config = open(".config", "rt")
except IOError:
print >> sys.stderr, "** ERROR: missing .config file; you probably need to run the download.py script first."
sys.exit(2)
config = dom.parse(dot_config)
dot_config.close()
ns3_config, = config.getElementsByTagName("ns-3")
ns3_dir = ns3_config.getAttribute("dir")
ns3_version = open(os.path.join(ns3_dir, "VERSION"), "rt").readline().strip()
print "NS-3 version: %r" % (ns3_version,)
dist_dir = "ns-allinone-%s" % ns3_version
arch_name = "%s.tar.bz2" % dist_dir
tar = tarfile.open(arch_name, 'w:bz2')
# Create/add a new .config file with modified dir attributes
new_config = config.cloneNode(True)
# add the ns-3 tree
new_ns3_dir = "ns-%s" % ns3_version
new_config.getElementsByTagName("ns-3")[0].setAttribute("dir", new_ns3_dir)
def dir_excl(reldirpath, dirname):
if dirname[0] == '.':
return True
if reldirpath == '' and dirname == 'build':
return True
return False
def file_excl(reldirpath, filename):
# Retain directories in doc/[manual|tutorial|models]/source/_static/
if filename == '.hidden':
return False
if filename == '.ns3rc':
return False
if filename.startswith('.'):
return True
if filename.endswith('.pyc') or filename.endswith('.pyo'):
return True
if filename.endswith('~'):
return True
return False
print "Adding %s as %s" % (ns3_dir, os.path.join(dist_dir, new_ns3_dir))
tar_add_tree(tar, ns3_dir, os.path.join(dist_dir, new_ns3_dir), dir_excl, file_excl)
# add pybindgen
pybindgen_dir = config.getElementsByTagName("pybindgen")[0].getAttribute("dir")
new_pybindgen_dir = "pybindgen-%s" % config.getElementsByTagName("pybindgen")[0].getAttribute("version")
new_config.getElementsByTagName("pybindgen")[0].setAttribute("dir", new_pybindgen_dir)
def dir_excl(reldirpath, dirname):
if dirname[0] == '.':
return True
if reldirpath == '' and dirname == 'build':
return True
return False
def file_excl(reldirpath, filename):
if filename.startswith('.'):
return True
if filename.endswith('.pyc') or filename.endswith('.pyo'):
return True
if filename.endswith('~'):
return True
return False
print "Adding %s as %s" % (pybindgen_dir, os.path.join(dist_dir, new_pybindgen_dir))
tar_add_tree(tar, pybindgen_dir, os.path.join(dist_dir, new_pybindgen_dir), dir_excl, file_excl)
# add network simulation cradle
nsc_dir = config.getElementsByTagName("nsc")[0].getAttribute("dir")
new_nsc_dir = config.getElementsByTagName("nsc")[0].getAttribute("version")
assert new_nsc_dir.startswith("nsc")
new_config.getElementsByTagName("nsc")[0].setAttribute("dir", new_nsc_dir)
def dir_excl(reldirpath, dirname):
if dirname[0] == '.':
return True
# FIXME: which files or directories to exclude for NSC?
return False
def file_excl(reldirpath, filename):
if filename.startswith('.'):
return True
if filename.endswith('~'):
return True
return False
print "Adding %s as %s" % (nsc_dir, os.path.join(dist_dir, new_nsc_dir))
tar_add_tree(tar, nsc_dir, os.path.join(dist_dir, new_nsc_dir), dir_excl, file_excl)
# add NetAnim
netanim_dir = config.getElementsByTagName("netanim")[0].getAttribute("dir");
new_netanim_dir = config.getElementsByTagName("netanim")[0].getAttribute("version")
assert new_netanim_dir.startswith("netanim")
new_config.getElementsByTagName("netanim")[0].setAttribute("dir", new_netanim_dir)
def dir_excl(reldirpath, dirname):
if dirname[0] == '.':
return True
return False
def file_excl(reldirpath, filename):
if filename.startswith('.'):
return True
if filename.endswith('~'):
return True
return False
print "Adding %s as %s" % (netanim_dir, os.path.join(dist_dir, new_netanim_dir))
tar_add_tree(tar, netanim_dir, os.path.join(dist_dir, new_netanim_dir), dir_excl, file_excl)
# add the build script files
print "Adding the build script files"
for filename in ["build.py", "constants.py", "util.py", "README"]:
tar.add(filename, os.path.join(dist_dir, filename))
# Add the modified .config file
new_config_file = StringIO()
new_config.writexml(new_config_file)
tarinfo = tarfile.TarInfo(os.path.join(dist_dir, ".config"))
tarinfo.mtime = time.time()
tarinfo.mode = 0644
tarinfo.type = tarfile.REGTYPE
tarinfo.size = new_config_file.tell()
new_config_file.seek(0)
tar.addfile(tarinfo, new_config_file)
tar.close()
return 0
if __name__ == '__main__':
sys.exit(main())