def run_script( argv, quiet=False, verbose=False, sudo=False, # pylint: disable=dangerous-default-value password=None, logfile=os.devnull, env=os.environ): args = ' '.join(argv) print_scpt(args, logfile, verbose) with open(logfile, 'a') as file: file.write(f'Script started on {date()}\n') file.write(f'command: {args!r}\n') try: if sudo: if password is not None: sudo_argv = ['sudo', '--stdin', '--prompt=Password:\n'] sudo_argv.extend(argv) with make_pipe(password, verbose) as pipe: proc = subprocess.check_output(sudo_argv, stdin=pipe.stdout, stderr=make_stderr(verbose), env=env) else: sudo_argv = ['sudo'] sudo_argv.extend(argv) proc = subprocess.check_output(sudo_argv, stderr=make_stderr(verbose), env=env) else: proc = subprocess.check_output(argv, stderr=make_stderr(verbose), env=env) except subprocess.CalledProcessError as error: print_text(traceback.format_exc(), logfile, redirect=verbose) print_term( f"macdaily: {red}error{reset}: " f"command `{bold}{' '.join(error.cmd)!r}{reset}' failed", logfile, redirect=quiet) raise else: context = proc.decode() print_text(context, logfile, redirect=verbose) finally: with open(logfile, 'a') as file: file.write(f'Script done on {date()}\n')
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 _sort_glob(path, *, flag): implementation = 'cpython' if flag else 'pypy' glob_path = glob.glob(path) if not glob_path: return glob_path.sort(reverse=True) path = glob_path[0] try: ver_proc = subprocess.check_output([path, '--version'], stderr=subprocess.STDOUT) except subprocess.CalledProcessError: return if re.match(rb"^Python \d\.\d", ver_proc, flags=(re.I | re.M)) is None: return ver_text = ver_proc.decode().split()[1] def _append_path(exec_path): exec_path[ver_text[:3]].append(path) exec_path[ver_text[0]].append(path) exec_path['main'].append(path) _append_path(EXEC_PATH['implementation'][implementation]) _append_path(EXEC_PATH['combination'][(True, flag)]) _append_path(EXEC_PATH['source']['brew']) _append_path(EXEC_PATH['version'])
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_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 _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 _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 cowfile(): try: proc = subprocess.check_output([COWSAY, '-l'], stderr=subprocess.DEVNULL) except subprocess.CalledProcessError: return list() cows = list() for line in proc.strip().splitlines()[1:]: cows.extend(line.decode().split()) return cows
def get_pass(askpass, queue=None): if sys.stdin.isatty(): try: return getpass.getpass(prompt='Password:'******'🔑 Enter your password for {USER}.'], # pylint: disable=E1101 stderr=subprocess.DEVNULL).strip().decode() if queue is not None: queue.put(RETURN) return RETURN
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 _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', '--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 _check_list(self, path): if (self._brew_renew is None or time.time() - self._brew_renew >= 300): self._proc_renew(path) self._brew_renew = time.time() if self._exhaust: self._exhaust_check(path) return text = f'Checking outdated {self.desc[1]}' print_info(text, self._file, redirect=self._vflag) argv = [path, 'cask', 'outdated'] if self._quiet: argv.append('--quiet') if self._verbose: argv.append('--verbose') if self._greedy: argv.append('--greedy') 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 line in filter(None, context.strip().splitlines()): _temp_pkgs.append(line.split(maxsplit=1)[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 _loc_exec(self): if not (self._brew and self._system): self._exec = {self._var__exec_path} else: _exec_path = list() if self._brew: text = 'Looking for brewed Ruby' print_info(text, self._file, redirect=self._vflag) argv = ['brew', '--prefix'] 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) else: context = proc.decode() print_text(context, self._file, redirect=self._vflag) _glob_path = glob.glob( os.path.join(context.strip(), 'Cellar/ruby/*/bin/gem')) _glob_path.sort(reverse=True) _exec_path.append(_glob_path[0]) finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n') if self._system: text = 'Looking for macOS-provided Ruby' print_info(text, self._file, redirect=self._vflag) if os.path.exists('/usr/bin/gem'): _exec_path.append('/usr/bin/gem') else: _exec_path.extend(glob.glob('/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/gem')) # pylint: disable=line-too-long self._exec = set(_exec_path) del self._var__exec_path
def _proc_dependency(package, _know_pkgs): _deps_pkgs = {package} if self._ignore_deps: return _deps_pkgs 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') _temp_pkgs = set() try: proc = subprocess.check_output(argv, stderr=make_stderr(self._vflag)) except subprocess.CalledProcessError: print_text(traceback.format_exc(), self._file, redirect=self._vflag) else: context = proc.decode() print_text(context, self._file, redirect=self._vflag) for line in filter(lambda s: s.startswith('Requires: '), context.strip().splitlines()): _temp_pkgs = set( map(lambda s: s.rstrip(','), line.split()[1:])) - _know_pkgs _deps_pkgs |= _temp_pkgs break finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n') for pkg in _temp_pkgs: _temp_pkgs = _proc_dependency(pkg, _know_pkgs) _deps_pkgs |= _temp_pkgs _know_pkgs |= _temp_pkgs return _deps_pkgs
def _check_list(self, path): text = f'Checking outdated {self.desc[1]}' print_info(text, self._file, redirect=self._vflag) argv = [path, '--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__rcmd_pkgs = set() # pylint: disable=attribute-defined-outside-init self._var__norm_pkgs = set() # pylint: disable=attribute-defined-outside-init else: context = proc.decode() print_text(context, self._file, redirect=self._vflag) _rcmd_pkgs = list() _norm_pkgs = list() for package in filter(lambda s: re.match(r'^\W*[-*]', s), context.strip().splitlines()): flag, name = package.strip().split(maxsplit=1) if flag == '*': _rcmd_pkgs.append(name) if flag == '-': _norm_pkgs.append(name) self._var__rcmd_pkgs = set(_rcmd_pkgs) # pylint: disable=attribute-defined-outside-init self._var__norm_pkgs = set(_norm_pkgs) # pylint: disable=attribute-defined-outside-init finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n') self._var__temp_pkgs = self._var__rcmd_pkgs | self._var__norm_pkgs # pylint: disable=attribute-defined-outside-init
def _proc_dependency(package): _deps_pkgs = [package] if self._ignore_deps: return _deps_pkgs text = f'Searching dependencies of {self.desc[0]} {under}{package}{reset}' print_info(text, self._file, redirect=self._vflag) argv = [path, 'deps', '--installed', '-n'] 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') try: proc = subprocess.check_output(argv, stderr=make_stderr(self._vflag)) except subprocess.CalledProcessError: print_text(traceback.format_exc(), self._file, redirect=self._vflag) else: context = proc.decode() _deps_pkgs.extend(reversed(context.strip().split())) print_text(context, self._file, redirect=self._vflag) finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n') return _deps_pkgs
def _check_list(self, path): text = f'Checking outdated {self.desc[1]}' print_info(text, self._file, redirect=self._vflag) argv = [path, 'outdated'] 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 = dict() for line in filter(None, context.strip().splitlines()): match = re.match(r'(?P<code>\d{9}) (?P<name>.*?) \(.+?\)', line) if match is None: continue _temp_pkgs[match.group('name')] = match.group('code') self._var__temp_pkgs = set(_temp_pkgs.keys()) # pylint: disable=attribute-defined-outside-init self._ver__dict_pkgs = _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 _exhaust_check(self, path): text = f'Checking outdated {self.desc[1]} exclusively' print_info(text, self._file, redirect=self._vflag) argv = ['brew', 'cask', 'outdated', '--exhaust'] if self._quiet: argv.append('--quiet') if self._verbose: argv.append('--verbose') argv.extend(self._logging_opts) print_scpt(' '.join(argv), self._file, redirect=self._vflag) text = f'Listing installed {self.desc[1]}' print_info(text, self._file, redirect=self._vflag) argv = [path, 'cask', '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) _list_pkgs = set() else: context = proc.decode() _list_pkgs = set(context.split()) print_text(context, self._file, redirect=self._vflag) finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n') text = f'Fetching Homebrew prefix' print_info(text, self._file, redirect=self._vflag) argv = [path, '--prefix'] 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') fail = False try: proc = subprocess.check_output(argv, stderr=make_stderr(self._vflag)) except subprocess.CalledProcessError: print_text(traceback.format_exc(), self._file, redirect=self._vflag) self._var__temp_pkgs = set() # pylint: disable=attribute-defined-outside-init fail = True else: context = proc.decode() print_text(context, self._file, redirect=self._vflag) prefix = context.strip() finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n') if fail: return text = f'Checking versions of installed {self.desc[1]}' print_info(text, self._file, redirect=self._vflag) _temp_pkgs = list() for cask in _list_pkgs: argv = [path, 'cask', 'info', cask] 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) else: context = proc.decode() print_text(context, self._file, redirect=self._vflag) version = context.split(maxsplit=2)[1] installed = os.path.join(prefix, 'Caskroom', cask, version) if os.path.isdir(installed): _temp_pkgs.append(cask) finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n') self._var__temp_pkgs = set(_temp_pkgs) # pylint: disable=attribute-defined-outside-init max_len = len(max(_temp_pkgs, key=len)) context = os.linesep.join( textwrap.wrap( ' '.join( map(lambda s: s.ljust(max_len), self._var__temp_pkgs)), length)) print_scpt(f'{path} cask outdated list', self._file, redirect=self._vflag) print_text(context, self._file, redirect=self._vflag)
def _proc_logging(self, path): text = f'Listing installed {self.desc[1]}' print_info(text, self._file, redirect=self._qflag) argv = [path, 'list'] 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) _real_pkgs = collections.defaultdict(list) for line in filter(None, context.strip().splitlines()): if line.startswith('***'): continue package, versions = line.split(maxsplit=1) _real_pkgs[package].extend( re.findall(r'\d+\.\d+\.\d+', versions)) finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n') _temp_pkgs = list() argv = [path, 'lock', ''] for package, versions in _real_pkgs.items(): for version in versions: argv[-1] = f'{package}-{version}' 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 = dict() else: context = proc.decode() print_text(context, self._file, redirect=self._vflag) _temp_pkgs.extend( filter(lambda s: s.startswith('gem'), context.splitlines(True))) # pylint: disable=filter-builtin-not-iterating finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n') suffix = path.replace('/', ':') logfile = os.path.join(self._logroot, f'{self.log}-{suffix}{self.ext}') with open(logfile, 'w') as file: file.write(f"require 'rubygems'{os.linesep}") file.writelines(sorted(set(_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 = ['brew', 'cleanup'] if self._verbose: argv.append('--verbose') if self._quiet: argv.append('--quiet') args = ' '.join(argv) print_scpt(args, self._file, redirect=self._qflag) flag = (not os.path.isdir(self._disk_dir)) for path in self._exec: logs = os.path.expanduser('~/Library/Logs/Homebrew/') if os.path.isdir(logs): argv = [path, 'logs', 'cleanup'] if self._verbose: argv.append('--verbose') if self._quiet: argv.append('--quiet') args = ' '.join(argv) print_scpt(args, self._file, redirect=self._vflag) argv = ['rm', '-rf'] if self._verbose: argv.append('-v') argv.append(logs) sudo(argv, self._file, self._password, redirect=self._qflag, verbose=self._vflag) # if external disk not attached if flag: continue argv = [path, '--cache'] 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') fail = False try: proc = subprocess.check_output(argv, stderr=make_stderr(self._vflag)) except subprocess.CalledProcessError: print_text(traceback.format_exc(), self._file, redirect=self._vflag) fail = True else: context = proc.decode() print_text(context, self._file, redirect=self._vflag) finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n') if fail: continue cache = context.strip() if os.path.isdir(cache): argv = [path, 'caches', 'archive'] if self._verbose: argv.append('--verbose') if self._quiet: argv.append('--quiet') args = ' '.join(argv) print_scpt(args, self._file, redirect=self._qflag) def _move(root, stem): arch = os.path.join(self._disk_dir, stem) pathlib.Path(arch).mkdir(parents=True, exist_ok=True) file_list = list() for name in os.listdir(root): path = os.path.join(root, name) if os.path.isdir(path): if name != 'Cask': file_list.extend( _move(path, os.path.join(stem, name))) elif os.path.splitext(name)[ 1] != '.incomplete' and path not in cask_list: # pylint: disable=cell-var-from-loop try: shutil.move(path, os.path.join(arch, name)) except (shutil.Error, FileExistsError): os.remove(path) # with contextlib.suppress(shutil.Error, FileExistsError): # shutil.move(path, os.path.join(arch, name)) file_list.append(path) return file_list cask_list = [ os.path.realpath(name) for name in glob.glob(os.path.join(cache, 'Cask/*')) ] file_list = _move(cache, 'Homebrew') print_text(os.linesep.join(sorted(file_list)), self._file, redirect=self._vflag) if flag: text = ( f'macdaily-{self.cmd}: {yellow}brew{reset}: ' f'archive directory {bold}{self._disk_dir}{reset} not found') print_term(text, self._file, redirect=self._vflag)
def _proc_cleanup(self): if self._no_cleanup: return text = 'Pruning caches and archives' print_info(text, self._file, redirect=self._qflag) if not os.path.isdir(self._disk_dir): text = ( f'macdaily-{self.cmd}: {yellow}cask{reset}: ' f'archive directory {bold}{self._disk_dir}{reset} not found') print_term(text, self._file, redirect=self._vflag) return argv = ['brew', 'cask', 'cleanup'] if self._verbose: argv.append('--verbose') if self._quiet: argv.append('--quiet') print_scpt(' '.join(argv), self._file, redirect=self._qflag) path_cask = os.path.join(self._disk_dir, 'Homebrew', 'Cask') path_down = os.path.join(self._disk_dir, 'Homebrew', 'download') pathlib.Path(path_cask).mkdir(parents=True, exist_ok=True) pathlib.Path(path_down).mkdir(parents=True, exist_ok=True) for path in self._exec: argv = [path, '--cache'] 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') fail = False try: proc = subprocess.check_output(argv, stderr=make_stderr(self._vflag)) except subprocess.CalledProcessError: print_text(traceback.format_exc(), self._file, redirect=self._vflag) fail = True else: context = proc.decode() print_text(context, self._file, redirect=self._vflag) finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n') if fail: continue cache = context.strip() if os.path.isdir(cache): argv = [path, 'cask', 'caches', 'archive'] if self._verbose: argv.append('--verbose') if self._quiet: argv.append('--quiet') print_scpt(' '.join(argv), self._file, redirect=self._qflag) file_list = list() link_list = glob.glob(os.path.join(cache, 'Cask/*')) cask_list = [os.path.realpath(name) for name in link_list] for link in link_list: file_list.append(link) try: shutil.move(link, path_cask) except (shutil.Error, FileExistsError): os.remove(link) for cask in filter( lambda p: os.path.splitext(p)[1] != '.incomplete', cask_list): try: shutil.move(cask, path_down) except (shutil.Error, FileExistsError): os.remove(cask) file_list.append(cask) print_text(os.linesep.join(sorted(file_list)), self._file, redirect=self._vflag)
def _check_pkgs(self, path): _temp_pkgs = list() _lost_pkgs = list() text = 'Checking existence of specified packages' print_info(text, self._file, redirect=self._vflag) for package in self._packages: 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') try: proc = subprocess.check_output(argv, stderr=subprocess.STDOUT) except subprocess.CalledProcessError: print_text(traceback.format_exc(), self._file, redirect=self._vflag) _lost_pkgs.append(package) else: print_text(proc.decode(), self._file, redirect=self._vflag) _temp_pkgs.append(package) finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n') if _lost_pkgs: text = f'Listing installed {self.desc[1]}' print_info(text, self._file, redirect=self._vflag) self._lost.extend(_lost_pkgs) argv = [path, '-m', 'pip', '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) self._var__real_pkgs = set() # pylint: disable=attribute-defined-outside-init else: context = proc.decode() print_text(context, self._file, redirect=self._vflag) self._var__real_pkgs = set( map(lambda pkg: pkg.split('==')[0], filter(None, context.splitlines()))) # pylint: disable=attribute-defined-outside-init,filter-builtin-not-iterating finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n') else: self._var__real_pkgs = set() # 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 _loc_exec(self): EXEC_PATH = dict( source=dict( brew=collections.defaultdict(list), system=collections.defaultdict(list), ), version=collections.defaultdict(list), combination={ (True, True): collections.defaultdict(list), (True, False): collections.defaultdict(list), (False, True): collections.defaultdict(list), (False, False): collections.defaultdict(list), }, implementation=dict( cpython=collections.defaultdict(list), pypy=collections.defaultdict(list), ), ) def _sort_glob(path, *, flag): implementation = 'cpython' if flag else 'pypy' glob_path = glob.glob(path) if not glob_path: return glob_path.sort(reverse=True) path = glob_path[0] try: ver_proc = subprocess.check_output([path, '--version'], stderr=subprocess.STDOUT) except subprocess.CalledProcessError: return if re.match(rb"^Python \d\.\d", ver_proc, flags=(re.I | re.M)) is None: return ver_text = ver_proc.decode().split()[1] def _append_path(exec_path): exec_path[ver_text[:3]].append(path) exec_path[ver_text[0]].append(path) exec_path['main'].append(path) _append_path(EXEC_PATH['implementation'][implementation]) _append_path(EXEC_PATH['combination'][(True, flag)]) _append_path(EXEC_PATH['source']['brew']) _append_path(EXEC_PATH['version']) try: proc = subprocess.check_output(['brew', '--prefix'], stderr=make_stderr(self._vflag)) except subprocess.CalledProcessError: prefix = '/usr/local' else: prefix = proc.decode().strip() _sort_glob(os.path.join(prefix, 'Cellar/pypy/*/bin/pypy'), flag=False) _sort_glob(os.path.join(prefix, 'Cellar/pypy3/*/bin/pypy3'), flag=False) _sort_glob(os.path.join(prefix, 'Cellar/python@2/*/bin/python2.?'), flag=True) _sort_glob(os.path.join(prefix, 'Cellar/python/*/bin/python3.?'), flag=True) def _append_path(exec_path): exec_path[version[0]].append(path) # pylint: disable=undefined-loop-variable exec_path[version].append(path) # pylint: disable=undefined-loop-variable exec_path['main'].append(path) # pylint: disable=undefined-loop-variable for path in glob.glob( '/Library/Frameworks/Python.framework/Versions/?.?/bin/python?.?' ): version = path[-3:] _append_path(EXEC_PATH['combination'][(False, True)]) _append_path(EXEC_PATH['implementation']['cpython']) _append_path(EXEC_PATH['source']['system']) _append_path(EXEC_PATH['version']) def _extend_version(exec_path): if self._version: for version in self._version: temp_exec.extend(exec_path[version]) else: temp_exec.extend(exec_path['main']) temp_exec = list() if not any([self._brew, self._cpython, self._pypy, self._system]): _extend_version(EXEC_PATH['version']) elif (any([self._brew, self._system]) and not any([self._cpython, self._pypy])): if self._brew: _extend_version(EXEC_PATH['source']['brew']) if self._system: _extend_version(EXEC_PATH['source']['system']) elif (any([self._cpython, self._pypy]) and not any([self._brew, self._system])): if self._cpython: _extend_version(EXEC_PATH['implementation']['cpython']) if self._pypy: _extend_version(EXEC_PATH['implementation']['pypy']) else: if self._brew and self._cpython: _extend_version(EXEC_PATH['combination'][(True, True)]) if self._brew and self._pypy: _extend_version(EXEC_PATH['combination'][(True, False)]) if self._system and self._cpython: _extend_version(EXEC_PATH['combination'][(False, True)]) if self._system and self._pypy: _extend_version(EXEC_PATH['combination'][(False, False)]) self._exec = set(temp_exec)