DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
oks_validate_repository.cxx File Reference
#include "oks/exceptions.hpp"
#include "oks/kernel.hpp"
#include "oks/pipeline.hpp"
#include <boost/program_options.hpp>
#include "ers/ers.hpp"
#include "logging/Logging.hpp"
#include <algorithm>
#include <chrono>
#include <filesystem>
#include <iostream>
#include <mutex>
#include <vector>
Include dependency graph for oks_validate_repository.cxx:

Go to the source code of this file.

Classes

struct  OksValidateJob
struct  FoundCircularDependency
struct  TestCircularDependency

Enumerations

enum  __OksValidateRepositoryExitStatus__ {
  __Success__ = 0 , __BadCommandLine__ , __UserAuthenticationFailure__ , __NoRepository__ ,
  __ConsistencyError__ , __IncludesCircularDependencyError__ , __AccessManagerAuthorizationFailed__ , __AccessManagerNoPermission__ ,
  __NoIncludedFile__ , __ExceptionCaught__
}

Functions

static void init_file_load_error (const std::string &file)
std::set< std::string > & define_includes (const std::string &f, const std::set< std::string > &s, std::map< std::string, std::set< std::string > > &file_all_includes, std::map< std::string, std::set< std::string > > &file_explicit_includes, TestCircularDependency &cd_fuse)
int main (int argc, char **argv)

Variables

std::string s_load_error
struct FoundCircularDependency s_circular_dependency_message

Enumeration Type Documentation

◆ __OksValidateRepositoryExitStatus__

Enumerator
__Success__ 
__BadCommandLine__ 
__UserAuthenticationFailure__ 
__NoRepository__ 
__ConsistencyError__ 
__IncludesCircularDependencyError__ 
__AccessManagerAuthorizationFailed__ 
__AccessManagerNoPermission__ 
__NoIncludedFile__ 
__ExceptionCaught__ 

Definition at line 31 of file oks_validate_repository.cxx.

Function Documentation

◆ define_includes()

std::set< std::string > & define_includes ( const std::string & f,
const std::set< std::string > & s,
std::map< std::string, std::set< std::string > > & file_all_includes,
std::map< std::string, std::set< std::string > > & file_explicit_includes,
TestCircularDependency & cd_fuse )

Definition at line 189 of file oks_validate_repository.cxx.

194{
195 std::set<std::string>& all_includes = file_all_includes[f];
196
197 if (all_includes.empty()) {
198 for (auto& x : s) {
199 if (cd_fuse.push(&x)) {
200 all_includes.insert(x);
201
202 std::set<std::string>& includes =
203 define_includes(x, file_explicit_includes[x], file_all_includes, file_explicit_includes, cd_fuse);
204 for (const auto& y : includes)
205 all_includes.insert(y);
206
207 cd_fuse.pop();
208 }
209 }
210 }
211
212 return all_includes;
213}
std::set< std::string > & define_includes(const std::string &f, const std::set< std::string > &s, std::map< std::string, std::set< std::string > > &file_all_includes, std::map< std::string, std::set< std::string > > &file_explicit_includes, TestCircularDependency &cd_fuse)
bool push(const std::string *file)

◆ init_file_load_error()

void init_file_load_error ( const std::string & file)
static

Definition at line 48 of file oks_validate_repository.cxx.

49{
50 s_load_error = "repository validation failed for file \'";
52 s_load_error += "\':\n";
53}
std::string s_load_error

◆ main()

int main ( int argc,
char ** argv )

Definition at line 216 of file oks_validate_repository.cxx.

