Beispiel #1
0
def check_sdk_root():
    """Check validity of NACL_SDK_ROOT."""
    if current_toolchain == 'emscripten':
        check_emscripten_root()
        return

    root = get_sdk_root()

    if not os.path.isdir(root):
        raise error.Error('$NACL_SDK_ROOT does not exist: %s' % root)

    landmark = os.path.join(root, 'tools', 'getos.py')
    if not os.path.exists(landmark):
        raise error.Error("$NACL_SDK_ROOT (%s) doesn't look right. "
                          "Couldn't find landmark file (%s)" %
                          (root, landmark))

    if not check_sdk_version(MIN_SDK_VERSION):
        raise error.Error(
            'This version of webports requires at least version %s of\n'
            'the NaCl SDK. The version in $NACL_SDK_ROOT is %s. If you want\n'
            'to use webports with an older version of the SDK please checkout\n'
            'one of the pepper_XX branches (or run with\n'
            '--skip-sdk-version-check).' %
            (MIN_SDK_VERSION, get_sdk_version()))
Beispiel #2
0
  def verify_archive_format(self):
    if not os.path.exists(self.filename):
      raise error.Error('package archive not found: %s' % self.filename)
    basename, extension = os.path.splitext(os.path.basename(self.filename))
    basename = os.path.splitext(basename)[0]
    if extension != '.bz2':
      raise error.Error('invalid file extension: %s' % extension)

    try:
      with tarfile.open(self.filename):
        pass
    except tarfile.TarError as e:
      raise error.PkgFormatError(e)
Beispiel #3
0
    def _install_files(self, force):
        dest = util.get_install_root(self.config)
        dest_tmp = os.path.join(dest, 'install_tmp')
        if os.path.exists(dest_tmp):
            shutil.rmtree(dest_tmp)

        if self.is_any_version_installed():
            raise error.Error('package already installed: %s' %
                              self.info_string())

        self.log_status('Installing')
        util.log_verbose('installing from: %s' % self.filename)
        util.makedirs(dest_tmp)

        names = []
        try:
            with tarfile.open(self.filename) as tar:
                for info in tar:
                    if info.isdir():
                        continue
                    name = posixpath.normpath(info.name)
                    if name == 'pkg_info':
                        continue
                    if not name.startswith(PAYLOAD_DIR + '/'):
                        raise error.PkgFormatError(
                            'invalid file in package: %s' % name)

                    name = name[len(PAYLOAD_DIR) + 1:]
                    names.append(name)

                if not force:
                    for name in names:
                        full_name = os.path.join(dest, name)
                        if os.path.exists(full_name):
                            raise error.Error('file already exists: %s' %
                                              full_name)

                tar.extractall(dest_tmp)
                payload_tree = os.path.join(dest_tmp, PAYLOAD_DIR)

                names = filter_out_executables(names, payload_tree)

                for name in names:
                    install_file(name, payload_tree, dest)
        finally:
            shutil.rmtree(dest_tmp)

        for name in names:
            relocate_file(name, dest)

        self.write_file_list(names)
Beispiel #4
0
def get_emscripten_root():
  emscripten = os.environ.get('EMSCRIPTEN')
  if emscripten is None:
    local_root = os.path.join(paths.OUT_DIR, 'emsdk', 'emscripten')
    if os.path.exists(local_root):
      emscripten = local_root
    else:
      raise error.Error('$EMSCRIPTEN not set and %s does not exist.' %
                        local_root)

  if not os.path.isdir(emscripten):
    raise error.Error('$EMSCRIPTEN environment variable does not point'
                      ' to a directory: %s' % emscripten)
  return emscripten
