-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·77 lines (61 loc) · 2.05 KB
/
main.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
#!/usr/bin/env python3
from profile import Profile
from datetime import date
import os
import sys
import traceback
def _export_profile(key, input_dir):
assert os.path.isdir(input_dir)
modified_time = os.path.getmtime(input_dir)
modified_date = date.fromtimestamp(modified_time).strftime('%Y%m%d')
os.makedirs('firefox-profiles', exist_ok = True)
output_dir = os.path.join('firefox-profiles', '{}-{}'.format(key, modified_date))
i = 0
while not _try_mkdir(output_dir):
i += 1
output_dir = os.path.join('firefox-profiles', '{}-{}.{}'.format(key, modified_date, i))
Profile(input_dir).save(output_dir)
def _try_mkdir(path):
try:
os.mkdir(path)
return True
except FileExistsError:
return False
def find_profiles():
if sys.platform == 'linux':
path = os.path.expanduser('~/.mozilla/firefox')
elif sys.platform == 'win32':
path = os.path.expanduser('~\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles')
else:
raise RuntimeError('unsupported system')
profiles = [ profile for profile in os.listdir(path) if profile.endswith('.default') ]
return { profile[:-8] : os.path.join(path, profile) for profile in profiles }
def export_default_profiles():
os.makedirs('firefox-profiles', exist_ok = True)
for key, input_dir in find_profiles().items():
_export_profile(key, input_dir)
def export_specific_profile(path):
assert os.path.isdir(path)
key = os.path.basename(path)
if not key:
key = os.path.basename(os.path.dirname(path))
if key.endswith('.default'):
key = key[:-8]
_export_profile(key, path)
def main(args):
if len(args) == 0:
export_default_profiles()
elif len(args) == 1:
export_specific_profile(args[0])
else:
_print_usage()
def _print_usage():
print('Usage:')
print(' {} [PROFILE_PATH]'.format(sys.argv[0]))
if __name__ == '__main__':
try:
main(sys.argv[1:])
except Exception:
_print_usage()
print()
traceback.print_exc()