1 1504fcbd 2015-06-16 xhr #!/usr/bin/env python
4 1504fcbd 2015-06-16 xhr import getopt, sqlite3
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
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
18 1504fcbd 2015-06-16 xhr def clear_tables(self):
19 1504fcbd 2015-06-16 xhr # Clean all tables
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 (
36 1504fcbd 2015-06-16 xhr self.conn.commit()
37 1504fcbd 2015-06-16 xhr print "Info >>> Initalized table(s)"
39 1504fcbd 2015-06-16 xhr def parse_csv(self, fileName):
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
47 1504fcbd 2015-06-16 xhr l = l.strip()
48 1504fcbd 2015-06-16 xhr if len(l) <= 0:
50 1504fcbd 2015-06-16 xhr sl = l.split(',')
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])
61 1504fcbd 2015-06-16 xhr size = int(sl[1])
62 1504fcbd 2015-06-16 xhr ftype = sl[2]
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)
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)
76 1504fcbd 2015-06-16 xhr self.conn.commit()
79 1504fcbd 2015-06-16 xhr def usage(msg = ""):
80 1504fcbd 2015-06-16 xhr print "Usage"
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)"
85 1504fcbd 2015-06-16 xhr print " -v - Be more verbose"
87 1504fcbd 2015-06-16 xhr print "%s" % msg
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))
97 1504fcbd 2015-06-16 xhr if len(opts) == 0:
100 1504fcbd 2015-06-16 xhr importCsv = ""
101 1504fcbd 2015-06-16 xhr importCsvF = 0
103 1504fcbd 2015-06-16 xhr verboseFlag = 0
105 1504fcbd 2015-06-16 xhr for opt, arg in opts:
106 1504fcbd 2015-06-16 xhr if opt == '-h':
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':
113 1504fcbd 2015-06-16 xhr elif opt == '-v':
114 1504fcbd 2015-06-16 xhr verboseFlag = 1
116 1504fcbd 2015-06-16 xhr usage("Unrecognized Option")
118 1504fcbd 2015-06-16 xhr p = ParseMactime(verboseFlag)
119 1504fcbd 2015-06-16 xhr p.connect_to_db()
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)