ActiveState Code

Recipe 576842: firefox sqlite files cleaner


Walks through the all the Firefox profiles in current user account and cleans all *.sqlite files with "vacuum". It makes firefox faster then often. Should work on Linux, too, when properly changed constants.

For: python 2.6 (2.5+sqlite3), FF 3.5

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*- 

import os, sqlite3
"""Walks through the all the Firefox profiles in current user account and cleans all
*.sqlite files with "vacuum". It makes firefox faster then often. Should work on Linux, too,
when properly changed constants."""

# -------------- constants -----------------------------------------

systemEncoding="mbcs"

profileUser= unicode(os.environ["USERPROFILE"], systemEncoding)
profileApp = unicode(os.environ["APPDATA"], systemEncoding) + ur"\Mozilla\Firefox\Profiles"


# -------------- functions -----------------------------------------

def searchProfil(profileApp):
    "all firefox profiles"
    for profile in os.listdir(profileApp):
        profileFull=os.path.join(profileApp, profile)
        searchSqlite(profileFull)
        

def searchSqlite(profile):
    "all sqlite file in each firefox profile"
    sq=[os.path.join(profile,s) for s in os.listdir(profile) if s.endswith(".sqlite")]
    print "\n..."+profile[len(profileUser):]
    for s in  sq:
        dirName, fileName=os.path.split(s)
        conn = sqlite3.connect(s)
        oldSize=os.path.getsize(s)
        print fileName+":",
        try: 
            c=conn.cursor()
            c.execute("VACUUM")  # this is the thing
            c.close()
            print "done.",
            print "%.1f%%" % (os.path.getsize(s)*1.0/oldSize*100)
            
        except:
            print "error."

# ----------------- main -------------------------------------------     

if __name__=="__main__":

    if os.path.isdir(profileApp):
        searchProfil(profileApp)
    else:
        print "Not exists:", profileApp

Comments

  1. 1. At 11:02 a.m. on 15 jul 2009, Vince Spicer said:

    Linux version Recipe 576843

  2. 2. At 11:15 p.m. on 15 jul 2009, Michal Niklas said:

    This recipe do not work on non-english version of Windows!

    On my Polish version of Windows Firefox profile is saved in Dane aplikacji while this recipe looks at Application Data.

    Instead of:

        profilApp = os.path.join(profilUser,profilAppMain)
    

    use:

        profilApp = os.environ['APPDATA'] + ur"\Mozilla\Firefox\Profiles"
    

    I have found info about APPDATA on How to find Windows "Application data" directory??

  3. 3. At 10:57 a.m. on 16 jul 2009, geon . (the author) said:

    thanks, updated together with other small things (better output)

Sign in to comment