Skip to content

Commit 782f28b

Browse files
committed
Convert scripts to python 3
1 parent 8e3bbe2 commit 782f28b

19 files changed

Lines changed: 210 additions & 216 deletions

File tree

scripts/network/exdhcp/dhcpd_edithosts.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,27 @@ def lock():
3636
sleep(1)
3737
count = count + 1
3838
if count > sleep_max:
39-
print "Can not get file lock at %s, time expired" % file_lock
39+
print("Can not get file lock at %s, time expired" % file_lock)
4040
return False
4141

4242
try:
4343
f = open(file_lock, "w")
4444
f.close()
4545
return True
46-
except IOError,e:
47-
print "Cannot create file lock at /etc/dhcpd.conf_locked,", e
46+
except IOError as e:
47+
print("Cannot create file lock at /etc/dhcpd.conf_locked,", e)
4848
return False
4949

5050

5151
def unlock():
5252
if exists(file_lock) == False:
53-
print "Cannot find %s when unlocking, race condition happens" % file_lock
53+
print("Cannot find %s when unlocking, race condition happens" % file_lock)
5454
else:
5555
try:
5656
remove(file_lock)
5757
return True
58-
except IOError, e:
59-
print "Cannot remove file lock at %s" % file_lock
58+
except IOError as e:
59+
print("Cannot remove file lock at %s" % file_lock)
6060
return False
6161

6262
def insert_host_entry(mac, ip, hostname, dns, gateway, next_server):
@@ -66,14 +66,14 @@ def insert_host_entry(mac, ip, hostname, dns, gateway, next_server):
6666
cmd = 'sed -i /"fixed-address %s"/d %s' % (ip, conf_path)
6767
ret = os.system(cmd)
6868
if ret != 0:
69-
print "Command %s failed" % cmd
69+
print("Command %s failed" % cmd)
7070
unlock()
7171
return 1
7272

7373
cmd = 'sed -i /"hardware ethernet %s"/d %s' % (mac, conf_path)
7474
ret = os.system(cmd)
7575
if ret != 0:
76-
print "Command %s failed" % cmd
76+
print("Command %s failed" % cmd)
7777
unlock()
7878
return 1
7979

@@ -84,14 +84,14 @@ def insert_host_entry(mac, ip, hostname, dns, gateway, next_server):
8484
cmd = '''echo '%s' >> %s''' % (entry, conf_path)
8585
ret = os.system(cmd)
8686
if ret != 0:
87-
print "Command %s failed" % cmd
87+
print("Command %s failed" % cmd)
8888
unlock()
8989
return 1
9090

9191
cmd = 'service dhcpd restart'
9292
ret = os.system(cmd)
9393
if ret != 0:
94-
print "Command %s failed" % cmd
94+
print("Command %s failed" % cmd)
9595
unlock()
9696
return 1
9797

@@ -102,7 +102,7 @@ def insert_host_entry(mac, ip, hostname, dns, gateway, next_server):
102102

103103
if __name__ == "__main__":
104104
if len(sys.argv) < 7:
105-
print usage
105+
print(usage)
106106
sys.exit(1)
107107

108108
mac = sys.argv[1]
@@ -115,7 +115,7 @@ def insert_host_entry(mac, ip, hostname, dns, gateway, next_server):
115115
if exists(conf_path) == False:
116116
conf_path = "/etc/dhcp/dhcpd.conf"
117117
if exists(conf_path) == False:
118-
print "Cannot find dhcpd.conf"
118+
print("Cannot find dhcpd.conf")
119119
sys.exit(1)
120120

121121
ret = insert_host_entry(mac, ip, hostname, dns, gateway, next_server)

scripts/network/ping/prepare_kickstart_bootfile.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ def prepare():
6262
f.write(stuff)
6363
f.close()
6464
return 0
65-
except Exception, e:
66-
print e
65+
except Exception as e:
66+
print(e)
6767
return 1
6868

6969

7070
if __name__ == "__main__":
7171
if len(sys.argv) < 7:
72-
print "Usage: prepare_kickstart_bootfile.py tftp_dir mac kernel initrd ks_file ks_device"
72+
print("Usage: prepare_kickstart_bootfile.py tftp_dir mac kernel initrd ks_file ks_device")
7373
exit(1)
7474

