mirror of
https://github.com/Dev-Wiki/git-repo.git
synced 2025-12-14 16:44:09 +08:00
Format codebase with black and check formatting in CQ
Apply rules set by https://gerrit-review.googlesource.com/c/git-repo/+/362954/ across the codebase and fix any lingering errors caught by flake8. Also check black formatting in run_tests (and CQ). Bug: b/267675342 Change-Id: I972d77649dac351150dcfeb1cd1ad0ea2efc1956 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/363474 Reviewed-by: Mike Frysinger <vapier@google.com> Tested-by: Gavin Mak <gavinmak@google.com> Commit-Queue: Gavin Mak <gavinmak@google.com>
This commit is contained in:
262
event_log.py
262
event_log.py
@@ -15,161 +15,169 @@
|
||||
import json
|
||||
import multiprocessing
|
||||
|
||||
TASK_COMMAND = 'command'
|
||||
TASK_SYNC_NETWORK = 'sync-network'
|
||||
TASK_SYNC_LOCAL = 'sync-local'
|
||||
TASK_COMMAND = "command"
|
||||
TASK_SYNC_NETWORK = "sync-network"
|
||||
TASK_SYNC_LOCAL = "sync-local"
|
||||
|
||||
|
||||
class EventLog(object):
|
||||
"""Event log that records events that occurred during a repo invocation.
|
||||
"""Event log that records events that occurred during a repo invocation.
|
||||
|
||||
Events are written to the log as a consecutive JSON entries, one per line.
|
||||
Each entry contains the following keys:
|
||||
- id: A ('RepoOp', ID) tuple, suitable for storing in a datastore.
|
||||
The ID is only unique for the invocation of the repo command.
|
||||
- name: Name of the object being operated upon.
|
||||
- task_name: The task that was performed.
|
||||
- start: Timestamp of when the operation started.
|
||||
- finish: Timestamp of when the operation finished.
|
||||
- success: Boolean indicating if the operation was successful.
|
||||
- try_count: A counter indicating the try count of this task.
|
||||
Events are written to the log as a consecutive JSON entries, one per line.
|
||||
Each entry contains the following keys:
|
||||
- id: A ('RepoOp', ID) tuple, suitable for storing in a datastore.
|
||||
The ID is only unique for the invocation of the repo command.
|
||||
- name: Name of the object being operated upon.
|
||||
- task_name: The task that was performed.
|
||||
- start: Timestamp of when the operation started.
|
||||
- finish: Timestamp of when the operation finished.
|
||||
- success: Boolean indicating if the operation was successful.
|
||||
- try_count: A counter indicating the try count of this task.
|
||||
|
||||
Optionally:
|
||||
- parent: A ('RepoOp', ID) tuple indicating the parent event for nested
|
||||
events.
|
||||
Optionally:
|
||||
- parent: A ('RepoOp', ID) tuple indicating the parent event for nested
|
||||
events.
|
||||
|
||||
Valid task_names include:
|
||||
- command: The invocation of a subcommand.
|
||||
- sync-network: The network component of a sync command.
|
||||
- sync-local: The local component of a sync command.
|
||||
Valid task_names include:
|
||||
- command: The invocation of a subcommand.
|
||||
- sync-network: The network component of a sync command.
|
||||
- sync-local: The local component of a sync command.
|
||||
|
||||
Specific tasks may include additional informational properties.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""Initializes the event log."""
|
||||
self._log = []
|
||||
self._parent = None
|
||||
|
||||
def Add(self, name, task_name, start, finish=None, success=None,
|
||||
try_count=1, kind='RepoOp'):
|
||||
"""Add an event to the log.
|
||||
|
||||
Args:
|
||||
name: Name of the object being operated upon.
|
||||
task_name: A sub-task that was performed for name.
|
||||
start: Timestamp of when the operation started.
|
||||
finish: Timestamp of when the operation finished.
|
||||
success: Boolean indicating if the operation was successful.
|
||||
try_count: A counter indicating the try count of this task.
|
||||
kind: The kind of the object for the unique identifier.
|
||||
|
||||
Returns:
|
||||
A dictionary of the event added to the log.
|
||||
Specific tasks may include additional informational properties.
|
||||
"""
|
||||
event = {
|
||||
'id': (kind, _NextEventId()),
|
||||
'name': name,
|
||||
'task_name': task_name,
|
||||
'start_time': start,
|
||||
'try': try_count,
|
||||
}
|
||||
|
||||
if self._parent:
|
||||
event['parent'] = self._parent['id']
|
||||
def __init__(self):
|
||||
"""Initializes the event log."""
|
||||
self._log = []
|
||||
self._parent = None
|
||||
|
||||
if success is not None or finish is not None:
|
||||
self.FinishEvent(event, finish, success)
|
||||
def Add(
|
||||
self,
|
||||
name,
|
||||
task_name,
|
||||
start,
|
||||
finish=None,
|
||||
success=None,
|
||||
try_count=1,
|
||||
kind="RepoOp",
|
||||
):
|
||||
"""Add an event to the log.
|
||||
|
||||
self._log.append(event)
|
||||
return event
|
||||
Args:
|
||||
name: Name of the object being operated upon.
|
||||
task_name: A sub-task that was performed for name.
|
||||
start: Timestamp of when the operation started.
|
||||
finish: Timestamp of when the operation finished.
|
||||
success: Boolean indicating if the operation was successful.
|
||||
try_count: A counter indicating the try count of this task.
|
||||
kind: The kind of the object for the unique identifier.
|
||||
|
||||
def AddSync(self, project, task_name, start, finish, success):
|
||||
"""Add a event to the log for a sync command.
|
||||
Returns:
|
||||
A dictionary of the event added to the log.
|
||||
"""
|
||||
event = {
|
||||
"id": (kind, _NextEventId()),
|
||||
"name": name,
|
||||
"task_name": task_name,
|
||||
"start_time": start,
|
||||
"try": try_count,
|
||||
}
|
||||
|
||||
Args:
|
||||
project: Project being synced.
|
||||
task_name: A sub-task that was performed for name.
|
||||
One of (TASK_SYNC_NETWORK, TASK_SYNC_LOCAL)
|
||||
start: Timestamp of when the operation started.
|
||||
finish: Timestamp of when the operation finished.
|
||||
success: Boolean indicating if the operation was successful.
|
||||
if self._parent:
|
||||
event["parent"] = self._parent["id"]
|
||||
|
||||
Returns:
|
||||
A dictionary of the event added to the log.
|
||||
"""
|
||||
event = self.Add(project.relpath, task_name, start, finish, success)
|
||||
if event is not None:
|
||||
event['project'] = project.name
|
||||
if project.revisionExpr:
|
||||
event['revision'] = project.revisionExpr
|
||||
if project.remote.url:
|
||||
event['project_url'] = project.remote.url
|
||||
if project.remote.fetchUrl:
|
||||
event['remote_url'] = project.remote.fetchUrl
|
||||
try:
|
||||
event['git_hash'] = project.GetCommitRevisionId()
|
||||
except Exception:
|
||||
pass
|
||||
return event
|
||||
if success is not None or finish is not None:
|
||||
self.FinishEvent(event, finish, success)
|
||||
|
||||
def GetStatusString(self, success):
|
||||
"""Converst a boolean success to a status string.
|
||||
self._log.append(event)
|
||||
return event
|
||||
|
||||
Args:
|
||||
success: Boolean indicating if the operation was successful.
|
||||
def AddSync(self, project, task_name, start, finish, success):
|
||||
"""Add a event to the log for a sync command.
|
||||
|
||||
Returns:
|
||||
status string.
|
||||
"""
|
||||
return 'pass' if success else 'fail'
|
||||
Args:
|
||||
project: Project being synced.
|
||||
task_name: A sub-task that was performed for name.
|
||||
One of (TASK_SYNC_NETWORK, TASK_SYNC_LOCAL)
|
||||
start: Timestamp of when the operation started.
|
||||
finish: Timestamp of when the operation finished.
|
||||
success: Boolean indicating if the operation was successful.
|
||||
|
||||
def FinishEvent(self, event, finish, success):
|
||||
"""Finishes an incomplete event.
|
||||
Returns:
|
||||
A dictionary of the event added to the log.
|
||||
"""
|
||||
event = self.Add(project.relpath, task_name, start, finish, success)
|
||||
if event is not None:
|
||||
event["project"] = project.name
|
||||
if project.revisionExpr:
|
||||
event["revision"] = project.revisionExpr
|
||||
if project.remote.url:
|
||||
event["project_url"] = project.remote.url
|
||||
if project.remote.fetchUrl:
|
||||
event["remote_url"] = project.remote.fetchUrl
|
||||
try:
|
||||
event["git_hash"] = project.GetCommitRevisionId()
|
||||
except Exception:
|
||||
pass
|
||||
return event
|
||||
|
||||
Args:
|
||||
event: An event that has been added to the log.
|
||||
finish: Timestamp of when the operation finished.
|
||||
success: Boolean indicating if the operation was successful.
|
||||
def GetStatusString(self, success):
|
||||
"""Converst a boolean success to a status string.
|
||||
|
||||
Returns:
|
||||
A dictionary of the event added to the log.
|
||||
"""
|
||||
event['status'] = self.GetStatusString(success)
|
||||
event['finish_time'] = finish
|
||||
return event
|
||||
Args:
|
||||
success: Boolean indicating if the operation was successful.
|
||||
|
||||
def SetParent(self, event):
|
||||
"""Set a parent event for all new entities.
|
||||
Returns:
|
||||
status string.
|
||||
"""
|
||||
return "pass" if success else "fail"
|
||||
|
||||
Args:
|
||||
event: The event to use as a parent.
|
||||
"""
|
||||
self._parent = event
|
||||
def FinishEvent(self, event, finish, success):
|
||||
"""Finishes an incomplete event.
|
||||
|
||||
def Write(self, filename):
|
||||
"""Writes the log out to a file.
|
||||
Args:
|
||||
event: An event that has been added to the log.
|
||||
finish: Timestamp of when the operation finished.
|
||||
success: Boolean indicating if the operation was successful.
|
||||
|
||||
Args:
|
||||
filename: The file to write the log to.
|
||||
"""
|
||||
with open(filename, 'w+') as f:
|
||||
for e in self._log:
|
||||
json.dump(e, f, sort_keys=True)
|
||||
f.write('\n')
|
||||
Returns:
|
||||
A dictionary of the event added to the log.
|
||||
"""
|
||||
event["status"] = self.GetStatusString(success)
|
||||
event["finish_time"] = finish
|
||||
return event
|
||||
|
||||
def SetParent(self, event):
|
||||
"""Set a parent event for all new entities.
|
||||
|
||||
Args:
|
||||
event: The event to use as a parent.
|
||||
"""
|
||||
self._parent = event
|
||||
|
||||
def Write(self, filename):
|
||||
"""Writes the log out to a file.
|
||||
|
||||
Args:
|
||||
filename: The file to write the log to.
|
||||
"""
|
||||
with open(filename, "w+") as f:
|
||||
for e in self._log:
|
||||
json.dump(e, f, sort_keys=True)
|
||||
f.write("\n")
|
||||
|
||||
|
||||
# An integer id that is unique across this invocation of the program.
|
||||
_EVENT_ID = multiprocessing.Value('i', 1)
|
||||
_EVENT_ID = multiprocessing.Value("i", 1)
|
||||
|
||||
|
||||
def _NextEventId():
|
||||
"""Helper function for grabbing the next unique id.
|
||||
"""Helper function for grabbing the next unique id.
|
||||
|
||||
Returns:
|
||||
A unique, to this invocation of the program, integer id.
|
||||
"""
|
||||
with _EVENT_ID.get_lock():
|
||||
val = _EVENT_ID.value
|
||||
_EVENT_ID.value += 1
|
||||
return val
|
||||
Returns:
|
||||
A unique, to this invocation of the program, integer id.
|
||||
"""
|
||||
with _EVENT_ID.get_lock():
|
||||
val = _EVENT_ID.value
|
||||
_EVENT_ID.value += 1
|
||||
return val
|
||||
|
||||
Reference in New Issue
Block a user