Imported Upstream version 17.0.2
[platform/upstream/libzypp.git] / tools / ToolScanRepos.cc
1 #define INCLUDE_TESTSETUP_WITHOUT_BOOST
2 #include "zypp/../tests/lib/TestSetup.h"
3 #undef  INCLUDE_TESTSETUP_WITHOUT_BOOST
4
5 static std::string appname( "ToolScanRepos" );
6
7 void message( const std::string & msg_r )
8 {
9   cerr << "*** " << msg_r << endl;
10 }
11
12 int usage( const std::string & msg_r = std::string(), int exit_r = 100 )
13 {
14   if ( ! msg_r.empty() )
15   {
16     cerr << endl;
17     message( msg_r );
18     cerr << endl;
19   }
20   cerr << "Usage: " << appname << "[OPTIONS] URL..." << endl;
21   cerr << "  Load repos from URL to test system below /tmp/" << appname << "." << endl;
22   cerr << "  -r ROOT  Use /tmp/ROOT as location of test system (default: " << appname << ")." << endl;
23   cerr << "  -a ARCH  Use ARCH as test system architecture (default: x86_64)." << endl;
24   cerr << "  -c       Clear an existing test system (default)." << endl;
25   cerr << "  -n       Do not clear an existing test system but reuse it." << endl;
26   return exit_r;
27 }
28
29 /******************************************************************
30 **
31 **      FUNCTION NAME : main
32 **      FUNCTION TYPE : int
33 */
34 int main( int argc, char * argv[] )
35 {
36   INT << "===[START]==========================================" << endl;
37   appname = Pathname::basename( argv[0] );
38   --argc;
39   ++argv;
40
41   if ( ! argc )
42   {
43     return usage();
44   }
45
46   ///////////////////////////////////////////////////////////////////
47   Pathname mtmp( "/tmp" );
48   Pathname mroot( mtmp/appname );
49   Arch     march( Arch_x86_64 );
50   bool     oClearRoot = true;
51
52   std::vector<std::string> urls;
53
54   while ( argc )
55   {
56     if ( argv[0] == std::string("-c") )
57     {
58       oClearRoot = true;
59     }
60     else if ( argv[0] == std::string("-n") )
61     {
62       oClearRoot = false;
63     }
64     else if ( argv[0] == std::string("-r") || argv[0] == std::string("--root"))
65     {
66       --argc;
67       ++argv;
68       if ( ! argc )
69         return usage( "Missing arg to -r ROOT", 101 );
70
71       if ( *(argv[0]) ) // empty
72         mroot = mtmp/argv[0];
73       else
74         mroot = mtmp/appname;
75     }
76      else if ( argv[0] == std::string("-a") )
77     {
78       --argc;
79       ++argv;
80       if ( ! argc )
81         return usage( "Missing arg to -a ARCH", 101 );
82
83       if ( *(argv[0]) ) // empty
84         march = Arch( argv[0] );
85       else
86         march = Arch_x86_64;
87     }
88    else
89     {
90       urls.push_back( argv[0] );
91     }
92     --argc;
93     ++argv;
94   }
95
96   if ( urls.empty() )
97   {
98     return usage( "Missing URLs", 102 );
99   }
100
101   ///////////////////////////////////////////////////////////////////
102
103   if ( oClearRoot )
104   {
105     message( "Clear test system at " + mroot.asString() );
106     filesystem::recursive_rmdir( mroot );
107   }
108   else
109   {
110     message( "Use test system at " + mroot.asString() );
111   }
112   filesystem::assert_dir( mroot );
113
114   message( "Use archiecture " + march.asString() );
115   TestSetup test( mroot, march );
116
117   int ret = 0;
118   for_( it, urls.begin(), urls.end() )
119   {
120     message( "Setup " + *it );
121     try
122     {
123       test.loadRepo( *it );
124     }
125     catch ( const Exception & exp )
126     {
127       message( exp.asString() + "\n" + exp.historyAsString() );
128       ++ret;
129     }
130   }
131
132   INT << "===[END]============================================" << endl << endl;
133   return ret;
134 }