def vp2f(self, patchname, params): """ List patch and files it uses Args: patchname (string): name of patch file params (dict): parameters patchdir (string, required): path of patches folder Raises: PT_ParameterError for any missing files """ #-- if (not isinstance(params, dict)): raise PT_ParameterError(self.name, 'params') sourcedir = self._check_directory_param(params, 'sourcedir') patchdir = self._check_directory_param(params, 'patchdir') patchpath = self._check_filename_param(patchdir, patchname, 'patchname') # Get list of source filenames referenced in patch # Note that a patch may refer to files that do not exist in the source tree. filelist = Patch.list_files(patchpath) filepaths = [] for file_ in filelist: path = ut.join_path(sourcedir, file_) if (ut.is_file(path)): filepaths += [path] paths = [patchpath] + filepaths return self._view(paths)
def __init__(self, params): """ Constructor Args: params (dict): parameters sourcedir (string,required): path to source directory patchdir (string,required): path to patch directory targets (string/list, optional): target file(s) indent (int, optional): indentation default = 3 mode (string, optional): scanning mode 'full' : report edit errors only 'complete' : report status of all edits default is 'full' find (bool, optional): find missing strings default is True debug (int, optional) debug options default is 0 Raises: PT_ParameterError Notes: If '' is passed as sourcedir or patchdir, the caller must supply file paths to the match method that are accessible from the caller's current dircectory. If 'targets' is specified the code will only scan diff sections that modify the filenames in params['targets] """ #-- self.name = 'Checker' if ((params is None) or (not isinstance(params, dict))): raise PT_ParameterError(self.name, 'params') self.sourcedir = self._check_required_string_param(params, 'sourcedir') self._check_path_param('sourcedir', self.sourcedir) self.patchdir = self._check_required_string_param(params, 'patchdir') self._check_path_param('patchdir', self.patchdir) self.indent = self._check_optional_string_param(params, 'indent', ' ') self.mode = self._check_optional_string_param(params, 'mode', 'full') self.find = self._check_optional_param(params, 'find', bool, False) self.debug = self._check_optional_param(params, 'debug', int, 0) if (not self.mode in ('full', 'complete')): raise PT_ParameterError(self.name, 'mode') if ('targets' in params): targets = params['targets'] if (ut.is_string_type(targets)): self.targets = [targets] elif (isinstance(targets, list) and ut.is_string_type(targets[0])): self.targets = targets else: raise PT_ParameterError(self.name, 'targets') else: self.targets = None
def _sort_on_date(self, patches): ''' Sort patches on date field. The format is like: Fri, 28 Dec 2012 21:00:31 +0200 ''' records = [] for patch in patches: strings = ut.read_strings(ut.join_path(self.patchdir, patch)) for string in strings: if ("Date:" in string ): # e.g. 'Date: Wed, 16 Jan 2013 19:09:47 +0000' fields = ut.string_to_words(string[6:].lstrip(' \t')) _, s_day, s_mon, s_year, s_hms, _ = fields s_hour, s_minute, s_second = s_hms.split(':') day, year = int(s_day), int(s_year) hour, minute, second = int(s_hour), int(s_minute), int( s_second) month = self.month_numbers[s_mon] # ignoring microseconds dt = datetime(year, month, day, hour, minute, second) records += [(dt, patch)] break records = sorted(records, key=lambda r: r[0]) patches = [patch for (_, patch) in records] return patches
def _check_paths(self, diff): ''' Check that: (1) file named in diff spec exists (GIT format only) (2) file named in '---' line exists or is /dev/null (3) file named in '+++' line exists or is /dev/null (4) file to be created exists in new tree (5) file to be deleted does not exist in old tree ''' if (not ut.is_file(ut.join_path(self.sourcedir, diff.a_path))): self._error_msg('"a" file not found: %s' % diff.a_path, 2) return False if (diff.old_path != '/dev/null'): if (not ut.is_file(ut.join_path(self.sourcedir, diff.old_path))): self._error_msg('"old" file not found: %s' % diff.old_path, 2) return False else: if (ut.is_file(ut.join_path(self.sourcedir, diff.new_path))): self._error_msg('"new" file found in old tree: %s' % diff.new_path, 2) return False if (diff.new_path != '/dev/null'): if (not ut.is_file(ut.join_path(self.sourcedir, diff.new_path))): self._error_msg('"new" file not found: %s' % diff.new_path, 2) return False else: if (not ut.is_file(ut.join_path(self.sourcedir, diff.old_path))): self._error_msg('"old" file not found in old tree: %s' % diff.old_path, 2) return False return True
def _sort_on_date(self, patches): ''' Sort patches on date field. The format is like: Fri, 28 Dec 2012 21:00:31 +0200 ''' records = [] for patch in patches: strings = ut.read_strings(ut.join_path(self.patchdir, patch)) for string in strings: if ("Date:" in string): # e.g. 'Date: Wed, 16 Jan 2013 19:09:47 +0000' fields = ut.string_to_words(string[6:].lstrip(' \t')) _, s_day, s_mon, s_year, s_hms, _ = fields s_hour, s_minute, s_second = s_hms.split(':') day, year = int(s_day), int(s_year) hour, minute, second = int(s_hour), int(s_minute), int(s_second) month = self.month_numbers[s_mon] # ignoring microseconds dt = datetime(year, month, day, hour, minute, second) records += [(dt, patch)] break records = sorted(records, key = lambda r: r[0]) patches = [patch for (_, patch) in records] return patches
def check_config(srcroot, docroot, modules): """ Check the configuration """ errors = 0 # Do all the Python/REST source files exist? for module in modules: # Python modules have a Python source file and a .rst docs file if (isinstance(module, list)): path = os.path.join(srcroot, module[1]) if (not ut.is_file(path)): print("check_config: %s not found" % path) errors += 1 path = os.path.join(docroot, module[0]) if (not ut.is_file(path)): print("check_config: %s not found" % path) errors += 1 else: # REST modules have only an .rst file path = os.path.join(docroot, module) if (not ut.is_file(path)): print("check_config: %s not found" % path) errors += 1 return errors
def vp2f(self, patchname, params): """ List patch and files it uses Args: patchname (string): name of patch file params (dict): parameters patchdir (string, required): path of patches folder Raises: PT_ParameterError for any missing files """ #-- if (not isinstance(params, dict)): raise PT_ParameterError(self.name, 'params') sourcedir = self._check_directory_param(params, 'sourcedir') patchdir = self._check_directory_param(params, 'patchdir') patchpath = self._check_filename_param(patchdir, patchname, 'patchname') # Get list of source filenames referenced in patch # Note that a patch may refer to files that do not exist in the source tree. filelist = Patch.list_files(patchpath) filepaths = [] for file_ in filelist: path = ut.join_path(sourcedir, file_) if (ut.is_file(path)): filepaths += [path] paths = [patchpath] + filepaths return self._view(paths)
def list_files(patchpath): """ List the files referenced by a patch, without duplicates Args: patchpath (string) path to patch file Returns: list of filenames Notes: A "filename" is the portion of the file's path after the kernel root, e.g. "drivers/iio/...". """ #-- strings = ut.read_strings(patchpath) files = {} for string in strings: if (string.startswith('diff --git ')): filename = ut.get_string_filename(string) files[filename] = True filenames = sorted(files) # 2to3 return filenames
def list_files(patchpath): """ List the files referenced by a patch, without duplicates Args: patchpath (string) path to patch file Returns: list of filenames Notes: A "filename" is the portion of the file's path after the kernel root, e.g. "drivers/iio/...". """ #-- strings = ut.read_strings(patchpath) files = {} for string in strings: if (string.startswith('diff --git ')): filename = ut.get_string_filename(string) files[filename] = True filenames = sorted(files) # 2to3 return filenames
def __init__(self, path): """ Constructor Args: path (string): path to patch archive file Raises: PT_ParameterError, PT_NotFoundError Notes: A "patch archive" file lists diff sections from patches that were applied to produce the associated kernel version. Since the patch archive files can be very large, we take care to avoid copying or storing data that is not of interest to the user. """ #-- self.name = 'Archive' if (not ut.is_string_type(path)): raise PT_ParameterError(self.name, path) if (not ut.is_file(path)): raise PT_NotFoundError(self.name, path) self._path = path
def _iterate(): r = c['sourcedir'] t = ut.join_path(c['tempdir'], 'encoding.dat') w = h.read('walk.txt') for file_ in w: path = ut.join_path(r, file_) s = ut.read_strings(path) ut.write_strings(s, t)
def _iterate(): r = c['sourcedir'] t = ut.join_path(c['tempdir'], 'encoding.dat') w = h.read('walk.txt') for file_ in w: path = ut.join_path(r, file_) s = ut.read_strings(path) ut.write_strings(s, t)
def _check_filename_param(self, prefix, value, name): if (not ut.is_string_type(value)): raise PT_ParameterError(self.name, name) path = ut.join_path(prefix, value) if (not ut.is_file(path)): raise PT_NotFoundError(self.name, value) return path
def _check_filename_param(self, prefix, value, name): if (not ut.is_string_type(value)): raise PT_ParameterError(self.name, name) path = ut.join_path(prefix, value) if (not ut.is_file(path)): raise PT_NotFoundError(self.name, value) return path
def vp2a(self, patchname, archpath, params): """ Display patch and archive diff sections that use its files Args: patchname (string): name of patch file archpath (string): path to archive file params (dict): parameters patchdir (string, required): path of patches folder tempdir (string, required): path to store temporary data Raises: PT_ParameterError for any missing files Notes: Launches the editor synchronously, since a temporary file is created to hold the archive sections. """ #-- if (not isinstance(params, dict)): raise PT_ParameterError(self.name, 'params') sourcedir = self._check_directory_param(params, 'sourcedir') patchdir = self._check_directory_param(params, 'patchdir') tempdir = self._check_directory_param(params, 'tempdir') patchpath = self._check_filename_param(patchdir, patchname, 'patchname') if (not ut.is_file(archpath)): raise PT_NotFoundError(self.name, archpath) # Get list of files used by the patch filelist = Patch.list_files(patchpath) filepaths = [ut.join_path(sourcedir, file_) for file_ in filelist] # Get archive diff sections that use any of the files strings = [] for diff in self._get_diffs(archpath, filepaths): strings += diff if (len(strings) == 0): return [] # Store the diff sections in a temporary file archname = archpath[archpath.rfind('/') + 1:] tempname = archname + '.tmp' temppath = ut.join_path(tempdir, tempname) ut.write_strings(strings, temppath) self._view([patchpath, temppath], True) # Note that we requested _view to wait for subprocess exit above, # so that we do not try to delete the file while it is in use. os.remove(temppath)
def is_text_file(string): if ('defkeymap.map' in string): pass path = ut.join_path(c['sourcedir'], string) inpt = open(path, "rb") fsiz = ut.file_size(path) abuf = inpt.read(min(1024, fsiz)) inpt.close() ret = chardetect(abuf) return (isinstance(ret, dict) and ('encoding' in ret) and (ret['encoding'] is not None))
def get_patch_names(self, params=None): """ Return list of names of patches in our patch set Args: params (dict, optional): parameters excl_dirs (list, optional): directories to exclude incl_dirs (list, optional): directories to include Returns: list of patch names in the order found in patchset Notes: The "name" of a patch is the concatenation of the name of its parent folder and its filename, as shown in the patchset description. If params is None, names of all patches are returned. """ #-- if ((params is not None) and isinstance(params, dict)): excl_dirs = self._check_optional_param(params, 'excl_dirs', list, []) incl_dirs = self._check_optional_param(params, 'incl_dirs', list, []) else: excl_dirs = [] incl_dirs = [] names = [] groups = self.patchset['groups'] if (groups[0] in self.patchset): # old format? for group in groups: names += self.patchset[group] else: # new format if (len(incl_dirs) > 0): dirs = incl_dirs else: dirs = groups for dir_ in excl_dirs: if (dir_ in dirs): dirs.remove(dir_) for dir_ in dirs: dirnames = [] path = ut.join_path(self.patchdir, dir_) for (_, _, files) in os.walk(path): for file_ in files: name = ut.join_path(dir_, file_) dirnames += [name] dirnames.sort() names += dirnames return names
def vp2a(self, patchname, archpath, params): """ Display patch and archive diff sections that use its files Args: patchname (string): name of patch file archpath (string): path to archive file params (dict): parameters patchdir (string, required): path of patches folder tempdir (string, required): path to store temporary data Raises: PT_ParameterError for any missing files Notes: Launches the editor synchronously, since a temporary file is created to hold the archive sections. """ #-- if (not isinstance(params, dict)): raise PT_ParameterError(self.name, 'params') sourcedir = self._check_directory_param(params, 'sourcedir') patchdir = self._check_directory_param(params, 'patchdir') tempdir = self._check_directory_param(params, 'tempdir') patchpath = self._check_filename_param(patchdir, patchname, 'patchname') if (not ut.is_file(archpath)): raise PT_NotFoundError(self.name, archpath) # Get list of files used by the patch filelist = Patch.list_files(patchpath) filepaths = [ut.join_path(sourcedir, file_) for file_ in filelist] # Get archive diff sections that use any of the files strings = [] for diff in self._get_diffs(archpath, filepaths): strings += diff if (len(strings) == 0): return [] # Store the diff sections in a temporary file archname = archpath[archpath.rfind('/') + 1:] tempname = archname + '.tmp' temppath = ut.join_path(tempdir, tempname) ut.write_strings(strings, temppath) self._view([patchpath, temppath], True) # Note that we requested _view to wait for subprocess exit above, # so that we do not try to delete the file while it is in use. os.remove(temppath)
def is_text_file(string): if ('defkeymap.map' in string): pass path = ut.join_path(c['sourcedir'], string) inpt = open(path, "rb") fsiz = ut.file_size(path) abuf = inpt.read(min(1024, fsiz)) inpt.close() ret = chardetect(abuf) return (isinstance(ret, dict) and ('encoding' in ret) and (ret['encoding'] is not None))
def get_patch_names(self, params=None): """ Return list of names of patches in our patch set Args: params (dict, optional): parameters excl_dirs (list, optional): directories to exclude incl_dirs (list, optional): directories to include Returns: list of patch names in the order found in patchset Notes: The "name" of a patch is the concatenation of the name of its parent folder and its filename, as shown in the patchset description. If params is None, names of all patches are returned. """ #-- if ((params is not None) and isinstance(params, dict)): excl_dirs = self._check_optional_param(params, 'excl_dirs', list, []) incl_dirs = self._check_optional_param(params, 'incl_dirs', list, []) else: excl_dirs = [] incl_dirs = [] names = [] groups = self.patchset['groups'] if (groups[0] in self.patchset): # old format? for group in groups: names += self.patchset[group] else: # new format if (len(incl_dirs) > 0): dirs = incl_dirs else: dirs = groups for dir_ in excl_dirs: if (dir_ in dirs): dirs.remove(dir_) for dir_ in dirs: dirnames = [] path = ut.join_path(self.patchdir, dir_) for (_, _, files) in os.walk(path): for file_ in files: name = ut.join_path(dir_, file_) dirnames += [name] dirnames.sort() names += dirnames return names
def _enumerate(self, dir_, files): results = [] for file_ in files: if (self.test_dirs): path = ut.join_path(dir_, file_) else: path = file_ if ((not self._is_file_excluded(path)) and self._is_file_included(path)): results += [ut.join_path(dir_, file_)] return results
def _sort_patches_new(self, patches): ''' In the "new" (kernel-3.8) format, the patchset "groups" item is a list of names of folders under self.patchdir, and the groups are intended to be processed in list order, apparently with the order of files within a group being controlled by the first four characters xof the file's name. ''' # Map the patches into a dict on the folder name dir_ = {} for patchname in patches: parts = patchname.split('/', 1) folder, filename = parts[0], parts[1] if (folder in dir_): dir_[folder] += [filename] else: dir_[folder] = [filename] # Extract the patch names in patchset order sorted_ = [] for group in self.patchset['groups']: if (group in dir_): files = dir_[group] if (len(files) > 1): files.sort() items = [ut.join_path(group, name) for name in files] sorted_ += items return sorted_
def _check_directory_param(self, params, field): path = self._check_required_string_param(params, field) if (not ut.is_dir(path)): raise PT_NotFoundError(self.name, field) return path
def _format_dict(self, result): ''' If the dict will fit on one line, just print it. Otherwise, recurse on each key-value pair. ''' string = str(result) if (len(string) <= 72): return [string] keys = sorted(result) # +2to3 #keys = .keys() #keys.sort() strings = ['{'] for key in keys: val = result[key] if (isinstance(val, int)): strings += [' "%s" : %d' % (key, val)] elif (ut.is_string_type(val)): val = val.rstrip(' \t\n') keystr = ' "%s" : ' % key if ('\n' in val): strs = val.split('\n') strs = [(' ' + astr.strip()) for astr in strs] strings += [keystr] + strs else: strings += [keystr + val] elif (isinstance(val, dict)): strings += self._format_dict(val) else: strings += [' "%s" : %s' % (key, str(val))] strings += ['}'] return strings
def _parse_hunk_line(self, string): ''' Parse hunk line like '@@ -428,7 +428,7 @@ DEFINE_...'. The text after the second '@@' is a 'hunk note'. ''' self.spec = string string = ut.normalize_string(string, False) (_, old, new, tail) = string.split(' ', 3) if (tail == '@@'): # no hunk note self.note = '' else: self.note = tail.split(' ')[1] parts = old[1:].split(',') self.old_start = int(parts[0]) if (',' in old): # old has line count self.old_count = int(parts[1]) else: self.old_count = 1 parts = new[1:].split(',') self.new_start = int(parts[0]) if (',' in new): # new has line count self.new_count = int(parts[1]) else: self.new_count = 1
def load(self, path): """ Load data that is formatted as a JSON object coded in a string Args: path (string) file path """ return json.loads(ut.read_file(path))
def _sort_patches_new(self, patches): ''' In the "new" (kernel-3.8) format, the patchset "groups" item is a list of names of folders under self.patchdir, and the groups are intended to be processed in list order, apparently with the order of files within a group being controlled by the first four characters xof the file's name. ''' # Map the patches into a dict on the folder name dir_ = {} for patchname in patches: parts = patchname.split('/', 1) folder, filename = parts[0], parts[1] if (folder in dir_): dir_[folder] += [filename] else: dir_[folder] = [filename] # Extract the patch names in patchset order sorted_ = [] for group in self.patchset['groups']: if (group in dir_): files = dir_[group] if (len(files) > 1): files.sort() items = [ut.join_path(group, name) for name in files] sorted_ += items return sorted_
def _check_directory_param(self, params, field): path = self._check_required_string_param(params, field) if (not ut.is_dir(path)): raise PT_NotFoundError(self.name, field) return path
def walk(self): """ The walk starts here Args: None Returns: A list of matching file paths Raises: None """ #-- results = [] for f in self._folders: for (dir1, _, files) in os.walk(f): if (len(files) > 0): # Pythons's os.walk function will prepend the root path to dir1 dir2 = ut.trim_path(self._root_path, dir1) if (self._trim_paths): results += self._enumerate(dir2, files) else: results += self._enumerate(dir1, files) return results
def load(self, path): """ Load data that is formatted as a JSON object coded in a string Args: path (string) file path """ return json.loads(ut.read_file(path))
def _format_dict(self, result): ''' If the dict will fit on one line, just print it. Otherwise, recurse on each key-value pair. ''' string = str(result) if (len(string) <= 72): return [string] keys = sorted(result) # +2to3 #keys = .keys() #keys.sort() strings = ['{'] for key in keys: val = result[key] if (isinstance(val, int)): strings += [' "%s" : %d' % (key, val)] elif (ut.is_string_type(val)): val = val.rstrip(' \t\n') keystr = ' "%s" : ' % key if ('\n' in val): strs = val.split('\n') strs = [(' ' + astr.strip()) for astr in strs] strings += [keystr] + strs else: strings += [keystr + val] elif (isinstance(val, dict)): strings += self._format_dict(val) else: strings += [' "%s" : %s' % (key, str(val))] strings += ['}'] return strings
def _parse_hunk_line(self, string): ''' Parse hunk line like '@@ -428,7 +428,7 @@ DEFINE_...'. The text after the second '@@' is a 'hunk note'. ''' self.spec = string string = ut.normalize_string(string, False) (_, old, new, tail) = string.split(' ', 3) if (tail == '@@'): # no hunk note self.note = '' else: self.note = tail.split(' ')[1] parts = old[1:].split(',') self.old_start = int(parts[0]) if (',' in old): # old has line count self.old_count = int(parts[1]) else: self.old_count = 1 parts = new[1:].split(',') self.new_start = int(parts[0]) if (',' in new): # new has line count self.new_count = int(parts[1]) else: self.new_count = 1
def _find_line(self, text, strings): ''' Try to find text in the source strings. Both text and strings are normalized to eliminate mismatches on varying whitespace. Source strings may contain items such as author name, etc. with unicode characters that can't be converted to ascii or latin-1, so we must not convert the strings to Python 2.x str objects. ''' text = ut.normalize_string(text, True) matches = [] for index in range(len(strings)): string = ut.normalize_string(strings[index], True) if (text == string): matches += [index] return matches
def _check_file_1_arg(self, name, value): ''' Validate file argument when only one file is allowed. ''' if (ut.is_string_type(value)): return value else: raise PT_ParameterError(self.name, name)
def _check_file_1_arg(self, name, value): ''' Validate file argument when only one file is allowed. ''' if (ut.is_string_type(value)): return value else: raise PT_ParameterError(self.name, name)
def _find_line(self, text, strings): ''' Try to find text in the source strings. Both text and strings are normalized to eliminate mismatches on varying whitespace. Source strings may contain items such as author name, etc. with unicode characters that can't be converted to ascii or latin-1, so we must not convert the strings to Python 2.x str objects. ''' text = ut.normalize_string(text, True) matches = [] for index in range(len(strings)): string = ut.normalize_string(strings[index], True) if (text == string): matches += [index] return matches
def convert_api_data(config): srcroot = config['srcroot'] sources = config['sources'] data = [] for source in sources: print('extract: ' + source) path = ut.join_path(srcroot, source) strings = Strings(ut.read_strings(path)) sections = strings.extract('#++','#--') if (len(sections) > 0): sections = [conv_section(section) for section in sections] data += Strings.join(sections) else: pass ut.write_strings(data, './_rest/api.txt')
def _match_patterns(self, path): ''' Search files in self.paths for match to self.matcher. ''' filepath = ut.join_path(self.root_path, path) strings = ut.read_strings(filepath) for index in range(len(strings)): text = strings[index] if (self.debug > 1): print(' "%s"' % text) pattern = self.matcher(text) if (pattern is not None): if (self.trim_paths): path_ = path else: path_ = filepath if (pattern not in self.matches): self.matches[pattern] = [] self.matches[pattern] += [(path_, index + 1, text.lstrip())]
def _match_patterns(self, path): ''' Search files in self.paths for match to self.matcher. ''' filepath = ut.join_path(self.root_path, path) strings = ut.read_strings(filepath) for index in range(len(strings)): text = strings[index] if (self.debug > 1): print(' "%s"' % text) pattern = self.matcher(text) if (pattern is not None): if (self.trim_paths): path_ = path else: path_ = filepath if (pattern not in self.matches): self.matches[pattern] = [] self.matches[pattern] += [(path_, index + 1, text.lstrip())]
def __init__(self, mod, name, value=None): s = '%s: invalid %s parameter' % (mod, name) if (value is not None): s += ': ' if (ut.is_string_type(value)): s += value else: s+= str(value) super(PT_ParameterError, self).__init__(mod, s)
def __init__(self, mod, name, value=None): s = '%s: invalid %s parameter' % (mod, name) if (value is not None): s += ': ' if (ut.is_string_type(value)): s += value else: s += str(value) super(PT_ParameterError, self).__init__(mod, s)
def __init__(self, path): """ Constructor Args: path (string): path to patch file Raises: PT_ParameterError PT_NotFoundError Notes: Commented out diff and hunk sections are omitted. """ #-- self.name = 'Patch' if (not ut.is_string_type(path)): raise PT_ParameterError(self.name, path) if (not ut.is_file(path)): raise PT_NotFoundError(self.name, path) strings = Strings(ut.read_strings(path)) strings = strings.discard('"""', '"""') # Split any email header from the patch data (_, body) = strings.partition('diff -') if (body is None): #all diffs commented out? self.diffs = [] self.patch_type = 'text' self.patch_mode = 'git' print "body is none" elif (body[0].startswith('diff --git ')): # Split any email footer from the patch data (body, _) = body.rpartition('-- ') self._parse_body(body, 'diff --git ') self.patch_mode = 'git' print "git diff" else: self._parse_body(body, 'diff -') self.patch_mode = 'urn' print "urn"
def _check_file_N_args(self, name, value): ''' Validate file argument when N files are allowed. ''' if (ut.is_string_type(value)): return [value] elif (isinstance(value, list)): return value else: raise PT_ParameterError(self.name, name)
def _check_optional_string_param(self, params, field, default): if (field in params): param = params[field] if (ut.is_string_type(param)): return param else: raise PT_TypeError(self.name, field, 'str') else: return default
def _check_file_params(self, params): if (('file' in params) and ut.is_string_type(params['file'])): return [params['file']] elif (('files' in params) and isinstance(params['files'], list)): return params['files'] else: raise PT_ParameterError(self.name, 'filespec')
def __init__(self, name, value): s = '%s: invalid %s parameter' % name if (value is not None): s += ': ' if (ut.is_string_type(value)): s += value else: s += str(value) super(CommandParameterError, self).__init__(s)
def _check_required_string_param(self, params, field): if (field in params): param = params[field] if (ut.is_string_type(param)): return param else: raise PT_TypeError(self.name, field, 'str') else: raise PT_ParameterError(self.name, field)
def _check_file_params(self, params): if (('file' in params) and ut.is_string_type(params['file'])): return [params['file']] elif (('files' in params) and isinstance(params['files'], list)): return params['files'] else: raise PT_ParameterError(self.name, 'filespec')
def _get_diffs(self, archive, filenames): ''' Generate list of diff sections that refer to files in filenames ''' strings = ut.read_strings(archive) diff = [] for index in range(len(strings)): string = strings[index] if (string.lstrip().startswith('diff --git ')): if (len(diff) > 0): yield diff filename = ut.get_string_filename(string) if (filename in filenames): diff = ["%04d: %s" % (index + 1, string)] elif (len(diff) > 0): diff += ["%04d: %s" % (index + 1, string)] if (len(diff) > 0): yield diff
def __init__(self, name, value): s = '%s: invalid %s parameter' % name if (value is not None): s += ': ' if (ut.is_string_type(value)): s += value else: s += str(value) super(CommandParameterError, self).__init__(s)
def _check(self, patchpath): self._misc_msg('\nPATCH: "%s"' % patchpath) patchpath = ut.join_path(self.patchdir, patchpath) pdata = Patch(patchpath) if (len(pdata.diffs) == 0): self._info_msg('skipping empty/commented patch', 1) return 0 if (pdata.patch_type == 'binary'): self._info_msg('skipping binary patch', 1) return -1 errors = 0 for diff in pdata.diffs: self._misc_msg('DIFF: "%s"' % diff.spec, 1) if (not self._check_paths(diff)): errors += 1 continue if (diff.old_path == '/dev/null'): # Can't fail on adding lines to a new file continue old_lines = ut.read_strings(ut.join_path(self.sourcedir, diff.old_path)) for hunk in diff.hunks: self._misc_msg('HUNK: "%s"' % hunk.spec, 2) edits = hunk.edits start = hunk.old_start count = hunk.old_count tag = 'old' note = hunk.note if (not self._check_hunk_format(start, count, len(old_lines), tag)): errors += 1 continue errors += self._check_hunk_edits(diff.old_path, edits, start, count, note, old_lines) self._info_msg("%d patch errors" % errors, 1) return errors
def read(self, filepath): """ Read data from path as a sequence of '\\n' terminated lines. Convert the lines to a list of strings Args: filepath (string): file path Notes: This function is used to input data output by the Checker, etc. """ return ut.read_strings(filepath)