1- from typing import List , Optional , Union , Tuple
1+ from typing import List , Optional , Tuple
22
33from contextlib import contextmanager
44import datetime
77import glob
88import os
99from pathlib import Path
10+ import re
1011import shlex
1112import shutil
1213import subprocess as sp
1516
1617from tue_get .install_yaml_parser import installyaml_parser
1718from tue_get .util .BackgroundPopen import BackgroundPopen
19+ from tue_get .util .grep import grep_directory , grep_file
1820
1921CI = None
2022
@@ -37,8 +39,22 @@ def _which_split_cmd(cmd: str) -> Tuple[str, List[str]]:
3739 return " " .join (cmds ), cmds
3840
3941
42+ def _wait_for_dpkg_lock ():
43+ i = 0
44+ rotate_list = ["-" , "\\ " , "|" , "/" ]
45+ cmd = "sudo fuser /var/lib/dpkg/lock"
46+ cmd , cmds = _which_split_cmd (cmd )
47+ while sp .run (cmds , stdout = sp .DEVNULL , stderr = sp .DEVNULL ).returncode == 0 :
48+ print (f"[{ rotate_list [i % len (rotate_list )]} ] Waiting for dpkg lock" , end = "\r " )
49+ i += 1
50+ sleep (0.4 )
51+ return
52+
53+
4054class InstallerImpl :
4155 _apt_get_updated_file = os .path .join (os .sep , "tmp" , "tue_get_apt_get_updated" )
56+ _sources_list = os .path .join (os .sep , "etc" , "apt" , "sources.list" )
57+ _sources_list_dir = os .path .join (os .sep , "etc" , "apt" , "sources.list.d" )
4258
4359 def __init__ (self , debug : bool = False ):
4460 self ._debug = debug
@@ -105,7 +121,8 @@ def _write_sorted_deps(self, child: str) -> None:
105121 self ._write_sorted_dep_file (os .path .join (self ._dependencies_dir , self ._current_target ), child )
106122 self ._write_sorted_dep_file (os .path .join (self ._dependencies_on_dir , child ), self ._current_target )
107123
108- def _write_sorted_dep_file (self , file : str , target : str ) -> None :
124+ @staticmethod
125+ def _write_sorted_dep_file (file : str , target : str ) -> None :
109126 if not os .path .isfile (file ):
110127 Path (file ).touch (exist_ok = False )
111128 targets = []
@@ -129,7 +146,7 @@ def _set_target(self, target: str):
129146 self ._current_target = parent_target
130147 self ._current_target_dir = parent_target_dir
131148
132- def _out_handler (self , sub : BackgroundPopen , line : Union [ bytes , str ] ) -> None :
149+ def _out_handler (self , sub : BackgroundPopen , line : str ) -> None :
133150 def _write_stdin (msg ) -> None :
134151 sub .stdin .write (f"{ msg } \n " )
135152 sub .stdin .flush ()
@@ -198,10 +215,12 @@ def _write_stdin(msg) -> None:
198215 _write_stdin (0 )
199216 elif line .startswith ("tue-install-ppa: " ):
200217 ppas = line [17 :].split ()
218+ ppas = [ppa .replace ("^" , " " ).strip () for ppa in ppas ]
201219 success = self .tue_install_ppa (ppas )
202220 _write_stdin (not success )
203221 elif line .startswith ("tue-install-ppa-now: " ):
204222 ppas = line [21 :].split ()
223+ ppas = [ppa .replace ("^" , " " ).strip () for ppa in ppas ]
205224 success = self .tue_install_ppa_now (ppas )
206225 _write_stdin (not success )
207226 elif line .startswith ("tue-install-pip: " ):
@@ -231,7 +250,7 @@ def _write_stdin(msg) -> None:
231250 else :
232251 self .tue_install_tee (line )
233252
234- def _err_handler (self , sub : BackgroundPopen , line : Union [ bytes , str ] ) -> None :
253+ def _err_handler (self , sub : BackgroundPopen , line : str ) -> None :
235254 line = line .strip ()
236255 if line .startswith ("[sudo] password for" ):
237256 if self ._sudo_password is None :
@@ -412,7 +431,7 @@ def tue_install_target(self, target: str, now: bool = False) -> bool:
412431 else :
413432 self .tue_install_debug (f"Sourcing { install_bash_file } " )
414433 resource_file = os .path .join (os .path .dirname (__file__ ), "resources" , "installer_impl.bash" )
415- cmd = f" bash -c \" source { resource_file } && source { install_bash_file } \" "
434+ cmd = f' bash -c \" source { resource_file } && source { install_bash_file } \" '
416435 sub = self ._default_background_popen (cmd )
417436 if sub .returncode != 0 :
418437 self .tue_install_error (f"Error while running({ sub .returncode } ):\n { repr (cmd )} " )
@@ -601,13 +620,13 @@ def tue_install_add_text(self, source_file: str, target_file: str) -> bool:
601620 with open (target_file_path , "r" ) as f :
602621 target_text = f .read ().splitlines ()
603622
604- if not begin_tag in target_text :
623+ if begin_tag not in target_text :
605624 self .tue_install_debug (
606625 f"tue-install-add-text: { begin_tag = } not found in { target_file_path = } , "
607626 "appending to {target_file_path}"
608627 )
609628 source_text = "\n " .join (source_text )
610- cmd = f"bash -c \" echo - e '{ source_text } ' | { sudo_cmd } tee -a { target_file_path } \" "
629+ cmd = f"bash -c \" echo - e \ '{ source_text } \ ' | { sudo_cmd } tee -a { target_file_path } \" "
611630 else :
612631 self .tue_install_debug (
613632 f"tue-install-add-text: { begin_tag = } found in { target_file_path = } , "
@@ -705,17 +724,6 @@ def _out_handler_installed_pkgs(sub: BackgroundPopen, line: str) -> None:
705724 apt_get_cmd = f"sudo apt-get install --assume-yes -q { ' ' .join (pkgs_to_install )} "
706725 self .tue_install_echo (f"Going to run the following command:\n { apt_get_cmd } " )
707726
708- def _wait_for_dpkg_lock ():
709- i = 0
710- rotate_list = ["-" , "\\ " , "|" , "/" ]
711- cmd = "sudo fuser /var/lib/dpkg/lock"
712- cmd , cmds = _which_split_cmd (cmd )
713- while sp .run (cmds , stdout = sp .DEVNULL , stderr = sp .DEVNULL ).returncode == 0 :
714- print (f"[{ rotate_list [i % len (rotate_list )]} ] Waiting for dpkg lock" , end = "\r " )
715- i += 1
716- sleep (0.4 )
717- return
718-
719727 _wait_for_dpkg_lock ()
720728
721729 if not os .path .isfile (self ._apt_get_updated_file ):
@@ -748,7 +756,7 @@ def tue_install_apt_get_update(self):
748756 def tue_install_ppa (self , ppas : List [str ]) -> bool :
749757 self .tue_install_debug (f"tue-install-ppa { ppas = } " )
750758 if not ppas :
751- self .tue_install_error ("Invalid tue-install-ppa call: needs ppa as argument" )
759+ self .tue_install_error ("Invalid tue-install-ppa call: needs ppas as argument" )
752760 # ToDo: This depends on behaviour of tue-install-error
753761 return False
754762
0 commit comments