- add sources.
[platform/framework/web/crosswalk.git] / src / components / test / data / autofill / merge / tools / serialize_profiles.py
1 #!/usr/bin/env python
2 # Copyright 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 import os.path
7 import sqlite3
8 import sys
9
10 from autofill_merge_common import SerializeProfiles, ColumnNameToFieldType
11
12
13 def main():
14   """Serializes the autofill_profiles table from the specified database."""
15
16   if len(sys.argv) != 2:
17     print "Usage: python serialize_profiles.py <path/to/database>"
18     return 1
19
20   database = sys.argv[1]
21   if not os.path.isfile(database):
22     print "Cannot read database at \"%s\"" % database
23     return 1
24
25   # Read the autofill_profile_names table.
26   try:
27     connection = sqlite3.connect(database, 0)
28     cursor = connection.cursor()
29     cursor.execute("SELECT * from autofill_profile_names;")
30   except sqlite3.OperationalError:
31     print ("Failed to read the autofill_profile_names table from \"%s\"" %
32            database)
33     raise
34
35   # For backward-compatibility, the result of |cursor.description| is a list of
36   # 7-tuples, in which the first item is the column name, and the remaining
37   # items are 'None'.
38   types = [ColumnNameToFieldType(item[0]) for item in cursor.description]
39   profiles = {}
40   for profile in cursor:
41     guid = profile[0]
42     profiles[guid] = zip(types, profile)
43
44   # Read the autofill_profile_emails table.
45   try:
46     cursor.execute("SELECT * from autofill_profile_emails;")
47   except sqlite3.OperationalError:
48     print ("Failed to read the autofill_profile_emails table from \"%s\"" %
49            database)
50     raise
51
52   types = [ColumnNameToFieldType(item[0]) for item in cursor.description]
53   for profile in cursor:
54     guid = profile[0]
55     profiles[guid].extend(zip(types, profile))
56
57   # Read the autofill_profiles table.
58   try:
59     cursor.execute("SELECT * from autofill_profiles;")
60   except sqlite3.OperationalError:
61     print "Failed to read the autofill_profiles table from \"%s\"" % database
62     raise
63
64   types = [ColumnNameToFieldType(item[0]) for item in cursor.description]
65   for profile in cursor:
66     guid = profile[0]
67     profiles[guid].extend(zip(types, profile))
68
69   # Read the autofill_profile_phones table.
70   try:
71     cursor.execute("SELECT * from autofill_profile_phones;")
72   except sqlite3.OperationalError:
73     print ("Failed to read the autofill_profile_phones table from \"%s\"" %
74            database)
75     raise
76
77   for profile in cursor:
78     guid = profile[0]
79     profiles[guid].append(("PHONE_HOME_WHOLE_NUMBER", profile[2]))
80
81   print SerializeProfiles(profiles.values())
82   return 0
83
84
85 if __name__ == '__main__':
86   sys.exit(main())