fix building of forked upstream packages
[ta/rpmbuilder.git] / rpmbuilder / buildhistory.py
1 # Copyright 2019 Nokia
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """ Writing of history for the build. History explain why different
16 projects were built at some time. """
17
18 import logging
19 import datetime
20 import json
21 import os
22
23 class Buildhistory(object):
24
25     """ Build history checks what has been built and
26     creates a file using this information """
27
28     def __init__(self):
29         self.logger = logging.getLogger(__name__)
30
31     def update_history(self, outfile, built_projects, projects):
32         """ Request history and push it to be written into file """
33         history = self.__gather_history(built_projects, projects)
34         self.__write_history_txt(outfile + '.log', history)
35         self.__write_history_json(outfile + '.json', history)
36
37     def __write_history_txt(self, outfile, history):
38         """ Write history to a file """
39         self.logger.info("Writing build history to %s", outfile)
40         with open(outfile, 'a') as fpoint:
41             for change in history:
42                 fpoint.write(change + '\n')
43                 for project in history[change]:
44                     fpoint.write('  ' + project)
45                     if 'commit' in history[change][project]:
46                         fpoint.write('  ' + history[change][project]['commit'] + '\n')
47                     else:
48                         fpoint.write('\n')
49                     for rpmfile in history[change][project]['rpmfiles']:
50                         fpoint.write('    ' + rpmfile + '\n')
51                     for rpmfile in history[change][project]['srpmfiles']:
52                         fpoint.write('    ' + rpmfile + '\n')
53
54     def __write_history_json(self, outfile, history):
55         """ Write dict history to a file as json """
56         self.logger.info("Writing build history to %s", outfile)
57         jsondata = {}
58         if os.path.isfile(outfile):
59             with open(outfile, 'r') as fpoint:
60                 jsondata = json.load(fpoint)[0]
61         jsondata.update(history)
62         with open(outfile, 'w') as fpoint:
63             fpoint.write(json.dumps([jsondata], indent=2, sort_keys=True) + '\n')
64
65         """ Example of output content
66 {
67   "2018-10-11 08:39:16.918914": {
68     "ansible-fm": {
69       "rpmfiles": [
70         "ansible-fm-c46.gde71b7e-1.el7.centos.noarch.rpm"
71       ],
72       "commit": "de71b7e7fc0410df3d74cf209f5216b24157988a",
73       "srpmfiles": [
74         "ansible-fm-c46.gde71b7e-1.el7.centos.src.rpm"
75       ]
76     }
77   }
78 }
79         """
80
81     @staticmethod
82     def __gather_history(built_projects, projects):
83         """ Loop projects and check what are the versions. This is then history """
84         builddate = str(datetime.datetime.now())
85         historydict = {builddate: {}} # dict for all projects
86         for project in built_projects:
87             # Store commit hash version
88             commitsha = None
89             if projects[project].project_changed and hasattr(projects[project], 'vcs') and projects[project].vcs.commitsha:
90                 commitsha = projects[project].vcs.commitsha
91
92             # List new rpm files from a project
93             rpmfiles = []
94             srpmfiles = []
95             for buildroot in projects[project].builders.roots:
96                 (rpmlist, srpmlist) = projects[project].list_buildproducts_for_mockroot(buildroot)
97                 rpmfiles.extend(rpmlist)
98                 srpmfiles.extend(srpmlist)
99             projectchange = {project: {'rpmfiles': rpmfiles, 'srpmfiles': srpmfiles}}
100             if commitsha:
101                 projectchange[project].update({'commit': commitsha})
102             historydict[builddate].update(projectchange)
103         return historydict