Exemple #1
0
    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)
Exemple #2
0
    def _call_softwareupdate(clazz, args, verbose):
        check.check_string_seq(args)
        check.check_bool(verbose)

        command_line.check_args_type(args)
        args = object_util.listify(args)

        exe = which.which('softwareupdate')
        if not exe:
            raise softwareupdater_error('softwareupdate not found')

        clazz._log.log_d('_call_softwareupdate: exe={} args={}'.format(
            exe, args))

        cmd = [exe] + args
        env = os_env.clone_current_env()
        rv = execute.execute(cmd,
                             env=env,
                             stderr_to_stdout=True,
                             raise_error=False,
                             non_blocking=verbose)
        if rv.exit_code != 0:
            cmd_flat = ' '.join(cmd)
            msg = 'softwareupdate command failed: {} - {}\n{}'.format(
                cmd, rv.exit_code, rv.stdout)
            raise softwareupdater_error(msg, status_code=rv.exit_code)
        return rv
Exemple #3
0
 def check_value_types(self):
     'Check the type of each option.'
     super(_test_cli_options_subclass, self).check_value_types()
     check.check_bool(self.sub_can_do)
     check.check_string(self.sub_fruit)
     check.check_string(self.sub_username, allow_none=True)
     check.check_string(self.sub_password, allow_none=True)
    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)
Exemple #5
0
    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
Exemple #6
0
    def replace_all(clazz,
                    text,
                    src_string,
                    dst_string,
                    word_boundary=False,
                    word_boundary_chars=None):
        'Replace src_string with dst_string optionally respecting word boundaries.'
        check.check_string(text)
        check.check_string(src_string)
        check.check_string(dst_string)
        check.check_bool(word_boundary)
        check.check_set(word_boundary_chars, allow_none=True)

        spans = clazz.find_all(text,
                               src_string,
                               word_boundary=word_boundary,
                               word_boundary_chars=word_boundary_chars)
        if not spans:
            return text
        last_start = 0
        buf = StringIO()
        last_span = None
        for span in spans:
            left = text[last_start:span.start]
            if left:
                buf.write(left)
            buf.write(dst_string)
            last_start = span.end + 1
            last_span = span
        if last_span:
            right = text[last_span.end + 1:]
            buf.write(right)
        return buf.getvalue()
Exemple #7
0
 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
Exemple #8
0
 def __init__(self, *args, **kargs):
   self.verbose = False
   self.blurber = blurber()
   for key, value in kargs.items():
     setattr(self, key, value)
   check.check_bool(self.verbose)
   check.check_blurber(self.blurber)
Exemple #9
0
    def reindent_files(clazz, files, indent, backup):
        check.check_int(indent)
        check.check_bool(backup)

        clazz._log.log_method_d()

        python_files = clazz.resolve_python_files(files)
        for filename in python_files:
            refactor_reindent.reindent_file(filename, indent, backup)
Exemple #10
0
 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 get_string(clazz, filename, key, value_maker, fallback=False):
        check.check_string(filename)
        check.check_string(key)
        check.check_callable(value_maker)
        check.check_bool(fallback)

        value = clazz.get_bytes(filename, key, value_maker, fallback=fallback)
        if value == None:
            return None
        return value.decode('utf-8')
    def get_bool(clazz, filename, key, value_maker, fallback=False):
        check.check_string(filename)
        check.check_string(key)
        check.check_callable(value_maker)
        check.check_bool(fallback)

        value = clazz.get_string(filename, key, value_maker, fallback=fallback)
        if value == None:
            return None
        return bool_util.parse_bool(value)
Exemple #14
0
    def reindent_file(clazz, filename, indent, backup):
        check.check_string(filename)
        check.check_int(indent)
        check.check_bool(backup)
        file_check.check_file(filename)

        clazz._log.log_method_d()

        backup_args = [] if backup else ['--nobackup']
        args = ['--indent', str(indent)] + backup_args + [filename]
        reindent_main(args)
Exemple #15
0
  def copy(clazz, files, src_pattern, dst_pattern, copy_dirs, options = None):
    check.check_string(src_pattern)
    check.check_string(dst_pattern)
    check.check_refactor_options(options, allow_none = True)
    check.check_bool(copy_dirs)

    clazz._log.log_method_d()

    copied_items = refactor_files.copy_files(files, src_pattern, dst_pattern, copy_dirs, options = options)
    copied_files = sorted([ item.dst for item in copied_items ])
    clazz.replace_text(copied_files, src_pattern, dst_pattern, options = options)
Exemple #16
0
 def run_script(self, script_name, args, print_only):
   check.check_string(script_name)
   check.check_string_seq(args, allow_none = True)
   check.check_bool(print_only)
   
   if print_only:
     tmp = brew_installer.download_script(script_name)
     file_util.page(tmp)
     return 0
   brew_installer.run_script(script_name, args, self.options)
   return 0
Exemple #17
0
 def files(self, package_name, print_inode):
   check.check_string(package_name)
   check.check_bool(print_inode)
   
   files = self._brew.files(package_name)
   for f in files:
     if print_inode:
       inode = '{} '.format(file_util.inode_number(f))
     else:
       inode = ''
     print('{}{}'.format(inode, f))
   return 0
Exemple #18
0
    def install(self, label, verbose):
        'Install an item by label.'
        check.check_string(label)
        check.check_bool(verbose)

        args = [
            '--verbose',
            '--install',
            #'--agree-to-license', # big sur only
            '"{}"'.format(label),
        ]
        self._call_softwareupdate(args, verbose)
