File: update_wbsys.py

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (350 lines) | stat: -rw-r--r-- 10,519 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/python

import subprocess
import os
import sys
import shutil
import xml.dom.minidom
import xml.dom
import uuid
import stat

WORKING_DIR = os.getcwd()

os.chdir(os.getcwd() + "/../..")

banned_files = ('README.md', 'LICENSE', '.git', 'COPYING', 'NEWS.md')
files_to_keep = ('sys_reports.js')

PROJECT_DIR = os.getcwd() + "/"
SCRIPT_DEPLOYMENT_DIR = PROJECT_DIR + "res/scripts/sys/"
MSI_FRAGMENT_FILE = PROJECT_DIR + "build/msi/source/mysql_workbench_fragment.xml"
CMAKE_FILE = PROJECT_DIR + "res/scripts/CMakeLists.txt"

clone_repo = False

def print_help():
    print 'Usage: python update_wbsys.py [Options]'
    print 'Options:'
    print '  --clone        Force to clone the repository'
    print '  --help         Prints this help'

if len(sys.argv) == 1:
    clone_repo = False
elif len(sys.argv) > 1:
    if sys.argv[1] == '--clone':
        clone_repo = True
    elif sys.argv[1] == '--help':
        print_help()
        exit(0)
    else:
        print 'Unknown option: ' + sys.argv[1]
        print_help()
        exit(0)
else:
    print_help()
    exit(0)


print "Working dir  : " + WORKING_DIR
print "Project dir  : " + PROJECT_DIR
print "MSI Frag file: " + MSI_FRAGMENT_FILE
print "CMake file   : " + CMAKE_FILE
print "Clone repo   : " + str(clone_repo)

# This class searches a MSI XML fragment file for the wbsys 
# files, so that they get installed along with WB. The fragment
# file is updated with the processed wbsys files
class XMLFileManager:
  
    # The constructor reads the fragment file and finds the right
    # component element that will contain the wbsys files
        
    def __init__(self):
        self.directories = dict()
        self.files = dict()
        self.fileCount = 0
        
        self.xml_document = xml.dom.minidom.parse(MSI_FRAGMENT_FILE)
    
        self.root = self.xml_document.documentElement
        directory_elements = self.root.getElementsByTagName("Directory")
  
        for directory_element in directory_elements:
    
            if directory_element.getAttribute("Id") == "sys_dir":
                component = directory_element.getElementsByTagName("Component").item(0)
                
                indent = directory_element.parentNode.firstChild

                # Reuse the base node, so that we have the same ID
                while component.firstChild != None:
                    component.removeChild(component.firstChild)
                    
                while directory_element.firstChild != None:
                    directory_element.removeChild(directory_element.firstChild)
                
                directory_element.appendChild(indent.cloneNode(False))
                
                self.appendNodeChild(component, directory_element)
                
                self.directories['sys'] = directory_element
                
                break

    
    def appendNodeChild(self, child, parent):
      
      parentIndent = parent.lastChild
      
      childIndent = parentIndent.cloneNode(False)
      childIndent.nodeValue = childIndent.nodeValue + '  '
      
      parent.insertBefore(childIndent.cloneNode(False), parent.lastChild)
      parent.insertBefore(child, parent.lastChild)

      ## Don't create child indentation
      if child.tagName == 'File':
        return

      if child.hasChildNodes() == False:
        child.appendChild(childIndent.cloneNode(False))
      elif child.lastChild.nodeType != child.TEXT_NODE:
        child.appendChild(childIndent.cloneNode(False))
      else:
        print 'Node already as indentation'
        
        
    def add_directory(self, directory):
        
        parent_dir = os.path.dirname(directory)
        current_dir = os.path.basename(directory)
        
        #parent_node = self.directories[parent_dir]
        entryId = directory.replace('/', '_')
        
        newEntry = self.xml_document.createElement("Directory")
        newEntry.setAttribute("Id", entryId + '_dir')
        newEntry.setAttribute("Name", current_dir)

        self.appendNodeChild(newEntry, self.directories[parent_dir])        
        self.directories[directory] = newEntry
      
      
    def add_file(self, directory, filename):
        self.fileCount = self.fileCount + 1
        
        newFile = self.xml_document.createElement("File")
        
        newFile.setAttribute('Id', 'wbsys' + '%03d' % self.fileCount)
        newFile.setAttribute('Name', filename)
        
        component = self.directories[directory].getElementsByTagName("Component").item(0)
        
        if component == None:
            component = self.xml_document.createElement("Component")
            component.setAttribute('DiskId', '1')
            component.setAttribute('Guid', str(uuid.uuid1()).upper())
            component.setAttribute('Id', directory.replace('/', '_'))
            component.setAttribute('Location', 'either')
            self.appendNodeChild(component, self.directories[directory])
        
        self.appendNodeChild(newFile, component)
    
    # Overwrites the original XML file
    def write_file(self):

        self.directories['sys'].normalize() 
        for node in self.directories:
          self.directories[node] = self.directories[node].normalize()

        text = self.xml_document.toxml()

        f = open(MSI_FRAGMENT_FILE, mode='w')
        f.write(text.replace("\r\n", "\n"))
        f.close()
           



