def _command_foo(self, address, version, debug, verbose): check.check_string(address) check.check_string(version) check.check_bool(debug) check.check_bool(verbose) print('foo:%s:%s:%s:%s' % (address, version, int(debug), int(verbose))) return 0
def split(clazz, src_dir, dst_dir, options = None): src_dir_abs = file_check.check_dir(src_dir) check.check_string(dst_dir) dst_dir_abs = path.abspath(dst_dir) check.check_dir_split_options(options, allow_none = True) options = options or dir_split_options() info = clazz._split_info(src_dir_abs, dst_dir_abs, options) num_items = len(info.items) if num_items == 0: return if options.threshold and num_items < options.threshold: return info.items.move_files(options.dup_file_timestamp, options.dup_file_count) for d in info.existing_split_dirs: if dir_util.is_empty(d): dir_util.remove(d) for next_possible_empty_root in info.possible_empty_dirs_roots: file_find.remove_empty_dirs(next_possible_empty_root) if path.exists(dst_dir_abs): file_find.remove_empty_dirs(dst_dir_abs)
def split_file(clazz, filename, chunk_size, zfill_length=None, output_directory=None): check.check_string(filename) check.check_int(chunk_size) check.check_int(zfill_length, allow_none=True) file_size = file_util.size(filename) clazz._log.log_method_d() num_total = int(math.ceil(float(file_size) / float(chunk_size))) result_file_list = [] zfill_length = zfill_length or len(str(num_total)) output_directory = output_directory or path.dirname(filename) with open(filename, 'rb') as fin: index = 0 while True: data = fin.read(chunk_size) if not data: break next_filename = clazz._make_split_filename( filename, output_directory, index + 1, zfill_length) with open(next_filename, 'wb') as fout: fout.write(data) result_file_list.append(next_filename) index += 1 return result_file_list
def get_value(clazz, filename, key, fallback=False, cached=False): check.check_string(filename) check.check_string(key) check.check_bool(fallback) check.check_bool(cached) if not key in clazz._getters: raise KeyError(f'No getter registered for: \"{key}\"') getter_item = clazz._getters[key] if cached: cache_key = clazz._make_cache_key(filename) if not cache_key in getter_item.cache: value = clazz.get_value(filename, key, fallback=fallback, cached=False) getter_item.cache[cache_key] = value return getter_item.cache[cache_key] def _value_maker(f): return getter_item.getter.get_value(clazz, f) value = clazz.get_bytes(filename, key, _value_maker, fallback=fallback) if value == None: return None return getter_item.getter.decode_value(value)
def keys(clazz, filename): 'Return all the keys set for filename.' check.check_string(filename) clazz.check_file_is_readable(filename) values = clazz._read_values(filename) return sorted([key for key in values.keys()])
def needs_update(self, package_name): 'Return a dictionary of outdated packages' check.check_string(package_name) if not package_name in self.installed(): return self._needs_update_result(True, None) self.update() cmd = [ 'outdated', '--json', package_name, ] rv = brew_command.call_command(cmd, raise_error=False) if rv.exit_code == 0: return self._needs_update_result(False, None) if rv.stderr: raise brew_error( 'failed to determine if "{}" needs update - {}'.format( package_name, rv.stderr)) outdated = json.loads(rv.stdout) assert 'formulae' in outdated formulae = outdated['formulae'] assert len(formulae) == 1 item = formulae[0] info = self._outdated_package(item['name'], item['installed_versions'], item['current_version']) return self._needs_update_result(True, info)
def keys(clazz, filename): 'Return all the keys set for filename.' check.check_string(filename) clazz.check_file_is_readable(filename) raw_keys = [key for key in xattr.xattr(filename).iterkeys()] return sorted([clazz._decode_key(key) for key in raw_keys])
def run_script(clazz, script_name, args, options): 'Download and run a brew script with optional args.' check.check_brew_installer_options(options) check.check_string(script_name) check.check_string_seq(args, allow_none=True) if not shell.has_shell('/bin/bash'): raise brew_error('/bin/bash is needed to run brew scripts.') args = args or [] tmp_script = clazz.download_script(script_name) sudo_options = sudo_cli_options() sudo_options.error_message = clazz._SUDO_ERROR_MESSAGE sudo_options.prompt = clazz._SUDO_PROMPT sudo_options.password = options.password clazz._log.log_d( 'run_script: calling sudo if needed: options={}'.format(options)) sudo.authenticate_if_needed(options=sudo_options) cmd = ['/bin/bash', tmp_script] + args clazz._log.log_d('run_script: script_name={} cmd={}'.format( script_name, cmd)) env = os_env.make_clean_env() env.update({ 'CI': '1', }) execute.execute(cmd, shell=False, env=env, non_blocking=options.verbose)
def get(self, class_name): check.check_string(class_name) c = self._registry.get(class_name, None) if c: return c return self._shortcuts.get(class_name, None)
def replace(clazz, filename, replacements, backup=True, word_boundary=False, word_boundary_chars=None): check.check_string(filename) check.check_dict(replacements, check.STRING_TYPES, check.STRING_TYPES) check.check_bool(backup) check.check_bool(word_boundary) check.check_set(word_boundary_chars, allow_none=True) content = file_util.read(filename, codec='utf-8') new_content = text_replace.replace( content, replacements, word_boundary=word_boundary, word_boundary_chars=word_boundary_chars) if content == new_content: return False if backup: file_util.backup(filename) file_util.save(filename, content=new_content.encode('utf-8'), mode=file_util.mode(filename)) return True
def hash_string_unsigned(clazz, s, num_digits=None): check.check_string(s) h = int(hashlib.sha1(s.encode('utf-8')).hexdigest(), 16) if num_digits == None: return h return h % (10**num_digits)
def rename_dirs(clazz, dirs, src_pattern, dst_pattern, options=None): check.check_string(src_pattern) check.check_string(dst_pattern) check.check_refactor_options(options, allow_none=True) options = options or refactor_options() clazz._log.log_method_d() resolved_empty_dirs = file_resolver.resolve_empty_dirs(dirs, recursive=True) # we need to figure out if there any empty directories that match the pattern # so we can manually rename them, since the _do_operation function only deal # with files. empty_dirs_operation_items, empty_dirs_affected_dirs = \ clazz._make_operation_items(refactor_operation_type.RENAME_DIRS, resolved_empty_dirs, src_pattern, dst_pattern, False, options.word_boundary, options.word_boundary_chars) result = clazz._do_operation(refactor_operation_type.RENAME_DIRS, dirs, src_pattern, dst_pattern, False, options) for item in empty_dirs_operation_items: file_util.mkdir(item.dst) assert dir_util.is_empty(item.src) dir_util.remove(item.src) for d in empty_dirs_affected_dirs: if path.exists(d) and dir_util.is_empty(d): dir_util.remove(d) return result
def find_all_generator(clazz, text, sub_string, word_boundary=False, word_boundary_chars=None): check.check_string(text) check.check_string(sub_string) check.check_bool(word_boundary) check.check_set(word_boundary_chars, allow_none=True) word_boundary_chars = word_boundary_chars or word_boundary_module.CHARS sub_string_length = len(sub_string) i = 0 while True: i = text.find(sub_string, i) if i < 0: return start = i end = i + sub_string_length - 1 i += sub_string_length if word_boundary: assert word_boundary_chars do_yield = word_boundary_module.word_has_boundary( text, start, end, boundary_chars=word_boundary_chars) else: do_yield = True if do_yield: yield clazz._span(start, end)
def __new__(clazz, filename): filename = path.abspath(filename) check.check_string(filename) file_check.check_file(filename) content = file_util.read(filename, codec='utf-8') root_dir = path.normpath(path.join(path.dirname(filename), '..')) data = config_data.parse(content, filename=filename) return clazz.__bases__[0].__new__(clazz, root_dir, filename, data)
def has_key(clazz, filename, key): 'Return all the keys set for filename.' check.check_string(filename) check.check_string(key) args = ['-p', key, shlex.quote(filename)] rv = clazz._call_xattr_exe(args) return rv.exit_code == 0
def remove(clazz, filename, key): 'Remove the attirbute with key from filename.' filename = file_check.check_file(filename) check.check_string(key) args = [ '-r', key, filename ] rv = clazz._call_linux_attr(args) linux_attr_command.check_result(rv, message = 'Failed to delete key "{}" for "{}"'.format(key, filename))
def make(self, class_name): check.check_string(class_name) object_class = self.get(class_name) if not object_class: raise KeyError(f'Unknown class: {class_name}') return object_class()
def replace_values(self, what, filename, values): check.check_string(what) check.check_string(filename) check.check_key_value_list(values) table_name = self._table_name(what, filename) self._ensure_hash_to_filename(filename) self._sqlite_write('replace_values', self._replace_values_i, table_name, values)
def __new__(clazz, config_env, filename, inspection): if filename is not None: check.check_string(filename) if not path.isfile(filename): raise IOError('File not found: %s' % (filename)) filename = path.abspath(filename) config = config_env.config_for_filename(filename) return clazz.__bases__[0].__new__(clazz, filename, config, inspection)
def download_script(clazz, script_name): 'Download a brew script to a temp file.' check.check_string(script_name) url = clazz._make_script_url(script_name) tmp = url_util.download_to_temp_file(url, suffix='-{}'.format(script_name)) return tmp
def parse_text(clazz, text): check.check_string(text) values = cpu_info_list() chunks = text.strip().split('\n\n') for chunk in chunks: value = cpu_info.parse_text(chunk) values.append(value) return values
def get_values(self, what, filename): check.check_string(what) check.check_string(filename) table_name = self._table_name(what, filename) if not self._db.has_table(table_name): return key_value_list() sql = 'select key, value from {table_name} order by key asc'.format( table_name=table_name) return key_value_list(self._db.select_all(sql))
def keys(clazz, filename): 'Return all the keys set for filename.' check.check_string(filename) args = [ '-q', '-l', filename ] rv = clazz._call_linux_attr(args) linux_attr_command.check_result(rv, message = 'Failed to get keys for {}'.format(filename)) keys = rv.stdout_lines() return sorted(keys)
def check_value_types(self): 'Check the type of each option.' check.check_bool(self.verbose) check.check_bool(self.debug) check.check_string(self.username, allow_none=True) check.check_string(self.password, allow_none=True) check.check_int(self.port, allow_none=True) check.check_int(self.num_tries) check.check_float(self.sleep_time)
def __init__(self, *args, **kargs): self.verbose = False self.password = None self.blurber = blurber() for key, value in kargs.items(): setattr(self, key, value) check.check_bool(self.verbose) check.check_string(self.password, allow_none=True) check.check_blurber(self.blurber)
def split_items(clazz, src_dir, dst_dir, options = None): 'Return a list of split items that when renaming each item implements split.' src_dir_abs = file_check.check_dir(src_dir) check.check_string(dst_dir) dst_dir_abs = path.abspath(dst_dir) check.check_dir_split_options(options, allow_none = True) info = clazz._split_info(src_dir_abs, dst_dir_abs, options) return info.items
def read_file(clazz, filename, codec=None): check.check_string(filename) check.check_string(codec, allow_none=True) codec = codec or 'utf-8' with open(filename, 'r', encoding=codec) as f: content = f.read() return json.loads(content) return None
def rename(clazz, files, src_pattern, dst_pattern, options = None): check.check_string(src_pattern) check.check_string(dst_pattern) check.check_refactor_options(options, allow_none = True) clazz._log.log_method_d() refactor_files.rename_files(files, src_pattern, dst_pattern, options = options) clazz.replace_text(files, src_pattern, dst_pattern, options = options)
def _make_ads_filename(clazz, filename, stream_name): 'Make an ADS filename from a regular filename.' check.check_string(filename) check.check_string(stream_name) basename = path.basename(filename) clazz.check_stream_name(basename) clazz.check_stream_name(stream_name) return '{}:{}'.format(filename, stream_name)
def rename_files(clazz, files, src_pattern, dst_pattern, options=None): check.check_string(src_pattern) check.check_string(dst_pattern) check.check_refactor_options(options, allow_none=True) clazz._log.log_method_d() options = options or refactor_options() return clazz._do_operation(refactor_operation_type.RENAME_FILES, files, src_pattern, dst_pattern, False, options)