7575
(tftp_dir, mac, kernel, initrd, ks_file, ks_device) = sys.argv[1:]

scripts/network/ping/prepare_kickstart_kernel_initrd.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
copy_to = None
2727

2828
def cmd(cmdstr, err=True):
29-
print cmdstr
29+
print(cmdstr)
3030
if os.system(cmdstr) != 0 and err:
3131
raise Exception("Failed to run shell command: %s" % cmdstr)
3232

@@ -36,7 +36,7 @@ def prepare():
3636
k = os.path.join(copy_to, "vmlinuz")
3737
i = os.path.join(copy_to, "initrd.img")
3838
if os.path.exists(k) and os.path.exists(i):
39-
print "Having template(%s) prepared already, skip copying" % copy_to
39+
print("Having template(%s) prepared already, skip copying" % copy_to)
4040
return 0
4141
else:
4242
if not os.path.exists(copy_to):
@@ -61,14 +61,14 @@ def copy_from_nfs(src, dst):
6161

6262
copy_from_nfs(kernel, copy_to)
6363
copy_from_nfs(initrd, copy_to)
64-
except Exception, e:
65-
print e
64+
except Exception as e:
65+
print(e)
6666
return 1
6767

6868
if __name__ == "__main__":
6969
if len(sys.argv) < 4:
70-
print "Usage: prepare_kickstart_kerneal_initrd.py path_to_kernel path_to_initrd path_kernel_initrd_copy_to"
71-
sys.exit(1)
70+
print("Usage: prepare_kickstart_kerneal_initrd.py path_to_kernel path_to_initrd path_kernel_initrd_copy_to")
71+
sys.exit(1)
7272

7373
(kernel, initrd, copy_to) = sys.argv[1:]
7474
sys.exit(prepare())

scripts/network/ping/prepare_tftp_bootfile.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ def prepare(is_restore):
7272
f.write(stuff)
7373
f.close()
7474
return 0
75-
except Exception, e:
76-
print e
75+
except Exception as e:
76+
print(e)
7777
return 1
7878

7979

8080
if __name__ == "__main__":
8181
if len(sys.argv) < 12:
82-
print "Usage: prepare_tftp_bootfile.py tftp_dir mac cifs_server share directory image_to_restor cifs_username cifs_password ip netmask gateway"
82+
print("Usage: prepare_tftp_bootfile.py tftp_dir mac cifs_server share directory image_to_restor cifs_username cifs_password ip netmask gateway")
8383
exit(1)
8484

8585
(cmd, tftp_dir, mac, cifs_server, share, directory, template_dir, cifs_username, cifs_password, ip, netmask, gateway) = sys.argv[1:]
@@ -89,7 +89,7 @@ def prepare(is_restore):
8989
elif cmd == "backup":
9090
ret = prepare(False)
9191
else:
92-
print "Unknown cmd: %s"%cmd
92+
print("Unknown cmd: %s"%cmd)
9393
ret = 1
9494

9595
exit(ret)

scripts/storage/secondary/cloud-install-sys-tmplt.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import argparse
2121
import sys
22-
import urllib
22+
import urllib.request, urllib.parse, urllib.error
2323
import uuid
2424
import subprocess
2525
import os
@@ -90,7 +90,7 @@ def populateOptions(self):
9090
self.databaseuserpassword = ""
9191
if self.args.templatesuffix:
9292
self.templatesuffix = self.args.templatesuffix
93-
print 'Password for DB: %s'%self.databaseuserpassword
93+
print('Password for DB: %s'%self.databaseuserpassword)
9494

