Esempio n. 1
0
 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
Esempio n. 2
0
    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
Esempio n. 3
0
 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
Esempio n. 4
0
    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
Esempio n. 5
0
 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)
Esempio n. 6
0
    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)
Esempio n. 7
0
 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)
Esempio n. 8
0
 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)
Esempio n. 9
0
 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)
Esempio n. 10
0
    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)
Esempio n. 11
0
 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')
Esempio n. 12
0
 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
Esempio n. 13
0
    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
Esempio n. 14
0
 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)
Esempio n. 15
0
    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')
Esempio n. 16
0
    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)
Esempio n. 17
0
    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)
Esempio n. 18
0
    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
Esempio n. 19
0
    def sync(self, cmd):
        """ Execute subprocess synchronously
        
        Args:
            cmd (string): shell command to execute
            cmd (list):   command arguments to pass
                
        Returns:
            A list of strings:
                ['retcode' : 0,
                 'output' : '...',
                 'errors' : '...'
                 ]
                 
        Raises:
            CommandParameterError when command is not a string type
                
        Notes:
            Shell command  is a string like "cd tmp && ls".
            Command arguments is a list like ['gedit', 'file1', file2',...]
            Output and errors strings are only returned to the caller
            when the subprocess returns output or errors.
        """
        #--
        if (ut.is_string_type(cmd)):
            shellmode = True
        elif (isinstance(cmd, (list, tuple))):
            shellmode = False
        else:
            raise CommandParameterError('cmd', cmd)

        try:
            self.proc = subprocess.Popen(cmd,
                                         shell=shellmode,
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.PIPE)
            bstdout, bstderr = self.proc.communicate()
            retcode = self.proc.returncode
            self.proc.stdout.close()
            self.proc.stderr.close()
            self.proc = None
            return self._format_result(retcode, bstdout, bstderr)
        except Exception as e:
            raise CommandError(ExceptionHandler.format_exception(e))
Esempio n. 20
0
 def sync(self, cmd):
     """ Execute subprocess synchronously
     
     Args:
         cmd (string): shell command to execute
         cmd (list):   command arguments to pass
             
     Returns:
         A list of strings:
             ['retcode' : 0,
              'output' : '...',
              'errors' : '...'
              ]
              
     Raises:
         CommandParameterError when command is not a string type
             
     Notes:
         Shell command  is a string like "cd tmp && ls".
         Command arguments is a list like ['gedit', 'file1', file2',...]
         Output and errors strings are only returned to the caller
         when the subprocess returns output or errors.
     """
     #--
     if (ut.is_string_type(cmd)):
         shellmode = True
     elif (isinstance(cmd, (list,tuple))):
         shellmode = False
     else:
         raise CommandParameterError('cmd', cmd)
     
     try:
         self.proc = subprocess.Popen(cmd,
                                      shell=shellmode,
                                      stdout=subprocess.PIPE,
                                      stderr=subprocess.PIPE)
         bstdout, bstderr = self.proc.communicate()
         retcode = self.proc.returncode
         self.proc.stdout.close()
         self.proc.stderr.close()
         self.proc = None      
         return self._format_result(retcode, bstdout, bstderr)
     except Exception as e:
         raise CommandError(ExceptionHandler.format_exception(e))
Esempio n. 21
0
    def match(self, param):
        """ Validate the contents of one or more Linux kernel patch files against a kernel

        Args:
            param (choice):
                (string): path to patch file
                (list): paths to patch files
                
        Returns:
            A list of strings describing the results of analysis

        Raises:
            PT_ParameterError
            PT_ParsingError
        """
        #--

        if (ut.is_string_type(param)):
            paths = [param]
        elif (isinstance(param, (list, tuple))):
            paths = param
        else:
            raise PT_ParameterError(self.name, param)

        self.msgs = []
        self._misc_msg('patchdir  = "%s":' % self.patchdir)
        self._misc_msg('sourcedir = "%s":' % self.sourcedir)

        passed = skipped = 0
        for path in paths:
            if (self.debug > 0):
                print("matching %s" % path)
            errors = self._check(path)
            if (errors == 0):
                passed += 1
            elif (errors < 0):
                skipped += 1

        self._misc_msg('\nSummary:')
        self._misc_msg(
            '%d passed, %d skipped, %d tested' % (passed, skipped, len(paths)),
            1)

        return self.msgs
