def _proc_logging(self, path): text = f'Listing installed {self.desc[1]}' print_info(text, self._file, redirect=self._qflag) suffix = path.replace('/', ':') with tempfile.NamedTemporaryFile() as _temp_file: logfile = os.path.join(self._logroot, f'{self.log}-{suffix}{self.ext}') argv = [ path, 'bundle', 'dump', '--force', f'--file={_temp_file.name}' ] print_scpt(argv, self._file, redirect=self._qflag) script(argv, self._file, shell=True, timeout=self._timeout, redirect=self._vflag) with open(_temp_file.name, 'r') as file: context = file.read() print_text(context, get_logfile(), redirect=self._vflag) with open(logfile, 'w') as file: file.writelines( filter(lambda s: s.startswith('brew'), context.strip().splitlines(True))) # pylint: disable=filter-builtin-not-iterating
def _proc_install(self, path): text = f'Installing specified {self.desc[1]}' print_info(text, self._file, redirect=self._qflag) argv = [path, '-m', 'pip', 'install'] if self._pre: argv.append('--pre') if self._user: argv.append('--user') if self._quiet: argv.append('--quiet') if self._verbose: argv.append('--verbose') argv.extend(self._install_opts) argv.append('') for package in self._var__temp_pkgs: argv[-1] = package print_scpt(argv, self._file, redirect=self._qflag) if sudo(argv, self._file, self._password, timeout=self._timeout, redirect=self._qflag, verbose=self._vflag, sethome=True): self._fail.append(package) else: self._pkgs.append(package) del self._var__temp_pkgs
def _proc_install(self, path): text = f'Installing specified {self.desc[1]}' print_info(text, self._file, redirect=self._qflag) argv = [path, 'install'] if self._force: argv.append('--force') argv.extend(self._install_opts) argv.append('') for package in self._var__temp_pkgs: if re.match(r'\d{10}', package) is None: argv[1] = 'lucky' argv[-1] = package print_scpt(' '.join(argv), self._file, redirect=self._qflag) if sudo(argv, self._file, self._password, timeout=self._timeout, redirect=self._qflag, verbose=self._vflag): self._fail.append(package) else: self._pkgs.append(package) del self._var__temp_pkgs
def _proc_cleanup(self): if self._no_cleanup: return text = 'Pruning caches and archives' print_info(text, self._file, redirect=self._qflag) argv = ['npm', 'cleanup'] if self._verbose: argv.append('--verbose') if self._quiet: argv.append('--quiet') print_scpt(' '.join(argv), self._file, redirect=self._qflag) def _cleanup(argv): if self._verbose: argv.append('--verbose') if self._quiet: argv.append('--quiet') args = ' '.join(argv) print_scpt(args, self._file, redirect=self._qflag) sudo(argv, self._file, self._password, redirect=self._qflag, verbose=self._vflag) for path in self._exec: _cleanup([path, 'dedupe', '--global']) _cleanup([path, 'cache', 'clean', '--force'])
def _proc_update(self, path): text = f'Upgrading outdated {self.desc[1]}' print_info(text, self._file, redirect=self._qflag) argv = [path, '-m', 'pip', 'install', '--upgrade'] if self._pre: argv.append('--pre') if self._user: argv.append('--user') if self._quiet: argv.append('--quiet') if self._verbose: argv.append('--verbose') argv.extend(self._update_opts) argc = ' '.join(argv) for package in self._var__temp_pkgs: args = f'{argc} {package}' print_scpt(args, self._file, redirect=self._qflag) if sudo(args, self._file, self._password, timeout=self._timeout, redirect=self._qflag, verbose=self._vflag, sethome=True): self._fail.append(package) else: self._pkgs.append(package) del self._var__temp_pkgs
def _proc_reinstall(self, path): text = f'Reinstalling specified {self.desc[1]}' print_info(text, self._file, redirect=self._qflag) argv = [path, 'cask', 'reinstall'] if self._force: argv.append('--force') if self._quiet: argv.append('--quiet') if self._verbose: argv.append('--verbose') argv.extend(self._reinstall_opts) argv.append('') askpass = f'SUDO_ASKPASS={self._askpass!r}' for package in self._var__temp_pkgs: argv[-1] = package print_scpt(' '.join(argv), self._file, redirect=self._qflag) if run(argv, self._file, shell=True, timeout=self._timeout, redirect=self._qflag, verbose=self._vflag, prefix=askpass): self._fail.append(package) else: self._pkgs.append(package) del self._var__temp_pkgs
def _proc_update(self, path): text = f'Upgrading outdated {self.desc[1]}' print_info(text, self._file, redirect=self._qflag) if self._recommend: _temp_pkgs = self._var__rcmd_pkgs else: _temp_pkgs = self._var__rcmd_pkgs | self._var__norm_pkgs argv = [path, '--install', '--no-scan'] if self._restart: argv.append('--restart') if self._quiet: argv.append('--quiet') argv.extend(self._update_opts) argc = ' '.join(argv) for package in _temp_pkgs: args = f'{argc} {package!r}' print_scpt(args, self._file, redirect=self._qflag) if sudo(args, self._file, self._password, timeout=self._timeout, redirect=self._qflag, verbose=self._vflag): self._fail.append(package) else: self._pkgs.append(package) del self._var__rcmd_pkgs del self._var__norm_pkgs del self._var__temp_pkgs
def _proc_update(self, path): text = f'Upgrading outdated {self.desc[1]}' print_info(text, self._file, redirect=self._qflag) argv = [path, 'update'] if self._quiet: argv.append('--quiet') if self._verbose: argv.append('--verbose') argv.extend(self._update_opts) argc = ' '.join(argv) for package in self._var__temp_pkgs: args = f'{argc} {package}' print_scpt(args, self._file, redirect=self._qflag) yes = 'y' if self._yes else None if sudo(argv, self._file, self._password, timeout=self._timeout, redirect=self._qflag, verbose=self._vflag, yes=yes): self._fail.append(package) else: self._pkgs.append(package) del self._var__temp_pkgs
def _proc_logging(self, path): text = f'Listing installed {self.desc[1]}' print_info(text, self._file, redirect=self._qflag) suffix = path.replace('/', ':') logfile = os.path.join(self._logroot, f'{self.log}-{suffix}{self.ext}') argv = [path, 'list', '--global', '--json'] if self._long: argv.append('--long') args = ' '.join(argv) print_scpt(args, self._file, redirect=self._qflag) with open(self._file, 'a') as file: file.write(f'Script started on {date()}\n') file.write(f'command: {args!r}\n') try: proc = subprocess.check_output(argv, stderr=make_stderr(self._vflag)) except subprocess.CalledProcessError: print_text(traceback.format_exc(), self._file, redirect=self._vflag) _real_pkgs = dict() else: context = proc.decode() print_text(context, self._file, redirect=self._vflag) content = json.loads(context.strip()) with open(logfile, 'w') as file: json.dump(content, file, indent=2) finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n')
def _proc_cleanup(self): if self._no_cleanup: return text = 'Pruning caches and archives' print_info(text, self._file, redirect=self._qflag) argv = ['pip', 'cleanup'] if self._verbose: argv.append('--verbose') if self._quiet: argv.append('--quiet') print_scpt(' '.join(argv), self._file, redirect=self._qflag) argv = ['rm', '-rf'] if self._verbose: argv.append('-v') argv.append('') for path in [ '/var/root/Library/Caches/pip/http/', '/var/root/Library/Caches/pip/wheels/', os.path.expanduser('~/Library/Caches/pip/http/'), os.path.expanduser('~/Library/Caches/pip/wheels/') ]: argv[-1] = path print_scpt(' '.join(argv), self._file, redirect=self._qflag) sudo(argv, self._file, self._password, redirect=self._qflag, verbose=self._vflag)
def _proc_logging(self, path): text = f'Listing installed {self.desc[1]}' print_info(text, self._file, redirect=self._qflag) suffix = path.replace('/', ':') logfile = os.path.join(self._logroot, f'{self.log}-{suffix}{self.ext}') argv = [path, '-m', 'pip', 'freeze'] if self._exclude_editable: argv.append('--exclude-editable') args = ' '.join(argv) print_scpt(args, self._file, redirect=self._qflag) with open(self._file, 'a') as file: file.write(f'Script started on {date()}\n') file.write(f'command: {args!r}\n') try: proc = subprocess.check_output(argv, stderr=make_stderr(self._vflag)) except subprocess.CalledProcessError: print_text(traceback.format_exc(), self._file, redirect=self._vflag) _real_pkgs = dict() else: context = proc.decode() print_text(context, self._file, redirect=self._vflag) with open(logfile, 'w') as file: file.writelines(filter(None, context.strip().splitlines(True))) # pylint: disable=filter-builtin-not-iterating finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n')
def _proc_update(self, path): text = f'Upgrading outdated {self.desc[1]}' print_info(text, self._file, redirect=self._qflag) argv = [path, 'upgrade'] argv.extend(self._update_opts) if self._yes: argv.append('--no-confirm') if self._verbose: argv.append('--verbose') if self._quiet: argv.append('--quiet') argv.append('--no-list') argv.append('--no-json') argv.append('') for package in self._var__temp_pkgs: argv[-1] = package print_scpt(argv, self._file, redirect=self._qflag) if run(argv, self._file, timeout=self._timeout, redirect=self._qflag, verbose=self._vflag): self._fail.append(package) else: self._pkgs.append(package) del self._var__temp_pkgs
def _proc_update(self, path): text = f'Upgrading outdated {self.desc[1]}' print_info(text, self._file, redirect=self._qflag) argv = [path, 'cask', 'upgrade'] if self._quiet: argv.append('--quiet') if self._verbose: argv.append('--verbose') if self._greedy: argv.append('--greedy') argv.extend(self._update_opts) temp = copy.copy(argv) if self._exhaust: temp.append('--exhaust') args = ' '.join(temp) argv.append('') askpass = f'SUDO_ASKPASS={self._askpass!r}' for package in self._var__temp_pkgs: argv[-1] = package print_scpt(f'{args} {package}', self._file, redirect=self._qflag) if run(argv, self._file, shell=True, timeout=self._timeout, redirect=self._qflag, verbose=self._vflag, prefix=askpass): self._fail.append(package) else: self._pkgs.append(package) del self._var__temp_pkgs
def _check_list(self, path): text = f'Checking outdated {self.desc[1]}' print_info(text, self._file, redirect=self._vflag) argv = [path, 'upgrade'] argv.extend(self._logging_opts) argv.append('--no-color') argv.append('--no-json') argv.append('--list') args = ' '.join(argv) print_scpt(args, self._file, redirect=self._vflag) with open(self._file, 'a') as file: file.write(f'Script started on {date()}\n') file.write(f'command: {args!r}\n') try: proc = subprocess.check_output(argv, stderr=make_stderr(self._vflag)) except subprocess.SubprocessError: print_text(traceback.format_exc(), self._file, redirect=self._vflag) self._var__temp_pkgs = set() # pylint: disable=attribute-defined-outside-init else: context = proc.decode() print_text(context, self._file, redirect=self._vflag) _temp_pkgs = list() for line in filter(lambda s: '->' in s, context.strip().splitlines()): _temp_pkgs.append(re.sub(r'.* (.*) .* -> .*', r'\1', line)) self._var__temp_pkgs = set(_temp_pkgs) # pylint: disable=attribute-defined-outside-init finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n')
def _proc_install(self, path): text = f'Installing specified {self.desc[1]}' print_info(text, self._file, redirect=self._qflag) argv = [path, '--install'] if self._restart: argv.append('--restart') if self._quiet: argv.append('--quiet') if self._verbose: argv.append('--verbose') argv.extend(self._install_opts) argc = ' '.join(argv) for package in self._var__temp_pkgs: args = f'{argc} {package!r}' print_scpt(args, self._file, redirect=self._qflag) if sudo(args, self._file, self._password, timeout=self._timeout, redirect=self._qflag, verbose=self._vflag): self._fail.append(package) else: self._pkgs.append(package) del self._var__temp_pkgs
def _check_list(self, path): argv = [path, '-m', 'pip', 'freeze'] argv.extend(self._logging_opts) text = f'Checking outdated {self.desc[1]}' print_info(text, self._file, redirect=self._vflag) try: proc = subprocess.check_output(argv, stderr=make_stderr(self._vflag)) except subprocess.SubprocessError: print_text(traceback.format_exc(), self._file, redirect=self._vflag) self._var__temp_pkgs = set() # pylint: disable=attribute-defined-outside-init else: context = proc.decode() print_text(context, self._file, redirect=self._qflag) _temp_pkgs = list() for line in filter(None, context.strip().splitlines()): _temp_pkgs.append(line.split('==')[0]) self._var__temp_pkgs = set(_temp_pkgs) # pylint: disable=attribute-defined-outside-init finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n')
def _check_list(self, path): text = f'Checking installed {self.desc[1]}' print_info(text, self._file, redirect=self._vflag) argv = [path, 'cask', 'list'] argv.extend(self._logging_opts) args = ' '.join(argv) print_scpt(args, self._file, redirect=self._vflag) with open(self._file, 'a') as file: file.write(f'Script started on {date()}\n') file.write(f'command: {args!r}\n') try: proc = subprocess.check_output(argv, stderr=make_stderr(self._vflag)) except subprocess.SubprocessError: print_text(traceback.format_exc(), self._file, redirect=self._vflag) self._var__temp_pkgs = set() # pylint: disable=attribute-defined-outside-init else: context = proc.decode() self._var__temp_pkgs = set(context.strip().split()) # pylint: disable=attribute-defined-outside-init print_text(context, self._file, redirect=self._vflag) finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n')
def make_storage(config, today, quiet=False, verbose=False, logfile=os.devnull): arclist = list() logdate = datetime.date.strftime(today, r'%y%m%d') dskpath = config['Path']['dskdir'] tarpath = os.path.join(config['Path']['logdir'], 'tarfile') if not os.path.isdir(dskpath): return arclist if not os.path.isdir(tarpath): return arclist text = f'Storing ancient archives at external hard disk {under}{dskpath}{reset}' print_info(text, logfile, redirect=quiet) days = calendar.monthrange(year=today.year, month=today.month)[1] ctime = datetime.datetime.fromtimestamp(os.stat(tarpath).st_birthtime) if (today - ctime) > datetime.timedelta(days=days): glob_list = glob.glob(os.path.join(tarpath, '*/*.tar.xz')) if glob_list: with tempfile.TemporaryDirectory() as tmppath: arcdate = datetime.date.strftime(ctime, r'%y%m%d') tarname = os.path.join(tmppath, f'{arcdate}-{logdate}.tar.bz') if verbose: print_scpt(f'tar -cjf {tarname} {tarpath}', logfile, redirect=quiet) else: print_scpt(f'tar -cjvf {tarname} {tarpath}', logfile, redirect=verbose) with tarfile.open(tarname, 'w:bz2') as bz: for absname in glob_list: arcname = pathlib.Path(absname).relative_to(tarpath) bz.add(absname, arcname) arclist.append(absname) print_text(absname, logfile, redirect=verbose) arcfile = os.path.join(config['Path']['arcdir'], 'archive.zip') if verbose: print_scpt(f'tar -cZf {arcfile} {tarname}', logfile, redirect=quiet) else: print_scpt(f'tar -cZvf {arcfile} {tarname}', logfile, redirect=verbose) with zipfile.ZipFile(arcfile, 'a', zipfile.ZIP_DEFLATED) as zf: arcname = os.path.split(tarname)[1] zf.write(tarname, arcname) print_text(tarname, logfile, redirect=verbose) shutil.rmtree(tarpath) return arclist
def _fetch_dependency(package, depth): if depth == 0: return dict() depth -= 1 dependencies = _data_pkgs.get(package) if dependencies is not None: return dependencies text = f'Searching dependencies of {self.desc[0]} {under}{package}{reset}' print_info(text, self._file, redirect=self._vflag) argv = [path, 'deps', '--installed', '-1'] if self._include_build: argv.append('--include-build') if self._include_optional: argv.append('--include-optional') if self._include_test: argv.append('--include-test') if self._skip_recommended: argv.append('--skip-recommended') if self._include_requirements: argv.append('--include-requirements') argv.append(package) args = ' '.join(argv) print_scpt(args, self._file, redirect=self._vflag) with open(self._file, 'a') as file: file.write(f'Script started on {date()}\n') file.write(f'command: {args!r}\n') _deps_pkgs = dict() try: proc = subprocess.check_output(argv, stderr=make_stderr(self._vflag)) except subprocess.CalledProcessError: self._fail.append(package) with contextlib.suppress(KeyError): self._var__temp_pkgs.remove(package) print_text(traceback.format_exc(), self._file, redirect=self._vflag) else: context = proc.decode() print_text(context, self._file, redirect=self._vflag) _list_pkgs.append(package) for item in filter(None, context.strip().splitlines()): if item in self._var__temp_pkgs: self._var__temp_pkgs.remove(item) _deps_pkgs[item] = _fetch_dependency(item, depth) _data_pkgs.update(_deps_pkgs) finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n') return _deps_pkgs
def _check_list(self, path): argv = [path, '-m', 'pip', 'list', '--outdated'] if self._pre: argv.append('--pre') argv.extend(self._logging_opts) text = f'Checking outdated {self.desc[1]}' print_info(text, self._file, redirect=self._vflag) temp = copy.copy(argv) temp.append('--format=columns') args = ' '.join(temp) print_scpt(args, self._file, redirect=self._vflag) with open(self._file, 'a') as file: file.write(f'Script started on {date()}\n') file.write(f'command: {args!r}\n') argv.append('--format=json') try: proc = subprocess.check_output(argv, stderr=make_stderr(self._vflag)) except subprocess.SubprocessError: print_text(traceback.format_exc(), self._file, redirect=self._vflag) self._var__temp_pkgs = set() # pylint: disable=attribute-defined-outside-init else: # self._var__temp_pkgs = set(map(lambda pkg: pkg.split('==')[0], proc.decode().split())) text = proc.decode() start = text.rfind('[') stop = text.rfind(']') + 1 context = json.loads(text[start:stop]) self._var__temp_pkgs = set(map(lambda item: item['name'], context)) # pylint: disable=attribute-defined-outside-init prefix = text[:start] if prefix: print_text(prefix, self._file, redirect=self._vflag) if context: name_len = max(7, max(map(lambda item: len(item['name']), context), default=7)) version_len = max(7, max(map(lambda item: len(item['version']), context), default=7)) latest_version_len = max(6, max(map(lambda item: len(item['latest_version']), context), default=6)) latest_filetype_len = max(4, max(map(lambda item: len(item['latest_filetype']), context), default=4)) def _pprint(package, version, latest, filetype): text = [package.ljust(name_len), version.ljust(version_len), latest.ljust(latest_version_len), filetype.ljust(latest_filetype_len)] return ' '.join(text) print_text(_pprint('Package', 'Version', 'Latest', 'Type'), self._file, redirect=self._vflag) print_text(' '.join(map(lambda length: '-' * length, [name_len, version_len, latest_version_len, latest_filetype_len])), self._file, redirect=self._vflag) for item in context: print_text(_pprint(item['name'], item['version'], item['latest_version'], item['latest_filetype']), self._file, redirect=self._vflag) finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n')
def _check_list(self, path): text = 'Updating RubyGems database' print_info(text, self._file, redirect=self._qflag) argv = [path, 'update', '--system'] if self._quiet: argv.append('--quiet') if self._verbose: argv.append('--verbose') args = ' '.join(argv) print_scpt(args, self._file, redirect=self._qflag) sudo(argv, self._file, self._password, redirect=self._qflag, verbose=self._vflag) text = f'Checking outdated {self.desc[1]}' print_info(text, self._file, redirect=self._vflag) argv = [path, 'outdated'] if self._quiet: argv.append('--quiet') if self._verbose: argv.append('--verbose') argv.extend(self._logging_opts) args = ' '.join(argv) print_scpt(args, self._file, redirect=self._vflag) with open(self._file, 'a') as file: file.write(f'Script started on {date()}\n') file.write(f'command: {args!r}\n') try: proc = subprocess.check_output(argv, stderr=make_stderr(self._vflag)) except subprocess.SubprocessError: print_text(traceback.format_exc(), self._file, redirect=self._vflag) self._var__temp_pkgs = set() # pylint: disable=attribute-defined-outside-init else: context = proc.decode() print_text(context, self._file, redirect=self._vflag) _temp_pkgs = list() for item in filter(lambda s: re.match(r'\w* \(.*\)', s), context.strip().splitlines()): _temp_pkgs.append(item.split()[0]) self._var__temp_pkgs = set(_temp_pkgs) # pylint: disable=attribute-defined-outside-init # self._var__temp_pkgs = set(map(lambda s: s.split()[0], filter(None, context.strip().splitlines()))) finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n')
def _run_proc(self): self._pkgs = list() self._fail = list() self._lost = list() for path in self._exec: text = f'Using {self.name} executable {path!r}' print_info(text, self._file, redirect=self._qflag) if self._proc_logging(path): self._proc_dependency(path) else: text = f'No {self.desc[1]} to query for executable {path!r}' print_info(text, self._file, redirect=self._qflag)
def _fetch_dependency(package, depth): if depth == 0: return dict() depth -= 1 dependencies = _data_pkgs.get(package) if dependencies is not None: return dependencies text = f'Searching dependencies of {self.desc[0]} {under}{package}{reset}' print_info(text, self._file, redirect=self._vflag) argv = [path, '-m', 'pip', 'show', package] args = ' '.join(argv) print_scpt(args, self._file, redirect=self._vflag) with open(self._file, 'a') as file: file.write(f'Script started on {date()}\n') file.write(f'command: {args!r}\n') _deps_pkgs = dict() try: proc = subprocess.check_output(argv, stderr=make_stderr(self._vflag)) except subprocess.CalledProcessError: self._fail.append(package) with contextlib.suppress(KeyError): self._var__temp_pkgs.remove(package) print_text(traceback.format_exc(), self._file, redirect=self._vflag) else: context = proc.decode() print_text(context, self._file, redirect=self._vflag) requirements = set() for line in context.strip().splitlines(): match = re.match(r'Requires: (.*)', line) if match is not None: requirements = set(match.groups()[0].split(', ')) break _list_pkgs.append(package) for item in filter(None, requirements): if item in self._var__temp_pkgs: self._var__temp_pkgs.remove(item) _deps_pkgs[item] = _fetch_dependency(item, depth) _data_pkgs.update(_deps_pkgs) finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n') return _deps_pkgs
def _check_pkgs(self, path): text = f'Listing installed {self.desc[1]}' print_info(text, self._file, redirect=self._vflag) argv = [path, 'list'] args = ' '.join(argv) print_scpt(args, self._file, redirect=self._vflag) with open(self._file, 'a') as file: file.write(f'Script started on {date()}\n') file.write(f'command: {args!r}\n') try: proc = subprocess.check_output(argv, stderr=make_stderr(self._vflag)) except subprocess.CalledProcessError: print_text(traceback.format_exc(), self._file, redirect=self._vflag) _real_pkgs = set() else: context = proc.decode() print_text(context, self._file, redirect=self._vflag) _list_pkgs = dict() for line in context.strip().splitlines(): match = re.match(r'(?P<code>\d{9}) (?P<name>.*?) \(.+?\)', line) if match is None: continue _list_pkgs[match.group('name')] = match.group('code') _real_pkgs = set(_list_pkgs.keys()) finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n') text = 'Checking existence of specified packages' print_info(text, self._file, redirect=self._vflag) _temp_pkgs = list() _lost_pkgs = list() for package in self._packages: if package in _real_pkgs: _temp_pkgs.append(package) else: _lost_pkgs.append(package) self._lost.extend(_lost_pkgs) self._var__dict_pkgs = _list_pkgs # pylint: disable=attribute-defined-outside-init self._var__real_pkgs = set(_real_pkgs) # pylint: disable=attribute-defined-outside-init self._var__lost_pkgs = set(_lost_pkgs) # pylint: disable=attribute-defined-outside-init self._var__temp_pkgs = set(_temp_pkgs) # pylint: disable=attribute-defined-outside-init
def _run_proc(self): self._pkgs = list() self._fail = list() self._lost = list() for path in self._exec: text = f'Using {self.name} executable {path!r}' print_info(text, self._file, redirect=self._qflag) if self._proc_logging(path): self._proc_postinstall(path) else: text = f'No {self.desc[1]} to postinstall for executable {path!r}' print_info(text, self._file, redirect=self._qflag) self._proc_fixmissing(path) self._proc_cleanup()
def launch_askpass(password=None, quiet=False, verbose=False, logfile=os.devnull, *args, **kwargs): # pylint: disable=unused-argument,keyword-arg-before-vararg text = 'Launching MacDaily SSH-AskPass program' print_info(text, logfile, quiet) path = f'Macintosh HD{ROOT.replace(os.path.sep, ":")}:img:askpass.icns' ASKPASS = ['#!/usr/bin/env osascript', '', '-- script based on https://github.com/theseal/ssh-askpass', '', 'on run argv', ' set args to argv as text', ' if args starts with "--help" or args starts with "-h" then', ' return "macdaily-askpass [-h|--help] [prompt]"', ' end if', f' display dialog args with icon file ("{path}") default button "OK" default answer "" with hidden answer', # pylint: disable=line-too-long " return result's text returned", 'end run', ''] askpass = os.path.join(ROOT, 'res', 'askpass.applescript') text = f'Making executable {askpass!r}' print_misc(text, logfile, verbose) user = owner = getpass.getuser() if os.path.isfile(askpass): owner = pwd.getpwuid(os.stat(askpass).st_uid).pw_name if user != owner: run_script(['chown', user, askpass], quiet, verbose, sudo=True, password=password, logfile=logfile) else: try: pathlib.Path(askpass).touch() except PermissionError: owner = 'root' run_script(['touch', askpass], quiet, verbose, sudo=True, password=password, logfile=logfile) run_script(['chown', user, askpass], quiet, verbose, sudo=True, password=password, logfile=logfile) with open(askpass, 'w') as file: file.write(os.linesep.join(ASKPASS)) if user != owner: run_script(['chmod', '+x', askpass], quiet, verbose, sudo=True, password=password, logfile=logfile) run_script(['chown', owner, askpass], quiet, verbose, sudo=True, password=password, logfile=logfile) else: run_script(['chmod', '+x', askpass], quiet, verbose, logfile=logfile) return askpass
def _proc_renew(self, path): text = 'Updating Homebrew database' print_info(text, self._file, redirect=self._qflag) argv = [path, 'update'] if self._force: argv.append('--force') if self._merge: argv.append('--merge') if self._quiet: argv.append('--quiet') if self._verbose: argv.append('--verbose') print_scpt(' '.join(argv), self._file, redirect=self._qflag) run(argv, self._file, redirect=self._qflag)
def _run_proc(self): self._pkgs = list() self._fail = list() for path in self._exec: text = f'Using {self.name} executable {path!r}' print_info(text, self._file, redirect=self._qflag) self._var__temp_pkgs = self._packages # pylint: disable=attribute-defined-outside-init if self._check_confirm(path): self._proc_install(path) else: text = f'No {self.desc[1]} to install for executable {path!r}' print_info(text, self._file, redirect=self._qflag) self._proc_fixmissing(path) self._proc_cleanup()
def _check_pkgs(self, path): text = f'Listing installed {self.desc[1]}' print_info(text, self._file, redirect=self._vflag) argv = [path, 'list', '--no-versions'] args = ' '.join(argv) print_scpt(args, self._file, redirect=self._vflag) with open(self._file, 'a') as file: file.write(f'Script started on {date()}\n') file.write(f'command: {args!r}\n') try: proc = subprocess.check_output(argv, stderr=make_stderr(self._vflag)) except subprocess.CalledProcessError: print_text(traceback.format_exc(), self._file, redirect=self._vflag) _real_pkgs = set() else: context = proc.decode() print_text(context, self._file, redirect=self._vflag) _temp_pkgs = list() for package in filter(None, context.strip().splitlines()): if package.startswith('***'): continue _temp_pkgs.append(package) _real_pkgs = set(_temp_pkgs) finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n') text = 'Checking existence of specified packages' print_info(text, self._file, redirect=self._vflag) _temp_pkgs = list() _lost_pkgs = list() for package in self._packages: if package in _real_pkgs: _temp_pkgs.append(package) else: _lost_pkgs.append(package) self._lost.extend(_lost_pkgs) self._var__real_pkgs = set(_real_pkgs) # pylint: disable=attribute-defined-outside-init self._var__lost_pkgs = set(_lost_pkgs) # pylint: disable=attribute-defined-outside-init self._var__temp_pkgs = set(_temp_pkgs) # pylint: disable=attribute-defined-outside-init
def launch_confirm(password=None, quiet=False, verbose=False, logfile=os.devnull, *args, **kwargs): # pylint: disable=unused-argument,keyword-arg-before-vararg text = 'Launching MacDaily Confirmation program' print_info(text, logfile, quiet) path = f'Macintosh HD{ROOT.replace(os.path.sep, ":")}:img:confirm.icns' ASKPASS = ['#!/usr/bin/env osascript', '', 'on run argv', ' set args to argv as text', ' if args starts with "--help" or args starts with "-h" then', ' return "macdaily-confirm [-h|--help] [prompt]"', ' end if', f' display dialog args with icon file ("{path}") default button "Cancel"', " return result's button returned", 'end run', ''] confirm = os.path.join(ROOT, 'res', 'confirm.applescript') text = f'Making executable {confirm!r}' print_misc(text, logfile, verbose) user = owner = getpass.getuser() if os.path.isfile(confirm): owner = pwd.getpwuid(os.stat(confirm).st_uid).pw_name if user != owner: run_script(['chown', user, confirm], quiet, verbose, sudo=True, password=password, logfile=logfile) else: try: pathlib.Path(confirm).touch() except PermissionError: owner = 'root' run_script(['touch', confirm], quiet, verbose, sudo=True, password=password, logfile=logfile) run_script(['chown', user, confirm], quiet, verbose, sudo=True, password=password, logfile=logfile) with open(confirm, 'w') as file: file.write(os.linesep.join(ASKPASS)) if user != owner: run_script(['chmod', '+x', confirm], quiet, verbose, sudo=True, password=password, logfile=logfile) run_script(['chown', owner, confirm], quiet, verbose, sudo=True, password=password, logfile=logfile) else: run_script(['chmod', '+x', confirm], quiet, verbose, logfile=logfile) return confirm