sync - Added logging of repo sync state and config options.

Change-Id: I9186f34e0cf9c7498149e666d7a035fe75c0f5c4
This commit is contained in:
Raman Tenneti 2021-07-19 16:40:58 -07:00
parent 0a1f533e28
commit f95db4d79e
3 changed files with 102 additions and 7 deletions

View File

@ -13,6 +13,7 @@
# limitations under the License. # limitations under the License.
import contextlib import contextlib
from datetime import datetime
import errno import errno
from http.client import HTTPException from http.client import HTTPException
import json import json
@ -262,6 +263,18 @@ class GitConfig(object):
self._branches[b.name] = b self._branches[b.name] = b
return b return b
def GetSyncState(self):
"""Get the state sync object."""
return self._syncState
def SetSyncState(self, sync_state):
"""Update Config's SyncState object with the new |sync_state| object.
Args:
sync_state: Current SyncState object.
"""
self._syncState = sync_state
def GetSubSections(self, section): def GetSubSections(self, section):
"""List all subsection names matching $section.*.* """List all subsection names matching $section.*.*
""" """
@ -717,3 +730,53 @@ class Branch(object):
def _Get(self, key, all_keys=False): def _Get(self, key, all_keys=False):
key = 'branch.%s.%s' % (self.name, key) key = 'branch.%s.%s' % (self.name, key)
return self._config.GetString(key, all_keys=all_keys) return self._config.GetString(key, all_keys=all_keys)
class SyncState(object):
"""Configuration options related Sync object.
"""
def __init__(self, config, options, superproject):
"""Initializes SyncState.
Saves argv, |options|, superproject and repo.*, branch.* and remote.*
parameters from |config| object. It also saves current time as synctime.
This object is versioned.
Args:
config: GitConfig object to store all options.
options: Options passed to sync returned from optparse. See _Options().
superproject: A dictionary of superproject configuration parameters.
"""
self._config = config
now = datetime.utcnow()
self._Set('synctime', now.strftime('%d/%m/%Y %H:%M:%S'))
self._Set('version', '1.0')
self._Set('argv', sys.argv)
self._SetDictionary(superproject)
for key, value in options.__dict__.items():
self._Set(key, value)
config_items = config.DumpConfigDict().items()
self._SetDictionary({k: v for k, v in config_items if k.startswith('repo.')})
self._SetDictionary({k: v for k, v in config_items if k.startswith('branch.')})
self._SetDictionary({k: v for k, v in config_items if k.startswith('remote.')})
def _SetDictionary(self, config_dict):
for key, value in config_dict.items():
self._Set(key, value)
def _Set(self, key, value):
if value is None:
return None
key = 'syncstate.%s' % (key)
if isinstance(value, str):
return self._config.SetString(key, value)
elif isinstance(value, bool):
return self._config.SetBoolean(key, value)
else:
return self._config.SetString(key, str(value))
def _Get(self, key, all_keys=False):
key = 'syncstate.%s' % (key)
return self._config.GetString(key, all_keys=all_keys)

View File

@ -144,6 +144,19 @@ class EventLog(object):
command_event['subcommands'] = subcommands command_event['subcommands'] = subcommands
self._log.append(command_event) self._log.append(command_event)
def _LogConfigEvents(self, config, event_dict_name):
"""Append a |event_dict_name| event for each config key in |config| to the current log.
Args:
config: Configuration dictionary
event_dict_name: Name of the event dictionary for items to be logged under.
"""
for param, value in config.items():
event = self._CreateEventDict(event_dict_name)
event['param'] = param
event['value'] = value
self._log.append(event)
def DefParamRepoEvents(self, config): def DefParamRepoEvents(self, config):
"""Append a 'def_param' event for each repo.* config key to the current log. """Append a 'def_param' event for each repo.* config key to the current log.
@ -152,12 +165,18 @@ class EventLog(object):
""" """
# Only output the repo.* config parameters. # Only output the repo.* config parameters.
repo_config = {k: v for k, v in config.items() if k.startswith('repo.')} repo_config = {k: v for k, v in config.items() if k.startswith('repo.')}
self._LogConfigEvents(repo_config, 'def_param')
for param, value in repo_config.items(): def AddSyncStateEvents(self, config, event_dict_name):
def_param_event = self._CreateEventDict('def_param') """Append a log event for each syncstate.* config key to the current log.
def_param_event['param'] = param
def_param_event['value'] = value Args:
self._log.append(def_param_event) config: SyncState configuration dictionary
event_dict_name: Name of the event dictionary for items to be logged under.
"""
# Only output syncstate.* config parameters.
sync_config = {k: v for k, v in config.items() if k.startswith('syncstate.')}
self._LogConfigEvents(sync_config, event_dict_name)
def ErrorEvent(self, msg, fmt): def ErrorEvent(self, msg, fmt):
"""Append a 'error' event to the current log.""" """Append a 'error' event to the current log."""

View File

@ -46,7 +46,7 @@ except ImportError:
import event_log import event_log
from git_command import git_require from git_command import git_require
from git_config import GetUrlCookieFile from git_config import GetUrlCookieFile, SyncState
from git_refs import R_HEADS, HEAD from git_refs import R_HEADS, HEAD
import git_superproject import git_superproject
import gitc_utils import gitc_utils
@ -306,6 +306,7 @@ later is required to fix a server side protocol bug.
submodules_ok=opt.fetch_submodules) submodules_ok=opt.fetch_submodules)
update_result = superproject.UpdateProjectsRevisionId(all_projects) update_result = superproject.UpdateProjectsRevisionId(all_projects)
manifest_path = update_result.manifest_path manifest_path = update_result.manifest_path
self.superproject['superprojectSyncSuccessful'] = True if manifest_path else False
if manifest_path: if manifest_path:
self._ReloadManifest(manifest_path, load_local_manifests) self._ReloadManifest(manifest_path, load_local_manifests)
else: else:
@ -958,7 +959,11 @@ later is required to fix a server side protocol bug.
self._UpdateManifestProject(opt, mp, manifest_name) self._UpdateManifestProject(opt, mp, manifest_name)
load_local_manifests = not self.manifest.HasLocalManifests load_local_manifests = not self.manifest.HasLocalManifests
if git_superproject.UseSuperproject(opt, self.manifest): self.superproject = {}
use_superproject = git_superproject.UseSuperproject(opt, self.manifest)
self.superproject['superproject'] = use_superproject
self.superproject['hasLocalManifests'] = True if self.manifest.HasLocalManifests else False
if use_superproject:
manifest_name = self._UpdateProjectsRevisionId(opt, args, load_local_manifests) or opt.manifest_name manifest_name = self._UpdateProjectsRevisionId(opt, args, load_local_manifests) or opt.manifest_name
if self.gitc_manifest: if self.gitc_manifest:
@ -1073,6 +1078,14 @@ later is required to fix a server side protocol bug.
file=sys.stderr) file=sys.stderr)
sys.exit(1) sys.exit(1)
# Log the previous sync state from the config.
self.git_event_log.AddSyncStateEvents(mp.config.DumpConfigDict(), 'previous_sync_state')
# Update and log with the new sync state.
sync_state = SyncState(config=mp.config, options=opt, superproject=self.superproject)
mp.config.SetSyncState(sync_state)
self.git_event_log.AddSyncStateEvents(mp.config.DumpConfigDict(), 'current_sync_state')
if not opt.quiet: if not opt.quiet:
print('repo sync has finished successfully.') print('repo sync has finished successfully.')