Esempio n. 22
0
    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"
Esempio n. 23
0
 def write(self, data, filepath):
     """ Write data to path as a sequence of '\\n' terminated lines.
         
         Args:
             data (various): any variable
             filepath (string): file path
         
         Notes:
             All data items are "pretty printed", i.e. they are split into lines
             according to content.
     """
     if (isinstance(data, list)):
         ut.write_strings(data, filepath)
     elif (isinstance(data, dict)):
         ut.write_strings(self._format_dict(data))
     elif (ut.is_string_type(data)):
         ut.write_file(data + '\n', filepath)
     else:
         raise ParametersError('text')
Esempio n. 24
0
 def write(self, data, filepath):
     """ Write data to path as a sequence of '\\n' terminated lines.
         
         Args:
             data (various): any variable
             filepath (string): file path
         
         Notes:
             All data items are "pretty printed", i.e. they are split into lines
             according to content.
     """
     if (isinstance(data, list)):
         ut.write_strings(data, filepath)
     elif (isinstance(data, dict)):
         ut.write_strings(self._format_dict(data))
     elif (ut.is_string_type(data)):
         ut.write_file(data + '\n', filepath)
     else:
         raise ParametersError('text')
Esempio n. 25
0
    def match(self, param):
        """ Validate the contents of one or more Linux kernel patch files against a kernel

        Args:
            param (choice):
                (string): path to patch file
                (list): paths to patch files
                
        Returns:
            A list of strings describing the results of analysis

        Raises:
            PT_ParameterError
            PT_ParsingError
        """
        #--
        
        if (ut.is_string_type(param)):
            paths = [param]
        elif (isinstance(param, (list, tuple))):
            paths = param
        else:
            raise PT_ParameterError(self.name, param)
        
        self.msgs = []
        self._misc_msg('patchdir  = "%s":' % self.patchdir)
        self._misc_msg('sourcedir = "%s":' % self.sourcedir)
        
        passed = skipped = 0
        for path in paths:
            if (self.debug > 0):
                print("matching %s" % path)
            errors = self._check(path)
            if (errors == 0):
                passed += 1
            elif (errors < 0):
                skipped += 1
                
        self._misc_msg('\nSummary:')
        self._misc_msg('%d passed, %d skipped, %d tested' % (passed, skipped, len(paths)), 1)
    
        return self.msgs
Esempio n. 26
0
    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'
        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'
        else:
            self._parse_body(body, 'diff -')
            self.patch_mode = 'urn'
Esempio n. 27
0
 def watch(self, archpath):
     """ View files related to archive diff sections
     
     Args:
         archpath (string): path to patch archive file
         
     Returns:
         None. Output is a series of launches of the Viewer to view the files.
         
     Raises:
         PT_ParameterError
         PT_NotFoundError
     """
     #--
     
     if (not ut.is_string_type(archpath)):
         raise PT_ParameterError(self.name, 'archpath') 
     
     if (not ut.is_file(archpath)):
         raise PT_NotFoundError(self.name, archpath)
            
     tempfile = ut.join_path(self._tempdir, 'archdata.txt')
     filedata = self._patchset.get_file_data()
     filenames = [key for key in filedata]
     a = Archive(archpath)
     s = a.sections(filenames)
     print("Found %d matching sections" % len(s))
     v = Viewer()
     for section in s:
         ut.write_strings(section, tempfile)
         filename = ut.get_string_filename(section[1])
         filepath = ut.join_path(self._sourcedir, filename)
         patchfiles = []
         for (fn, _) in filedata[filename]:
             patchfiles += [ut.join_path(self._patchdir, fn)]
         r = v.view([tempfile, filepath] + patchfiles)
         print(r)