backend/satellite_tools/reports.py | 49 +++++++++++++++++++++++++ backend/satellite_tools/reports/data/inventory | 5 ++ backend/satellite_tools/spacewalk-report | 38 ++++++++++++++++++- 3 files changed, 91 insertions(+), 1 deletion(-)
New commits: commit 2a5dbae1c9258bdb96df78152ff66b92071fff66 Author: Jan Pazdziora jpazdziora@redhat.com Date: Fri Jul 24 09:43:35 2009 +0200
reporting: add the --multival-separator option (semicolon is the default).
diff --git a/backend/satellite_tools/spacewalk-report b/backend/satellite_tools/spacewalk-report index 7384f98..5d2f911 100755 --- a/backend/satellite_tools/spacewalk-report +++ b/backend/satellite_tools/spacewalk-report @@ -51,6 +51,10 @@ def processCommandline(): optionsTable = [ Option('--multival-on-rows', action='store_true', dest='multivalonrows', help='if there are multiple values for certain field, repeat the rows'), + Option('--multival-separator', action='store', dest='multivalseparator', + default=';', + help='if there are multiple values for certain field, separate them with this string' \ + ' (unless --multival-on-rows)'), Option('--db', action='store', type="string", help='alternative database string (username/password@sid);' \ + ' default is default_db from /etc/rhn/rhn.conf'), @@ -133,7 +137,7 @@ if __name__ == '__main__': if prevrow[m] != row[m]: if m in report.multival_columns_reverted: for v in report.multival_columns_reverted[m]: - outrow[v] = str(outrow[v]) + ';' + str(row[v]) + outrow[v] = str(outrow[v]) + options.multivalseparator + str(row[v]) go = False break else:
commit c1f2d53585dbf80d9eed8ea3ee5ef276e571456e Author: Jan Pazdziora jpazdziora@redhat.com Date: Fri Jul 24 09:40:17 2009 +0200
reporting: make merging multiple values the default, and add the --multival-on-rows option to prevent it.
diff --git a/backend/satellite_tools/reports.py b/backend/satellite_tools/reports.py index b584347..d947244 100644 --- a/backend/satellite_tools/reports.py +++ b/backend/satellite_tools/reports.py @@ -12,6 +12,10 @@ class report: full_path = os.path.join(REPORT_DEFINITIONS, name) self.sql = None self.columns = None + self.column_indexes = None + self.multival_column_names = {} + self.multival_columns_reverted = {} + self.multival_columns_stop = [] self._load(full_path)
def _load(self, full_path): @@ -42,9 +46,51 @@ class report: if tag != None: self._set(tag, value)
+ if self.multival_column_names != None: + unknown_columns = [] + + for c in self.multival_column_names: + if c in self.column_indexes: + c_id = self.column_indexes[c] + v = self.multival_column_names[c] + if v == None: + self.multival_columns_stop.append(c_id) + elif v in self.column_indexes: + v_id = self.column_indexes[v] + if v_id in self.multival_columns_reverted: + self.multival_columns_reverted[v_id].append(c_id) + else: + self.multival_columns_reverted[v_id] = [ c_id ] + else: + unknown_columns.append(c) + if len(unknown_columns) > 0: + raise spacewalk_report_unknown_multival_column_exception(unknown_columns) + + def _set(self, tag, value): if tag == 'columns': self.columns = filter(lambda x: x != '', re.split('\s+', value)) + # set mapping from column name to column position in column_indexes + self.column_indexes = {} + i = 0 + for c in self.columns: + self.column_indexes[c] = i + i = i + 1 + elif tag == 'multival_columns': + # the multival_columns specifies either + # a "stop" column, usually the first one, + # or a pair of column names separated by colon, + # where the first on is column which should be + # joined together and the second one is column + # whose value should be used to distinguish if + # we still have the same entity or not. + for l in filter(lambda x: x != '', re.split('\n', value)): + m = re.match('^\s*(\S+?)(\s*:\s*(\S*)\s*)?$', l) + if m == None: + continue + ( col, id_col ) = ( m.group(1), m.group(3) ) + if col != None: + self.multival_column_names[col] = id_col elif tag == 'sql': self.sql = value else: @@ -57,3 +103,6 @@ class spacewalk_unknown_report(Exception): class spacewalk_report_unknown_tag_exception(Exception): pass
+class spacewalk_report_unknown_multival_column_exception(Exception): + pass + diff --git a/backend/satellite_tools/reports/data/inventory b/backend/satellite_tools/reports/data/inventory index 1921ccb..1423fc8 100755 --- a/backend/satellite_tools/reports/data/inventory +++ b/backend/satellite_tools/reports/data/inventory @@ -13,6 +13,11 @@ columns: errata_out_of_date software_channel
+multival_columns: + + server_id + software_channel : software_channel + sql:
select rhnserver.id as server_id, rhnserver.name as profile_name, diff --git a/backend/satellite_tools/spacewalk-report b/backend/satellite_tools/spacewalk-report index 48c1357..7384f98 100755 --- a/backend/satellite_tools/spacewalk-report +++ b/backend/satellite_tools/spacewalk-report @@ -49,6 +49,8 @@ except KeyboardInterrupt: def processCommandline(): """ process the commandline """ optionsTable = [ + Option('--multival-on-rows', action='store_true', dest='multivalonrows', + help='if there are multiple values for certain field, repeat the rows'), Option('--db', action='store', type="string", help='alternative database string (username/password@sid);' \ + ' default is default_db from /etc/rhn/rhn.conf'), @@ -112,10 +114,40 @@ if __name__ == '__main__': "Columns in report spec and in the database do not match:\nexpected %s\n got %s" % ( report.columns, db_columns )) writer.writerow(report.columns)
+ multival_keys = sorted(report.multival_columns_stop + report.multival_columns_reverted.keys()) + row = h.fetchone() + prevrow = None + outrow = None while row != None: - writer.writerow(row) + if options.multivalonrows: + writer.writerow(row) + row = h.fetchone() + continue + + if outrow == None: + outrow = [ x for x in row ] + else: + go = True + for m in multival_keys: + if prevrow[m] != row[m]: + if m in report.multival_columns_reverted: + for v in report.multival_columns_reverted[m]: + outrow[v] = str(outrow[v]) + ';' + str(row[v]) + go = False + break + else: + break + if not go: + break + + if go: + writer.writerow(outrow) + outrow = [ x for x in row ] + prevrow = row row = h.fetchone() + if outrow != None: + writer.writerow(outrow) else: for r in reports.available_reports(): print r
spacewalk-commits@lists.fedorahosted.org