Exemple #19
0
    def call_handle(clazz, args, raise_error=True):
        command_line.check_args_type(args)
        check.check_bool(raise_error)

        if isinstance(args, (list, tuple)):
            parsed_args = list(args)
        else:
            parsed_args = command_line.parse_args(args)

        handle_exe = clazz._find_handle_exe()

        cmd = [handle_exe, '-nobanner'] + args
        return execute.execute(cmd, raise_error=raise_error)
Exemple #20
0
    def save_file(clazz,
                  filename,
                  o,
                  indent=None,
                  sort_keys=False,
                  codec=None):
        check.check_string(filename)
        check.check_int(indent, allow_none=True)
        check.check_bool(sort_keys)
        check.check_string(codec, allow_none=True)

        content = clazz.to_json(o, indent=indent, sort_keys=sort_keys)
        codec = codec or 'utf-8'
        with open(filename, 'w', encoding=codec) as f:
            f.write(content)
Exemple #21
0
    def copy_files(clazz,
                   files,
                   src_pattern,
                   dst_pattern,
                   copy_dirs,
                   options=None):
        check.check_string(src_pattern)
        check.check_string(dst_pattern)
        check.check_refactor_options(options, allow_none=True)
        check.check_bool(copy_dirs)

        clazz._log.log_method_d()
        options = options or refactor_options()
        return clazz._do_operation(refactor_operation_type.COPY_FILES, files,
                                   src_pattern, dst_pattern, copy_dirs,
                                   options)
Exemple #22
0
    def to_json(clazz, o, indent=None, sort_keys=False):
        check.check_int(indent, allow_none=True)
        check.check_bool(sort_keys)
        '''
    Like json.dumps plus the following:
     - same white space results on both python 2 and 3
     - __dict__ is used when object is not json encodable
    '''
        def default(o):
            return o.__dict__

        return json.dumps(o,
                          indent=indent,
                          default=default,
                          sort_keys=sort_keys,
                          separators=(', ', ': '))
Exemple #23
0
 def __init__(self, *args, **kargs):
     self.verbose = False
     self.password = None
     self.blurber = blurber()
     self.working_dir = tempfile.gettempdir()
     self.prompt = 'sudo password: '
     self.force_auth = False
     self.error_message = None
     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)
     check.check_string(self.working_dir, allow_none=True)
     check.check_string(self.prompt, allow_none=True)
     check.check_bool(self.force_auth)
     check.check_string(self.error_message, allow_none=True)
Exemple #24
0
    def call_pkgutil(clazz, args, msg=None, use_sudo=False):
        check.check_string_seq(args)
        check.check_string(msg, allow_none=True)
        check.check_bool(use_sudo)

        env = os_env.clone_current_env(d={})
        rv = pkgutil_command.call_command(args,
                                          raise_error=False,
                                          env=env,
                                          use_sudo=use_sudo)
        if rv.exit_code != 0:
            if not msg:
                cmd_flat = ' '.join(args)
                msg = 'pkgutil command failed: {}\n{}'.format(
                    cmd_flat, rv.stdout)
            raise pkgutil_error(msg)
        return rv
Exemple #25
0
    def find_all(clazz,
                 text,
                 sub_string,
                 word_boundary=False,
                 word_boundary_chars=None):
        'Returns a list of of all the spans containing sub_string in text'
        check.check_string(text)
        check.check_string(sub_string)
        check.check_bool(word_boundary)
        check.check_set(word_boundary_chars, allow_none=True)

        return [
            span for span in clazz.find_all_generator(
                text,
                sub_string,
                word_boundary=word_boundary,
                word_boundary_chars=word_boundary_chars)
        ]
Exemple #26
0
    def replace(clazz,
                s,
                replacements,
                word_boundary=False,
                word_boundary_chars=None):
        'Replace all instances of dict d in string s.'
        check.check_string(s)
        check.check_dict(replacements, check.STRING_TYPES, check.STRING_TYPES)
        check.check_bool(word_boundary)
        check.check_set(word_boundary_chars, allow_none=True)

        for src_string, dst_string in replacements.items():
            s = clazz.replace_all(s,
                                  src_string,
                                  dst_string,
                                  word_boundary=word_boundary,
                                  word_boundary_chars=word_boundary_chars)
        return s
Exemple #27
0
    def search(clazz,
               root_dir,
               text,
               relative=True,
               min_depth=None,
               max_depth=None):
        check.check_string(root_dir)
        check.check_string(text)
        check.check_bool(relative)
        check.check_int(min_depth, allow_none=True)
        check.check_int(max_depth, allow_none=True)

        files = file_find.find(root_dir,
                               relative=relative,
                               min_depth=min_depth,
                               max_depth=max_depth)
        items = []
        for f in files:
            fpath = path.join(root_dir, f)
            next_items = clazz.search_file(fpath, text)
            items += next_items
        if relative:
            return [item.become_relative(root_dir) for item in items]
        return items
Exemple #28
0
 def _command_bar(self, branch, verbose):
   check.check_string(branch)
   check.check_bool(verbose)
   print('bar:%s:%s' % (branch, int(verbose)))
   return 0
Exemple #29
0
 def randomize(self, randomize):
     check.check_bool(randomize)
     self._randomize = randomize
Exemple #30
0
    def test_check_bool(self):
        C.check_bool(True)

        with self.assertRaises(TypeError) as context:
            C.check_bool(6)