-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppBuild.py
More file actions
203 lines (164 loc) · 5.72 KB
/
AppBuild.py
File metadata and controls
203 lines (164 loc) · 5.72 KB
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
###########################################################################
#### ####
#### Dependable v0.0.1 pre-release ####
#### (C) copyright Jack McCrea 2014, used with permission ####
#### ####
###########################################################################
####
#### NOTE: THIS IS A PRE-RELEASE OF Dependable AND IS NOT CONSIDERED ####
#### STABLE.
####
###########################################################################
INPUT_FORMAT_VERSION = 00003
###########################################################################
## \file AppBuild.py
## \brief
## \author
###########################################################################
## \package Dependable
## \brief
###########################################################################
import xml.etree.ElementTree as ET
import os
import urllib2
import sys
import shutil
import zipfile
import distutils.core
tree = ET.parse('dependencies.xml')
root = tree.getroot()
config = {}
packages = {}
args = {'update' : True, 'overwrite' : True}
###########################################################################
## \brief
## \param
## \return
## \author
###########################################################################
def die(msg):
print msg
sys.exit(1)
###########################################################################
## \brief
## \param
## \param
## \return
## \author
###########################################################################
def mkdir(directory, need_overwrite=True):
if os.path.exists(directory):
if need_overwrite and (not (args['overwrite'])):
die("Directory " + directory + " already exists.")
else:
try:
os.makedirs(directory)
except:
die("Couldn't make directory: " + directory)
###########################################################################
## \brief
## \param
## \param
## \return
## \author
###########################################################################
def get_file_name(dict, prefix=""):
if 'dlname' in dict:
return prefix+dict['dlname']
else:
return prefix+dict['uri'].rpartition('/')[2].rpartition('\\')[2]
###########################################################################
## \brief
## \param
## \param
## \return
## \author
###########################################################################
def temp_file_name(filedir, dict, prefix=""):
return os.path.join(filedir, get_file_name(dict, prefix))
###########################################################################
## \brief
## \param
## \param
## \return
## \author
###########################################################################
def node_text(node, child):
sub_node = node.find(child)
if sub_node is not None:
return sub_node.text
else:
return None
###########################################################################
###########################################################################
############################## MAIN BODY ##############################
###########################################################################
###########################################################################
for param in root.find('setup').findall('param'):
config.update({param.attrib['name'] : param.text })
if 'input-version' in config:
if int(config['input-version']) > INPUT_FORMAT_VERSION:
die("The input file version is newer than this version of Dependable. Please update Dependable then try again.")
for package in root.find('packages').findall('package'):
dict = {}
name = package.attrib['name']
for package_conf in package.getchildren():
if package_conf.tag != 'move' and package_conf.tag != 'mkdir':
dict.update( { package_conf.tag : package_conf.text } )
moves = []
for fmove in package.findall('move'):
movdict = {}
inner_source = fmove.find('innersource')
isdir = False
if inner_source is not None:
if inner_source.text is None:
inner_source_text = ""
else:
inner_source_text = inner_source.text
movdict.update( { 'from' : inner_source_text } )
if 'dir' in inner_source.attrib:
if int(inner_source.attrib['dir']):
isdir = True
movdict.update( { 'isdir' : isdir } )
movdict.update( { 'to' : node_text(fmove, 'destination') } )
newdirs = []
for newdir in fmove.findall('mkdir'):
newdirs.append( { 'dirname' : newdir.text } )
movdict.update( { 'mkdir' : newdirs } )
moves.append(movdict)
dict.update( { 'moves' : moves } )
packages.update( { name : dict } )
tmp_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), config['tempdir'])
mkdir(tmp_dir, False)
for name, dict in packages.iteritems():
filedir = os.path.join(tmp_dir, name)
mkdir(filedir)
filename = temp_file_name(filedir, dict)
sys.stdout.write("Downloading '%s' ... "%(dict['uri']))
sys.stdout.flush()
req = urllib2.Request(dict['uri'])
rhandle = urllib2.urlopen(req)
with open(filename, 'wb') as outfile:
shutil.copyfileobj(rhandle, outfile)
print "Done!"
for name, dict in packages.iteritems():
dl_dir = os.path.join(tmp_dir, name)
if int(dict['unzip']):
zip_file = temp_file_name(dl_dir, dict)
sys.stdout.write("unzipping " + name + " ... ")
sys.stdout.flush()
with zipfile.ZipFile(zip_file, 'r') as zip:
zip.extractall(dl_dir)
print "Done!"
for fmove in dict['moves']:
for newdir in fmove['mkdir']:
mkdir(os.path.join(os.path.dirname(os.path.abspath(__file__)), newdir['dirname']))
if int(dict['unzip']) and fmove['from'] is not None:
src_file = os.path.join(dl_dir, fmove['from'])
else:
src_file = temp_file_name(dl_dir, dict)
dst_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), fmove['to'])
if fmove['isdir']:
distutils.dir_util.copy_tree(src_file, dst_file)
else:
shutil.copy2(src_file, dst_file);