-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_firefox_cleanprofile.py
executable file
·112 lines (93 loc) · 3.98 KB
/
start_firefox_cleanprofile.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
# vim: set fileencoding=utf-8 :
""" start_firefox_cleanprofile.py
Starts firefox with an empty (clean) profile"""
# The MIT License (MIT)
#
# Copyright (c) 2016-2020 Georg Lutz
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# Standard library imports:
import argparse
import os
import platform
import shutil
import subprocess
import sys
import configparser
def get_args():
'''Configures command line parser and returns parsed parameters'''
parser = argparse.ArgumentParser(
description="Starts firefox with an empty (clean) profile")
parser.add_argument("--profile", help="profile name", default="clean")
return parser.parse_args()
def get_profiles_folder():
'''Returns platform specific folder where profile.ini is stored'''
system = platform.system()
if system == "Windows":
return os.path.join(os.environ["APPDATA"], "Mozilla", "Firefox")
if system == "Linux":
return os.path.join(os.environ["HOME"], ".mozilla", "firefox")
raise NameError("Unsupported platform")
def get_clean_profile_folder(profiles_folder, profilename):
'''Parses profile.ini and returns absolute path to the profile
Args:
profile_ini: Full path to the profile.ini file
profilename: Name of the profile
Returns:
Absolute path to the profile directory with the given name
'''
profile_path = ""
config = configparser.RawConfigParser()
profiles_ini = os.path.join(profiles_folder, "profiles.ini")
config.read(profiles_ini)
config.sections()
for section in config.sections():
if section.startswith("Profile"):
if config.get(section, "Name") == profilename:
profile_path = config.get(section, "Path")
if profile_path:
return os.path.join(profiles_folder, profile_path)
print("I could read out the profiles.ini file at " + profiles_ini + ".")
print("But I couldn't find a profile with the name " + profilename + ".")
print("Please create one with \"firefox --ProfileManager\" as I do not support")
print("profile creation of my own.")
sys.exit(1)
def get_executable_path():
'''Returns executable path based on guess'''
system = platform.system()
if system == "Windows":
return os.path.join(os.environ["ProgramFiles(x86)"], "Mozilla Firefox", "firefox.exe")
if system == "Linux":
return "/usr/bin/firefox"
raise NameError("Unsupported platform")
def start_clean(profilename):
'''Starts with the given profile name'''
clean_profile_folder = get_clean_profile_folder(get_profiles_folder(), profilename)
print("profile folder: " + clean_profile_folder)
shutil.rmtree(clean_profile_folder)
os.mkdir(clean_profile_folder)
executable_path = get_executable_path()
subprocess.Popen([executable_path, "-P", profilename, "--no-remote"])
def main():
'''main function, called when script file is executed directly'''
args = get_args()
start_clean(args.profile)
if __name__ == "__main__":
main()