Blame


1 1504fcbd 2015-06-16 xhr #!/usr/bin/env python
2 1504fcbd 2015-06-16 xhr
3 1504fcbd 2015-06-16 xhr import sys
4 1504fcbd 2015-06-16 xhr import getopt, sqlite3
5 1504fcbd 2015-06-16 xhr
6 1504fcbd 2015-06-16 xhr class ParseMactime():
7 1504fcbd 2015-06-16 xhr def __init__(self, verboseFlag):
8 1504fcbd 2015-06-16 xhr self.db_name = "timeline.db"
9 1504fcbd 2015-06-16 xhr self.conn = None
10 1504fcbd 2015-06-16 xhr self.cursor = None
11 1504fcbd 2015-06-16 xhr self.verbose = verboseFlag
12 1504fcbd 2015-06-16 xhr
13 1504fcbd 2015-06-16 xhr def connect_to_db(self):
14 1504fcbd 2015-06-16 xhr self.conn = sqlite3.connect(self.db_name)
15 1504fcbd 2015-06-16 xhr self.cursor = self.conn.cursor()
16 1504fcbd 2015-06-16 xhr self.conn.text_factory = str
17 1504fcbd 2015-06-16 xhr
18 1504fcbd 2015-06-16 xhr def clear_tables(self):
19 1504fcbd 2015-06-16 xhr # Clean all tables
20 1504fcbd 2015-06-16 xhr try:
21 1504fcbd 2015-06-16 xhr self.cursor.execute("DROP TABLE tl")
22 1504fcbd 2015-06-16 xhr except sqlite3.OperationalError:
23 1504fcbd 2015-06-16 xhr print "Warning >>> Nothing to clean."
24 1504fcbd 2015-06-16 xhr # Date,Size,Type,Mode,UID,GID,Meta,File Name
25 1504fcbd 2015-06-16 xhr self.cursor.execute("""CREATE TABLE tl (
26 1504fcbd 2015-06-16 xhr datum TEXT,
27 1504fcbd 2015-06-16 xhr size INT,
28 1504fcbd 2015-06-16 xhr type TEXT,
29 1504fcbd 2015-06-16 xhr mode TEXT,
30 1504fcbd 2015-06-16 xhr uid TEXT,
31 1504fcbd 2015-06-16 xhr gid TEXT,
32 1504fcbd 2015-06-16 xhr meta TEXT,
33 1504fcbd 2015-06-16 xhr file TEXT)
34 1504fcbd 2015-06-16 xhr """)
35 1504fcbd 2015-06-16 xhr
36 1504fcbd 2015-06-16 xhr self.conn.commit()
37 1504fcbd 2015-06-16 xhr print "Info >>> Initalized table(s)"
38 1504fcbd 2015-06-16 xhr
39 1504fcbd 2015-06-16 xhr def parse_csv(self, fileName):
40 1504fcbd 2015-06-16 xhr try:
41 1504fcbd 2015-06-16 xhr f = open(fileName)
42 1504fcbd 2015-06-16 xhr except Exception:
43 1504fcbd 2015-06-16 xhr print ">>> Cannot open file %s" % fileName
44 1504fcbd 2015-06-16 xhr sys.exit(1)
45 1504fcbd 2015-06-16 xhr
46 1504fcbd 2015-06-16 xhr for l in f:
47 1504fcbd 2015-06-16 xhr l = l.strip()
48 1504fcbd 2015-06-16 xhr if len(l) <= 0:
49 1504fcbd 2015-06-16 xhr continue
50 1504fcbd 2015-06-16 xhr sl = l.split(',')
51 1504fcbd 2015-06-16 xhr
52 1504fcbd 2015-06-16 xhr # Comma delimited mactime file has always 8 entries. If we detect
53 1504fcbd 2015-06-16 xhr # more than 8, we have commas in the filename
54 1504fcbd 2015-06-16 xhr if len(sl) > 8:
55 1504fcbd 2015-06-16 xhr sl[7] = "".join(sl[7:])
56 1504fcbd 2015-06-16 xhr if self.verbose:
57 1504fcbd 2015-06-16 xhr print ">>> Found bogus entry with length %d, shorten it to 8: %s" % (len(sl), sl[7])
58 1504fcbd 2015-06-16 xhr
59 1504fcbd 2015-06-16 xhr try:
60 1504fcbd 2015-06-16 xhr date = sl[0]
61 1504fcbd 2015-06-16 xhr size = int(sl[1])
62 1504fcbd 2015-06-16 xhr ftype = sl[2]
63 1504fcbd 2015-06-16 xhr mode = sl[3]
64 1504fcbd 2015-06-16 xhr uid = sl[4]
65 1504fcbd 2015-06-16 xhr gid = sl[5]
66 1504fcbd 2015-06-16 xhr meta = sl[6]
67 1504fcbd 2015-06-16 xhr fname = sl[7]
68 1504fcbd 2015-06-16 xhr except IndexError as e:
69 1504fcbd 2015-06-16 xhr print ">>> Parsing error: %s" % str(e)
70 1504fcbd 2015-06-16 xhr continue
71 1504fcbd 2015-06-16 xhr
72 1504fcbd 2015-06-16 xhr query = "INSERT INTO tl VALUES (?,?,?,?,?,?,?,?)"
73 1504fcbd 2015-06-16 xhr values = (date,size,ftype,mode,uid,gid,meta,fname)
74 1504fcbd 2015-06-16 xhr self.cursor.execute(query, values)
75 1504fcbd 2015-06-16 xhr
76 1504fcbd 2015-06-16 xhr self.conn.commit()
77 1504fcbd 2015-06-16 xhr f.close()
78 1504fcbd 2015-06-16 xhr
79 1504fcbd 2015-06-16 xhr def usage(msg = ""):
80 1504fcbd 2015-06-16 xhr print "Usage"
81 1504fcbd 2015-06-16 xhr print
82 1504fcbd 2015-06-16 xhr print " --initdb - Initialize SQLite database (destroys all data!)"
83 1504fcbd 2015-06-16 xhr print " --import <csv-file> - Import timeline from CSV file (comma separated)"
84 1504fcbd 2015-06-16 xhr print
85 1504fcbd 2015-06-16 xhr print " -v - Be more verbose"
86 1504fcbd 2015-06-16 xhr print
87 1504fcbd 2015-06-16 xhr print "%s" % msg
88 1504fcbd 2015-06-16 xhr sys.exit(1)
89 1504fcbd 2015-06-16 xhr
90 1504fcbd 2015-06-16 xhr def main():
91 1504fcbd 2015-06-16 xhr try:
92 1504fcbd 2015-06-16 xhr opts, args = getopt.getopt(sys.argv[1:], 'hv',
93 1504fcbd 2015-06-16 xhr ["import=", "initdb"])
94 1504fcbd 2015-06-16 xhr except getopt.GetoptError as err:
95 1504fcbd 2015-06-16 xhr usage(str(err))
96 1504fcbd 2015-06-16 xhr
97 1504fcbd 2015-06-16 xhr if len(opts) == 0:
98 1504fcbd 2015-06-16 xhr usage("")
99 1504fcbd 2015-06-16 xhr
100 1504fcbd 2015-06-16 xhr importCsv = ""
101 1504fcbd 2015-06-16 xhr importCsvF = 0
102 1504fcbd 2015-06-16 xhr initDB = 0
103 1504fcbd 2015-06-16 xhr verboseFlag = 0
104 1504fcbd 2015-06-16 xhr
105 1504fcbd 2015-06-16 xhr for opt, arg in opts:
106 1504fcbd 2015-06-16 xhr if opt == '-h':
107 1504fcbd 2015-06-16 xhr usage("")
108 1504fcbd 2015-06-16 xhr elif opt == '--import':
109 1504fcbd 2015-06-16 xhr importCsv = arg
110 1504fcbd 2015-06-16 xhr importCsvF = 1
111 1504fcbd 2015-06-16 xhr elif opt == '--initdb':
112 1504fcbd 2015-06-16 xhr initDB = 1
113 1504fcbd 2015-06-16 xhr elif opt == '-v':
114 1504fcbd 2015-06-16 xhr verboseFlag = 1
115 1504fcbd 2015-06-16 xhr else:
116 1504fcbd 2015-06-16 xhr usage("Unrecognized Option")
117 1504fcbd 2015-06-16 xhr
118 1504fcbd 2015-06-16 xhr p = ParseMactime(verboseFlag)
119 1504fcbd 2015-06-16 xhr p.connect_to_db()
120 1504fcbd 2015-06-16 xhr
121 1504fcbd 2015-06-16 xhr if initDB > 0:
122 1504fcbd 2015-06-16 xhr p.clear_tables()
123 1504fcbd 2015-06-16 xhr elif importCsvF > 0:
124 1504fcbd 2015-06-16 xhr p.parse_csv(importCsv)
125 1504fcbd 2015-06-16 xhr
126 1504fcbd 2015-06-16 xhr main()