rbtools.clients.perforce¶
A client for Perforce.
Classes
|
A wrapper around p4 commands. |
|
A client for Perforce. |
|
A patcher that applies Perforce patches to a tree. |
- class rbtools.clients.perforce.P4Wrapper(options)[source]¶
Bases:
objectA wrapper around p4 commands.
All calls out to p4 go through an instance of this class. It keeps a separation between all the standard SCMClient logic and any parsing and handling of p4 invocation and results.
- __annotations_cache__ = {}¶
- __firstlineno__ = 49¶
- __init__(options)[source]¶
Initialize the wrapper.
- Parameters:
options (
argparse.Namespace) – The parsed command line options.
- __static_attributes__ = ('options',)¶
- change(changenum: int, marshalled: Literal[True] = True) Sequence[Mapping[str, Any]][source]¶
- change(changenum: int, marshalled: Literal[False]) str
Return the contents of a p4 change description.
- check_dependencies() None[source]¶
Check whether all base dependencies are available.
This checks for the presence of p4 in the system path.
Added in version 4.0.
- Raises:
rbtools.clients.errors.SCMClientDependencyError – A p4 tool could not be found.
- counters()[source]¶
Return the Perforce counters.
- Returns:
The parsed Perforce counters.
- Return type:
- filelog(path: str) Sequence[Mapping[str, Any]][source]¶
Return a list of all the changed files within the given path.
- files(path: str) Sequence[Mapping[str, Any]][source]¶
Return the opened files within the given path.
- fstat(depot_path: str, fields: Sequence[str] | None = None) Mapping[str, str][source]¶
Run p4 fstat on a given depot path.
- info()[source]¶
Run p4 info and return the results.
- Returns:
The parsed output from p4 info.
- Return type:
- is_supported()[source]¶
Check whether the p4 command is usable.
- Returns:
Trueif there’s an executable p4.- Return type:
- modify_change(new_change_spec: str, *, changenum: str | None = None) None[source]¶
Modify a change description.
- opened(changenum: int) Sequence[Mapping[str, Any]][source]¶
Return the list of opened files in the given changeset.
- print_file(depot_path: str, out_file: str | None = None) str[source]¶
Print the contents of the given file.
- run_p4(p4_args: Sequence[str], *args, marshalled: Literal[True], **kwargs: Unpack[RunProcessKwargs]) Sequence[Mapping[str, Any]][source]¶
- run_p4(p4_args: Sequence[str], *args, marshalled: Literal[False] = False, **kwargs: Unpack[RunProcessKwargs]) RunProcessResult
Invoke p4.
Changed in version 6.0: Updated to use
run_process()instead ofrbtools.utils.execute(). This means in cases wheremarshalledisFalse, this method now returns aRunProcessResult.- Parameters:
p4_args (
list) – Additional arguments to pass to p4.marshalled (
bool, optional) – Whether to return the data in marshalled format. This will return a more computer-readable version.*args (
list) – Additional arguments to pass through torbtools.utils.process.execute().**kwargs (
dict) – Additional keyword arguments to pass through torbtools.utils.process.execute().
- Returns:
If passing
marshalled=True, then this will be a list of dictionaries containing results from the command.In all other cases, this will return the result of
RunProcessResult.- Return type:
- Raises:
rbtools.clients.errors.SCMError – There was an error with the call to Perforce. Details are in the error message.
- class rbtools.clients.perforce.PerforceClient(p4_class=<class 'rbtools.clients.perforce.P4Wrapper'>, **kwargs)[source]¶
Bases:
BaseSCMClientA client for Perforce.
This is a wrapper around the p4 executable that fetches repository information and generates compatible diffs.
- DATE_RE = re.compile(b'(\\w+)\\s+(\\w+)\\s+(\\d+)\\s+(\\d\\d:\\d\\d:\\d\\d)\\s+(\\d\\d\\d\\d)')[source]¶
- __firstlineno__ = 788¶
- __init__(p4_class=<class 'rbtools.clients.perforce.P4Wrapper'>, **kwargs)[source]¶
Initialize the client.
- __static_attributes__ = ('_p4_info', 'p4', 'p4d_version')¶
- amend_commit_description(message, revisions)[source]¶
Update a commit message to the given string.
Since local changelists on Perforce have no ordering with respect to each other, the revisions argument is mandatory.
- Parameters:
message (
unicode) – The commit message to use when amending the commit.revisions (
dict) – A dictionary of revisions, as returned byparse_revision_spec().
- Raises:
rbtools.clients.errors.AmendError – The given changelist could not be amended.
- can_get_file_content: bool = True[source]¶
Whether the tool can get files at specific revisions.
Added in version 5.0.
- Type:
- check_dependencies() None[source]¶
Check whether all base dependencies are available.
This checks for the presence of p4 in the system path.
Added in version 4.0.
- Raises:
rbtools.clients.errors.SCMClientDependencyError – A hg tool could not be found.
- diff(revisions: SCMClientRevisionSpec | None, *, include_files: Sequence[str] | None = None, exclude_patterns: Sequence[str] | None = None, extra_args: Sequence[str] | None = None, **kwargs) SCMClientDiffResult[source]¶
Perform a diff using the given revisions.
This goes through the hard work of generating a diff on Perforce in order to take into account adds/deletes and to provide the necessary revision information.
- Parameters:
revisions (
dict) – A dictionary of revisions, as returned byparse_revision_spec().include_files (
listofunicode, optional) – A list of files to whitelist during the diff generation.exclude_patterns (
listofunicode, optional) – A list of shell-style glob patterns to blacklist during diff generation.extra_args (
listofstr) – Additional arguments to be passed to the diff generation.**kwargs (
dict, unused) – Unused keyword arguments.
- Returns:
A dictionary containing the following keys:
diff(bytes):The contents of the diff to upload.
changenum(unicode):The number of the changeset being posted (if
revisionsrepresents a single changeset).
- Return type:
- get_changenum(revisions)[source]¶
Return the change number for the given revisions.
This is only used when the client is supposed to send a change number to the server (such as with Perforce).
- get_file_content(*, filename: str, revision: str) bytes[source]¶
Return the contents of a file at a given revision.
Added in version 5.0.
- Parameters:
- Returns:
The read file.
- Return type:
- Raises:
rbtools.clients.errors.SCMError – The file could not be found.
- get_file_size(*, filename: str, revision: str) int[source]¶
Return the size of a file at a given revision.
Added in version 5.0.
- get_local_path() str | None[source]¶
Return the local path to the working tree.
- Returns:
The filesystem path of the repository on the client system.
- Return type:
- get_raw_commit_message(revisions)[source]¶
Extract the commit message based on the provided revision range.
Since local changelists in Perforce are not ordered with respect to one another, this implementation looks at only the tip revision.
- get_repository_info() RepositoryInfo | None[source]¶
Return repository information for the current working tree.
- Returns:
The repository info structure.
- Return type:
- get_repository_name()[source]¶
Return any repository name configured in the repository.
This is used as a fallback from the standard config options, for repository types that support configuring the name in repository metadata.
- Returns:
The configured repository name, or None.
- Return type:
- normalize_exclude_patterns(patterns)[source]¶
Normalize the set of patterns so all non-depot paths are absolute.
A path with a leading // is interpreted as a depot pattern and remains unchanged. A path with a leading path separator is interpreted as being relative to the Perforce client root. All other paths are interpreted as being relative to the current working directory. Non-depot paths are transformed into absolute paths.
- parse_revision_spec(revisions: Sequence[str] | None = None) SCMClientRevisionSpec[source]¶
Parse the given revision spec.
These will be used to generate the diffs to upload to Review Board (or print). The diff for review will include the changes in (base, tip].
If zero revisions are passed in, this will return the current sync changelist as “tip”, and the upstream branch as “base”. The result may have special internal revisions or prefixes based on whether the changeset is submitted, pending, or shelved.
If a single revision is passed in, this will return the parent of that revision for “base” and the passed-in revision for “tip”.
If two revisions are passed in, they need to both be submitted changesets.
- Parameters:
revisions (
listofstr, optional) – A list of revisions as specified by the user. Items in the list do not necessarily represent a single revision, since the user can use SCM-native syntaxes such asr1..r2orr1:r2. SCMTool-specific overrides of this method are expected to deal with such syntaxes.- Returns:
The parsed revision spec.
See
SCMClientRevisionSpecfor the format of this dictionary.This always populates the
baseandtipkeys.- Return type:
- Raises:
rbtools.clients.errors.InvalidRevisionSpecError – The given revisions could not be parsed.
rbtools.clients.errors.TooManyRevisionsError – The specified revisions list contained too many revisions.
- patcher_cls[source]¶
alias of
PerforcePatcher
- requires_diff_tool: bool | Sequence[str] = True[source]¶
Whether this tool requires a command line diff tool.
This may be a boolean or a list.
If a boolean, then this must be
Falseif no command line tool is required, orTrueif any command line tool supported by RBTools is available (in which case the SCMClient is responsible for ensuring compatibility).If a list, then this must be a list of registered diff tool IDs that are compatible.
Added in version 4.0.
- scan_for_server(repository_info)[source]¶
Find if a Review Board server has been defined in the p4 counters.
This checks the Perforce counters to see if the Review Board server’s URL is specified. Since Perforce only started supporting non-numeric counter values in server version 2008.1, we support both a normal counter
reviewboard.urlwith a string value and embedding the URL in a counter name likereviewboard.url.http:||reviewboard.example.com. Note that forward slashes aren’t allowed in counter names, so pipe (‘|’) characters should be used. These should be safe because they should not be used unencoded in URLs.- Parameters:
repository_info (
rbtools.clients.base.repository.RepositoryInfo) – The repository information structure.- Returns:
The Review Board server URL, if available.
- Return type:
- scmclient_id: str = 'perforce'[source]¶
The unique ID of the client.
Added in version 4.0: This will be required in RBTools 5.0.
- Type:
- server_tool_ids: ClassVar[Sequence[str] | None] = ['perforce'][source]¶
A comma-separated list of SCMClient IDs on the server.
This supersedes
server_tool_nameswhen running on a version of Review Board that supports passing tool IDs to the repositories list API.Added in version 5.0.1.
- Type:
- server_tool_names: ClassVar[str | None] = 'Perforce'[source]¶
A comma-separated list of SCMClient names on the server.
Added in version 3.0.
- Type:
- supports_changesets: bool = True[source]¶
Whether the SCM uses server-side changesets
Added in version 3.0.
- Type:
- supports_diff_exclude_patterns: bool = True[source]¶
Whether the SCM client supports excluding files from the diff.
- Type:
- supports_diff_extra_args: bool = True[source]¶
Whether the SCM client’s diff method takes the
extra_argsparameter.- Type:
- class rbtools.clients.perforce.PerforcePatcher(*, scmclient: TSCMClient, **kwargs: Unpack[PatcherKwargs])[source]¶
Bases:
SCMClientPatcher[PerforceClient]A patcher that applies Perforce patches to a tree.
This applies patches using the standard patch command, but taking care to specially-handle added and deleted empty files.
Added in version 5.1.
- __annotations_cache__ = {'_patched': 'bool', '_patching': 'bool', 'applied_patch_results': 'list[PatchResult]', 'can_commit': 'bool', 'can_patch_empty_files': 'bool', 'commit': 'bool', 'dest_path': 'Path', 'patches': 'Sequence[Patch]', 'repository_info': 'RepositoryInfo | None', 'revert': 'bool', 'run_commit_editor': 'bool', 'scmclient': 'TSCMClient', 'squash': 'bool'}¶
- __firstlineno__ = 510¶
- __static_attributes__ = ()¶
- apply_binary_file(binary_file: BinaryFilePatch) tuple[bool, str | None][source]¶
Apply a single binary file.
Added in version 6.0.
- Parameters:
binary_file (
rbtools.diffs.patches.BinaryFilePatch) – The binary file to apply.- Returns:
A 2-tuple of:
- Return type:
- apply_patch_for_empty_files(patch: Patch) bool[source]¶
Return whether any empty files in the patch are applied.
- Parameters:
patch (
rbtools.diffs.patches.Patch) – The opened patch to check and possibly apply.- Returns:
Trueif there are empty files in the patch.Falseif there were no empty files, or if an error occurred while applying the patch.
- apply_single_patch(*, patch: Patch, patch_num: int) PatchResult[source]¶
Apply a single patch.
This is an internal method that will take a single patch and apply it. It may be applied to files that already contain other modifications or have had other patches applied to it.
Added in version 6.0.
- Parameters:
patch (
rbtools.diffs.patches.Patch) – The patch to apply, opened for reading.patch_num (
int) – The 1-based index of this patch in the full list of patches.
- Returns:
The result of the patch application, whether the patch applied successfully or with normal patch failures.
- Return type:
- Raises:
rbtools.diffs.errors.ApplyPatchResult – There was an error attempting to apply the patch. This won’t be raised simply for conflicts or normal patch failures. It may be raised for errors encountered during the patching process.