Hand-apply 111f02f4fc8ace359a16aea1c88c2821bf3dde31 from master; improve progress...
[dcpomatic.git] / src / tools / dcpomatic_cli.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <iostream>
21 #include <iomanip>
22 #include <getopt.h>
23 #include <dcp/version.h>
24 #include "lib/film.h"
25 #include "lib/filter.h"
26 #include "lib/transcode_job.h"
27 #include "lib/job_manager.h"
28 #include "lib/util.h"
29 #include "lib/scaler.h"
30 #include "lib/version.h"
31 #include "lib/cross.h"
32 #include "lib/config.h"
33 #include "lib/log.h"
34 #include "lib/ui_signaller.h"
35 #include "lib/server_finder.h"
36
37 using std::string;
38 using std::cerr;
39 using std::cout;
40 using std::vector;
41 using std::pair;
42 using std::list;
43 using boost::shared_ptr;
44
45 static void
46 help (string n)
47 {
48         cerr << "Syntax: " << n << " [OPTION] <FILM>\n"
49              << "  -v, --version      show DCP-o-matic version\n"
50              << "  -h, --help         show this help\n"
51              << "  -d, --deps         list DCP-o-matic dependency details and quit\n"
52              << "  -f, --flags        show flags passed to C++ compiler on build\n"
53              << "  -n, --no-progress  do not print progress to stdout\n"
54              << "  -r, --no-remote    do not use any remote servers\n"
55              << "  -k, --keep-going   keep running even when the job is complete\n"
56              << "\n"
57              << "<FILM> is the film directory.\n";
58 }
59
60 int
61 main (int argc, char* argv[])
62 {
63         string film_dir;
64         bool progress = true;
65         bool no_remote = false;
66         bool keep_going = false;
67
68         int option_index = 0;
69         while (true) {
70                 static struct option long_options[] = {
71                         { "version", no_argument, 0, 'v'},
72                         { "help", no_argument, 0, 'h'},
73                         { "deps", no_argument, 0, 'd'},
74                         { "flags", no_argument, 0, 'f'},
75                         { "no-progress", no_argument, 0, 'n'},
76                         { "no-remote", no_argument, 0, 'r'},
77                         { "keep-going", no_argument, 0, 'k' },
78                         { 0, 0, 0, 0 }
79                 };
80
81                 int c = getopt_long (argc, argv, "vhdfnrk", long_options, &option_index);
82
83                 if (c == -1) {
84                         break;
85                 }
86
87                 switch (c) {
88                 case 'v':
89                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
90                         exit (EXIT_SUCCESS);
91                 case 'h':
92                         help (argv[0]);
93                         exit (EXIT_SUCCESS);
94                 case 'd':
95                         cout << dependency_version_summary () << "\n";
96                         exit (EXIT_SUCCESS);
97                 case 'f':
98                         cout << dcpomatic_cxx_flags << "\n";
99                         exit (EXIT_SUCCESS);
100                 case 'n':
101                         progress = false;
102                         break;
103                 case 'r':
104                         no_remote = true;
105                         break;
106                 case 'k':
107                         keep_going = true;
108                         break;
109                 }
110         }
111
112         if (optind >= argc) {
113                 help (argv[0]);
114                 exit (EXIT_FAILURE);
115         }
116
117         film_dir = argv[optind];
118                         
119         dcpomatic_setup ();
120         ui_signaller = new UISignaller ();
121
122         if (no_remote) {
123                 ServerFinder::instance()->disable ();
124         }
125
126         cout << "DCP-o-matic " << dcpomatic_version << " git " << dcpomatic_git_commit;
127         char buf[256];
128         if (gethostname (buf, 256) == 0) {
129                 cout << " on " << buf;
130         }
131         cout << "\n";
132
133         shared_ptr<Film> film;
134         try {
135                 film.reset (new Film (film_dir));
136                 film->read_metadata ();
137         } catch (std::exception& e) {
138                 cerr << argv[0] << ": error reading film `" << film_dir << "' (" << e.what() << ")\n";
139                 exit (EXIT_FAILURE);
140         }
141
142         ContentList content = film->content ();
143         for (ContentList::const_iterator i = content.begin(); i != content.end(); ++i) {
144                 vector<boost::filesystem::path> paths = (*i)->paths ();
145                 for (vector<boost::filesystem::path>::const_iterator j = paths.begin(); j != paths.end(); ++j) {
146                         if (!boost::filesystem::exists (*j)) {
147                                 cerr << argv[0] << ": content file " << *j << " not found.\n";
148                                 exit (EXIT_FAILURE);
149                         }
150                 }
151         }
152                 
153         cout << "\nMaking DCP for " << film->name() << "\n";
154
155         film->make_dcp ();
156
157         bool should_stop = false;
158         bool first = true;
159         bool error = false;
160         while (!should_stop) {
161
162                 dcpomatic_sleep (5);
163
164                 list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
165
166                 if (!first && progress) {
167                         cout << "\033[" << jobs.size() << "A";
168                         cout.flush ();
169                 }
170
171                 first = false;
172
173                 int unfinished = 0;
174                 int finished_in_error = 0;
175
176                 for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
177                         if (progress) {
178                                 cout << (*i)->name() << ": ";
179                                 
180                                 if ((*i)->progress ()) {
181                                         cout << (*i)->status() << "                         \n";
182                                 } else {
183                                         cout << ": Running           \n";
184                                 }
185                         }
186
187                         if (!(*i)->finished ()) {
188                                 ++unfinished;
189                         }
190
191                         if ((*i)->finished_in_error ()) {
192                                 ++finished_in_error;
193                                 error = true;
194                         }
195
196                         if (!progress && (*i)->finished_in_error ()) {
197                                 /* We won't see this error if we haven't been showing progress,
198                                    so show it now.
199                                 */
200                                 cout << (*i)->status() << "\n";
201                         }
202                 }
203
204                 if (unfinished == 0 || finished_in_error != 0) {
205                         should_stop = true;
206                 }
207         }
208
209         if (keep_going) {
210                 while (true) {
211                         dcpomatic_sleep (3600);
212                 }
213         }
214
215         /* This is just to stop valgrind reporting leaks due to JobManager
216            indirectly holding onto codecs.
217         */
218         JobManager::drop ();
219         
220         return error ? EXIT_FAILURE : EXIT_SUCCESS;
221 }
222
223