103 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Python
		
	
	
			
		
		
	
	
			103 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Python
		
	
	
| # -*- coding: utf-8 -*-
 | |
| 
 | |
| from subprocess import check_output
 | |
| import pytz
 | |
| import os
 | |
| import multiprocessing
 | |
| 
 | |
| from odoo import models, fields, api, tools
 | |
| 
 | |
| 
 | |
| class ChangeLog(models.Model):
 | |
|     _name = 'dp.changelog'
 | |
|     _description = 'Changelogs'
 | |
|     _order = 'id desc'
 | |
| 
 | |
|     @api.model
 | |
|     def _get_changelog_name(self):
 | |
|         """
 | |
|         Returns the default name for the changelog table
 | |
|         :return:
 | |
|         """
 | |
|         tz_name = self.env.context.get('tz') or tools.config.get('timezone')
 | |
|         date_string = fields.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
 | |
|         date_string = fields.Datetime.to_string(
 | |
|             pytz.timezone('UTC').localize(fields.Datetime.from_string(date_string),
 | |
|                                           is_dst=None).astimezone(
 | |
|                 pytz.timezone(tz_name)))
 | |
|         return 'Changelog ' + date_string[:-3]
 | |
| 
 | |
|     name = fields.Char('Name', required=True, default=_get_changelog_name)
 | |
|     date = fields.Datetime('Timestamp', required=True, default=lambda *a: fields.datetime.now())
 | |
|     commit = fields.Char('Commit')
 | |
|     changelogs = fields.Text('Changelogs')
 | |
|     ppid = fields.Char('letzte ppid')
 | |
| 
 | |
|     @api.model
 | |
|     def cron_get_changelog(self):
 | |
|         vals = self._get_changelogs_vals()
 | |
|         changelog = False
 | |
|         if vals:
 | |
|             changelog = self.create(vals)
 | |
|         return changelog
 | |
| 
 | |
|     @api.model
 | |
|     def _get_changelogs_vals(self):
 | |
|         commit = check_output(["git", "log", "--pretty=oneline", "-1"])
 | |
|         last_commit = self.search([], limit=1)
 | |
|         com2 = commit.decode("utf-8")
 | |
|         com2 = com2.split(' ')[0]
 | |
|         new_logs = b''
 | |
|         news = False
 | |
|         vals = {}
 | |
|         if last_commit:
 | |
|             com1 = last_commit.commit
 | |
|             changelogs = check_output(["git", "log", "--pretty=oneline", com1 + "..." + com2])
 | |
|             for changelog in changelogs.splitlines():
 | |
|                 new_logs += b' '.join(changelog.split(b' ')[1:]) + b'\n'
 | |
|             if not new_logs == b'':
 | |
|                 news = True
 | |
|         else:
 | |
|             news = True
 | |
| 
 | |
|         if news:
 | |
|             vals = {
 | |
|                 'commit': com2,
 | |
|                 'changelogs': new_logs,
 | |
|                 'ppid': last_commit.ppid
 | |
|             }
 | |
|         return vals
 | |
| 
 | |
|     @api.model
 | |
|     def _get_reboot_vals(self):
 | |
|         last_commit = self.search([], limit=1)
 | |
|         workers = 1
 | |
|         vals = {}
 | |
|         if tools.config.get('workers', False):
 | |
|             workers = tools.config.get('workers', False)
 | |
|         if last_commit:
 | |
|             if workers > 1 and last_commit.ppid != str(os.getppid()):
 | |
|                 vals = {
 | |
|                     'commit': last_commit.commit,
 | |
|                     'changelogs': 'Reboot',
 | |
|                     'ppid': str(os.getppid())
 | |
|                     }
 | |
|             if last_commit.ppid != str(os.getpid()):
 | |
|                 vals = {
 | |
|                     'commit': last_commit.commit,
 | |
|                     'changelogs': 'Reboot',
 | |
|                     'ppid': str(os.getpid())
 | |
|                 }
 | |
|             return vals
 | |
| 
 | |
|     @api.model_cr
 | |
|     def _register_hook(self):
 | |
|         try:
 | |
|             self.cron_get_changelog()
 | |
|             vals = self._get_reboot_vals()
 | |
|             if vals:
 | |
|                 self.create(vals)
 | |
|         except:
 | |
|             # falls git nicht vorhanden ist wird sicher eine Fehler ausgegeben.
 | |
|             pass
 |