-
Notifications
You must be signed in to change notification settings - Fork 1
/
autogen_python.py
264 lines (203 loc) · 7.93 KB
/
autogen_python.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/python
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
# This file is part of Guadalinex
#
# This software is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this package; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
__author__ = "Abraham Macias Paredes <[email protected]>"
__copyright__ = "Copyright (C) 2011, Junta de Andalucía <[email protected]>"
__license__ = "GPL-2"
#
# Pre-build script for GECOS agent for Windows.
# This script performs the following tasks:
# 1) Downloads chef-client-12.22.5-1-x86.msi from Opscode Chef (when necessary)
# 2) Copies the python27.dll (when necessary)
# 3) Packs into a ZIP file all the necessary libraries to execute the python scripts
# 4) Build the reserved.nsh, pythonfiles.nsh and toolsfiles.nsh
#
import glob
import hashlib
import os
import platform
import sys
from urllib2 import urlopen, URLError, HTTPError
import logging
import certifi
from distutils.core import setup
import py2exe
import shutil
import glob
import subprocess
import re
logging.basicConfig(level=logging.INFO)
CHEF_VERSION = '12.22.5'
CHEF_YEAR = '2016'
CHEF_DOWNLOAD_URL = 'https://packages.chef.io/files/stable/chef/'+CHEF_VERSION+'/windows/'+CHEF_YEAR+'/chef-client-'+CHEF_VERSION+'-1-x86.msi'
CHEF_MSI_FILE = os.path.basename(CHEF_DOWNLOAD_URL)
# Function to download Opscode Chef client
def doDownloadChef():
# Open the url
try:
f = urlopen(CHEF_DOWNLOAD_URL, cafile=certifi.where())
logging.info('Downloading ' + CHEF_DOWNLOAD_URL)
# Open our local file for writing
with open(os.path.basename(CHEF_MSI_FILE), "wb") as local_file:
local_file.write(f.read())
logging.info('Opscode Chef download to ' + CHEF_MSI_FILE)
#handle errors
except HTTPError, e:
logging.error('HTTP Error: %s'%(e.code))
sys.exit(-1)
except URLError, e:
logging.error('URL Error: %s'%(e.reason))
sys.exit(-1)
def doReservedFiles():
lines = getReservedFiles()
f = open("reserved.nsh", "w")
f.write("\n".join(lines))
f.write("\n")
f.close()
def getReservedFiles():
pyfiles = []
pyfiles.append(';')
pyfiles.append("; AUTOGENERATED: Please don't edit this file")
pyfiles.append(';')
pyfiles.append('')
for f in glob.glob("*.dll"):
pyfiles.append("ReserveFile \""+str(f).replace(os.path.abspath(".")+"\\", '')+"\"")
for f in glob.glob("*.pyd"):
pyfiles.append("ReserveFile \""+str(f).replace(os.path.abspath(".")+"\\", '')+"\"")
f = 'library.zip'
pyfiles.append("ReserveFile \""+str(f).replace(os.path.abspath(".")+"\\", '')+"\"")
return pyfiles
def doPythonFiles():
lines = getPythonFiles()
f = open("pythonfiles.nsh", "w")
f.write("\n".join(lines))
f.write("\n")
f.close()
def getPythonFiles():
pyfiles = []
pyfiles.append(';')
pyfiles.append("; AUTOGENERATED: Please don't edit this file")
pyfiles.append(';')
pyfiles.append('')
pyfiles.append('!Macro PreparePythonFiles')
for f in glob.glob("*.dll"):
pyfiles.append(" "+'File "/oname=$PLUGINSDIR\\'+f+'" "'+f+'"')
for f in glob.glob("*.pyd"):
pyfiles.append(" "+'File "/oname=$PLUGINSDIR\\'+f+'" "'+f+'"')
f = 'library.zip'
pyfiles.append(" "+'File "/oname=$PLUGINSDIR\\'+f+'" "'+f+'"')
pyfiles.append('!MacroEnd')
return pyfiles
def doToolsFiles():
lines = getToolsFiles(os.path.abspath("tools"))
f = open("toolsfiles.nsh", "w")
f.write("\n".join(lines))
f.write("\n")
f.close()
def getToolsFiles(firstdir):
dirs = [firstdir]
pyfiles = []
pyfiles.append(';')
pyfiles.append("; AUTOGENERATED: Please don't edit this file")
pyfiles.append(';')
pyfiles.append('')
pyfiles.append('!Macro PrepareToolsFiles')
pyfiles.append(" "+'CreateDirectory "$INSTDIR\\tools"')
for d in dirs:
files = glob.glob(d+"/*")
for f in files:
if os.path.isdir(f):
dirs.append(f)
pyfiles.append(" "+'CreateDirectory "$INSTDIR\\'+str(f).replace(os.path.abspath(".")+"\\", '')+'"')
else:
name = str(f).replace(os.path.abspath(".")+"\\", '')
pyfiles.append(" "+'File "/oname=$INSTDIR\\'+name+'" "'+name+'"')
pyfiles.append('!MacroEnd')
return pyfiles
if __name__ == "__main__":
# Check Opscode Chef client
if not os.path.isfile(CHEF_MSI_FILE):
logging.info('Is necessary to download Opscode Chef client')
doDownloadChef()
else:
logging.info('Opscode Chef client already downloaded')
# Update 'main.nsi' file
content = False
replacement = False
with open('main.nsi', 'r') as file:
content = file.read()
replacement = re.sub(r"chef-client-[0-9]+\.[0-9]+\.[0-9]+-[0-9]+\-x86.msi", CHEF_MSI_FILE, content)
replacement = re.sub(r"Chef Client v[0-9]+\.[0-9]+\.[0-9]+", "Chef Client v%s"%(CHEF_VERSION), replacement)
if replacement != False and content != replacement:
with open('main.nsi', 'w') as file:
file.write(replacement)
# Update 'GECOSSetupData.nsh' file
content = False
replacement = False
with open('GECOSSetupData.nsh', 'r') as file:
content = file.read()
replacement = re.sub(r"chef-client-[0-9]+\.[0-9]+\.[0-9]+-[0-9]+\-x86.msi", CHEF_MSI_FILE, content)
if replacement != False and content != replacement:
with open('GECOSSetupData.nsh', 'w') as file:
file.write(replacement)
# Delete dependencies in current directory
todel = []
for f in glob.glob('*.dll'):
todel.append(str(f))
for f in glob.glob('*.pyd'):
todel.append(str(f))
if os.path.isfile('library.zip'):
todel.append('library.zip')
for f in todel:
logging.info('Remove old %s'%(f))
try:
os.remove(f)
except Exception as e:
logging.error('Error removing old file "%s": %s'%(f, str(e)))
logging.info('Please delete this file manualy!')
logging.info('RUN: del *.pyd')
logging.info('RUN: del *.dll')
sys.exit(-1)
# Ejecute py2exe to pack all the dependencies in a ZIP file
sys.argv.append('py2exe')
setup(
options={
'py2exe': {
'packages': ['cffi']
}
},console=['check_dependencies.py']
)
# Check the dependencies
ret = subprocess.call(["dist\\check_dependencies.exe"])
if ret != 0:
logging.error("Error in dependencies check!")
sys.exit(-1)
# Copy the dependencies to current directory
for f in glob.glob('dist/*.dll'):
logging.info('Copy %s to %s'%(f, os.path.basename(f)))
shutil.copy(f, os.path.basename(f))
for f in glob.glob('dist/*.pyd'):
logging.info('Copy %s to %s'%(f, os.path.basename(f)))
shutil.copy(f, os.path.basename(f))
logging.info('Copy %s to %s'%(os.path.join('dist', 'library.zip'), 'library.zip'))
shutil.copy(os.path.join('dist', 'library.zip'), 'library.zip')
# Remove the 'build' and 'dist' directory
shutil.rmtree('dist', ignore_errors=True)
shutil.rmtree('build', ignore_errors=True)
doReservedFiles()
doPythonFiles()
doToolsFiles()