|
4 | 4 |
|
5 | 5 | from __future__ import absolute_import, print_function
|
6 | 6 |
|
| 7 | +import hashlib |
7 | 8 | import json
|
8 | 9 | import os
|
9 | 10 | import stat
|
| 11 | +import subprocess |
10 | 12 | import sys
|
11 | 13 | import time
|
12 | 14 | import zipfile
|
13 | 15 | from abc import ABCMeta, abstractmethod
|
14 | 16 | from enum import Enum
|
| 17 | +from shutil import move |
15 | 18 | from subprocess import STDOUT, CalledProcessError, call, check_output
|
16 | 19 | from threading import Thread
|
17 | 20 |
|
|
22 | 25 | from mozfile import remove
|
23 | 26 | from mozlog.structured import get_default_logger, get_proxy_logger
|
24 | 27 | from mozprofile import Profile, ThunderbirdProfile
|
25 |
| -from mozrunner import Runner |
| 28 | +from mozrunner import GeckoRuntimeRunner, Runner |
26 | 29 |
|
27 | 30 | from mozregression.class_registry import ClassRegistry
|
28 | 31 | from mozregression.errors import LauncherError, LauncherNotRunnable
|
@@ -338,11 +341,15 @@ def get_app_info(self):
|
338 | 341 | REGISTRY = ClassRegistry("app_name")
|
339 | 342 |
|
340 | 343 |
|
341 |
| -def create_launcher(buildinfo): |
| 344 | +def create_launcher(buildinfo, launcher_args=None): |
342 | 345 | """
|
343 | 346 | Create and returns an instance launcher for the given buildinfo.
|
344 | 347 | """
|
345 |
| - return REGISTRY.get(buildinfo.app_name)(buildinfo.build_file, task_id=buildinfo.task_id) |
| 348 | + return REGISTRY.get(buildinfo.app_name)( |
| 349 | + buildinfo.build_file, |
| 350 | + task_id=buildinfo.task_id, |
| 351 | + launcher_args=launcher_args, |
| 352 | + ) |
346 | 353 |
|
347 | 354 |
|
348 | 355 | class FirefoxRegressionProfile(Profile):
|
@@ -616,3 +623,171 @@ def cleanup(self):
|
616 | 623 | # always remove tempdir
|
617 | 624 | if self.tempdir is not None:
|
618 | 625 | remove(self.tempdir)
|
| 626 | + |
| 627 | + |
| 628 | +# Should this be part of mozrunner ? |
| 629 | +class SnapRunner(GeckoRuntimeRunner): |
| 630 | + _allow_sudo = False |
| 631 | + _snap_pkg = None |
| 632 | + |
| 633 | + def __init__(self, binary, cmdargs, allow_sudo=False, snap_pkg=None, **runner_args): |
| 634 | + self._allow_sudo = allow_sudo |
| 635 | + self._snap_pkg = snap_pkg |
| 636 | + super().__init__(binary, cmdargs, **runner_args) |
| 637 | + |
| 638 | + @property |
| 639 | + def command(self): |
| 640 | + """ |
| 641 | + Rewrite the command for performing the actual execution with |
| 642 | + "snap run PKG", keeping everything else |
| 643 | + """ |
| 644 | + self._command = FirefoxSnapLauncher._get_snap_command( |
| 645 | + self._allow_sudo, "run", [self._snap_pkg] + super().command[1:] |
| 646 | + ) |
| 647 | + return self._command |
| 648 | + |
| 649 | + |
| 650 | +@REGISTRY.register("firefox-snap") |
| 651 | +class FirefoxSnapLauncher(MozRunnerLauncher): |
| 652 | + profile_class = FirefoxRegressionProfile |
| 653 | + instanceKey = None |
| 654 | + snap_pkg = None |
| 655 | + binary = None |
| 656 | + allow_sudo = False |
| 657 | + disable_snap_connect = False |
| 658 | + runner = None |
| 659 | + |
| 660 | + def __init__(self, dest, **kwargs): |
| 661 | + self.allow_sudo = kwargs["launcher_args"]["allow_sudo"] |
| 662 | + self.disable_snap_connect = kwargs["launcher_args"]["disable_snap_connect"] |
| 663 | + |
| 664 | + if not self.allow_sudo: |
| 665 | + LOG.info( |
| 666 | + "Working with snap requires several 'sudo snap' commands. " |
| 667 | + "Not allowing the use of sudo will trigger many password confirmation dialog boxes." |
| 668 | + ) |
| 669 | + else: |
| 670 | + LOG.info("Usage of sudo enabled, you should be prompted for your password once.") |
| 671 | + |
| 672 | + super().__init__(dest) |
| 673 | + |
| 674 | + def get_snap_command(self, action, extra): |
| 675 | + return FirefoxSnapLauncher._get_snap_command(self.allow_sudo, action, extra) |
| 676 | + |
| 677 | + def _get_snap_command(allow_sudo, action, extra): |
| 678 | + if action not in ("connect", "install", "run", "refresh", "remove"): |
| 679 | + raise LauncherError(f"Snap operation {action} unsupported") |
| 680 | + |
| 681 | + cmd = [] |
| 682 | + if allow_sudo and action in ("connect", "install", "refresh", "remove"): |
| 683 | + cmd += ["sudo"] |
| 684 | + |
| 685 | + cmd += ["snap", action] |
| 686 | + cmd += extra |
| 687 | + |
| 688 | + return cmd |
| 689 | + |
| 690 | + def _install(self, dest): |
| 691 | + # From https://snapcraft.io/docs/parallel-installs#heading--naming |
| 692 | + # - The instance key needs to be manually appended to the snap name, |
| 693 | + # and takes the following format: <snap>_<instance-key> |
| 694 | + # - The instance key must match the following regular expression: |
| 695 | + # ^[a-z0-9]{1,10}$. |
| 696 | + self.instanceKey = hashlib.sha1(os.path.basename(dest).encode("utf8")).hexdigest()[0:9] |
| 697 | + self.snap_pkg = "firefox_{}".format(self.instanceKey) |
| 698 | + self.binary = "/snap/{}/current/usr/lib/firefox/firefox".format(self.snap_pkg) |
| 699 | + |
| 700 | + subprocess.run( |
| 701 | + self.get_snap_command( |
| 702 | + "install", ["--name", self.snap_pkg, "--dangerous", "{}".format(dest)] |
| 703 | + ), |
| 704 | + check=True, |
| 705 | + ) |
| 706 | + self._fix_connections() |
| 707 | + |
| 708 | + self.binarydir = os.path.dirname(self.binary) |
| 709 | + self.appdir = os.path.normpath(os.path.join(self.binarydir, "..", "..")) |
| 710 | + |
| 711 | + LOG.debug(f"snap package: {self.snap_pkg} {self.binary}") |
| 712 | + |
| 713 | + # On Snap updates are already disabled |
| 714 | + |
| 715 | + def _fix_connections(self): |
| 716 | + if self.disable_snap_connect: |
| 717 | + return |
| 718 | + |
| 719 | + existing = {} |
| 720 | + for line in subprocess.getoutput("snap connections {}".format(self.snap_pkg)).splitlines()[ |
| 721 | + 1: |
| 722 | + ]: |
| 723 | + interface, plug, slot, _ = line.split() |
| 724 | + existing[plug] = slot |
| 725 | + |
| 726 | + for line in subprocess.getoutput("snap connections firefox").splitlines()[1:]: |
| 727 | + interface, plug, slot, _ = line.split() |
| 728 | + ex_plug = plug.replace("firefox:", "{}:".format(self.snap_pkg)) |
| 729 | + ex_slot = slot.replace("firefox:", "{}:".format(self.snap_pkg)) |
| 730 | + if existing[ex_plug] == "-": |
| 731 | + if ex_plug != "-" and ex_slot != "-": |
| 732 | + cmd = self.get_snap_command( |
| 733 | + "connect", ["{}".format(ex_plug), "{}".format(ex_slot)] |
| 734 | + ) |
| 735 | + LOG.debug(f"snap connect: {cmd}") |
| 736 | + subprocess.run(cmd, check=True) |
| 737 | + |
| 738 | + def _create_profile(self, profile=None, addons=(), preferences=None): |
| 739 | + """ |
| 740 | + Let's create a profile as usual, but rewrite its path to be in Snap's |
| 741 | + dir because it looks like MozProfile class will consider a profile=xxx |
| 742 | + to be a pre-existing one |
| 743 | + """ |
| 744 | + real_profile = super()._create_profile(profile, addons, preferences) |
| 745 | + snap_profile_dir = os.path.abspath( |
| 746 | + os.path.expanduser("~/snap/{}/common/.mozilla/firefox/".format(self.snap_pkg)) |
| 747 | + ) |
| 748 | + if not os.path.exists(snap_profile_dir): |
| 749 | + os.makedirs(snap_profile_dir) |
| 750 | + profile_dir_name = os.path.basename(real_profile.profile) |
| 751 | + snap_profile = os.path.join(snap_profile_dir, profile_dir_name) |
| 752 | + move(real_profile.profile, snap_profile_dir) |
| 753 | + real_profile.profile = snap_profile |
| 754 | + return real_profile |
| 755 | + |
| 756 | + def _start( |
| 757 | + self, |
| 758 | + profile=None, |
| 759 | + addons=(), |
| 760 | + cmdargs=(), |
| 761 | + preferences=None, |
| 762 | + adb_profile_dir=None, |
| 763 | + allow_sudo=False, |
| 764 | + disable_snap_connect=False, |
| 765 | + ): |
| 766 | + profile = self._create_profile(profile=profile, addons=addons, preferences=preferences) |
| 767 | + |
| 768 | + LOG.info("Launching %s [%s]" % (self.binary, self.allow_sudo)) |
| 769 | + self.runner = SnapRunner( |
| 770 | + binary=self.binary, |
| 771 | + cmdargs=cmdargs, |
| 772 | + profile=profile, |
| 773 | + allow_sudo=self.allow_sudo, |
| 774 | + snap_pkg=self.snap_pkg, |
| 775 | + ) |
| 776 | + self.runner.start() |
| 777 | + |
| 778 | + def _wait(self): |
| 779 | + self.runner.wait() |
| 780 | + |
| 781 | + def _stop(self): |
| 782 | + self.runner.stop() |
| 783 | + # release the runner since it holds a profile reference |
| 784 | + del self.runner |
| 785 | + |
| 786 | + def cleanup(self): |
| 787 | + try: |
| 788 | + Launcher.cleanup(self) |
| 789 | + finally: |
| 790 | + subprocess.run(self.get_snap_command("remove", [self.snap_pkg])) |
| 791 | + |
| 792 | + def get_app_info(self): |
| 793 | + return safe_get_version(binary=self.binary) |
0 commit comments