Browse code

installer:

-- remove unused shebangs.
-- interger division with operator //
-- replace Set with set.
-- replace `` with repr.
-- replace has_key with in

Change-Id: I8ef92b42cb1a4477eb8be23da6a86499a3436876
Reviewed-on: http://photon-jenkins.eng.vmware.com:8082/3860
Tested-by: gerrit-photon <photon-checkins@vmware.com>
Reviewed-by: Bo Gan <ganb@vmware.com>

xiaolin-vmware authored on 2017/09/27 06:29:39
Showing 25 changed files
... ...
@@ -1,4 +1,3 @@
1
-#! /usr/bin/python2
2 1
 #
3 2
 #    Copyright (C) 2015 vmware inc.
4 3
 #
... ...
@@ -9,10 +8,6 @@ class Action(object):
9 9
 
10 10
     def do_action(self, params):
11 11
         raise NameError('Abstract method, this should be implemented in the child class')
12
-    
12
+
13 13
     def hide(self, params):
14 14
         raise NameError('Abstract method, this should be implemented in the child class')
15
-
16
-
17
-    
18
-    
19 15
\ No newline at end of file
... ...
@@ -1,4 +1,3 @@
1
-#! /usr/bin/python2
2 1
 #
3 2
 #    Copyright (C) 2015 vmware inc.
4 3
 #
... ...
@@ -10,5 +9,3 @@ class ActionResult(object):
10 10
     def __init__(self, success, result):
11 11
         self.success = success
12 12
         self.result = result
13
-
14
-    
15 13
\ No newline at end of file
... ...
@@ -1,4 +1,3 @@
1
-#!/usr/bin/python2
2 1
 #
3 2
 #    Copyright (C) 2015 vmware inc.
4 3
 #
... ...
@@ -1,4 +1,3 @@
1
-#! /usr/bin/python2
2 1
 #
3 2
 #    Copyright (C) 2015 vmware inc.
4 3
 #
... ...
@@ -19,7 +19,6 @@ from jsonwrapper import JsonWrapper
19 19
 from progressbar import ProgressBar
20 20
 from window import Window
21 21
 from actionresult import ActionResult
22
-from __builtin__ import isinstance
23 22
 
24 23
 class Installer(object):