217{
218 boost::program_options::options_description desc(
219 "This program validates OKS git repository for commit by pre-receive hook");
220
221 std::vector<std::string> created, updated, deleted;
222 bool circular_dependency_between_includes_is_error = true;
223 bool verbose = false;
224 std::string user;
225 std::size_t pipeline_size = 4;
226
227 try {
228 std::vector<std::string> app_types_list;
229 std::vector<std::string> segments_list;
230
231 desc.add_options()("add,a",
232 boost::program_options::value<std::vector<std::string>>(&created)->multitoken(),
233 "list of new OKS files and directories to be added to the repository")(
234 "update,u",
235 boost::program_options::value<std::vector<std::string>>(&updated)->multitoken(),
236 "list of new OKS files and directories to be updated in the repository")(
237 "remove,r",
238 boost::program_options::value<std::vector<std::string>>(&deleted)->multitoken(),
239 "list of new OKS files and directories to be removed from the repository")(
240 "permissive-circular-dependencies-between-includes,C",
241 "downgrade severity of detected circular dependencies between includes from errors to warnings")(
242 "user,U", boost::program_options::value<std::string>(&user), "user id")(
243 "threads-number,t",
244 boost::program_options::value<std::size_t>(&pipeline_size)->default_value(pipeline_size),
245 "number of threads used by validation pipeline")("verbose,v", "Print debug information")("help,h",
246 "Print help message");
247
248 boost::program_options::variables_map vm;
249 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
250
251 if (vm.count("help")) {
252 std::cout << desc << std::endl;
253 return __Success__;
254 }
255
256 if (vm.count("permissive-circular-dependencies-between-includes"))
257 circular_dependency_between_includes_is_error = false;
258
259 if (vm.count("verbose"))
260 verbose = true;
261
262 boost::program_options::notify(vm);
263
264 } catch (std::exception& ex) {
265 std::cerr << "Command line parsing errors occurred:\n" << ex.what() << std::endl;
266 return __BadCommandLine__;
267 }
268
269 try {
270 OksKernel kernel;
271
274
275 if (kernel.get_user_repository_root().empty()) {
276 std::cerr << "There is no OKS repository set (check TDAQ_DB_REPOSITORY)" << std::endl;
277 return __NoRepository__;
278 }
279
280 std::filesystem::current_path(kernel.get_user_repository_root());
281
282 auto start_usage = std::chrono::steady_clock::now();
283
284 // directories
285
286 std::set<std::string> directories;
287
288 // file: explicit includes
289 std::map<std::string, std::set<std::string>> file_explicit_includes;
290
291 for (auto& p : std::filesystem::recursive_directory_iterator("."))
292 if (std::filesystem::is_directory(p))
293 directories.insert(p.path().native().substr(2));
294 else if (std::filesystem::is_regular_file(p) && p.path().native().find("./.git") != 0 &&
295 p.path().native().find("./admin") != 0 && p.path().native().find("./README.md") != 0)
296 kernel.get_includes(p.path().native(), file_explicit_includes[p.path().native().substr(2)], true);
297
298 if (verbose)
299 log_timestamp(Debug) << "scan " << file_explicit_includes.size() << " repository files in "
300 << std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() -
301 start_usage)
302 .count() /
303 1000.
304 << " ms\n";
305
306 auto start_usage2 = std::chrono::steady_clock::now();
307
308 std::set<std::string> all_includes;
309
310 // check every include exists
311 for (const auto& f : file_explicit_includes) {
312 for (const auto& i : f.second) {
313 if (file_explicit_includes.find(i) != file_explicit_includes.end()) {
314 all_includes.insert(i);
315 } else {
316 std::cerr << "Cannot find file \"" << i << "\" included by \"" << f.first << "\"" << std::endl;
317 return __NoIncludedFile__;
318 }
319 }
320 }
321
322 if (verbose)
323 log_timestamp(Debug) << "check existence of includes in "
324 << std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() -
325 start_usage2)
326 .count() /
327 1000.
328 << " ms\n";
329
330 auto start_usage3 = std::chrono::steady_clock::now();
331
332 // file: all includes
333 std::map<std::string, std::set<std::string>> file_all_includes;
334
335 for (auto& x : file_explicit_includes) {
336 TestCircularDependency cd_fuse(&x.first);
337 define_includes(x.first, x.second, file_all_includes, file_explicit_includes, cd_fuse);
338 }
339
340 auto stop_usage3 = std::chrono::steady_clock::now();
341
342 if (verbose)
344 << "calculated inclusion graph in "
345 << std::chrono::duration_cast<std::chrono::microseconds>(stop_usage3 - start_usage3).count() / 1000. << " ms\n";
346
347 log_timestamp() << "process " << file_explicit_includes.size() << " repository files and their includes in "
348 << std::chrono::duration_cast<std::chrono::microseconds>(stop_usage3 - start_usage).count() / 1000.
349 << " ms" << std::endl;
350
352 log_timestamp((circular_dependency_between_includes_is_error == true ? Error : Warning))
353 << "Detected " << s_circular_dependency_message.m_count
354 << " circular dependencies between includes of the repository files:"
355 << s_circular_dependency_message.m_text.str() << std::endl;
356
357 if (circular_dependency_between_includes_is_error == true)
359 }
360
361 if (ers::debug_level() >= 2) {
362 std::ostringstream text;
363 text << "ALL INCLUDES:\n";
364
365 for (const auto& x : file_all_includes) {
366 text << "FILE \"" << x.first << "\" has " << x.second.size() << " includes:\n";
367
368 for (const auto& y : x.second)
369 text << " - \"" << y << "\"\n";
370 }
371
372 TLOG_DEBUG(2) << text.str();
373 }
374
375 OksPipeline pipeline(pipeline_size);
376
377 // do not run check for README file
378 auto ignore_files = [](std::string& x) {
379 static const std::string readme_file("README.md");
380 return x != readme_file;
381 };
382
383 // all modified file paths
384 std::set<std::string> modified;
385 std::copy_if(created.begin(), created.end(), std::inserter(modified, modified.end()), ignore_files);
386 std::copy_if(updated.begin(), updated.end(), std::inserter(modified, modified.end()), ignore_files);
387
388 // validate independently every created or updated file
389 for (const auto& x : modified)
390 pipeline.addJob(new OksValidateJob(kernel, x));
391
392 std::copy_if(deleted.begin(), deleted.end(), std::inserter(modified, modified.end()), ignore_files);
393
394 for (const auto& f : file_explicit_includes)
395 if (all_includes.find(f.first) == all_includes.end()) {
396 if (modified.empty() == false) {
397 if (modified.find(f.first) == modified.end()) {
398 const auto& file_includes = file_all_includes[f.first];
399
400 bool found = false;
401
402 for (const auto& x : modified)
403 if (file_includes.find(x) != file_includes.end()) {
404 found = true;
405 TLOG_DEBUG(1) << "file \"" << f.first << "\" contains modified include \"" << x << '\"';
406 break;
407 }
408
409 if (found == false) {
410 TLOG_DEBUG(1) << "skip file \"" << f.first << '\"';
411 continue;
412 }
413 } else {
414 TLOG_DEBUG(1) << "list of modified files contains file \"" << f.first << '\"';
415 }
416 }
417
418 if (modified.find(f.first) == modified.end())
419 pipeline.addJob(new OksValidateJob(kernel, f.first));
420 }
421
422 pipeline.waitForCompletion();
423
424 if (!s_load_error.empty()) {
425 log_timestamp(Error) << s_load_error << std::endl;
427 }
428 } catch (exception& ex) {
429 log_timestamp(Error) << "Caught oks exception:\n" << ex << std::endl;
430 return __ExceptionCaught__;
431 } catch (std::exception& e) {
432 log_timestamp(Error) << "Caught standard C++ exception:\n" << e.what() << std::endl;
433 return __ExceptionCaught__;
434 } catch (...) {
435 log_timestamp(Error) << "Caught unknown exception" << std::endl;
436 return __ExceptionCaught__;
437 }
438
439 return __Success__;
440}
Provides interface to the OKS kernel.
Definition kernel.hpp:615
const std::string & get_user_repository_root() const
Get user OKS repository root.
Definition kernel.cpp:335
void set_allow_duplicated_objects_mode(const bool b)
Set status of duplicated objects mode. To switch 'On'/'Off' use the method's parameter:
Definition kernel.hpp:813
void set_test_duplicated_objects_via_inheritance_mode(const bool b)
Set status of test inherited duplicated objects mode. To switch 'On'/'Off' use the method's parameter...
Definition kernel.hpp:782
void get_includes(const std::string &file_name, std::set< std::string > &includes, bool use_repository_name=false)
Opens file and reads its shallow includes.
Definition kernel.cpp:1752
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:116
std::ostream & log_timestamp(__LogSeverity__ severity=Log)
Definition kernel.cpp:5470
int debug_level()
Definition ers.hpp:80
struct FoundCircularDependency s_circular_dependency_message
OksValidateJob(OksKernel &kernel, const std::string &file_name)

Variable Documentation

◆ s_circular_dependency_message

struct FoundCircularDependency s_circular_dependency_message

◆ s_load_error

std::string s_load_error

Definition at line 45 of file oks_validate_repository.cxx.