-
Notifications
You must be signed in to change notification settings - Fork 0
/
installTools.py
428 lines (388 loc) · 17 KB
/
installTools.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/usr/bin/env python
import platform
import os
import ntpath
import sys
import signal
import subprocess
import errno
import shutil
import logging
import time
import datetime
from logging.handlers import TimedRotatingFileHandler
import wget
platform = platform.system()
app_path = ''
config_path = ''
# the application main entry point
def createLogger():
global logger
# first check if the log directory exists
if(not os.path.exists("logs")):
# create the log directory
os.makedirs("logs")
# the log directory should exist now.
logFile = "logs\\installTools_{0}.log".format(
datetime.datetime.now().strftime('%Y_%m_%d %H_%M_%S'))
# logFile = "installTools.log"
logger = logging.getLogger("Rotating Log")
logger.setLevel(logging.INFO)
# add a rotating handler
handler = TimedRotatingFileHandler(
logFile, when='d', interval=1, backupCount=10)
logger.addHandler(handler)
logger.info("******************************")
logger.info("Logging Initiated")
logger.info("{0}".format(
datetime.datetime.now().strftime('%Y_%m_%d %H_%M_%S')))
logger.info("******************************")
def main():
global logger
createLogger()
# Windows Setup:
# - check and notify user to install
if platform == 'Windows':
if(logger == None):
print("Logger object is null or undefined. Logging will not be done.");
logger.info("Windows OS detected")
installChocolateyIfNotInstalled()
print("End Work. For more information please see the log file")
sys.exit(0)
else:
print("Operating system is not supported. Please make sure that you are running this script on a WindowsOS")
raise Exception('Operating system not supported')
sys.exit(1)
# Windows installation instructions
def installChocolateyIfNotInstalled():
print('Will check if we need to install choco')
try:
if not Helpers.is_installed(['chocolatey']):
print('Chocolatey is not installed. Will attempt to install chocolatey')
logger.info(
'Chocolatey is not installed. Will attempt to install chocolatey')
# wget.download("https://chocolatey.org/install.ps1", "install.ps1")
try:
wget.download("https://chocolatey.org/install.ps1", "install.ps1")
scriptPath = os.path.join(os.getcwd(), "install.ps1")
print("Current Script path : " + scriptPath)
if(os.path.exists("install.ps1")):
command="""%systemroot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& '{0}' %*" """.format(scriptPath)
os.system(command)
except Exception as e:
raise e
# if(os.path.exists("chocoinstall.cmd")) :
# os.system("chocoinstall.cmd")
# logger.info("chocolatey installed")
# print('Chocolatey Installation complete. Please launch this exe again as an admin. Please note this is only required if choco is not installed. If it is already installed, this step is skipped...')
# else:
# errorString = "chocoinstall.cmd file is not present. It should be present in the same folder as this script."
# print(errorString)
# logger.info(errorString)
else:
installOtherTools()
except OSError as e:
print(e)
sys.exit(1)
def installOtherTools():
import xml.etree.ElementTree as ET
try:
appsRoot = ET.parse("installTools.config").getroot()
apps = appsRoot.findall(".//app")
toolsConfig = appsRoot.find(".//toolsConfig")
for app in apps:
logger.info("refreshing the environment")
os.system("refreshenv")
appName = app.attrib["name"]
appConfig = app.find(".//config")
logger.info("Now checking if I need to install : {0}".format(appName))
if(app.attrib["install"] == 'true'):
if "force" in app.attrib and app.attrib["force"]== 'true':
logger.info("App : {0} will be forcefully installed".format(appName));
os.system("choco install -y --force {0}".format(appName))
executeConfigurationInformation(appConfig, toolsConfig)
else:
logger.info("{0} will be installed".format(appName))
os.system("choco install -y {0}".format(appName))
executeConfigurationInformation(appConfig, toolsConfig)
else:
logger.info("{0} will NOT be installed".format(appName))
pass
except Exception as e:
raise e
pass
def executeConfigurationInformation(appConfig, toolsConfig):
if(appConfig == None):
logger.info("appConfig entry not present.")
return
for entry in appConfig:
cls = eval(entry.tag)
cls.handle(entry, toolsConfig);
pass
# configuration instructions
def config(config_path):
packages = config_path + '/Installed Packages'
settings = config_path + '/Packages/User'
# create the settings directories
Helpers.make_dir(config_path)
Helpers.make_dir(packages)
Helpers.make_dir(settings)
# install 'Package Control'
Helpers.install_package_control(config_path)
# copy the themes
Helpers.copytree('./themes', packages)
# copy the user preferences
Helpers.copytree('./user-settings', settings)
print('Configuration complete...')
# Download File class
class downloadfile():
@staticmethod
def handle(entry, config):
Helpers.logStatement("Download File Module")
downloadParams = config.find(".//downloadParams").attrib['params']
source = entry.attrib['source']
target = entry.attrib['target']
active = entry.attrib['active']
if(active == 'true'):
sourcePathFileName = ntpath.basename(source)
logger.info("Will download the file : {0}".format(sourcePathFileName))
logger.info("Download Location : {0}".format(source))
logger.info("Target Location : {0}".format(target))
targetFileName = target+'\\'+sourcePathFileName
downloadFileCompleteCommand = downloadParams.format(target = targetFileName,source = source)
logging.info(downloadFileCompleteCommand)
try:
os.system(downloadFileCompleteCommand)
logger.info("File download completed successfully")
pass
except Exception as e:
logger.error("There was an error while downloading the file. Please see the error below..")
print(str(e))
logger.error(e)
pass
# FileDelete class
class runcommand():
@staticmethod
def handle(entry,config):
Helpers.logStatement("Run Command Module")
try:
logger.info("Run command module stated")
commandToRun = entry.text
active = entry.attrib['active']
logger.info("Command to run :{0}".format(commandToRun))
if(active == 'true' and commandToRun ):
os.system(commandToRun)
logger.info("Command : {0} was executed".format(commandToRun))
else:
logger.info("Please make sure that active flag is set to true. If it is set to true then check the command specified. The command that was supplied : {0}".format(commandToRun))
pass
except Exception as e:
logger.error(str(e))
print(str(e))
pass
# FileDelete class
class filedelete():
@staticmethod
def handle(entry,config):
Helpers.logStatement("File Delete Module")
try:
# logger.info("File delete process started")
fileToDelete = entry.text
active = entry.attrib['active']
logger.info("File to delete :{0}".format(fileToDelete))
if(active == 'true' and os.path.exists(fileToDelete) and os.path.isfile(fileToDelete)):
os.remove(fileToDelete)
logger.info("File : {0} was removed".format(fileToDelete))
else:
logger.info("Please make sure that active flag is set to true. If it is set to true then either the file does not exist, or the file is not of type file. The path that was supplied : {0}".format(fileToDelete))
pass
except Exception as e:
logger.error(str(e))
print(str(e))
pass
# MakeDir class
class makedirectory():
@staticmethod
def handle(entry,config):
Helpers.logStatement("Make Directory Module")
try:
directoryToMake = entry.text
active = entry.attrib['active']
if(active == 'true'):
logger.info("Directory to make : {0}".format(directoryToMake))
if(os.path.exists(directoryToMake) and os.path.isdir(directoryToMake)):
logger.info("Directory : {0} already exists".format(directoryToMake));
return
else:
Helpers.make_dir(directoryToMake)
logger.info("Directory : {0} created successfully".format(directoryToMake))
else:
logger.info("active flag is set to false, make directory will not executed")
pass
except Exception as e:
logger.error(str(e))
print(str(e))
pass
# Delete Directory class
class deletedirectory():
@staticmethod
def handle(entry,config):
Helpers.logStatement("Delete Directory Module")
try:
active = entry.attrib['active']
if(active == 'true'):
directoryToDelete = entry.text
logger.info("Directory to delete : {0}".format(directoryToDelete))
if(os.path.exists(directoryToDelete) and os.path.isdir(directoryToDelete)):
shutil.rmtree(directoryToDelete)
logger.info("Directory was removed")
else:
logger.info("Directory does not exist, or the given path is not a directory")
else:
logger.info("Delete directory module is not set active. It will not be executed")
pass
except Exception as e:
logger.error(str(e))
print(str(e))
pass
# Copy File class
class copyfile():
@staticmethod
def handle(entry,config):
Helpers.logStatement("Copy File Module")
try:
sourceFile = entry.attrib['sourceFile']
active = entry.attrib['active']
if(active == 'true' and os.path.exists(sourceFile) and os.path.isfile(sourceFile)):
targetDirectory = entry.attrib['targetDirectory']
newFileName = entry.attrib['newFileName']
if(newFileName is None):
newFileName = ntpath.basename(sourceFile)
newFileNameAndLocation = targetDirectory+"\\"+newFileName
# check copyfile options in the toolsconfig section
shouldRenameExistingFile = config.find(".//copyfile").attrib['renameExistingFile']
if(shouldRenameExistingFile == 'true'):
# ok - we have to rename the existing file
Helpers.renameExistingFile(newFileNameAndLocation)
if(os.path.exists(newFileNameAndLocation) and os.path.isfile(newFileNameAndLocation)):
print("File already exists - will not copy the file. You should use the tools config options for the copyfile command ")
logger.info("Will not copy the file because the file already exists in the target location")
return
logger.info("New File Name location : {0}".format(newFileNameAndLocation))
shutil.copy2(sourceFile,newFileNameAndLocation)
logger.info("File was copied successfully to the source location")
else:
logger.info("Either the source file does not exist or it is not a file")
pass
except Exception as e:
logger.error(str(e))
print(str(e))
pass
# Copy directory class
class copydirectory():
@staticmethod
def handle(entry,config):
Helpers.logStatement("Copy Directory Module");
try:
sourceDirectory = entry.attrib['sourcedirectory']
targetDirectory = entry.attrib['targetdirectory']
active = entry.attrib['active']
if(active == 'true'):
logger.info("source directory : {0}".format(sourceDirectory))
logger.info("target directory : {0}".format(targetDirectory))
Helpers.copytree(sourceDirectory, targetDirectory)
logger.info("Directory copied successfully")
else:
logger.info("Copy directory is not active, hence the step will not be executed.")
pass
except Exception as e:
print(str(e))
logger.error(str(e))
pass
# checkout class
class checkout():
@staticmethod
def handle(entry,config):
Helpers.logStatement("Checkout source Module")
try:
sourceLocation = entry.attrib["sourceLocation"]
targetLocation = entry.attrib["targetLocation"]
active = entry.attrib["active"]
checkoutParams = config.find(".//checkout").attrib['command']
if(active == 'true'):
logger.info("source location : {0}".format(sourceLocation))
logger.info("target location : {0}".format(targetLocation))
logger.info("checkout params: {0}".format(checkoutParams))
if(checkoutParams):
completeCommand = checkoutParams.format(sourceLocation = sourceLocation, targetDirectory = targetLocation)
logging.info("complete checkout command : {0}".format(completeCommand))
os.system(completeCommand)
logger.info("checkout command completed successfully")
else:
logger.info("checkout params is null or empty. Command not executed")
else:
logger.info("Active flag is not set to true. This instruction will not be executed")
pass
except Exception as e:
print(e)
logger.error(e)
# A class containting static helper methods used to perform the install +
# config
class Helpers():
@staticmethod
# Log a statement to the logger and print it to the console
def logStatement(statement):
logger.info("---------------------")
logger.info(statement)
logger.info("---------------------")
print(statement)
# Check to see if an application is installed
@staticmethod
def is_installed(app_path):
try:
p = subprocess.Popen(
app_path, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
os.kill(p.pid, signal.SIGTERM)
return True
except OSError as e:
return False
# rename an existing file
@staticmethod
def renameExistingFile(existingFile):
if(os.path.exists(existingFile) and os.path.isfile(existingFile)):
currentFileNameWithExtension = ntpath.basename(existingFile)
currentFileName = os.path.splitext(currentFileNameWithExtension)[0]
currentExtension = os.path.splitext(currentFileNameWithExtension)[1]
newFileName = "{0}_{1}{2}".format(currentFileName,datetime.datetime.now().strftime('%Y_%m_%d %H_%M_%S'), currentExtension)
logging.info("Generated New File Name : {0}".format(newFileName))
newFileNameLocation = os.path.dirname(existingFile)+"\\"+newFileName
shutil.copy2(existingFile,newFileNameLocation)
logging.info("Copied the new file from : {0} to : {1}".format(existingFile, newFileNameLocation))
logging.info("since the file has already been copied , delete the original file")
os.remove(existingFile)
# create a directory if it doesn't already exist
@staticmethod
def make_dir(dir):
try:
os.makedirs(dir)
# capture any non-file-creation errors
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
# recursively copies all the files in a directory
# required because shutil.copytree can't overwrite directories
@staticmethod
def copytree(src, dst, symlinks=False, ignore=None):
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
try:
shutil.copytree(s, d, symlinks, ignore)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
else:
shutil.copy2(s, d)
if __name__ == '__main__':
main()