25 24
     def __init__(self, install_config, maxy = 0, maxx = 0, iso_installer = False,
... ...
@@ -57,10 +56,10 @@ class Installer(object):
57 57
             self.progress_padding = 5
58 58
 
59 59
             self.progress_width = self.width - self.progress_padding
60
-            self.starty = (self.maxy - self.height) / 2
61
-            self.startx = (self.maxx - self.width) / 2
60
+            self.starty = (self.maxy - self.height) // 2
61
+            self.startx = (self.maxx - self.width) // 2
62 62
             self.window = Window(self.height, self.width, self.maxy, self.maxx, 'Installing Photon', False, items =[])
63
-            self.progress_bar = ProgressBar(self.starty + 3, self.startx + self.progress_padding / 2, self.progress_width)
63
+            self.progress_bar = ProgressBar(self.starty + 3, self.startx + self.progress_padding // 2, self.progress_width)
64 64
 
65 65
         signal.signal(signal.SIGINT, self.exit_gracefully)
66 66
 
... ...
@@ -152,6 +151,7 @@ class Installer(object):
152 152
                         if output.startswith(prefix):
153 153
                             package = output[len(prefix):].rstrip('\n')
154 154
                             self.progress_bar.increment(packages_to_install[package])
155
+
155 156
                         self.progress_bar.update_message(output)
156 157
                 # 0 : succeed; 137 : package already installed; 65 : package not found in repo.
157 158
                 if retval != 0 and retval != 137:
... ...
@@ -1,4 +1,3 @@
1
-#! /usr/bin/python2
2 1
 #
3 2
 #    Copyright (C) 2015 vmware inc.
4 3
 #
... ...
@@ -109,7 +109,7 @@ class IsoInstaller(object):
109 109
     def validate_password(text):
110 110
         try:
111 111
             p = cracklib.VeryFascistCheck(text)
112
-        except ValueError, message:
112
+        except ValueError as message:
113 113
             p = str(message)
114 114
         return p == text, "Error: " + p
115 115
 
... ...
@@ -1,4 +1,3 @@
1
-#!/usr/bin/python2
2 1
 #
3 2
 #    Copyright (C) 2015 vmware inc.
4 3
 #
... ...
@@ -1,4 +1,3 @@
1
-#! /usr/bin/python2
2 1
 #
3 2
 #    Copyright (C) 2015 vmware inc.
4 3
 #
... ...
@@ -15,8 +14,8 @@ class License(object):
15 15
         self.win_width = maxx - 4
16 16
         self.win_height = maxy - 4
17 17
 
18
-        self.win_starty = (self.maxy - self.win_height) / 2
19
-        self.win_startx = (self.maxx - self.win_width) / 2
18
+        self.win_starty = (self.maxy - self.win_height) // 2
19
+        self.win_startx = (self.maxx - self.win_width) // 2
20 20
 
21 21
         self.text_starty = self.win_starty + 4
22 22
         self.text_height = self.win_height - 6
... ...
@@ -31,7 +30,7 @@ class License(object):
31 31
                                 ]
32 32
 
33 33
         title = 'VMWARE 2.0 LICENSE AGREEMENT'
34
-        self.window.addstr(0, (self.win_width - len(title)) / 2, title)
34
+        self.window.addstr(0, (self.win_width - len(title)) // 2, title)
35 35
         self.text_pane = TextPane(self.text_starty, self.maxx, self.text_width, "EULA.txt", self.text_height, accept_decline_items)
36 36
 
37 37
         self.window.set_action_panel(self.text_pane)
... ...
@@ -44,6 +43,3 @@ class License(object):
44 44
 
45 45
     def exit_function(self):
46 46
         exit(0)
47
-
48
-
49
-
... ...
@@ -1,4 +1,3 @@
1
-#! /usr/bin/python2
2 1
 #
3 2
 #    Copyright (C) 2015 vmware inc.
4 3
 #
... ...
@@ -7,7 +6,6 @@
7 7
 import curses
8 8
 from actionresult import ActionResult
9 9
 from action import Action
10
-from sets import Set
11 10
 
12 11
 class Menu(Action):
13 12
     def __init__(self, starty, maxx, items, height = 0, selector_menu = False, can_navigate_outside = True, horizontal = False, default_selected = 0, save_sel = False, tab_enable = True):
... ...
@@ -46,7 +44,7 @@ class Menu(Action):
46 46
         self.selector_menu = selector_menu
47 47
         if self.selector_menu:
48 48
             self.width += 4
49
-            self.selected_items = Set([])
49
+            self.selected_items = set([])
50 50
 
51 51
         if self.horizontal:
52 52
             menu_win_width = (self.width + self.horizontal_padding) * self.num_items
... ...
@@ -59,7 +57,7 @@ class Menu(Action):
59 59
         self.window.keypad(1)
60 60
         self.panel = curses.panel.new_panel(self.window)
61 61
 
62
-        self.panel.move(starty, (maxx - menu_win_width) / 2)
62
+        self.panel.move(starty, (maxx - menu_win_width) // 2)
63 63
         self.panel.hide()
64 64
         curses.panel.update_panels()
65 65
 
... ...
@@ -142,7 +140,7 @@ class Menu(Action):
142 142
                 else:
143 143
                     item = '[ ] ' + item
144 144
             if self.horizontal:
145
-                x = self.horizontal_padding / 2 + index * self.horizontal_padding
145
+                x = self.horizontal_padding // 2 + index * self.horizontal_padding
146 146
                 y = 0
147 147
             else:
148 148
                 x = 0
... ...
@@ -38,7 +38,7 @@ def partition_disk(disk, partitions):
38 38
     output = open(os.devnull, 'w')
39 39
 
40 40
     # Clear the disk
41
-    process = subprocess.Popen(['sgdisk', '-o', '-g', disk], stdout = output)
41
+    process = subprocess.Popen(['sgdisk', '-o', '-g', disk], stderr = output, stdout = output)
42 42
     retval = process.wait()
43 43
     if retval != 0:
44 44
         log(LOG_ERROR, "Failed clearing disk {0}".format(disk))
... ...
@@ -72,23 +72,23 @@ def partition_disk(disk, partitions):
72 72
         prefix = ''
73 73
         if 'nvme' in disk:
74 74
             prefix = 'p'
75
-        partition['path'] = disk + prefix + `partition_number`
75
+        partition['path'] = disk + prefix + repr(partition_number)
76 76
         partition_number = partition_number + 1
77 77
 
78 78
     # Adding the last extendible partition
79 79
     if extensible_partition:
80
-        partition_cmd.extend(['-n', `extensible_partition['partition_number']`])
80
+        partition_cmd.extend(['-n', repr(extensible_partition['partition_number'])])
81 81
 
82 82
     partition_cmd.extend(['-p', disk])
83 83
 
84 84
     # Run the partitioning command
85
-    process = subprocess.Popen(partition_cmd, stdout = output)
85
+    process = subprocess.Popen(partition_cmd, stderr = output, stdout = output)
86 86
     retval = process.wait()
87 87
     if retval != 0:
88 88
         log(LOG_ERROR, "Faild partition disk, command: {0}". format(partition_cmd))
89 89
         return None
90 90
 
91
-    process = subprocess.Popen(['sgdisk', '-t1' + grub_flag, disk], stdout = output)
91
+    process = subprocess.Popen(['sgdisk', '-t1' + grub_flag, disk], stderr = output, stdout = output)
92 92
     retval = process.wait()
93 93
     if retval != 0:
94 94
         log(LOG_ERROR, "Failed to setup grub partition")
... ...
@@ -105,13 +105,14 @@ def partition_disk(disk, partitions):
105 105
                 partitions_data['boot_partition_number'] = partition['partition_number']
106 106
                 partitions_data['bootdirectory'] = '/'
107 107
         if partition['filesystem'] == "swap":
108
-            process = subprocess.Popen(['mkswap', partition['path']], stdout = output)
108
+            process = subprocess.Popen(['mkswap', partition['path']], stderr = output, stdout = output)
109 109
             retval = process.wait()
110 110
             if retval != 0:
111 111
                 log(LOG_ERROR, "Failed to create swap partition @ {}".format(partition['path']))
112 112
                 return None
113 113
         else:
114
-            process = subprocess.Popen(['mkfs', '-t', partition['filesystem'], partition['path']], stdout = output)
114
+            mkfs_cmd = ['mkfs', '-t', partition['filesystem'], partition['path']]
115
+            process = subprocess.Popen(mkfs_cmd, stderr = output, stdout = output)
115 116
             retval = process.wait()
116 117
             if retval != 0:
117 118
                 log(LOG_ERROR, "Failed to format {} partition @ {}".format(partition['filesystem'], partition['path']))
... ...
@@ -137,7 +138,7 @@ def replace_string_in_file(filename,  search_string,  replace_string):
137 137
 
138 138
     with open(filename, "w") as destination:
139 139
         for line in lines:
140
-            destination.write(re.sub(search_string,  replace_string,  line))
140
+            destination.write(re.sub(search_string, replace_string, line))
141 141
 
142 142
 def log(type, message):
143 143
     command = 'systemd-cat echo \"<{}> {} : {}\"'.format(type, LOG_LEVEL_DESC[type], message)
... ...
@@ -16,7 +16,7 @@ def execute(name, config, root):
16 16
     with open(script_file,  'wb') as outfile:
17 17
         outfile.write("\n".join(script))
18 18
 
19
-    os.chmod(script_file, 0700);
19
+    os.chmod(script_file, 0o700);
20 20
     with open(commons.KS_POST_INSTALL_LOG_FILE_NAME,"w") as logfile:
21 21
         process = subprocess.Popen(["./mk-run-chroot.sh", '-w', root, "/etc/tmpfiles.d/postinstall.sh"],
22 22
             stdout=logfile,stderr=logfile)
... ...
@@ -9,9 +9,9 @@ def execute(name, config, root):
9 9
     hostname = config['hostname']
10 10
 
11 11
     hostname_file = os.path.join(root, 'etc/hostname')
12
-    hosts_file = os.path.join(root, 'etc/hosts')
12
+    hosts_file    = os.path.join(root, 'etc/hosts')
13 13
 
14
-    with open(hostname_file,  'wb') as outfile:
14
+    with open(hostname_file, 'wb') as outfile:
15 15
         outfile.write(hostname)
16 16
 
17 17
     commons.replace_string_in_file(hosts_file, r'127\.0\.0\.1\s+localhost\s*\Z', '127.0.0.1\tlocalhost\n127.0.0.1\t' + hostname)
... ...
@@ -18,7 +18,7 @@ def execute(name, config, root):
18 18
         os.makedirs(authorized_keys_dir)
19 19
     with open(authorized_keys_filename, "a") as destination:
20 20
         destination.write(config['public_key'] + "\n")
21
-    os.chmod(authorized_keys_filename, 0600)
21
+    os.chmod(authorized_keys_filename, 0o600)
22 22
 
23 23
     # Change the sshd config to allow root login
24 24
     process = subprocess.Popen(["sed", "-i", "s/^\\s*PermitRootLogin\s\+no/PermitRootLogin yes/", sshd_config_filename])
... ...
@@ -1,4 +1,3 @@
1
-#!/usr/bin/python2
2 1
 #
3 2
 #    Copyright (C) 2015 vmware inc.
4 3
 #
... ...
@@ -1,4 +1,3 @@
1
-#!/usr/bin/python2
2 1
 #
3 2
 #    Copyright (C) 2015 vmware inc.
4 3
 #
... ...
@@ -1,4 +1,3 @@
1
-#!/usr/bin/python2
2 1
 #
3 2
 #    Copyright (C) 2015 vmware inc.
4 3
 #
... ...
@@ -7,7 +6,6 @@
7 7
 import json
8 8
 import os
9 9
 import curses
10
-from sets import Set
11 10
 from jsonwrapper import JsonWrapper
12 11
 from menu import Menu
13 12
 from window import Window
... ...
@@ -21,8 +19,8 @@ class PackageSelector(object):
21 21
         self.win_width = 50
22 22
         self.win_height = 13
23 23
 
24
-        self.win_starty = (self.maxy - self.win_height) / 2
25
-        self.win_startx = (self.maxx - self.win_width) / 2
24
+        self.win_starty = (self.maxy - self.win_height) // 2
25
+        self.win_startx = (self.maxx - self.win_width) // 2
26 26
 
27 27
         self.menu_starty = self.win_starty + 3
28 28
 
... ...
@@ -50,7 +48,7 @@ class PackageSelector(object):
50 50
         additional_files = []
51 51
         for install_option in options:
52 52
             if install_option[0] == config_type:
53
-                if install_option[1].has_key("additional-files"):
53
+                if "additional-files" in install_option[1]:
54 54
                     additional_files = install_option[1]["additional-files"]
55 55
                 break
56 56
         return additional_files
... ...
@@ -112,7 +112,7 @@ def create_additional_file_list_to_copy_in_iso(base_path, build_install_option):
112 112
     options_sorted = option_list_json.items()
113 113
     file_list = []
114 114
     for install_option in options_sorted:
115
-        if install_option[1].has_key("additional-files"):
115
+        if "additional-files" in install_option[1]:
116 116
             file_list = file_list + map(lambda filename: os.path.join(base_path, filename), install_option[1].get("additional-files"))
117 117
     return file_list
118 118
 
... ...
@@ -122,7 +122,7 @@ def get_live_cd_status_string(build_install_option):
122 122
     options_sorted = option_list_json.items()
123 123
     file_list = []
124 124
     for install_option in options_sorted:
125
-        if install_option[1].has_key("live-cd"):
125
+        if "live-cd" in install_option[1]:
126 126
             if install_option[1].get("live-cd") == True:
127 127
                 return "true"
128 128
     return "false"
... ...
@@ -212,7 +212,7 @@ if __name__ == '__main__':
212 212
             config = (JsonWrapper(options.configfile)).read()
213 213
             config['disk'], success = create_vmdk_and_partition(config, options.vmdk_path)
214 214
             if not success:
215
-                print "Unexpected failure, please check the logs"
215
+                print("Unexpected failure, please check the logs")
216 216
                 sys.exit(1)
217 217
 
218 218
             config['iso_system'] = False
... ...
@@ -1,4 +1,3 @@
1
-#! /usr/bin/python2
2 1
 #
3 2
 #    Copyright (C) 2015 vmware inc.
4 3
 #
... ...
@@ -85,8 +84,8 @@ class ProgressBar(object):
85 85
     def render_progress(self):
86 86
         if self.num_items == 0:
87 87
             return
88
-        completed = self.progress * 100 / self.num_items
89
-        completed_width = completed * self.width / 100
88
+        completed = self.progress * 100 // self.num_items
89
+        completed_width = completed * self.width // 100
90 90
         completed_str, remaining_str = self.get_spaces(completed_width, self.width, completed)
91 91
 
92 92
         self.window.addstr(0, 0, completed_str, curses.color_pair(3))
... ...
@@ -162,7 +161,7 @@ class ProgressBar(object):
162 162
     def get_spaces(self, completed_width, total_width, per):
163 163
         per = str(per) + '%'
164 164
 
165
-        start = (total_width + 2 - len(per)) / 2
165
+        start = (total_width + 2 - len(per)) // 2
166 166
         end = start + len(per)
167 167
 
168 168
         index = 0
... ...
@@ -1,4 +1,3 @@
1
-#! /usr/bin/python2
2 1
 #
3 2
 #    Copyright (C) 2015 vmware inc.
4 3
 #
... ...
@@ -1,4 +1,3 @@
1
-#! /usr/bin/python2
2 1
 #
3 2
 #    Copyright (C) 2015 vmware inc.
4 3
 #
... ...
@@ -1,4 +1,3 @@
1
-#! /usr/bin/python2
2 1
 #
3 2
 #    Copyright (C) 2015 vmware inc.
4 3
 #
... ...
@@ -22,14 +21,14 @@ class SelectDisk(object):
22 22
         self.win_width = 70
23 23
         self.win_height = 16
24 24
 
25
-        self.win_starty = (self.maxy - self.win_height) / 2
26
-        self.win_startx = (self.maxx - self.win_width) / 2
25
+        self.win_starty = (self.maxy - self.win_height) // 2
26
+        self.win_startx = (self.maxx - self.win_width) // 2
27 27
 
28 28
         self.menu_starty = self.win_starty + 6
29 29
         self.menu_height = 5
30 30
         self.progress_padding = 5
31 31
         self.progress_width = self.win_width - self.progress_padding
32
-        self.progress_bar = ProgressBar(self.win_starty + 6, self.win_startx + self.progress_padding / 2, self.progress_width, new_win=True)
32
+        self.progress_bar = ProgressBar(self.win_starty + 6, self.win_startx + (self.progress_padding // 2), self.progress_width, new_win=True)
33 33
 
34 34
         self.disk_buttom_items = []
35 35
         self.disk_buttom_items.append(('<Custom>', self.custom_function, False))
... ...
@@ -47,7 +46,7 @@ class SelectDisk(object):
47 47
 
48 48
         menu_height = 9
49 49
         menu_width = 40
50
-        menu_starty = (self.maxy - menu_height) / 2 + 5
50
+        menu_starty = (self.maxy - menu_height) // 2 + 5
51 51
         self.install_config['delete_partition'] = True
52 52
         confrim_window = ConfirmWindow(menu_height, menu_width, self.maxy, self.maxx, menu_starty, 'This will erase the disk.\nAre you sure?')
53 53
         confirmed = confrim_window.do_action().result['yes']
... ...
@@ -57,7 +57,7 @@ class TextPane(Action):
57 57
         self.window.keypad(1)
58 58
         self.panel = curses.panel.new_panel(self.window)
59 59
 
60
-        self.panel.move(starty, (maxx - self.width) / 2)
60
+        self.panel.move(starty, (maxx - self.width) // 2)
61 61
         self.panel.hide()
62 62
         curses.panel.update_panels()
63 63
 
... ...
@@ -1,4 +1,3 @@
1
-#!/usr/bin/python2
2 1
 #
3 2
 #    Copyright (C) 2015 vmware inc.
4 3
 #
... ...
@@ -15,8 +14,8 @@ class Window(Action):
15 15
         self.can_go_next = can_go_next
16 16
         self.height = height
17 17
         self.width = width;
18
-        self.y = (maxy - height) / 2
19
-        self.x = (maxx - width ) / 2
18
+        self.y = (maxy - height) // 2
19
+        self.x = (maxx - width ) // 2
20 20
         title = ' ' + title + ' '
21 21
 
22 22
         self.contentwin = curses.newwin(height - 1, width -1)
... ...
@@ -29,7 +28,7 @@ class Window(Action):
29 29
         self.position = position
30 30
         self.items = items
31 31
         self.menu_helper = menu_helper
32
-        self.contentwin.addstr(0, (width - 1 - len(title)) / 2 , title)#
32
+        self.contentwin.addstr(0, (width - 1 - len(title)) // 2 , title)#
33 33
         newy = 5;
34 34
 
35 35
         if self.can_go_back:
... ...
@@ -48,7 +47,7 @@ class Window(Action):
48 48
             for item in self.items:
49 49
                 self.dist-=len(item[0])
50 50
                 count+=1
51
-            self.dist=self.dist/count
51
+            self.dist=self.dist//count
52 52
             self.contentwin.keypad(1)
53 53
             newy += len('<Go Back>')
54 54
             newy += self.dist
... ...
@@ -1,4 +1,3 @@
1
-#!/usr/bin/python2
2 1
 #
3 2
 #    Copyright (C) 2015 vmware inc.
4 3
 #