Error runProgram(const core::FilePath& programFilePath,
                 const std::vector<std::string>& args,
                 const core::system::Options& extraEnvVars,
                 const core::FilePath& workingDir,
                 const boost::function<void(const std::string&)>& onOutput,
                 const boost::function<void(int,const std::string&)>& onExited)
{
   // get system program file path
   std::string programPath = string_utils::utf8ToSystem(
                                          programFilePath.absolutePath());

   // setup options
   core::system::ProcessOptions options;
   options.terminateChildren = true;
   options.redirectStdErrToStdOut = true;
   core::system::Options env;
   core::system::getModifiedEnv(extraEnvVars, &env);
   options.environment = env;
   options.workingDir = workingDir;

   // setup callbacks
   boost::shared_ptr<CB> pCB(new CB(onOutput, onExited));
   core::system::ProcessCallbacks cb;
   cb.onStdout = cb.onStderr = boost::bind(&CB::onOutput, pCB, _2);
   cb.onExit = boost::bind(&CB::onExit, pCB, _1);

   // run process using supervisor
   return s_processSupervisor.runProgram(programPath, args, options, cb);
}
inline Error initLocalStreamAcceptor(
   SocketAcceptorService<boost::asio::local::stream_protocol>& acceptorService,
   const core::FilePath& localStreamPath)
{
   // initialize endpoint
   using boost::asio::local::stream_protocol;
   stream_protocol::endpoint endpoint(localStreamPath.absolutePath());
   
   // get acceptor
   stream_protocol::acceptor& acceptor = acceptorService.acceptor();
   
   // open
   boost::system::error_code ec;
   acceptor.open(endpoint.protocol(), ec) ;
   if (ec)
      return Error(ec, ERROR_LOCATION) ;
   
   // bind
   acceptor.bind(endpoint, ec) ;
   if (ec)
      return Error(ec, ERROR_LOCATION) ;
   
   // chmod on the stream file
   Error error = changeFileMode(localStreamPath,
                                system::EveryoneReadWriteMode);
   if (error)
      return error;
   
   // listen
   acceptor.listen(boost::asio::socket_base::max_connections, ec) ;
   if (ec)
      return Error(ec, ERROR_LOCATION) ;
   
   return Success() ;
}
Beispiel #3
0
Error appendToFile(const core::FilePath& filePath,
                       const ContentType& content)
{
   using namespace boost::system::errc ;
   
   // open the file stream
   boost::shared_ptr<std::ostream> pOfs;
   Error error = filePath.open_w(&pOfs, false);
   if (error)
      return error;

   try
   {
      pOfs->seekp(0, std::ios_base::end);

      // append the content
      *pOfs << content  ;
      if (pOfs->fail())
         return systemError(io_error, ERROR_LOCATION);
   }
   catch(const std::exception& e)
   {
      Error error = systemError(boost::system::errc::io_error,
                                ERROR_LOCATION);
      error.addProperty("what", e.what());
      error.addProperty("path", filePath.absolutePath());
      return error;
   }

   return Success() ;
}
Beispiel #4
0
core::Error DataCapture::connectDataCapture(
    const core::FilePath& outputFolder,
    const json::Object& chunkOptions)
{
    return r::exec::RFunction(".rs.initDataCapture",
                              string_utils::utf8ToSystem(outputFolder.absolutePath()),
                              chunkOptions).call();
}
core::Error HtmlCapture::connectHtmlCapture(
              const core::FilePath& outputFolder,
              const core::FilePath& libraryFolder,
              const json::Object& chunkOptions)
{
   return r::exec::RFunction(".rs.initHtmlCapture", 
         string_utils::utf8ToSystem(outputFolder.absolutePath()),
         string_utils::utf8ToSystem(outputFolder.complete(kChunkLibDir).absolutePath()),
         chunkOptions).call();
}
Beispiel #6
0
Error readStringFromFile(
    const core::FilePath& filePath,
    const Filter& filter,
    std::string* pContents,
    string_utils::LineEnding lineEnding=string_utils::LineEndingPassthrough)
{
    try
    {
        // open the file stream (report errors with exceptions)
        boost::shared_ptr<std::istream> pIfs;
        Error error = filePath.open_r(&pIfs);
        if (error)
            return error;
        pIfs->exceptions(std::istream::failbit | std::istream::badbit);

        // output string stream (report errors with exceptions)
        std::stringstream ostr;
        ostr.exceptions(std::ostream::failbit | std::ostream::badbit);

        // do the copy
        boost::iostreams::filtering_ostream filteringOStream ;
        filteringOStream.push(filter);
        filteringOStream.push(ostr);
        boost::iostreams::copy(*pIfs, filteringOStream, 128);

        // return contents with requested line endings
        *pContents = ostr.str();
        string_utils::convertLineEndings(pContents, lineEnding);

        return Success();
    }
    catch(const std::exception& e)
    {
        Error error = systemError(boost::system::errc::io_error,
                                  ERROR_LOCATION);
        error.addProperty("what", e.what());
        error.addProperty("path", filePath.absolutePath());
        return error;
    }

    return Success();
}
Beispiel #7
0
Error writeCollectionToFile(
         const core::FilePath& filePath, 
         const CollectionType& collection,
         boost::function<std::string(
                                 const typename CollectionType::value_type&)>
                         stringifyFunction)
{
   using namespace boost::system::errc ;
   
   // open the file stream
   boost::shared_ptr<std::ostream> pOfs;
   Error error = filePath.open_w(&pOfs, true);
   if (error)
      return error;

   try
   {
      // write each line
      for (typename CollectionType::const_iterator
            it = collection.begin();
            it != collection.end();
            ++it)
      {
         *pOfs << stringifyFunction(*it) << std::endl ;

        if (pOfs->fail())
             return systemError(io_error, ERROR_LOCATION);
      }
   }
   catch(const std::exception& e)
   {
      Error error = systemError(boost::system::errc::io_error,
                                ERROR_LOCATION);
      error.addProperty("what", e.what());
      error.addProperty("path", filePath.absolutePath());
      return error;
   }

   return Success() ;
}
Error PlotManager::savePlotAsMetafile(const core::FilePath& filePath,
                                      int widthPx,
                                      int heightPx)
{ 
#ifdef _WIN32
   // calculate size in inches
   double widthInches = pixelsToInches(widthPx);
   double heightInches = pixelsToInches(heightPx);

   // generate code for creating metafile device
   boost::format fmt("{ require(grDevices, quietly=TRUE); "
                     "  win.metafile(filename=\"%1%\", width=%2%, height=%3%, "
                     "               restoreConsole=FALSE); }");
   std::string deviceCreationCode = boost::str(fmt % string_utils::utf8ToSystem(filePath.absolutePath()) %
                                                     widthInches %
                                                     heightInches);

   return savePlotAsFile(deviceCreationCode);

#else
   return systemError(boost::system::errc::not_supported, ERROR_LOCATION);
#endif
}
Beispiel #9
0
std::string escape(const ::core::FilePath &path)
{
   return escape(string_utils::utf8ToSystem(path.absolutePath()));
}
Beispiel #10
0
void runEmbeddedR(const core::FilePath& rHome,
                  const core::FilePath& userHome,
                  bool quiet,
                  bool loadInitFile,
                  SA_TYPE defaultSaveAction,
                  const Callbacks& callbacks,
                  InternalCallbacks* pInternal)
{
   // no signal handlers (see comment in REmbeddedPosix.cpp for rationale)
   R_SignalHandlers = 0;

   // set start time
   ::R_setStartTime();

   // setup params structure
   structRstart rp;
   Rstart pRP = &rp;
   ::R_DefParams(pRP);

   // set paths (copy to new string so we can provide char*)
   std::string* pRHome = new std::string(rHome.absolutePath());
   std::string* pUserHome = new std::string(userHome.absolutePath());
   pRP->rhome = const_cast<char*>(pRHome->c_str());
   pRP->home = const_cast<char*>(pUserHome->c_str());

   // more configuration
   pRP->CharacterMode = RGui;
   pRP->R_Slave = FALSE;
   pRP->R_Quiet = quiet ? TRUE : FALSE;
   pRP->R_Interactive = TRUE;
   pRP->SaveAction = defaultSaveAction;
   pRP->RestoreAction = SA_NORESTORE;
   pRP->LoadInitFile = loadInitFile ? TRUE : FALSE;

   // hooks
   pRP->ReadConsole = callbacks.readConsole;
   pRP->WriteConsole = NULL;
   pRP->WriteConsoleEx = callbacks.writeConsoleEx;
   pRP->CallBack = rPolledEventCallback;
   pRP->ShowMessage = showMessage;
   pRP->YesNoCancel = askYesNoCancel;
   pRP->Busy = callbacks.busy;

   // set internal callbacks
   pInternal->cleanUp = R_CleanUp;
   pInternal->suicide = R_Suicide;

   // set command line
   const char *args[]= {"RStudio", "--interactive"};
   int argc = sizeof(args)/sizeof(args[0]);
   ::R_set_command_line_arguments(argc, (char**)args);

   // set params
   ::R_SetParams(pRP);

   // clear console input buffer
   ::FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));

   // R global ui initialization
   ::GA_initapp(0, 0);
   ::readconsolecfg();

   // Set CharacterMode to LinkDLL during main loop setup. The mode can't be
   // RGui during setup_Rmainloop or calls to history functions (e.g. timestamp)
   // which occur during .Rprofile execution will crash when R attempts to
   // interact with the (non-existent) R gui console data structures. Note that
   // there are no references to CharacterMode within setup_Rmainloop that we
   // can see so the only side effect of this should be the disabling of the
   // console history mechanism.
   CharacterMode = LinkDLL;

   // setup main loop
   ::setup_Rmainloop();

   // reset character mode to RGui
   CharacterMode = RGui;

   // run main loop
   ::run_Rmainloop();
}
Beispiel #11
0
 explicit ShellCommand(const core::FilePath& filePath)
 {
    output_ = escape(string_utils::utf8ToSystem(filePath.absolutePath()));
 }