# This class deals with a CMakeLists.txt file in order
# to setup the proper wbsys files to install.
class CMakeFileManager:
    file_lines_before = None
    file_lines = None
    file_lines_after = None
    state = 0
    
    # The constructor read the file into line arrays, so
    # They can be writen again properly. The automates files
    # are always discarted, and new ones are set during the
    # process.
    def __init__(self):
        with open(CMAKE_FILE) as f:
            self.file_lines_before = []
            self.file_lines_after = []
            self.file_lines = dict()
            
            for line in f:
                if self.state == 0:
                    self.file_lines_before.append(line)
                    if line.startswith("# SYS_BEGIN"):
                        self.state = 1
                elif self.state == 1:
                    if line.startswith("# SYS_END"):
                        self.file_lines_after.append(line)
                        self.state = 2
                else:
                    self.file_lines_after.append(line)

    def add_file(self, filename):
        if not os.path.dirname(filename) in self.file_lines:
            self.file_lines[os.path.dirname(filename)] = []
            
        self.file_lines[os.path.dirname(filename)].append(filename)
        
    def write_file(self):
        # Overwrite the original file
        f = open(CMAKE_FILE, mode='w')
        f.writelines(self.file_lines_before)
        
        for cmake_dir in self.file_lines:
            file_tag = cmake_dir.replace('/', '_').upper() + '_FILES'
            f.write('\nset(' + file_tag + '\n')
        
            for file in self.file_lines[cmake_dir]:
                f.write('    ' + file + '\n')
            
            f.write(')\n\n')
            f.write('install(FILES ${%s} DESTINATION ${WB_PACKAGE_SHARED_DIR}/%s)\n' % (file_tag, cmake_dir))
        
        f.writelines(self.file_lines_after)
        f.close()
        

def remove_repository_files(dest):
    entries = os.listdir(dest)

    current_path = os.getcwd()
    os.chdir(dest)

    for entry in entries:
        if entry in files_to_keep:
            continue
                
        if os.path.isfile(entry):
            os.remove(entry)
        else:
            shutil.rmtree(entry)
    
    os.chdir(current_path)
    
    
def recursive_copy(src, dest):
    if os.path.isdir(src):
      
        # Create dir if it doesn't exist in the destination dir
        if not os.path.isdir(dest):
            os.makedirs(dest)
            
        files = os.listdir(src)

        for file in files:
            recursive_copy(os.path.join(src, file), os.path.join(dest, file))
    else:
        shutil.copyfile(src, dest)
        subprocess.Popen(["sed", "-i", "s/\r//g", dest])
        st = os.stat(dest)
        os.chmod(dest, st.st_mode | stat.S_IEXEC)
        
        
def iterate_path(path):
  
  os.chdir(os.path.basename(path))
  current_path = os.getcwd()

  entries = sorted(os.listdir(os.curdir))

  # This would print all the files and directories
  for entry in entries:
    
      full_entry = os.path.join(path, entry)
    
      if entry in banned_files:
        continue
    
      if os.path.isdir(entry):          
          xml_manager.add_directory(full_entry)
          
          iterate_path(full_entry)
          
          os.chdir(current_path)
      else: 
          xml_manager.add_file(path, entry)
          cmake_manager.add_file(full_entry)
          #print entry          
  
  
  
  
  

# TODO: Remove this when done
os.chdir(WORKING_DIR)
print "Preparing...please be patient..."

if clone_repo == True:
    print "  - Checking git proxy configuration..."
    proc = subprocess.Popen(["git", "config", "--global", "http.proxy"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    out, err = proc.communicate()

    if not out.strip():
        print "    - WARNING: No proxy configured. Try using git config --global http.proxy <proxy>"
    else:
        print "    - Using proxy: " + out.strip()


    print "  - Cloning remote repository..."  

    os.chdir(WORKING_DIR)
    if (os.path.isdir("mysql-sys")):
        shutil.rmtree("mysql-sys")

    proc = subprocess.Popen(["git", "clone", "https://github.com/MarkLeith/mysql-sys"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    out, err = proc.communicate()

    if proc.returncode > 0:
        print "    - ERROR[" + str(proc.returncode) + "] - There was an error on 'git clone'"
        print err
        sys.exit(1)





print "Processing files..."

xml_manager = XMLFileManager()
cmake_manager = CMakeFileManager()

if clone_repo == True:
    if os.path.isdir('sys') == False:
        os.makedirs('sys')
    remove_repository_files('sys')
    recursive_copy('mysql-sys', 'sys')

if os.path.isdir('sys') == False:
    print 'Error: The sys directory does not exist. Please run this script using the --clone parameter.'
    exit(1)

if os.path.isdir('sys/.git'):
    shutil.rmtree(os.path.join('sys', '.git'))

iterate_path('sys')

xml_manager.write_file()
cmake_manager.write_file()