9595
def errorAndExit(self, msg):
9696
err = '''\n\nWe apologize for below error:
@@ -117,11 +117,11 @@ def runCmd(self, cmds):
117117

118118
def runMysql(self, query):
119119
try:
120-
print 'Running Query: %s' % query
120+
print('Running Query: %s' % query)
121121
mysqlCmds = ['mysql', '--user=%s'%self.databaseusername, '--host=%s'%self.databasehostname, '--password=%s'%self.databaseuserpassword, '--skip-column-names', '-U', 'cloud', '-e "%s"'%query]
122122
templateId = self.runCmd(mysqlCmds)
123-
print 'TemplateId is : %s' % templateId
124-
except Exception, e:
123+
print('TemplateId is : %s' % templateId)
124+
except Exception as e:
125125
err = '''Encountering an error when executing mysql script\n%s''' % str(e)
126126
self.errorAndExit(err)
127127
return templateId
@@ -137,9 +137,9 @@ def fetchTemplateDetails(self):
137137

138138
def downloadTemplate(self):
139139
self.systemvmtemplatepath = self.templateName + "." + self.fileextension
140-
print 'Downloading template from %s To %s' % (self.systemvmtemplateurl, self.systemvmtemplatepath)
140+
print('Downloading template from %s To %s' % (self.systemvmtemplateurl, self.systemvmtemplatepath))
141141
try:
142-
templateFileDownloadUrl = urllib.urlretrieve(self.systemvmtemplateurl, self.systemvmtemplatepath, reporthook=self.report)
142+
templateFileDownloadUrl = urllib.request.urlretrieve(self.systemvmtemplateurl, self.systemvmtemplatepath, reporthook=self.report)
143143
except Exception:
144144
self.errorAndExit("Failed to download template file from %s" % self.systemvmtemplateurl)
145145

@@ -150,23 +150,23 @@ def report(tmp, blocknr, blocksize, size):
150150
def installTemplate(self):
151151
destDir = self.mountpoint + os.sep + "template" + os.sep + "tmpl" + os.sep + "1" + os.sep + str(self.template)
152152
self.destDir = destDir
153-
print 'The desination Directory is : %s' % destDir
153+
print('The desination Directory is : %s' % destDir)
154154
try:
155155
if self.forcecleanup:
156156
if os.path.exists(destDir):
157157
shutil.rmtree(destDir)
158158
if not os.path.exists(destDir):
159159
os.makedirs(destDir)
160-
except Exception, e:
160+
except Exception as e:
161161
self.errorAndExit('Failed to create directories on the mounted path.. %s' % str (e))
162-
print 'Installing Template to : %s' % destDir
162+
print('Installing Template to : %s' % destDir)
163163
tmpFile = self.templateName + "." + "tmp"
164164
self.uncompressFile(tmpFile)
165-
print 'Moving the decompressed file to destination directory %s... which could take a long time, please wait' % destDir
165+
print('Moving the decompressed file to destination directory %s... which could take a long time, please wait' % destDir)
166166
shutil.move(tmpFile, destDir + os.sep + self.templateName)
167167

168168
def uncompressFile(self, fileName):
169-
print 'Uncompressing the file %s... which could take a long time, please wait' % self.systemvmtemplatepath
169+
print('Uncompressing the file %s... which could take a long time, please wait' % self.systemvmtemplatepath)
170170
if self.fileextension == 'gz':
171171
compressedFile = gzip.GzipFile(self.systemvmtemplatepath, 'rb')
172172
decompressedData = compressedFile.read()
@@ -181,7 +181,7 @@ def uncompressFile(self, fileName):
181181
decompressedFile = file(fileName, 'wb')
182182
decompressedFile.write(decompressedData)
183183
decompressedFile.close()
184-
print ''
184+
print('')
185185
elif self.fileextension == 'zip':
186186
zippedFile = zipfile.ZipFile(self.systemvmtemplatepath, 'r')
187187
zippedFiles = zippedFile.namelist()
@@ -191,7 +191,7 @@ def uncompressFile(self, fileName):
191191
decompressedFile.write(decompressedData)
192192
decompressedFile.close()
193193
zippedFile.close()
194-
print ''
194+
print('')
195195
else:
196196
self.errorAndExit('Not supported file type %s to decompress' % self.fileextension)
197197
self.fileSize = os.path.getsize(fileName)
@@ -226,10 +226,10 @@ def run(self):
226226
self.installTemplate()
227227
self.writeProperties()
228228
finally:
229-
print ''
230-
print ''
231-
print "CloudStack has successfully installed system template"
232-
print ''
229+
print('')
230+
print('')
231+
print("CloudStack has successfully installed system template")
232+
print('')
233233

234234
if __name__ == "__main__":
235235
o = InstallSysTemplate()

0 commit comments

Comments
 (0)