std::string createViewPdfUrl(const core::FilePath& filePath)
{
   return "view_pdf?path=" + http::util::urlEncode(filePath.absolutePath(),
                                                   true);
}
Beispiel #13
0
Error readCollectionFromFile(
         const core::FilePath& filePath,
         CollectionType* pCollection,
         boost::function<ReadCollectionAction(const std::string& line, 
                                 typename CollectionType::value_type* pValue)>
                         parseFunction,
                         bool trimAndIgnoreBlankLines=true)
{
   using namespace boost::system::errc ;
   
   // open the file stream
   boost::shared_ptr<std::istream> pIfs;
   Error error = filePath.open_r(&pIfs);
   if (error)
      return error;
   
   // create insert iterator
   std::insert_iterator<CollectionType> insertIterator(*pCollection, 
                                                       pCollection->begin());

   try
   {
      // read each line
      std::string nextLine ;
      while (true)
      {
         // read the next line
         std::getline(*pIfs, nextLine) ;
         if (pIfs->eof())
            break;
         else if (pIfs->fail())
            return systemError(io_error, ERROR_LOCATION);

         // trim whitespace then ignore it if it is a blank line
         if (trimAndIgnoreBlankLines)
         {
            boost::algorithm::trim(nextLine) ;
            if (nextLine.empty())
               continue ;
         }

         // parse it and add it to the collection
         typename CollectionType::value_type value ;
         ReadCollectionAction action = parseFunction(nextLine, &value);
         if (action == ReadCollectionAddLine)
         {
            *insertIterator++ = value ;
         }
         else if (action == ReadCollectionIgnoreLine)
         {
            // do nothing
         }
         else if (action == ReadCollectionTerminate)
         {
            break; // exit read loop
         }
      }
   }
   catch(const std::exception& e)
   {
      Error error = systemError(boost::system::errc::io_error,
                                ERROR_LOCATION);
      error.addProperty("what", e.what());
      error.addProperty("path", filePath.absolutePath());
      return error;
   }
   
   return Success() ;
}