@@ -131,6 +131,14 @@ def __init__(
131131 def __del__ (self ):
132132 shutil .rmtree (self ._state_dir )
133133
134+ def _get_installed_targets (self ) -> List [str ]:
135+ return [
136+ pkg for pkg in os .listdir (self ._installed_dir ) if os .path .isfile (os .path .join (self ._installed_dir , pkg ))
137+ ]
138+
139+ def _target_exist (self , target : str ) -> bool :
140+ return os .path .isdir (os .path .join (self ._targets_dir , target ))
141+
134142 def _log_to_file (self , msg : str , end = "\n " ) -> None :
135143 with open (self ._log_file , "a" ) as f :
136144 f .write (msg + end )
@@ -361,7 +369,7 @@ def tue_install_target(self, target: str, now: bool = False) -> bool:
361369 self .tue_install_debug (f"Installing target: { target } " )
362370
363371 # Check if valid target received as input
364- if not os . path . isdir ( os . path . join ( self ._targets_dir , target ) ):
372+ if not self ._target_exist ( target ):
365373 self .tue_install_debug (f"Target '{ target } ' does not exist." )
366374 return False
367375
@@ -1376,7 +1384,195 @@ def tue_install_ros(self, source_type: str, **kwargs) -> bool:
13761384 # ToDO: TUE_INSTALL_PKG_DIR was set ros_pkg_dir which was then use in tue-install-apply-patch; we are not doing that not (yet) in python
13771385 return True
13781386
1387+ def install (self , targets : List [str ]) -> bool :
1388+ if not targets :
1389+ self .tue_install_error ("No targets to install" )
1390+ # ToDo: This depends on behaviour of tue-install-error
1391+ return False
1392+
1393+ missing_targets = []
1394+ for target in targets :
1395+ if not self ._target_exist (target ):
1396+ missing_targets .append (target )
1397+
1398+ if missing_targets :
1399+ self .tue_install_error (f"The following installed targets don't exist (anymore):\n { sorted (missing_targets )} " )
1400+ # ToDo: This depends on behaviour of tue-install-error
1401+ return False
1402+
1403+ for target in targets :
1404+ self .tue_install_debug (f"Installing target '{ target } '" )
1405+ if not self .tue_install_target (target ):
1406+ self .tue_install_error (f"Failed to install target '{ target } '" )
1407+ return False
1408+
1409+ # Mark as installed
1410+ self .tue_install_debug (f"Marking '{ target } ' as installed after successful installation" )
1411+ Path (os .path .join (self ._installed_dir , target )).touch (exist_ok = True )
1412+
1413+ return True
1414+
1415+ def update (self , targets : List [str ]) -> bool :
1416+ if not targets :
1417+ targets = self ._get_installed_targets ()
1418+ else :
1419+ installed_targets = self ._get_installed_targets ()
1420+ for target in targets :
1421+ if target not in installed_targets :
1422+ self .tue_install_error (f"Target '{ target } ' is not installed" )
1423+ # ToDo: This depends on behaviour of tue-install-error
1424+ return False
1425+
1426+ missing_targets = []
1427+ for target in targets :
1428+ if not self ._target_exist (target ):
1429+ missing_targets .append (target )
1430+
1431+ if missing_targets :
1432+ self .tue_install_error (f"The following installed targets don't exist (anymore):\n { sorted (missing_targets )} " )
1433+ # ToDo: This depends on behaviour of tue-install-error
1434+ return False
1435+
1436+ for target in targets :
1437+ self .tue_install_debug (f"Updating target '{ target } '" )
1438+ if not self .tue_install_target (target ):
1439+ self .tue_install_error (f"Failed to update target '{ target } '" )
1440+ return False
1441+ self .tue_install_debug (f"{ target } succesfully updated" )
1442+
1443+ return True
1444+
1445+ def print_queued_logs (self ) -> bool :
1446+ if self ._info_logs :
1447+ logs = "\n " .join (self ._info_logs )
1448+ print (f"Some information you may have missed:\n \n { logs } \n " )
1449+
1450+ if self ._warn_logs :
1451+ logs = "\n " .join (self ._warn_logs )
1452+ print (f"Overview of warnings:\n \n { logs } \n " )
1453+
1454+ return True
1455+
1456+ def install_queued_pkgs (self ) -> bool :
1457+ if self ._ppas :
1458+ with self ._set_target ("PPA-ADD" ):
1459+ self .tue_install_debug (f"calling tue-install-ppa-now { self ._ppas } " )
1460+ if not self .tue_install_ppa_now (self ._ppas ):
1461+ self .tue_install_error (f"Failed to add PPA's: { self ._ppas } " )
1462+ # ToDo: This depends on behaviour of tue-install-error
1463+ return False
1464+
1465+ if self ._systems :
1466+ with self ._set_target ("APT-GET" ):
1467+ self .tue_install_debug (f"calling tue-install-system-now { self ._systems } " )
1468+ if not self .tue_install_system_now (self ._systems ):
1469+ self .tue_install_error (f"Failed to install system packages: { self ._systems } " )
1470+ # ToDo: This depends on behaviour of tue-install-error
1471+ return False
1472+
1473+ if self ._pips :
1474+ with self ._set_target ("PIP" ):
1475+ self .tue_install_debug (f"calling tue-install-pip-now { self ._pips } " )
1476+ if not self .tue_install_pip_now (self ._pips ):
1477+ self .tue_install_error (f"Failed to install pip packages: { self ._pips } " )
1478+ # ToDo: This depends on behaviour of tue-install-error
1479+ return False
1480+
1481+ if self ._snaps :
1482+ with self ._set_target ("SNAP" ):
1483+ self .tue_install_debug (f"calling tue-install-snap-now { self ._snaps } " )
1484+ if not self .tue_install_snap_now (self ._snaps ):
1485+ self .tue_install_error (f"Failed to install snap packages: { self ._snaps } " )
1486+ # ToDo: This depends on behaviour of tue-install-error
1487+ return False
1488+
1489+ if self ._gems :
1490+ with self ._set_target ("GEM" ):
1491+ self .tue_install_debug (f"calling tue-install-gem-now { self ._gems } " )
1492+ if not self .tue_install_gem_now (self ._gems ):
1493+ self .tue_install_error (f"Failed to install gem packages: { self ._gems } " )
1494+ # ToDo: This depends on behaviour of tue-install-error
1495+ return False
1496+
1497+ return True
1498+
13791499
13801500if __name__ == "__main__" :
1381- bla = InstallerImpl (debug = True )
1382- bla .tue_install_target ("test" , True )
1501+ import argparse
1502+ import sys
1503+
1504+ ros_test_depends = os .environ .get ("TUE_INSTALL_TEST_DEPENDS" , False )
1505+ ros_doc_depends = os .environ .get ("TUE_INSTALL_DOC_DEPENDS" , False )
1506+
1507+ parser = argparse .ArgumentParser (prog = "tue-get" , description = "Installs all your (ROS) dependencies" )
1508+ parser .add_argument ("--branch" , "-b" , help = "Branch to checkout" , default = None )
1509+ parser .add_argument ("--debug" , "-d" , action = "store_true" , help = "Enable debug output" , default = False )
1510+ parser .add_argument ("--no-ros-deps" , action = "store_true" , help = "Skip resolving of ROS dependencies" , default = False )
1511+ parser .add_argument (
1512+ "mode" ,
1513+ choices = ["install" , "update" ],
1514+ type = str ,
1515+ help = "Install OR update the targets" ,
1516+ )
1517+ m = parser .add_mutually_exclusive_group (required = False )
1518+ m .add_argument (
1519+ "--test-depends" ,
1520+ dest = "test_depends" ,
1521+ action = "store_true" ,
1522+ help = "Also resolve ROS test dependencies" ,
1523+ default = ros_test_depends ,
1524+ )
1525+ m .add_argument (
1526+ "--no-test-depends" ,
1527+ dest = "test_depends" ,
1528+ action = "store_false" ,
1529+ help = "Do not resolve ROS test dependencies" ,
1530+ default = ros_test_depends ,
1531+ )
1532+ m = parser .add_mutually_exclusive_group (required = False )
1533+ m .add_argument (
1534+ "--doc-depends" ,
1535+ dest = "doc_depends" ,
1536+ action = "store_true" ,
1537+ help = "Also resolve ROS doc dependencies" ,
1538+ default = ros_doc_depends ,
1539+ )
1540+ m .add_argument (
1541+ "--no-doc-depends" ,
1542+ dest = "doc_depends" ,
1543+ action = "store_false" ,
1544+ help = "Do not resolve ROS doc dependencies" ,
1545+ default = ros_doc_depends ,
1546+ )
1547+ parser .add_argument ("targets" , help = "Targets to install" , nargs = argparse .REMAINDER )
1548+
1549+ args = parser .parse_args ()
1550+ if args .mode == "install" and not args .targets :
1551+ parser .error ("Minimal one target should be specified, when installing" )
1552+
1553+ installer = InstallerImpl (
1554+ branch = args .branch ,
1555+ debug = args .debug ,
1556+ skip_ros_deps = args .no_ros_deps ,
1557+ ros_test_deps = args .test_depends ,
1558+ ros_doc_deps = args .doc_depends ,
1559+ )
1560+
1561+ targets = sorted (args .targets )
1562+ if args .mode == "install" :
1563+ if not installer .install (targets ):
1564+ installer .tue_install_error (f"Failed to install targets: { targets } " )
1565+ sys .exit (1 )
1566+ elif args .mode == "update" :
1567+ if not installer .update (targets ):
1568+ installer .tue_install_error (f"Failed to update targets: { targets } " )
1569+ sys .exit (1 )
1570+
1571+ if not installer .print_queued_logs ():
1572+ pass
1573+
1574+ if not installer .install_queued_pkgs ():
1575+ sys .exit (1 )
1576+
1577+ installer .tue_install_echo ("Installer completed successfully" )
1578+ sys .exit (0 )
0 commit comments