def _add_line_value_to(self, category, value, key=None): """ Change a key-value line, to make sure we have the right spacing. Note: since we don't have a key <-> category matching, we need to redo one. (Eg: Provides and Obsoletes are in the same category) """ keylen = len('BuildRequires: ') if key: pass elif category in self.category_to_key: key = self.category_to_key[category] else: raise RpmException('Unhandled category in preamble: %s' % category) # append : only if the thing is not known macro if not key.startswith('%'): key += ':' # if the key is already longer then just add one space if len(key) >= keylen: key += ' ' # fillup rest of the alignment if key is shorter than muster while len(key) < keylen: key += ' ' if category in self.categories_with_package_tokens: values = self._fix_list_of_packages(value) else: values = [value] for value in values: line = key + value self._add_line_to(category, line)
def _sort_helper_key(self, a): t = type(a) if t == str: key = a elif t == list: key = a[-1] else: raise RpmException('Unknown type during sort: %s' % t) # Special case is the category grouping where we have to get the number in # after the value if self.reg.re_patch.match(key): match = self.reg.re_patch.match(key) key = int(match.group(2)) elif self.reg.re_source.match(key): match = self.reg.re_source.match(key) value = match.group(1) if value == '': value = '0' key = int(value) # Put pkgconfig()-style packages at the end of the list, after all # non-pkgconfig()-style packages elif key.find('pkgconfig(') != -1: key = '1' + key else: key = '0' + key return key
def run(self): # We always start with Copyright self.current_section = RpmCopyright(self.specfile) # FIXME: we need to store the content localy and then reorder # to maintain the specs all the same (eg somebody put # filelist to the top). for line in self.fin: # Stop at the end of the file if len(line) == 0: break # Remove \n to make it easier to parse things line = line.rstrip('\n') line = line.rstrip('\r') new_class = self._detect_new_section(line) # Following line is debug output with class info # USE: 'spec-cleaner file > /dev/null' to see the stderr output #sys.stderr.write("class: '{0}' line: '{1}'\n".format(new_class, line)) if new_class: # We don't want to print newlines before %else and %endif if new_class == Section and self.reg.re_else.match( line) or self.reg.re_endif.match(line): newline = False else: newline = True self.current_section.output(self.fout, newline) # we need to sent pkgconfig option to preamble and package if new_class == RpmPreamble or new_class == RpmPackage: self.current_section = new_class(self.specfile, self.pkgconfig) else: self.current_section = new_class(self.specfile) # skip empty line adding if we are switching sections if self._previous_line == '' and line == '': continue # Do not store data from clean and skip out here if isinstance(self.current_section, RpmClean): continue self.current_section.add(line) self._previous_line = line if line != '' or not line.startswith('#'): self._previous_nonempty_line = line self.current_section.output(self.fout) self.fout.flush() if self.diff: cmd = shlex.split(self.diff_prog + " " + self.specfile.replace(" ", "\\ ") + " " + self.fout.name.replace(" ", "\\ ")) try: subprocess.call(cmd, shell=False) except OSError as e: raise RpmException('Could not execute %s (%s)' % (self.diff_prog.split()[0], e.strerror))
def open(self, FILE, mode): """ Function to open regular files with exception handling. """ try: f = open(FILE, mode) except IOError as error: raise RpmException(error) self.f = f
def _add_group(self, group): """ Actually store the lines from groups to resulting output """ t = type(group) if t == str: return [group] elif t == list: x = [] for subgroup in group: x += self._add_group(subgroup) return x else: raise RpmException('Unknown type of group in preamble: %s' % t)
def open_datafile(self, FILE): """ Function to open data files. Used all around so kept glob here for importing. """ try: f = open('{0}/../data/{1}'.format(os.path.dirname(os.path.realpath(__file__)), FILE), 'r') except IOError: # the .. is appended as we are in spec_cleaner sub_folder try: f = open('/usr/share/spec-cleaner/{0}'.format(FILE), 'r') except IOError as e: raise RpmException(e) self.f = f
def run(self): # We always start with Copyright self.current_section = RpmCopyright(self.specfile) # FIXME: we need to store the content localy and then reorder # to maintain the specs all the same (eg somebody put # filelist to the top). for line in self.fin: # Stop at the end of the file if len(line) == 0: break # Remove \n to make it easier to parse things line = line.rstrip('\n') line = line.rstrip('\r') new_class = self._detect_new_section(line) if new_class: self.current_section.output(self.fout) # we need to sent pkgconfig option to preamble and package if new_class == RpmPreamble or new_class == RpmPackage: self.current_section = new_class(self.specfile, self.pkgconfig) else: self.current_section = new_class(self.specfile) self.current_section.add(line) self._previous_line = line if line != '' or not line.startswith('#'): self._previous_nonempty_line = line self.current_section.output(self.fout) self.fout.flush() if self.diff: cmd = shlex.split(self.diff_prog + " " + self.specfile.replace(" ", "\\ ") + " " + self.fout.name.replace(" ", "\\ ")) try: subprocess.call(cmd, shell=False) except OSError as e: raise RpmException('Could not execute %s (%s)' % (self.diff_prog.split()[0], e.strerror))