Open Bug 1246284 Opened 4 years ago Updated 4 years ago

MozProfile only writes user.js if I explicitly call Preferences.write()

Categories

(Testing :: Mozbase, defect)

Version 3
defect
Not set

Tracking

(Not tracked)

People

(Reporter: pdehaan, Unassigned)

Details

I'm almost certainly using this wrong, but I can't seem to find a solution reading the readthedocs.

Basically I want to create a new Firefox Profile using the mozprofile library, and then set the user.js configs to a combination of two .ini files.
I have it working below, but it only seems to write the user.js file if I explicitly call `Preferences.write(profile.profile + '/user.js', all_prefs)`

I've tried passing `preferences` via the Profile() constructor, using `profile.set_preferences()`, `profile.set_persistent_preferences()`, and tried finding a way to pass preference files directly to the Profile constructor without needing to explicitly create two Preferences() objects first, without any luck.



```
from mozprofile import Profile, Preferences
import os

default_prefs = Preferences()
default_prefs.add_file('default.prefs.ini')

autopush_prefs = Preferences()
autopush_prefs.add_file('autopush.prefs.ini:dev')


all_prefs = autopush_prefs() + default_prefs()


profile = Profile(profile='prof6', preferences=all_prefs)
profile.set_preferences(all_prefs)
profile.set_persistent_preferences(all_prefs)

# This feels hacky, but seems to be the only way that my prefs write to disk.
userjs_path = os.path.join(profile.profile, 'user.js')
Preferences.write(userjs_path, all_prefs)

print profile.profile  # this is the path to the created profile

print profile

# del profile
# the profile path has been removed from disk
```

I've found a way to make this work using the mozprofile CLI, but I'd rather not try and use fabric or something to wrap the CLI tool and just use the mozprofile module directly.
Hi Henrik, any thoughts on this?
Flags: needinfo?(hskupin)
(In reply to Peter deHaan [:pdehaan] from comment #0)
> default_prefs = Preferences()
> default_prefs.add_file('default.prefs.ini')
> 
> autopush_prefs = Preferences()
> autopush_prefs.add_file('autopush.prefs.ini:dev')
> 
> all_prefs = autopush_prefs() + default_prefs()

There shouldn't be a need for two preferences instances. You can call `add_file()` multiple times on the same instance. Prefs will then be added or overwritten.

> profile = Profile(profile='prof6', preferences=all_prefs)
> profile.set_preferences(all_prefs)
> profile.set_persistent_preferences(all_prefs)

It's not clear to me from the original description what does not work. Are you saying that only the last method actually sets the preferences?

> # This feels hacky, but seems to be the only way that my prefs write to disk.
> userjs_path = os.path.join(profile.profile, 'user.js')
> Preferences.write(userjs_path, all_prefs)

Maybe you want to debug the mozprofile code like that:

http://mxr.mozilla.org/mozilla-central/source/testing/mozbase/mozprofile/mozprofile/profile.py#193

> I've found a way to make this work using the mozprofile CLI, but I'd rather
> not try and use fabric or something to wrap the CLI tool and just use the
> mozprofile module directly.

What did you specify for the mozprofile cli?
Flags: needinfo?(hskupin)
Ah, OK, maybe this illustrates it better:
```
# test.py
from mozprofile import Profile, Preferences

prefs = Preferences()
prefs.add_file('default.prefs.ini')
prefs.add_file('autopush.prefs.ini:dev')

profile = Profile(profile='prof7', preferences=prefs())

print profile.profile  # this is the path to the created profile

print profile
```

Where `default.prefs.ini` is:
```
[DEFAULT]
browser.feeds.showFirstRunUI = false
browser.shell.checkDefaultBrowser = false
browser.uitour.enabled = false
devtools.chrome.enabled = true
devtools.debugger.remote-enabled = true
startup.homepage_welcome_url = about:blank
```

And my `autoprefs.prefs.ini` is:
```
[DEFAULT]
default_pref: 1

[dev]
default_pref: override default
dev_pref: dev specific pref

[stage]
stage_pref: stage specific pref
```

If I run my Python script from the CLI as `$ python test.py`, I get the following output, but the actual prefs.js and user.js files are blank (despite seeing the output of [user.js] in the Terminal):

```
$ python test.py
/Users/pdehaan/dev/tmp/del/mozprofile-3/prof7
[Path]: /Users/pdehaan/dev/tmp/del/mozprofile-3/prof7

[Files]:
/Users/pdehaan/dev/tmp/del/mozprofile-3/prof7
├prefs.js
└user.js

[user.js]:
browser.feeds.showFirstRunUI: False
browser.shell.checkDefaultBrowser: False
browser.uitour.enabled: False
default_pref: override default
dev_pref: dev specific pref
devtools.chrome.enabled: True
devtools.debugger.remote-enabled: True
startup.homepage_welcome_url: about:blank
```
Flags: needinfo?(hskupin)
Long story longer, I think I may need to set `restore=False` in my Profile constructor:
```
profile = Profile(profile='prof9', restore=False, preferences=prefs())
```

> :param restore: Flag for removing all custom settings during cleanup


That seems to write the data to user.js:
```
#MozRunner Prefs Start 1456436276.75 91bfe7ea-433c-4c4c-9098-70559821812b
user_pref("browser.feeds.showFirstRunUI", false);
user_pref("browser.shell.checkDefaultBrowser", false);
user_pref("browser.uitour.enabled", false);
user_pref("devtools.chrome.enabled", true);
user_pref("devtools.debugger.remote-enabled", true);
user_pref("startup.homepage_welcome_url", "about:blank");
user_pref("default_pref", "override default");
user_pref("dev_pref", "dev specific pref");
#MozRunner Prefs End 1456436276.75 91bfe7ea-433c-4c4c-9098-70559821812b
```
Sorry for the delay here. Would it be possible for you to create a minimized testcase which illustrates that behavior? I will not be around next month but maybe someone else from the #ateam channel can help out.
Flags: needinfo?(hskupin)
You need to log in before you can comment on or make changes to this bug.