Beispiel #5
0
  def _InstallFiles(self, force):
    dest = util.GetInstallRoot(self.config)
    dest_tmp = os.path.join(dest, 'install_tmp')
    if os.path.exists(dest_tmp):
      shutil.rmtree(dest_tmp)

    if self.IsAnyVersionInstalled():
      raise error.Error('package already installed: %s' % self.InfoString())

    self.LogStatus('Installing')
    util.LogVerbose('installing from: %s' % self.filename)
    util.Makedirs(dest_tmp)

    names = []
    try:
      with tarfile.open(self.filename) as tar:
        for info in tar:
          if info.isdir():
            continue
          name = posixpath.normpath(info.name)
          if name == 'pkg_info':
            continue
          if not name.startswith(PAYLOAD_DIR + '/'):
            raise error.PkgFormatError('invalid file in package: %s' % name)

          name = name[len(PAYLOAD_DIR) + 1:]
          names.append(name)

        if not force:
          for name in names:
            full_name = os.path.join(dest, name)
            if os.path.exists(full_name):
              raise error.Error('file already exists: %s' % full_name)

        tar.extractall(dest_tmp)
        payload_tree = os.path.join(dest_tmp, PAYLOAD_DIR)

        names = FilterOutExecutables(names, payload_tree)

        for name in names:
          InstallFile(name, payload_tree, dest)
    finally:
      shutil.rmtree(dest_tmp)

    for name in names:
      RelocateFile(name, dest)

    self.WriteFileList(names)
Beispiel #6
0
def create_installed_package(package_name, config=None):
    stamp_root = util.get_install_stamp_root(config)
    info_file = os.path.join(stamp_root, package_name + '.info')
    if not os.path.exists(info_file):
        raise error.Error('package not installed: %s [%s]' %
                          (package_name, config))
    return InstalledPackage(info_file)
Beispiel #7
0
def download_file(filename, url):
  """Download a file from a given URL.

  Args:
    filename: the name of the file to download the URL to.
    url: then URL to fetch.
  """
  temp_filename = filename + '.partial'
  # Ensure curl is in user's PATH
  find_in_path('curl')
  curl_cmd = ['curl', '-k', '--fail', '--location', '--stderr', '-', '-o',
              temp_filename]
  if hasattr(sys.stdout, 'fileno') and os.isatty(sys.stdout.fileno()):
    # Add --progress-bar but only if stdout is a TTY device.
    curl_cmd.append('--progress-bar')
  else:
    # otherwise suppress status output, since curl always assumes its
    # talking to a TTY and writes \r and \b characters.  But add
    # --show-error so that when curl fails it at least prints something.
    curl_cmd += ['--silent', '--show-error']
  curl_cmd.append(url)

  if log_level > LOG_WARN:
    log('Downloading: %s [%s]' % (url, filename))
  else:
    log('Downloading: %s' % url.replace(GS_URL, ''))
  try:
    subprocess.check_call(curl_cmd)
  except subprocess.CalledProcessError as e:
    raise error.Error('Error downloading file: %s' % str(e))

  os.rename(temp_filename, filename)
Beispiel #8
0
def remove_tree(directory):
  """Recursively remove a directory and its contents."""
  if not os.path.exists(directory):
    return
  if not os.path.isdir(directory):
    raise error.Error('RemoveTree: not a directory: %s', directory)
  shutil.rmtree(directory)
