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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 """ Writing of history for the build. History explain why different
16 projects were built at some time. """
23 class Buildhistory(object):
25 """ Build history checks what has been built and
26 creates a file using this information """
29 self.logger = logging.getLogger(__name__)
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)
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')
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')
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)
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')
65 """ Example of output content
67 "2018-10-11 08:39:16.918914": {
70 "ansible-fm-c46.gde71b7e-1.el7.centos.noarch.rpm"
72 "commit": "de71b7e7fc0410df3d74cf209f5216b24157988a",
74 "ansible-fm-c46.gde71b7e-1.el7.centos.src.rpm"
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
89 if projects[project].project_changed and hasattr(projects[project], 'vcs') and projects[project].vcs.commitsha:
90 commitsha = projects[project].vcs.commitsha
92 # List new rpm files from a project
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}}
101 projectchange[project].update({'commit': commitsha})
102 historydict[builddate].update(projectchange)