Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
nbmain.cxx
Go to the documentation of this file.
1// @(#)root/main:$Id$
2// Author: Enric Tejedor 07/10/15
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12//////////////////////////////////////////////////////////////////////////
13// //
14// NBMain //
15// //
16// Main program used to spawn a ROOT notebook //
17// //
18//////////////////////////////////////////////////////////////////////////
19
20#include "RConfigure.h"
21
22#include "TCollection.h"
23#include "TList.h"
24#include "TROOT.h"
25#include "TSystem.h"
26#include "TSystemDirectory.h"
27#include "TSystemFile.h"
28
29#include <fstream>
30#include <string>
31#include <memory>
32
33#ifdef WIN32
34#include <algorithm> // for std::replace
35#endif
36
37#define JUPYTER_CMD "jupyter"
38#define NB_OPT "notebook"
39#define JUPYTER_CONF_PATH_V "JUPYTER_CONFIG_PATH"
40#define JUPYTER_PATH_V "JUPYTER_PATH"
41#define NB_CONF_DIR "notebook"
42#define ROOTNB_DIR ".rootnb"
43#define COMMIT_FILE ".rootcommit"
44#define JUPYTER_CONFIG "jupyter_notebook_config.py"
45
46using std::string, std::ifstream, std::ofstream, std::ios, std::endl;
47
48#ifdef WIN32
49#include <process.h>
50constexpr const char *pathsep = "\\";
51#define execlp _execlp
52#else
53constexpr const char *pathsep = "/";
54#endif
55
56////////////////////////////////////////////////////////////////////////////////
57/// Checks whether ROOT notebook files are installed and they are
58/// the current version.
59
60static int CheckNbInstallation(string dir)
61{
62 string commit(gROOT->GetGitCommit());
63 string inputfname(dir + pathsep + ROOTNB_DIR + pathsep + COMMIT_FILE);
64 ifstream in(inputfname);
65 if (in.is_open()) {
66 string line;
67 in >> line;
68 in.close();
69 if (line.compare(commit) == 0) return 0; // already installed
70 else return -1; // install, it's outdated
71 }
72 else if (gSystem->AccessPathName(inputfname.c_str())) {
73 // There is no installation
74 return -1;
75 }
76 else {
77 fprintf(stderr,
78 "Error checking notebook installation -- cannot open %s\n",
79 inputfname.c_str());
80 return -2;
81 }
82}
83
84////////////////////////////////////////////////////////////////////////////////
85/// Installs ROOT notebook files in the user's home directory.
86
87static bool InstallNbFiles(string source, string dest)
88{
89 // Create installation directory
90 if (gSystem->AccessPathName(dest.c_str())) {
91 if (gSystem->mkdir(dest.c_str())) {
92 fprintf(stderr,
93 "Error installing notebook configuration files -- cannot create directory %s\n",
94 dest.c_str());
95 return false;
96 }
97 }
98
99 // Copy files in source to dest
100 TSystemDirectory dir(source.c_str(), source.c_str());
101 std::unique_ptr<TList> files;
102 files.reset(dir.GetListOfFiles());
103 if (files) {
105 TListIter it(files.get());
106 while ((file = (TSystemFile*)it())) {
107 TString s = file->GetName();
108 string fname(s.Data());
109 string sourcefile = source + pathsep + fname;
110 string destfile = dest + pathsep + fname;
111 if (!file->IsDirectory()) {
112 if (gSystem->CopyFile(sourcefile.c_str(), destfile.c_str(), true)) {
113 fprintf(stderr,
114 "Error installing notebook configuration files -- cannot copy file %s to %s\n",
115 sourcefile.c_str(), destfile.c_str());
116 return false;
117 }
118 }
119 else if (fname.compare(".") && fname.compare("..") && fname.compare("html")) {
120 if (!InstallNbFiles(sourcefile, destfile))
121 return false;
122 }
123 }
124 }
125
126 return true;
127}
128
129////////////////////////////////////////////////////////////////////////////////
130/// Creates the Jupyter notebook configuration file that sets the
131/// necessary environment.
132
133static bool CreateJupyterConfig(string dest, string rootbin, string rootlib, string rootdata)
134{
135 string jupyconfig = dest + pathsep + JUPYTER_CONFIG;
136 ofstream out(jupyconfig, ios::trunc);
137 if (out.is_open()) {
138 out << "import os" << endl;
139#ifdef WIN32
140 std::replace( rootbin.begin(), rootbin.end(), '\\', '/');
141 std::replace( rootdata.begin(), rootdata.end(), '\\', '/');
142 out << "rootbin = '" << rootbin << "'" << endl;
143 string jsrootsys = rootdata + "/js/";
144 out << "os.environ['PYTHONPATH'] = '%s' % rootbin + ';' + os.getenv('PYTHONPATH', '')" << endl;
145 out << "os.environ['PATH'] = '%s;%s/bin' % (rootbin,rootbin) + ';' + os.getenv('PATH', '')" << endl;
146#else
147 out << "rootbin = '" << rootbin << "'" << endl;
148 out << "rootlib = '" << rootlib << "'" << endl;
149 string jsrootsys = rootdata + "/js/";
150 out << "os.environ['PYTHONPATH'] = '%s' % rootlib + ':' + os.getenv('PYTHONPATH', '')" << endl;
151 out << "os.environ['PATH'] = '%s:%s/bin' % (rootbin,rootbin) + ':' + os.getenv('PATH', '')" << endl;
152 out << "os.environ['LD_LIBRARY_PATH'] = '%s' % rootlib + ':' + os.getenv('LD_LIBRARY_PATH', '')" << endl;
153#endif
154 out << "c.NotebookApp.extra_static_paths = ['" << jsrootsys << "']" << endl;
155 out.close();
156 return true;
157 }
158 else {
159 fprintf(stderr,
160 "Error installing notebook configuration files -- cannot create IPython config file at %s\n",
161 jupyconfig.c_str());
162 return false;
163 }
164}
165
166////////////////////////////////////////////////////////////////////////////////
167/// Creates a file that stores the current commit id in it.
168
169static bool CreateStamp(string dest)
170{
171 ofstream out(dest + pathsep + COMMIT_FILE, ios::trunc);
172 if (out.is_open()) {
173 out << gROOT->GetGitCommit();
174 out.close();
175 return true;
176 }
177 else {
178 fprintf(stderr,
179 "Error installing notebook configuration files -- cannot create %s\n",
181 return false;
182 }
183}
184
185////////////////////////////////////////////////////////////////////////////////
186/// Spawn a Jupyter notebook customised by ROOT.
187
188int main(int argc, char **argv)
189{
190 string rootbin(TROOT::GetBinDir().Data());
191 string rootlib(TROOT::GetLibDir().Data());
192 string rootetc(TROOT::GetEtcDir().Data());
193 string rootdata(TROOT::GetDataDir().Data());
194
195 // If needed, install ROOT notebook files in the user's home directory
196#ifdef WIN32
197 string homedir(getenv("USERPROFILE"));
198#else
199 string homedir(getenv("HOME"));
200#endif
201 int inst = CheckNbInstallation(homedir);
202 if (inst == -1) {
203 // The etc directory contains the ROOT notebook files to install
204 string source(rootetc + pathsep + NB_CONF_DIR);
205 string dest(homedir + pathsep + ROOTNB_DIR);
206 bool res = InstallNbFiles(source, dest) &&
207 CreateJupyterConfig(dest, rootbin, rootlib, rootdata) &&
208 CreateStamp(dest);
209 if (!res) return 1;
210 }
211 else if (inst == -2) return 1;
212
213 // Set IPython directory for the ROOT notebook flavour
214 string rootnbpath = homedir + pathsep + ROOTNB_DIR;
215 string jupyconfpathdir(JUPYTER_CONF_PATH_V + ("=" + rootnbpath));
216 string jupypathdir(JUPYTER_PATH_V + ("=" + rootnbpath));
217 putenv((char *)jupyconfpathdir.c_str());
218 putenv((char *)jupypathdir.c_str());
219
220 char **jargv = new char* [argc + 2];
221 jargv[0] = (char *) JUPYTER_CMD;
222 jargv[1] = (char *) NB_OPT;
223 for (int n=1;n<argc;++n)
224 jargv[n+1] = argv[n];
225 jargv[argc+1] = nullptr;
226
227 // Execute IPython notebook
228 execvp(JUPYTER_CMD, jargv);
229
230 // Exec failed
231 fprintf(stderr,
232 "Error starting ROOT notebook -- please check that Jupyter is installed\n");
233
234 delete [] jargv;
235 return 1;
236}
#define gROOT
Definition TROOT.h:417
externTSystem * gSystem
Definition TSystem.h:582
Iterator of linked list.
Definition TList.h:196
static const TString & GetBinDir()
Get the binary directory in the installation. Static utility function.
Definition TROOT.cxx:3150
static const TString & GetEtcDir()
Get the sysconfig directory in the installation. Static utility function.
Definition TROOT.cxx:3381
static const TString & GetLibDir()
Get the library directory in the installation.
Definition TROOT.cxx:3178
static const TString & GetDataDir()
Get the data directory in the installation. Static utility function.
Definition TROOT.cxx:3391
Basic string class.
Definition TString.h:138
Describes an Operating System directory for the browser.
A TSystemFile describes an operating system file.
Definition TSystemFile.h:29
TLine * line
void file()
Definition file.C:11
int main()
const Int_t n
Definition legend1.C:16
const std::vector< std::string > files
#define COMMIT_FILE
Definition nbmain.cxx:43
static bool InstallNbFiles(string source, string dest)
Installs ROOT notebook files in the user's home directory.
Definition nbmain.cxx:87
#define JUPYTER_CONF_PATH_V
Definition nbmain.cxx:39
#define NB_OPT
Definition nbmain.cxx:38
#define JUPYTER_PATH_V
Definition nbmain.cxx:40
static int CheckNbInstallation(string dir)
Checks whether ROOT notebook files are installed and they are the current version.
Definition nbmain.cxx:60
#define JUPYTER_CONFIG
Definition nbmain.cxx:44
static bool CreateStamp(string dest)
Creates a file that stores the current commit id in it.
Definition nbmain.cxx:169
#define NB_CONF_DIR
Definition nbmain.cxx:41
constexpr const char * pathsep
Definition nbmain.cxx:53
static bool CreateJupyterConfig(string dest, string rootbin, string rootlib, string rootdata)
Creates the Jupyter notebook configuration file that sets the necessary environment.
Definition nbmain.cxx:133
#define ROOTNB_DIR
Definition nbmain.cxx:42
#define JUPYTER_CMD
Definition nbmain.cxx:37
TSpectrum2 * s
Definition peaks2.C:33
void dir(char *path=0)
Definition rootalias.C:42