Beispiel #9
0
 def __enter__(self):
     try:
         fcntl.flock(self.fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
     except Exception:
         raise error.Error(
             "Unable to acquire lock (%s): Is webports already "
             "running?" % self.file_name)
Beispiel #10
0
def makedirs(directory):
  if os.path.isdir(directory):
    return
  if os.path.exists(directory):
    raise error.Error('mkdir: File exists and is not a directory: %s' %
                      directory)
  trace("mkdir: %s" % directory)
  os.makedirs(directory)
Beispiel #11
0
 def check_deps(pkg_name, required_by):
     if pkg_name in checked:
         return
     checked.add(pkg_name)
     pkg = installed_map.get(pkg_name)
     if not pkg:
         raise error.Error("missing package '%s' required by '%s'" %
                           (pkg_name, required_by))
     for dep in pkg.DEPENDS:
         check_deps(dep, pkg.NAME)
Beispiel #12
0
def CmdPkgUninstall(package, options):
  """Uninstall package(s)"""
  for dep in package.ReverseDependencies():
    if options.force or options.all:
      PerformUninstall(dep)
    else:
      raise error.Error("unable to uninstall '%s'. another package is "
          "installed which depends on it: '%s'" % (package.NAME, dep.NAME))

  package.Uninstall()
Beispiel #13
0
def cmd_info(config, options, args):
    """Print infomation on installed package(s)"""
    if len(args) != 1:
        raise error.Error('info command takes a single package name')
    package_name = args[0]
    pkg = installed_package.create_installed_package(package_name, config)
    info_file = pkg.get_install_stamp()
    print('Install receipt: %s' % info_file)
    with open(info_file) as f:
        sys.stdout.write(f.read())
Beispiel #14
0
def cmd_pkg_uninstall(package, options):
    """Uninstall package(s)"""
    for dep in package.reverse_dependencies():
        if options.force or options.all:
            perform_uninstall(dep)
        else:
            raise error.Error("unable to uninstall '%s'. another package is "
                              "installed which depends on it: '%s'" %
                              (package.NAME, dep.NAME))

    package.uninstall()
Beispiel #15
0
def get_sdk_root():
  """Returns the root of the currently configured Native Client SDK."""
  root = os.environ.get('NACL_SDK_ROOT')
  if root is None:
    local_sdk_root = os.path.join(paths.OUT_DIR, 'nacl_sdk')
    if os.path.exists(local_sdk_root):
      root = local_sdk_root
    else:
      raise error.Error('$NACL_SDK_ROOT not set')
  if sys.platform == "cygwin":
    root = root.replace('\\', '/')
  return root
Beispiel #16
0
    def __init__(self, arch=None, toolchain=None, debug=None):
        self.debug = None
        self.libc = None
        self.config_name = None

        self.SetConfig(debug)

        if arch is None:
            arch = os.environ.get('NACL_ARCH')

        if toolchain is None:
            toolchain = os.environ.get('TOOLCHAIN')

        if not toolchain:
            if arch == 'pnacl':
                toolchain = 'pnacl'
            elif arch == 'emscripten':
                toolchain = 'emscripten'
            else:
                toolchain = self.default_toolchain

        self.toolchain = toolchain
        if self.toolchain not in VALID_TOOLCHAINS:
            raise error.Error("Invalid toolchain: %s" % self.toolchain)

        if not arch:
            if self.toolchain == 'pnacl':
                arch = 'pnacl'
            elif self.toolchain == 'emscripten':
                arch = 'emscripten'
            elif platform.machine() == 'i686':
                arch = 'i686'
            else:
                arch = 'x86_64'

        self.arch = arch
        if self.arch not in util.arch_to_pkgarch:
            raise error.Error("Invalid arch: %s" % arch)

        self.SetLibc()
Beispiel #17
0
  def parse_index(self, index_data):
    if not index_data:
      return

    for info_files in index_data.split('\n\n'):
      info = pkg_info.parse_pkg_info(info_files, self.filename, self.valid_keys,
                                     self.required_keys)
      debug = info['BUILD_CONFIG'] == 'debug'
      config = configuration.Configuration(info['BUILD_ARCH'],
                                           info['BUILD_TOOLCHAIN'], debug)
      key = (info['NAME'], config)
      if key in self.packages:
        error.Error('package index contains duplicate: %s' % str(key))
      self.packages[key] = info
Beispiel #18
0
def cmd_list(config, options, args):
    """List installed packages"""
    if len(args):
        raise error.Error('list command takes no arguments')
    if options.all:
        iterator = source_package.source_package_iterator()
    else:
        iterator = installed_package.installed_package_iterator(config)
    for package in iterator:
        if options.verbosity:
            sys.stdout.write('%-15s %s\n' % (package.NAME, package.VERSION))
        else:
            sys.stdout.write(package.NAME + '\n')
    return 0
Beispiel #19
0
def find_in_path(command_name):
  """Search user's PATH for a given executable.

  Returns:
    Full path to executable.
  """
  extensions = ('',)
  if not os.path.splitext(command_name)[1] and os.name == 'nt':
    extensions = ('.bat', '.com', '.exe')

  for path in os.environ.get('PATH', '').split(os.pathsep):
    for ext in extensions:
      full_name = os.path.join(path, command_name + ext)
      if os.path.exists(full_name) and os.path.isfile(full_name):
        return full_name

  raise error.Error('command not found: %s' % command_name)
Beispiel #20
0
def setup_emscripten():
  if 'EMSCRIPTEN' in os.environ:
    return

  local_root = get_emscripten_root()
  os.environ['EMSCRIPTEN'] = local_root
  os.environ['EM_CONFIG'] = os.path.join(
      os.path.dirname(local_root), '.emscripten')
  try:
    find_in_path('node')
  except error.Error:
    node_bin = os.path.join(paths.OUT_DIR, 'node', 'bin')
    if not os.path.isdir(node_bin):
      raise error.Error(
          'node not found in path and default path not found: %s' % node_bin)

    os.environ['PATH'] += os.pathsep + node_bin
    find_in_path('node')
Beispiel #21
0
def SetupEmscripten():
    if 'EMSCRIPTEN' in os.environ:
        return

    local_root = GetEmscriptenRoot()
    os.environ['EMSCRIPTEN'] = local_root
    os.environ['EM_CONFIG'] = os.path.join(os.path.dirname(local_root),
                                           '.emscripten')
    try:
        FindInPath('node')
    except error.Error:
        node_bin = os.path.join(paths.OUT_DIR, 'node', 'bin')
        if not os.path.isdir(node_bin):
            raise error.Error(
                'node not found in path and default path not found: %s' %
                node_bin)

        os.environ['PATH'] += ':' + node_bin
        FindInPath('node')
Beispiel #22
0
class TestMain(common.NaclportsTest):
    def setUp(self):
        super(TestMain, self).setUp()
        self.AddPatch(patch('webports.util.CheckSDKRoot'))

    @patch('webports.util.Log', Mock())
    @patch('webports.util.RemoveTree')
    def testCleanAll(self, mock_rmtree):
        config = Configuration()
        webports.__main__.CleanAll(config)
        mock_rmtree.assert_any_call('/package/install/path')

    @patch('webports.__main__.RunMain', Mock(side_effect=error.Error('oops')))
    def testErrorReport(self):
        # Verify that exceptions of the type error.Error are printed
        # to stderr and result in a return code of 1
        with patch('sys.stderr', new_callable=StringIO.StringIO) as stderr:
            self.assertEqual(webports.__main__.main(None), 1)
        self.assertRegexpMatches(stderr.getvalue(), '^webports: oops')

    @patch('webports.__main__.CmdPkgClean')
    def testMainCommandDispatch(self, cmd_pkg_clean):
        mock_pkg = Mock()
        with patch('webports.source_package.CreatePackage',
                   Mock(return_value=mock_pkg)):
            webports.__main__.RunMain(['clean', 'foo'])
        cmd_pkg_clean.assert_called_once_with(mock_pkg, mock.ANY)

    @patch('webports.__main__.CmdPkgClean',
           Mock(side_effect=error.DisabledError()))
    def testMainHandlePackageDisabled(self):
        mock_pkg = Mock()
        with patch('webports.source_package.CreatePackage',
                   Mock(return_value=mock_pkg)):
            with self.assertRaises(error.DisabledError):
                webports.__main__.RunMain(['clean', 'foo'])

    @patch('webports.__main__.CleanAll')
    def testMainCleanAll(self, clean_all_mock):
        webports.__main__.RunMain(['clean', '--all'])
        clean_all_mock.assert_called_once_with(Configuration())
Beispiel #23
0
class TestMain(common.NaclportsTest):
    def setUp(self):
        super(TestMain, self).setUp()
        self.add_patch(patch('webports.util.check_sdk_root'))

    @patch('webports.util.log', Mock())
    @patch('webports.util.remove_tree')
    def test_clean_all(self, mock_rmtree):
        config = Configuration()
        webports.__main__.clean_all(config)
        mock_rmtree.assert_any_call('/package/install/path')

    @patch('webports.__main__.run_main', Mock(side_effect=error.Error('oops')))
    def test_error_report(self):
        # Verify that exceptions of the type error.Error are printed
        # to stderr and result in a return code of 1
        with patch('sys.stderr', new_callable=io.StringIO) as stderr:
            self.assertEqual(webports.__main__.main(None), 1)
        self.assertRegex(stderr.getvalue(), '^webports: oops')

    @patch('webports.__main__.cmd_pkg_clean')
    def test_main_command_dispatch(self, cmd_pkg_clean):
        mock_pkg = Mock()
        with patch('webports.source_package.create_package',
                   Mock(return_value=mock_pkg)):
            webports.__main__.run_main(['clean', 'foo'])
        cmd_pkg_clean.assert_called_once_with(mock_pkg, mock.ANY)

    @patch('webports.__main__.cmd_pkg_clean',
           Mock(side_effect=error.DisabledError()))
    def test_main_handle_package_disabled(self):
        mock_pkg = Mock()
        with patch('webports.source_package.create_package',
                   Mock(return_value=mock_pkg)):
            with self.assertRaises(error.DisabledError):
                webports.__main__.run_main(['clean', 'foo'])

    @patch('webports.__main__.clean_all')
    def test_main_clean_all(self, clean_all_mock):
        webports.__main__.run_main(['clean', '--all'])
        clean_all_mock.assert_called_once_with(Configuration())
Beispiel #24
0
def CmdCheck(config, options, args):
  """Verify the dependecies of all install packages are also installed."""
  if len(args):
    raise error.Error('check command takes no arguments')

  installed_packages = installed_package.InstalledPackageIterator(config)
  installed_map = {pkg.NAME: pkg for pkg in installed_packages}

  checked = set()
  def CheckDeps(pkg_name, required_by):
    if pkg_name in checked:
      return
    checked.add(pkg_name)
    pkg = installed_map.get(pkg_name)
    if not pkg:
      raise error.Error("missing package '%s' required by '%s'" % (pkg_name,
          required_by))
    for dep in pkg.DEPENDS:
      CheckDeps(dep, pkg.NAME)

  for pkg in installed_map.itervalues():
    CheckDeps(pkg.NAME, None)
Beispiel #25
0
    def do_uninstall(self, force):
        with util.InstallLock(self.config):
            if not force:
                for pkg in installed_package_iterator(self.config):
                    if self.NAME in pkg.DEPENDS:
                        raise error.Error(
                            "Unable to uninstall '%s' (depended on by '%s')" %
                            (self.NAME, pkg.NAME))
            remove_file(self.get_install_stamp())

            root = util.get_install_root(self.config)
            for filename in self.files():
                fullname = os.path.join(root, filename)
                if not os.path.lexists(fullname):
                    util.warn('File not found while uninstalling: %s' %
                              fullname)
                    continue
                util.log_verbose('uninstall: %s' % filename)
                remove_file(fullname)

            if os.path.exists(self.get_list_file()):
                remove_file(self.get_list_file())
Beispiel #26
0
def check_emscripten_root():
  root = get_emscripten_root()
  if not os.path.isdir(root):
    raise error.Error('$EMSCRIPTEN does not exist: %s' % root)