1# This Source Code Form is subject to the terms of the Mozilla Public
2# License, v. 2.0. If a copy of the MPL was not distributed with this
3# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5from __future__ import absolute_import
6
7from mozboot.base import BaseBootstrapper
8from mozboot.linux_common import StyloInstall
9
10
11class GentooBootstrapper(StyloInstall, BaseBootstrapper):
12    def __init__(self, version, dist_id, **kwargs):
13        BaseBootstrapper.__init__(self, **kwargs)
14
15        self.version = version
16        self.dist_id = dist_id
17
18    def install_system_packages(self):
19        self.run_as_root(['emerge', '--noreplace', '--quiet', 'dev-vcs/git',
20                          'mercurial', 'nodejs'])
21
22    def install_browser_packages(self):
23        self.ensure_browser_packages()
24
25    def install_browser_artifact_mode_packages(self):
26        self.ensure_browser_packages(artifact_mode=True)
27
28    def install_mobile_android_packages(self):
29        self.ensure_mobile_android_packages(artifact_mode=False)
30
31    def install_mobile_android_artifact_mode_packages(self):
32        self.ensure_mobile_android_packages(artifact_mode=True)
33
34    def ensure_browser_packages(self, artifact_mode=False):
35        # TODO: Figure out what not to install for artifact mode
36        self.run_as_root(['emerge', '--onlydeps', '--quiet', 'firefox'])
37        self.run_as_root(['emerge', '--noreplace', '--quiet', 'gtk+'])
38
39    def ensure_mobile_android_packages(self, artifact_mode=False):
40        import re
41        import subprocess
42
43        # For downloading the Oracle JDK, Android SDK and NDK.
44        self.run_as_root(['emerge', '--noreplace', '--quiet', 'wget'])
45
46        # Obtain the path held in the DISTDIR portage variable
47        emerge_info = subprocess.check_output(['emerge', '--info'])
48        distdir_re = re.compile('^DISTDIR="(.*)"$', re.MULTILINE)
49        distdir = distdir_re.search(emerge_info).group(1)
50
51        # Fetch the Oracle JDK since portage can't fetch it on its own
52        base_url = 'http://download.oracle.com/otn-pub/java/jdk'
53        jdk_dir = '8u162-b12/0da788060d494f5095bf8624735fa2f1'
54        jdk_file = 'jdk-8u162-linux-x64.tar.gz'
55        cookie = 'Cookie: oraclelicense=accept-securebackup-cookie'
56        self.run_as_root(['wget', '-c', '-O', distdir + '/' + jdk_file,
57                          '--header', cookie,
58                          base_url + '/' + jdk_dir + '/' + jdk_file])
59
60        # Install the Oracle JDK. We explicitly prompt the user to accept the
61        # changes because this command might need to modify the portage
62        # configuration files and doing so without user supervision is dangerous
63        self.run_as_root(['emerge', '--noreplace', '--quiet',
64                          '--autounmask-continue', '--ask',
65                          '=dev-java/oracle-jdk-bin-1.8.0.162'])
66
67        from mozboot import android
68        android.ensure_android('linux', artifact_mode=artifact_mode,
69                               no_interactive=self.no_interactive)
70
71    def suggest_mobile_android_mozconfig(self, artifact_mode=False):
72        from mozboot import android
73        android.suggest_mozconfig('linux', artifact_mode=artifact_mode)
74
75    def suggest_mobile_android_artifact_mode_mozconfig(self):
76        self.suggest_mobile_android_mozconfig(artifact_mode=True)
77
78    def _update_package_manager(self):
79        self.run_as_root(['emerge', '--sync'])
80
81    def upgrade_mercurial(self, current):
82        self.run_as_root(['emerge', '--update', 'mercurial'])
83