From f95db4d79e30956042d8b171bcc3596b7cc25f48 Mon Sep 17 00:00:00 2001 From: Raman Tenneti Date: Mon, 19 Jul 2021 16:40:58 -0700 Subject: [PATCH] sync - Added logging of repo sync state and config options. Change-Id: I9186f34e0cf9c7498149e666d7a035fe75c0f5c4 --- git_config.py | 63 +++++++++++++++++++++++++++++++++++++++++ git_trace2_event_log.py | 29 +++++++++++++++---- subcmds/sync.py | 17 +++++++++-- 3 files changed, 102 insertions(+), 7 deletions(-) diff --git a/git_config.py b/git_config.py index 3eaf201c..23557c6e 100644 --- a/git_config.py +++ b/git_config.py @@ -13,6 +13,7 @@ # limitations under the License. import contextlib +from datetime import datetime import errno from http.client import HTTPException import json @@ -262,6 +263,18 @@ class GitConfig(object): self._branches[b.name] = 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): """List all subsection names matching $section.*.* """ @@ -717,3 +730,53 @@ class Branch(object): def _Get(self, key, all_keys=False): key = 'branch.%s.%s' % (self.name, key) 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) diff --git a/git_trace2_event_log.py b/git_trace2_event_log.py index fae3d4c8..60c1ee54 100644 --- a/git_trace2_event_log.py +++ b/git_trace2_event_log.py @@ -144,6 +144,19 @@ class EventLog(object): command_event['subcommands'] = subcommands 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): """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. 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_param_event = self._CreateEventDict('def_param') - def_param_event['param'] = param - def_param_event['value'] = value - self._log.append(def_param_event) + def AddSyncStateEvents(self, config, event_dict_name): + """Append a log event for each syncstate.* config key to the current log. + + Args: + 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): """Append a 'error' event to the current log.""" diff --git a/subcmds/sync.py b/subcmds/sync.py index a770c253..d659500e 100644 --- a/subcmds/sync.py +++ b/subcmds/sync.py @@ -46,7 +46,7 @@ except ImportError: import event_log from git_command import git_require -from git_config import GetUrlCookieFile +from git_config import GetUrlCookieFile, SyncState from git_refs import R_HEADS, HEAD import git_superproject import gitc_utils @@ -306,6 +306,7 @@ later is required to fix a server side protocol bug. submodules_ok=opt.fetch_submodules) update_result = superproject.UpdateProjectsRevisionId(all_projects) manifest_path = update_result.manifest_path + self.superproject['superprojectSyncSuccessful'] = True if manifest_path else False if manifest_path: self._ReloadManifest(manifest_path, load_local_manifests) else: @@ -958,7 +959,11 @@ later is required to fix a server side protocol bug. self._UpdateManifestProject(opt, mp, manifest_name) 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 if self.gitc_manifest: @@ -1073,6 +1078,14 @@ later is required to fix a server side protocol bug. file=sys.stderr) 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: print('repo sync has finished successfully.')