Fixed when user tap on linkable contents, somtimes focus ring is not shown.
[framework/web/webkit-efl.git] / TizenScripts / tizen-dev-tool
1 #!/usr/bin/python
2
3 import argparse
4 import bz2
5 import ftplib
6 import getpass
7 import os
8 import os.path
9 import string
10 import sqlite3
11 import subprocess
12 import sys
13 import urllib2
14
15 from xml.dom.minidom import parseString
16 from types import *
17
18 this='tizen-dev-tool'
19 tool_base_dir='/var/tmp/tizen-dev-tool'
20
21 def updateOsceBuildRoot(arch):
22     os.environ['OSCE_BUILD_ROOT'] = tool_base_dir + '/build_root_%s' % arch
23
24 def getOsceBuildRoot():
25     return os.environ['OSCE_BUILD_ROOT']
26
27 def getOsceArchType(arch):
28     if arch == 'ia32':
29         return 'i586'
30     else:
31         return 'armv7el'
32
33
34 def getRepoInformation(repo_type, snapshot_base_url, arch):
35     information = {}
36     information['repo_url'] = snapshot_base_url + '/repos/' + repo_type
37     information['bin'] = {}
38     information['bin']['db_file_name'] = repo_type + '.sqlite'
39     information['bin']['base_url'] = information['repo_url'] + '/' + arch + '/packages'
40     information['dbg'] = {}
41     information['dbg']['db_file_name'] = repo_type + '-dbg.sqlite'
42     information['dbg']['base_url'] = information['repo_url'] + '/' + arch + '/debug'
43     return information
44
45 def guessReleaseTypeWithSnapshotID(snapshot_id):
46     key = snapshot_id.split('_')[0]
47     if key == 'SLPRelease':
48         return 'release'
49     elif key == 'SLP':
50         return 'standard'
51     elif key == 'tizen-SDK':
52         return 'SDK'
53     else:
54         print 'Cannot guess release type with ' + snapshot_id
55         print 'Please update guessReleaseTypeWithSnapshotID() function in ' + this + ' script'
56         sys.exit()
57
58 def getInformation(snapshot_id, release_type, arch):
59     information = {}
60     information['snapshot_id'] = snapshot_id
61     if not release_type:
62         release_type = guessReleaseTypeWithSnapshotID(snapshot_id)
63     information['release_type'] = release_type
64     information['base_dir'] = '%s/%s' % (snapshot_id, arch)
65     information['base_dir_abs'] = tool_base_dir + '/' + information['base_dir']
66     if not os.path.exists(information['base_dir_abs']):
67         os.makedirs(information['base_dir_abs'])
68     if release_type == 'SDK':
69         information['server'] = 'download.tizendev.org'
70         information['snapshot_base_url'] = 'https://%s/snapshots/SDK/common/%s' % (information['server'], information['snapshot_id'])
71         information['repo_type_list'] = ['tizen-base', 'tizen-main']
72         information['auth'] = { 'user': None, 'pass': None }
73     else:
74         information['server'] = 'slp-build.sec.samsung.net'
75         information['snapshot_base_url'] = 'http://%s:8008/snapshots/%s/%s' % (information['server'], information['release_type'], information['snapshot_id'])
76         information['repo_type_list'] = ['slp-base', 'slp-system', 'slp-%s' % information['release_type']]
77         information['auth'] = None
78
79     for repo_type in information['repo_type_list']:
80         information[repo_type] = getRepoInformation(repo_type, information['snapshot_base_url'], arch)
81     return information
82
83 def needAuth(information):
84     return information['auth']
85
86 def getUser(information):
87     if not information['auth']:
88         return None
89     if not information['auth']['user']:
90         print information['server']
91         information['auth']['user'] = raw_input('user : ')
92         information['auth']['pass'] = getpass.getpass('password: ')
93     return information['auth']['user']
94
95 def getPass(information):
96     if not information['auth']:
97         return None
98     if not information['auth']['user']:
99         print information['server']
100         information['auth']['user'] = raw_input('user : ')
101         information['auth']['pass'] = getpass.getpass('password: ')
102     return information['auth']['pass']
103
104 def getDBFilePath(information, repo_type, pkg_type):
105     return information['base_dir_abs'] + '/' + information[repo_type][pkg_type]['db_file_name']
106
107 def getDB(information, repo_type, pkg_type):
108     if needAuth(information):
109         authinfo = urllib2.HTTPPasswordMgrWithDefaultRealm()
110         authinfo.add_password(None, information['server'], getUser(information), getPass(information))
111         handler = urllib2.HTTPBasicAuthHandler(authinfo)
112         myopener = urllib2.build_opener(handler)
113         opened = urllib2.install_opener(myopener)
114     file = urllib2.urlopen(information[repo_type][pkg_type]['base_url'] + '/repodata/repomd.xml')
115     data = file.read()
116     file.close()
117     dom = parseString(data)
118     for element in dom.getElementsByTagName('data'):
119         if element.hasAttribute('type') and element.getAttribute('type') == 'primary_db':
120             db_bz2_url = information[repo_type][pkg_type]['base_url'] + '/' + element.getElementsByTagName('location')[0].getAttribute('href')
121             file = urllib2.urlopen(db_bz2_url)
122             data = file.read()
123             file.close()
124             file = open(getDBFilePath(information, repo_type, pkg_type), 'wb')
125             file.write(bz2.decompress(data))
126             file.close()
127             return True
128     return False
129
130 def downloadPackages(information, repo_type, pkg_type, package_list):
131     conn = sqlite3.connect(getDBFilePath(information, repo_type, pkg_type))
132     c = conn.cursor()
133     downloaded = set([])
134     for package in package_list:
135         c.execute('SELECT location_href, version, release, arch FROM packages WHERE name = "%s"' % package)
136         result = c.fetchone()
137
138         if result:
139             package_file_name = package + '-' + result[1] + '-' + result[2] + '.' + result[3] + '.rpm'
140             package_file_path = information['base_dir_abs'] + '/' + package_file_name
141             package_url = information[repo_type][pkg_type]['base_url'] + '/' + result[0]
142
143             if not os.path.isfile(package_file_path):
144                 print 'download ' + package_file_name
145                 if needAuth(information):
146                     command = 'curl -o%s %s -u%s:%s -k' % (package_file_path, package_url, getUser(information), getPass(information))
147                 else:
148                     command = 'curl -o%s %s -k' % (package_file_path, package_url)
149                 subprocess.call(command.split())
150
151             if os.path.isfile(package_file_path):
152                 current_dir = os.getcwd()
153                 os.chdir(information['base_dir_abs'])
154                 print 'extracting ' + package_file_name
155                 pipe = subprocess.Popen(['rpm2cpio', package_file_name], stdout=subprocess.PIPE)
156                 subprocess.Popen(['cpio', '-mid'], stdin=subprocess.PIPE).communicate(input=pipe.stdout.read())
157                 os.chdir(current_dir)
158
159             downloaded |= set([package])
160     conn.close()
161     return downloaded
162
163
164 def getPackages(snapshot_id, release_type, arch, package_list):
165     information = getInformation(snapshot_id, release_type, arch)
166
167     for repo_type in information['repo_type_list']:
168         for pkg_type in ['bin', 'dbg']:
169             if not os.path.isfile(getDBFilePath(information, repo_type, pkg_type)):
170                 if not getDB(information, repo_type, pkg_type):
171                     print 'Failed to get DB'
172                     return
173             package_list -= downloadPackages(information, repo_type, pkg_type, package_list)
174             if len(package_list) == 0:
175                 return
176
177     print 'Cannot find packages : %r' % package_list
178
179 def commandGetPkgs(program, args):
180     parser = argparse.ArgumentParser(prog=program, description='Download packages')
181     parser.add_argument('snapshot_id', action='store')
182     parser.add_argument('package_name', nargs='+')
183     parser.add_argument('-a', '--arch', choices=['armv7l', 'ia32'], default='armv7l')
184     parser.add_argument('-t', '--type', choices=['standard', 'release', 'SDK'], dest='release_type', help='release type. If this is not specified, ' + this + 'tries to guess it with snapshot_id')
185     parsedArgs = parser.parse_args(args);
186     getPackages(parsedArgs.snapshot_id, parsedArgs.release_type, parsedArgs.arch, set(parsedArgs.package_name))
187
188 def getAllPackages(dbfile):
189     conn = sqlite3.connect(dbfile)
190     c = conn.cursor()
191     pkg_list = set()
192     for row in c.execute('SELECT name, version, release, arch FROM packages'):
193         pkg_list |= set([row[0] + '-' + row[1] + '-' + row[2] + '.' + row[3] + '.rpm'])
194     return pkg_list
195
196
197 def listPackages(snapshot_id, release_type, arch):
198     information = getInformation(snapshot_id, release_type, arch)
199
200     pkg_list = set()
201     for repo_type in information['repo_type_list']:
202         for pkg_type in ['bin', 'dbg']:
203             dbfile = getDBFilePath(information, repo_type, pkg_type)
204             if not os.path.isfile(dbfile):
205                 if not getDB(information, repo_type, pkg_type):
206                     print 'Failed to get DB'
207                     return
208             pkg_list |= getAllPackages(dbfile)
209     for pkg_name in sorted(pkg_list):
210         print pkg_name
211
212
213 def commandPkgList(program, args):
214     parser = argparse.ArgumentParser(prog=program, description='Show package list')
215     parser.add_argument('snapshot_id', action='store')
216     parser.add_argument('-a', '--arch', choices=['armv7l', 'ia32'], default='armv7l')
217     parser.add_argument('-t', '--type', choices=['standard', 'release', 'SDK'], dest='release_type', help='release type. If this is not specified, ' + this + 'tries to guess it with snapshot_id')
218     parsedArgs = parser.parse_args(args);
219     listPackages(parsedArgs.snapshot_id, parsedArgs.release_type, parsedArgs.arch)
220
221
222 def GT_I8800_e4412(key_list):
223     if key_list[2] >= '20121017' and key_list[2] < '20121024':
224         return None
225     else:
226         return 'slp-release.sec.samsung.net', '/pub/GT-I8800_E4412_Release/release/GT-I8800_e4412_%s_1/information' % key_list[2], 'GT-I8800_e4412_%s_1.ks' % key_list[2]
227
228 def GT_I8800_e4412_cluster(key_list):
229     return 'slp-release.sec.samsung.net', '/pub/GT-I8800_E4412_Release/release/GT-I8800_e4412_%s_1/Redwood_Cluster' % key_list[3], 'GT-I8800_e4412_cluster_%s_1.ks' % key_list[3]
230
231 def GT_I8800_e4412v34_cluster(key_list):
232     return 'slp-release.sec.samsung.net', '/pub/GT-I8800_E4412_Release/release/GT-I8800_e4412_%s_1/Redwoodv34_Cluster' % key_list[3], 'GT-I8800_e4412v34_cluster_%s_1.ks' % key_list[3]
233
234 def getKsPathInfo(key_list):
235     search_dict = {}
236     search_dict['GT-I8800'] = {}
237     search_dict['GT-I8800']['e4412'] = {'default': GT_I8800_e4412}
238     search_dict['GT-I8800']['e4412']['cluster'] = GT_I8800_e4412_cluster
239     search_dict['GT-I8800']['e4412v34'] = {}
240     search_dict['GT-I8800']['e4412v34']['cluster'] = GT_I8800_e4412v34_cluster
241
242     search = search_dict
243     for key in key_list:
244         if key not in search.keys():
245             if type(search['default']) is FunctionType:
246                 return search['default'](key_list)
247             return None
248         search = search[key]
249         if not search:
250             return None
251         if type(search) is FunctionType:
252             return search(key_list)
253
254     return None
255
256 def findSnapshotID(release_version):
257     ksPathInfo = getKsPathInfo(release_version.split('_'))
258     if not ksPathInfo:
259         print 'Have no information of ' + release_version
260         print 'Please update search_dict in the ' + this + ' script'
261         return None
262
263     ksdir = tool_base_dir + '/ks'
264     if not os.path.exists(ksdir):
265         os.makedirs(ksdir)
266
267     ks_server = ksPathInfo[0]
268     ks_server_dir = ksPathInfo[1]
269     ks_file_name = ksPathInfo[2]
270
271     ks_path = ksdir + '/' + ks_file_name
272     if not os.path.isfile(ks_path):
273         ftp = ftplib.FTP(ks_server)
274         print ks_server
275         ks_user = raw_input('user : ')
276         ks_pass = getpass.getpass('password: ')
277         ftp.login(ks_user, ks_pass)
278         try:
279             ftp.retrbinary('RETR %s/%s' % (ks_server_dir, ks_file_name), open(ks_path, 'wb').write)
280         except:
281             os.remove(ks_path)
282             print 'Failed to get %s:%s/%s' % (ks_server, ks_server_dir, ks_file_name)
283             print 'Maybe there is not such path in the server.'
284             print 'Please update search_dict in the ' + this + ' script'
285             return None
286         ftp.quit()
287     for line in open(ks_path).readlines():
288         if line.startswith("repo "):
289             snapshot_id = line.split('/repos/')[0].split('/')[-1]
290             print 'SnapshotID: ' + snapshot_id
291             return snapshot_id
292
293
294 def commandFindSnapshotID(program, args):
295     parser = argparse.ArgumentParser(prog=program, description='Find repo id from release version')
296     parser.add_argument('release_version', action='store', help='release version of target binary or SDK')
297     parsedArgs = parser.parse_args(args);
298     return findSnapshotID(parsedArgs.release_version)
299
300 def __findProvides(information, repo_type, pkg_type, found_pkgs, file_path_list):
301     conn = sqlite3.connect(getDBFilePath(information, repo_type, pkg_type))
302     c = conn.cursor()
303     found_files = set()
304     for file_path in file_path_list:
305         file_name = file_path.split('/')[-1]
306         isLibrary = False
307         if file_name.endswith('.so'):
308             isLibrary = True
309         else:
310             file_token = file_name.split('.so.')
311             if len(file_token) is 2:
312                 isLibrary = True
313                 file_name = file_token[0] + '.so.' + file_token[1].split('.')[0]
314
315         if isLibrary:
316             for row in c.execute('select provides.name, packages.name from provides, packages where provides.name = "' + file_name + '" and provides.pkgKey == packages.pkgKey;'):
317                 if row[0] not in found_pkgs.keys():
318                     found_pkgs[row[0]] = set()
319                 found_pkgs[row[0]] |= set([row[1]])
320                 found_files |= set([file_path])
321         else:
322             for row in c.execute('select files.name, packages.name from files, packages where files.name = "' + file_path + '" and files.pkgKey == packages.pkgKey;'):
323                 if row[0] not in found_pkgs.keys():
324                     found_pkgs[row[0]] = set()
325                 found_pkgs[row[0]] |= set([row[1]])
326                 found_files |= set([file_path])
327     conn.close()
328     return found_files
329
330 def _findProvides(information, file_path_list):
331     found_pkgs = {}
332     found_files = set()
333     for repo_type in information['repo_type_list']:
334         for pkg_type in ['bin', 'dbg']:
335             dbfile = getDBFilePath(information, repo_type, pkg_type)
336             if not os.path.isfile(dbfile):
337                 if not getDB(information, repo_type, pkg_type):
338                     print 'Failed to get DB'
339                     return
340             found_files |= __findProvides(information, repo_type, pkg_type, found_pkgs, file_path_list)
341     return found_pkgs, found_files
342
343
344 def findProvides(snapshot_id, release_type, arch, file_path_list):
345     information = getInformation(snapshot_id, release_type, arch)
346
347     ret = _findProvides(information, file_path_list)
348
349     for key in sorted(ret[0].keys()):
350         for package_name in sorted(ret[0][key]):
351             print key + ' is provided by [' + package_name + ']'
352
353     file_path_list -= ret[1]
354     if len(file_path_list) > 0:
355         print 'Cannot find provider for %r' % sorted(file_path_list)
356
357 def commandProvides(program, args):
358     parser = argparse.ArgumentParser(prog=program, description='Find package that provides certain library')
359     parser.add_argument('snapshot_id', action='store')
360     parser.add_argument('file_path', nargs='+')
361     parser.add_argument('-a', '--arch', choices=['armv7l', 'ia32'], default='armv7l')
362     parser.add_argument('-t', '--type', choices=['standard', 'release', 'SDK'], dest='release_type', help='release type. If this is not specified, ' + this + 'tries to guess it with snapshot_id')
363     parsedArgs = parser.parse_args(args);
364     findProvides(parsedArgs.snapshot_id, parsedArgs.release_type, parsedArgs.arch, set(parsedArgs.file_path))
365
366 def findPID(parse_dict, line):
367     if parse_dict['FindPID']['finished']:
368         return
369     if line.startswith('Pid :') or line.startswith('pid :'):
370         parse_dict['FindPID']['finished'] = True
371         parse_dict['FindPID']['result'] = line.split()[-1]
372
373 def findExecPath(parse_dict, line):
374     if parse_dict['FindExecPath']['finished']:
375         return
376     if line.startswith('ExePath:'):
377         parse_dict['FindExecPath']['finished'] = True
378         parse_dict['FindExecPath']['result'] = line.split()[-1]
379     elif parse_dict['FindMapsStart']['finished'] and len(line.split()) is 4:
380         parse_dict['FindExecPath']['finished'] = True
381         parse_dict['FindExecPath']['result'] = line.split()[3]
382
383 def findMapsStart(parse_dict, line):
384     if parse_dict['FindMapsStart']['finished']:
385         return
386     if line.startswith('maps  information'):
387         parse_dict['FindMapsStart']['finished'] = True
388
389 def findReleaseVersion(parse_dict, line):
390     if parse_dict['FindReleaseVersion']['finished']:
391         return
392     if line.startswith('Build='):
393         parse_dict['FindReleaseVersion']['finished'] = True
394         parse_dict['FindReleaseVersion']['result'] = line.split('=')[1].split(';')[0]
395
396 def findRelatedFiles(parse_dict, line):
397     if parse_dict['FindRelatedFiles']['finished']:
398         return
399
400     if line.startswith('end of maps information'):
401         parse_dict['FindRelatedFiles']['finished'] = True
402     else:
403         line_tokens = line.split()
404         if parse_dict['FindMapsStart']['finished'] and len(line_tokens) is 4:
405             if line_tokens[3].startswith('['):
406                 return
407             if parse_dict['FindRelatedFiles']['result'] is None:
408                 parse_dict['FindRelatedFiles']['result'] = []
409             else:
410                 parse_dict['FindRelatedFiles']['result'].append(line_tokens[3])
411
412 def parseCSFile(cs_file):
413     parse_dict = {}
414     parse_dict['FindPID'] = {'finished': False, 'function': findPID, 'result': None}
415     parse_dict['FindExecPath'] = {'finished': False, 'function': findExecPath, 'result': None}
416     parse_dict['FindMapsStart'] = {'finished': False, 'function': findMapsStart}
417     parse_dict['FindReleaseVersion'] = {'finished': False, 'function': findReleaseVersion, 'result': None}
418     parse_dict['FindRelatedFiles'] = {'finished': False, 'function': findRelatedFiles, 'result': None}
419
420     if not os.path.isfile(cs_file):
421         print cs_file + ' is not valid file'
422         return None
423
424     for line in open(cs_file).readlines():
425         for key in parse_dict.keys():
426             parse_dict[key]['function'](parse_dict, line)
427
428     if not parse_dict['FindPID']['result']:
429         print 'Failed to get PID'
430         print 'Please update parse_dict in ' + this + ' script'
431         return None
432
433     if not parse_dict['FindExecPath']['result']:
434         print 'Failed to get exec path'
435         print 'Please update parse_dict in ' + this + ' script'
436         return None
437
438     if not parse_dict['FindReleaseVersion']['result']:
439         print 'Failed to get release version'
440         print 'Please update parse_dict in ' + this + ' script'
441         return None
442
443     print 'Pid: ' + parse_dict['FindPID']['result']
444     print 'ExecPath: ' + parse_dict['FindExecPath']['result']
445     print 'ReleaseVersion: ' + parse_dict['FindReleaseVersion']['result']
446     #print 'RelatedFiles: %r' % parse_dict['FindRelatedFiles']['result']
447
448     return parse_dict['FindPID']['result'], parse_dict['FindExecPath']['result'], parse_dict['FindReleaseVersion']['result'], parse_dict['FindRelatedFiles']['result']
449
450 def commandCsInfo(program, args):
451     parser = argparse.ArgumentParser(prog=program)
452     parser.add_argument('cs_file', action='store')
453     parsedArgs = parser.parse_args(args);
454     return parseCSFile(parsedArgs.cs_file)
455
456
457 def doChroot(arch):
458     updateOsceBuildRoot(arch)
459     getbt_dir = tool_base_dir + '/getbt'
460     current_dir = os.getcwd()
461     os.chdir(getbt_dir)
462     subprocess.call(['osce', 'chroot', getOsceArchType(arch)])
463     os.chdir(current_dir)
464
465 def commandChroot(program, args):
466     parser = argparse.ArgumentParser(prog=program)
467     parser.add_argument('-a', '--arch', choices=['armv7l', 'ia32'], default='armv7l')
468     parsedArgs = parser.parse_args(args);
469     doChroot(parsedArgs.arch)
470
471 def getBackTrace(snapshot_id, release_type, arch, core_file, exec_path, force_copy, related_files):
472     if not os.path.isfile(core_file):
473         print core_file + ' is not valid file'
474
475     core_file_name = core_file.split('/')[-1]
476
477     information = getInformation(snapshot_id, release_type, arch)
478
479     if not os.path.isfile(information['base_dir_abs'] + exec_path):
480         print 'executive file is not exists : ' + information['base_dir_abs'] + exec_path
481         ret = _findProvides(information, [exec_path])
482         if len(ret[0]) > 0:
483             print 'please install ' + string.join(ret[0][exec_path]) + ' with below command'
484             print '  ' + this + ' getpkgs ' + snapshot_id + ' -a' + arch + ' ' + string.join(ret[0][exec_path])
485         else:
486             print 'cannot find package that provides ' + exec_path
487
488         if len(related_files) > 0:
489             print '\nrelated packages from memory map'
490             ret = _findProvides(information, related_files)
491             pkg_name_list = []
492             for pkg_name_set in ret[0].values():
493                 for pkg_name in pkg_name_set:
494                     pkg_name_list.append(pkg_name)
495             print string.join(sorted(pkg_name_list))
496         return
497
498     getbt_dir = tool_base_dir + '/getbt'
499     if not os.path.exists(getbt_dir):
500         os.makedirs(getbt_dir)
501
502     current_dir = os.getcwd()
503     os.chdir(getbt_dir)
504
505     if not os.path.exists('.git'):
506         os.makedirs('.git')
507
508     if not os.path.exists('packaging'):
509         os.makedirs('packaging')
510
511     updateOsceBuildRoot(arch)
512
513     package_file = 'packaging/webkit2-efl.spec'
514     if not os.path.exists(package_file):
515         spec_data = [
516                 'Name: webkit2-efl',
517                 'Summary: get bt',
518                 'Version: 1',
519                 'Release: 1',
520                 'Group: System/Libraries',
521                 'License: BSD',
522                 'Source0: %{name}-%{version}.tar.gz',
523                 '',
524                 'BuildRequires: gdb cmake python bison flex gperf perl gettext-tools vi',
525                 '',
526                 '%description',
527                 'Browser Engine based on Webkit2 EFL (Shared Library)',
528                 '',
529                 '%prep',
530                 '%%setup -q',
531                 '',
532                 '%build',
533                 'echo Finished to create getbt environment',
534                 ]
535         file = open(package_file, 'w')
536         for line in spec_data:
537             file.write(line)
538             file.write('\n')
539         file.close()
540
541     if not os.path.exists(getOsceBuildRoot()):
542         subprocess.call(['osce', 'build', getOsceArchType(arch)])
543     os.chdir(current_dir)
544
545     osce_home_abuild = getOsceBuildRoot() + '/home/abuild'
546     subprocess.call(['sudo', 'mkdir', '-p', osce_home_abuild + '/' + snapshot_id])
547
548     if force_copy or not os.path.exists(osce_home_abuild + '/' + information['base_dir']):
549         print 'Coping snapshot data to build root'
550         subprocess.call(['sudo', 'cp', '-rfa', information['base_dir_abs'], osce_home_abuild + '/' + snapshot_id + '/'])
551
552     subprocess.call(['sudo', 'cp', core_file, osce_home_abuild])
553
554     gdb_sysroot = '/home/abuild/' + information['base_dir']
555     file = open('/tmp/cmd', 'w')
556     cmd_data = [
557             'exec-file ' + gdb_sysroot + exec_path,
558             'set sysroot ' + gdb_sysroot,
559             'set debug-file-directory ' + gdb_sysroot + '/usr/lib/debug',
560             'set substitute-path /usr/src ' + gdb_sysroot + '/usr/src',
561             'core-file ' + core_file_name,
562             'bt',
563             ]
564     for line in cmd_data:
565         file.write(line)
566         file.write('\n')
567     file.close()
568
569     subprocess.call(['sudo', 'cp', '/tmp/cmd', osce_home_abuild])
570
571     print '\n\n====================================================================='
572     print '- type \'gdb -x cmd\' to open ' + core_file_name + ' with gdb'
573     print '=====================================================================\n\n'
574
575     doChroot(arch)
576
577 def commandGetBt(program, args):
578     parser = argparse.ArgumentParser(prog=program)
579     parser.add_argument('snapshot_id', action='store')
580     parser.add_argument('core_file', action='store')
581     parser.add_argument('exec_path', action='store')
582     parser.add_argument('-a', '--arch', choices=['armv7l', 'ia32'], default='armv7l')
583     parser.add_argument('-t', '--type', choices=['standard', 'release', 'SDK'], dest='release_type', help='release type. If this is not specified, ' + this + 'tries to guess it with snapshot_id')
584     parser.add_argument('-f', '--force', dest='force_copy', default=False, action='store_true', help='Copy snapshot data to the qemu forcely. This will take some time, but this need to be set when new packages are downloaded.')
585     parsedArgs = parser.parse_args(args);
586     getBackTrace(parsedArgs.snapshot_id, parsedArgs.release_type, parsedArgs.arch, parsedArgs.core_file, parsedArgs.exec_path, parsedArgs.force_copy, None)
587
588
589 def checkCrash(arch, core_file, cs_file, force_copy):
590     if not os.path.isfile(core_file):
591         print core_file + ' is not valid file'
592
593     result = parseCSFile(cs_file)
594     if not result:
595         print 'Failed to parse cs file'
596         return
597
598     crash_pid = result[0]
599     exec_path = result[1]
600     release_version = result[2]
601     related_files = result[3]
602
603     core_file_name = 'core.' + crash_pid
604     if core_file.split('/')[-1] != core_file_name:
605         print 'core file is not matched with cs file'
606         return
607
608     snapshot_id = findSnapshotID(release_version)
609     if not snapshot_id:
610         print 'Failed to find snapshot ID with ' + release_version
611         return
612
613     getBackTrace(snapshot_id, None, arch, core_file, exec_path, force_copy, set(related_files))
614
615
616 def commandCrash(program, args):
617     parser = argparse.ArgumentParser(prog=program)
618     parser.add_argument('core_file', action='store')
619     parser.add_argument('cs_file', action='store')
620     parser.add_argument('-a', '--arch', choices=['armv7l', 'ia32'], default='armv7l')
621     parser.add_argument('-f', '--force', dest='force_copy', default=False, action='store_true', help='Copy snapshot data to the qemu forcely. This will take some time, but this need to be set when new packages are downloaded.')
622     parsedArgs = parser.parse_args(args);
623     checkCrash(parsedArgs.arch, parsedArgs.core_file, parsedArgs.cs_file, parsedArgs.force_copy)
624
625 def main():
626     commandMap = {}
627     commandMap['getpkgs'] = commandGetPkgs
628     commandMap['pkglist'] = commandPkgList
629     commandMap['findsnapshot'] = commandFindSnapshotID
630     commandMap['provides'] = commandProvides
631     commandMap['csinfo'] = commandCsInfo
632     commandMap['getbt'] = commandGetBt
633     commandMap['crash'] = commandCrash
634     commandMap['chroot'] = commandChroot
635
636     parser = argparse.ArgumentParser()
637     parser.add_argument('command', choices=commandMap.keys(), help='sub command. Type ' + this + ' <sub command> -h for help')
638     parser.add_argument('args', nargs=argparse.REMAINDER, help='arguments of command')
639     parsedArgs = parser.parse_args()
640     commandMap[parsedArgs.command](this + ' ' + parsedArgs.command, parsedArgs.args)
641
642
643 if __name__ == '__main__':
644     main()