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_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_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_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) 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_check(): argv = [path, '-m', 'pip', 'check'] 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 = list() try: # pip check exits with a non-zero status if any packages are missing dependencies proc = subprocess.run(argv, stdout=subprocess.PIPE, stderr=make_stderr(self._vflag)) except subprocess.SubprocessError: print_text(traceback.format_exc(), self._file, redirect=self._vflag) else: context = proc.stdout.decode() print_text(context, self._file, redirect=self._vflag) for line in filter(None, context.strip().splitlines()): if line == 'No broken requirements found.': return set() if 'which is not installed' in line: _deps_pkgs.append(line.split()[3][:-1]) else: _deps_pkgs.append(line.split()[4][:-1]) finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n') return set(_deps_pkgs)
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_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 _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 _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_check(): argv = [path, 'missing', f'--hide={",".join(self._ignore)!r}'] 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 = list() try: # brew missing exits with a non-zero status if any formulae are missing dependencies proc = subprocess.run(argv, stdout=subprocess.PIPE, stderr=make_stderr(self._vflag)) except subprocess.SubprocessError: print_text(traceback.format_exc(), self._file, redirect=self._vflag) else: context = proc.stdout.decode() print_text(context, self._file, redirect=self._vflag) for line in filter(None, context.strip().splitlines()): _deps_pkgs.extend(line.split()[1:]) finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n') return set(_deps_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_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_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 _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_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 _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_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 _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)
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 _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): 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 _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 _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_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 _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 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 _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