tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

run-wizard (5243B)


      1 #!/usr/bin/env python3
      2 # This Source Code Form is subject to the terms of the Mozilla Public
      3 # License, v. 2.0. If a copy of the MPL was not distributed with this,
      4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
      5 
      6 import datetime
      7 import os
      8 import subprocess
      9 import sys
     10 import time
     11 import shutil
     12 from textwrap import wrap
     13 
     14 here = os.path.dirname(os.path.abspath(__file__))
     15 MOZHARNESS_WORKDIR = os.path.expanduser(os.path.join('~', 'workspace', 'build'))
     16 
     17 MACH_SETUP_FINISHED = """
     18 Mozharness has finished downloading the build and tests to:
     19 {}
     20 
     21 A limited mach environment has also been set up and added to the $PATH, but
     22 it may be missing the command you need. To see a list of commands, run:
     23     $ mach help
     24 """.lstrip().format(MOZHARNESS_WORKDIR)
     25 
     26 MACH_SETUP_FAILED = """
     27 Could not set up mach environment, no mach binary detected.
     28 """.lstrip()
     29 
     30 
     31 def call(cmd, **kwargs):
     32     print(" ".join(cmd))
     33     return subprocess.call(cmd, **kwargs)
     34 
     35 
     36 def wait_for_run_mozharness(timeout=60):
     37     starttime = datetime.datetime.now()
     38     while datetime.datetime.now() - starttime < datetime.timedelta(seconds=timeout):
     39         if os.path.isfile(os.path.join(here, 'run-mozharness')):
     40             break
     41         time.sleep(0.2)
     42     else:
     43         print("Timed out after %d seconds waiting for the 'run-mozharness' binary" % timeout)
     44         return 1
     45 
     46 
     47 def setup_mach_environment():
     48     mach_src = os.path.join(MOZHARNESS_WORKDIR, 'tests', 'mach')
     49     if not os.path.isfile(mach_src):
     50         return 1
     51 
     52     mach_dest = os.path.expanduser(os.path.join('~', 'bin', 'mach'))
     53     if os.path.exists(mach_dest):
     54         os.remove(mach_dest)
     55     os.symlink(mach_src, mach_dest)
     56     return 0
     57 
     58 
     59 def run_mozharness(*args):
     60     wait_for_run_mozharness()
     61     try:
     62         return call(['run-mozharness'] + list(args))
     63     finally:
     64         setup_mach_environment()
     65 
     66 
     67 def setup():
     68     """Run the mozharness script without the 'run-tests' action.
     69 
     70     This will do all the necessary setup steps like creating a virtualenv and
     71     downloading the tests and firefox binary. But it stops before running the
     72     tests.
     73     """
     74     status = run_mozharness('--no-run-tests')
     75 
     76     if shutil.which('mach'):
     77         print(MACH_SETUP_FINISHED)
     78     else:
     79         print(MACH_SETUP_FAILED)
     80 
     81     return status
     82 
     83 
     84 def clone():
     85     """Clone the correct gecko repository and update to the proper revision."""
     86     base_repo = os.environ['GECKO_HEAD_REPOSITORY']
     87     dest = os.path.expanduser(os.path.join('~', 'gecko'))
     88 
     89     # Specify method to checkout a revision. This defaults to revisions as
     90     # SHA-1 strings, but also supports symbolic revisions like `tip` via the
     91     # branch flag.
     92     if os.environ.get('GECKO_HEAD_REV'):
     93         revision_flag = b'--revision'
     94         revision = os.environ['GECKO_HEAD_REV']
     95     elif os.environ.get('GECKO_HEAD_REF'):
     96         revision_flag = b'--branch'
     97         revision = os.environ['GECKO_HEAD_REF']
     98     else:
     99         print('revision is not specified for checkout')
    100         return 1
    101 
    102     # TODO Bug 1301382 - pin hg.mozilla.org fingerprint.
    103     call([
    104         b'/usr/bin/hg', b'robustcheckout',
    105         b'--sharebase', os.environ['HG_STORE_PATH'],
    106         b'--purge',
    107         b'--upstream', b'https://hg.mozilla.org/mozilla-unified',
    108         revision_flag, revision,
    109         base_repo, dest
    110     ])
    111     print("Finished cloning to {} at revision {}.".format(dest, revision))
    112 
    113 
    114 def exit():
    115     pass
    116 
    117 
    118 OPTIONS = [
    119     ('Resume task', run_mozharness,
    120      "Resume the original task without modification. This can be useful for "
    121      "passively monitoring it from another shell."),
    122     ('Setup task', setup,
    123      "Setup the task (download the application and tests) but don't run the "
    124      "tests just yet. The tests can be run with a custom configuration later. "
    125      "This will provide a mach environment (experimental)."),
    126     ('Clone gecko', clone,
    127      "Perform a clone of gecko using the task's repo and update it to the "
    128      "task's revision."),
    129     ('Exit', exit, "Exit this wizard and return to the shell.")
    130 ]
    131 
    132 
    133 def _fmt_options():
    134     max_line_len = 60
    135     max_name_len = max(len(o[0]) for o in OPTIONS)
    136 
    137     # TODO Pad will be off if there are more than 9 options.
    138     pad = ' ' * (max_name_len+6)
    139 
    140     msg = []
    141     for i, (name, _, desc) in enumerate(OPTIONS):
    142         desc = wrap(desc, width=max_line_len)
    143         desc = [desc[0]] + [pad + l for l in desc[1:]]
    144 
    145         optstr = '{}) {} - {}\n'.format(
    146             i+1, name.ljust(max_name_len), '\n'.join(desc))
    147         msg.append(optstr)
    148     msg.append("Select one of the above options: ")
    149     return '\n'.join(msg)
    150 
    151 
    152 def wizard():
    153     print("This wizard can help you get started with some common debugging "
    154           "workflows.\nWhat would you like to do?\n")
    155     print(_fmt_options(), end="")
    156     choice = None
    157     while True:
    158         choice = input()
    159         try:
    160             choice = int(choice)-1
    161             if 0 <= choice < len(OPTIONS):
    162                 break
    163         except ValueError:
    164             pass
    165 
    166         print("Must provide an integer from 1-{}:".format(len(OPTIONS)))
    167 
    168     func = OPTIONS[choice][1]
    169     ret = func()
    170 
    171     print("Use the 'run-wizard' command to start this wizard again.")
    172     return ret
    173 
    174 
    175 if __name__ == '__main__':
    176     sys.exit(wizard())