Esempio n. 1
0
def prepareDataFile(suffix): 
    """
    Application to wrap the history of the Firefox browser. This
    script should be executed with each svn commit.
    """
    
    global dataDir
    global dataFile
    global _tmpFile

    pla.logMessage('firefox: prepare ' + dataFile)

    # If no file is present in pladirectory, no instrumentation
    if not os.path.exists(dataDir):
        pla.logMessage('firefox: Disabled. Skipping')
        return []

    # Copy the Firefox SQLite database to the tmp directory, in order to avoid
    # lock issues.
    ffoxDir = os.path.expanduser('~/.mozilla/firefox/')
        
    # Parse the ffox configuration
    config = ConfigParser.ConfigParser()
    config.read(os.path.join(ffoxDir, 'profiles.ini'))
    profileDir = os.path.join(ffoxDir, config.get('Profile0', 'Path'))
        
    sqliteFile = os.path.join(profileDir, 'places.sqlite')
    pla.logMessage('firefox: copy file ' + sqliteFile + ' to ' + _tmpFile)
    shutil.copyfile(sqliteFile, _tmpFile)
        
    # Get the timestamp for the last execution
    lastExecution = pla.getLastExecutionTStamp()
    pla.logMessage('Last execution: ' + str(lastExecution))

    date_clause = ''
    if lastExecution != None:
        date_clause = "AND visit_date > " + str(int(lastExecution * 1000000))
        
    # Get the last activity from Firefox, through a query to the
    # history table
    conn = sqlite3.connect(_tmpFile)
    conn.row_factory = sqlite3.Row
    
    c = conn.cursor()
    query = """
          SELECT url, DATETIME(CAST (visit_date/1000000.0 AS INTEGER), 'unixepoch', 'localtime') AS timestamp
          FROM   moz_historyvisits h, moz_places p
          WHERE  h.place_id = p.id
          """ + date_clause + """ ORDER  BY visit_date """
    pla.logMessage('firefox: Query = ' + query)

    # Create a duplicate of the data file with the suffix
    toSendFileName = dataFile + '_' + suffix

    # Dump the data. Detect empty data because "rowcount" seems broken.
    dataOut = open(toSendFileName, 'w')
 
    # Boolean to capture if an alternative executable is needed
    use_executable = False
    try:
        # Try to execute the query
        c.execute(query)

        for row in c:
            dataOut.write(row['timestamp'] + ' ' + row['url'] + '\n')

        # Close the statement and the data file
        c.close()

        
    except sqlite3.DatabaseError, e:
        # Failed, this means the version of the sqlite executable is not
        # correct, use an alternative.
        pla.logMessage('Unable to read FFox places with sqlite library')
        pla.logMessage('Resorting to included binary')
        use_executable = True
Esempio n. 2
0
File: chrome.py Progetto: dleony/PLA
def prepareDataFile(suffix): 
    """
    Application to wrap the history of the Google Chrome browser. This
    script should be executed with each svn commit.
    """
    
    global dataDir
    global dataFile
    global _tmpFile

    epoch_offset = 11644473600000000
    pla.logMessage('chrome: prepare ' + dataFile)

    # If no file is present in pladirectory, no instrumentation
    if not os.path.exists(dataDir):
        pla.logMessage('chrome: Disabled. Skipping')
        return []

    # Copy the Google Chrome history database to the tmp directory, in order to
    # avoid lock issues.
    gchromeDir = os.path.expanduser('~/.config/google-chrome/Default')
    sqliteFile = os.path.join(gchromeDir, 'History')
    pla.logMessage('chrome: copy file ' + sqliteFile + ' to ' + _tmpFile)
    shutil.copyfile(sqliteFile, _tmpFile)
        
    # Get the timestamp for the last execution
    lastExecution = pla.getLastExecutionTStamp()
    pla.logMessage('Last execution: ' + str(lastExecution))

    date_clause = ''
    if lastExecution != None:
        date_clause = "AND (v.visit_time - " + str(epoch_offset) + ") > " + \
            str(int(lastExecution * 1000000))
        
    # Get the last activity from Google Chrome, through a query to the
    # history table
    conn = sqlite3.connect(_tmpFile)
    conn.row_factory = sqlite3.Row
    
    c = conn.cursor()
    query = """
          SELECT u.url, 
          DATETIME(CAST ((v.visit_time - """ + str(epoch_offset) + \
        """)/1000000.0 AS INTEGER), 'unixepoch', 'localtime') AS timestamp
          FROM   urls u, visits v
          WHERE  v.url = u.id
          """ + date_clause + " ORDER  BY v.visit_time "
    pla.logMessage('chrome: Query = ' + query)

    # Create a duplicate of the data file with the suffix
    toSendFileName = dataFile + '_' + suffix

    # Dump the data. Detect empty data because "rowcount" seems broken.
    dataOut = open(toSendFileName, 'w')
 
    # Boolean to capture if an alternative executable is needed
    use_executable = False
    try:
        # Try to execute the query
        c.execute(query)

        for row in c:
            dataOut.write(row['timestamp'] + ' ' + row['url'] + '\n')

        # Close the statement and the data file
        c.close()

        
    except sqlite3.DatabaseError, e:
        # Failed, this means the version of the sqlite executable is not
        # correct, use an alternative.
        pla.logMessage('Unable to read Chrome history with sqlite library')
        pla.logMessage('Resorting to included binary')
        use_executable = True