-
Notifications
You must be signed in to change notification settings - Fork 9
P4: Add support for json output #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jasugun
wants to merge
17
commits into
main
Choose a base branch
from
lca/p4/new_functions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
60b64d0
P4: Add support for json output
jasugun 85b6bd0
P4: get_file_workspace_current_revision handles file not existing
jasugun 30864d1
P4: tranform p4_print_** into a p4 print wrapper function
jasugun 218b829
P4: fix use splitlines instead of split('\n')
jasugun 90e34df
P4: rework submit_default_changes function
jasugun 3dc4a95
P4: enable optionnal hide_output flag back to p4 commands
jasugun 1566e40
P4: fix recursive search safety net when reconciling with a path
jasugun f3b029a
P4Utils: fix python styling
jasugun e6e441e
P4: avoid possible parsing errors when dispalying p4 command error
jasugun d916073
P4: fix get_modified_files displaying command output
jasugun 25856d9
P4: inline code to extract json data for printing in print function
jasugun 4919739
P4: reconcile now takes a lists of paths
jasugun 59298fb
P4: use _run_with_arg_file for reconcile and submit funcs
jasugun 9aed010
P4: print function takes one file at a time
jasugun 7098e24
P4: modifiy submit function so it updates cl spec before submitting
jasugun b2ba53b
P4: use os.linesep when updating cl spec field
jasugun 95a7c73
P4: fix cl spec field updating process
jasugun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| ''' Perforce utilities ''' | ||
|
|
||
| import argparse | ||
| import json | ||
| import logging | ||
| import os | ||
| import os.path | ||
|
|
@@ -228,6 +229,23 @@ def reconcile(self, cl_number, *files): | |
|
|
||
| return ret | ||
|
|
||
| def reconcile_workspace(self, cl_number=None, workspace_path_to_reconcile=None, dry_run=False): | ||
| ''' Reconciles given workspace ''' | ||
| p4_reconcile_args = ['reconcile', '-f', '-e', '-a', '-d'] | ||
| if dry_run: | ||
| p4_reconcile_args.append('-n') | ||
| if cl_number: | ||
| p4_reconcile_args.extend(['-c', cl_number]) | ||
| if workspace_path_to_reconcile: | ||
| if not workspace_path_to_reconcile.endswith('...'): | ||
jasugun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| workspace_path_to_reconcile = os.path.join(workspace_path_to_reconcile, '...') | ||
| workspace_path_to_reconcile = nimp.system.sanitize_path(workspace_path_to_reconcile) | ||
| p4_reconcile_args.append(workspace_path_to_reconcile) | ||
|
|
||
| if self._run(*p4_reconcile_args) is None: | ||
jasugun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return False | ||
| return True | ||
|
|
||
| def get_changelist_description(self, cl_number): | ||
| ''' Returns description of given changelist ''' | ||
| desc, = next(self._parse_command_output(["describe", cl_number], r"\.\.\. desc (.*)")) | ||
|
|
@@ -248,6 +266,29 @@ def get_last_synced_changelist(self): | |
|
|
||
| return cl_number | ||
|
|
||
| def get_file_workspace_current_revision(self, file): | ||
| ''' Returns the file revision currently synced in the workspace ''' | ||
| try: | ||
| revision, = next(self._parse_command_output(['have', file], r'\.\.\. haveRev (\d+)')) | ||
jasugun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| except StopIteration as e: | ||
| revision = None | ||
| return revision | ||
|
|
||
| def print(self, p4_print_command_args): | ||
jasugun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ''' wrapper for p4 print command ''' | ||
| output = self._run('print', f'{p4_print_command_args}', use_json_format=True) | ||
jasugun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return self.parse_json_output_for_data(output) | ||
jasugun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @staticmethod | ||
| def parse_json_output_for_data(output): | ||
jasugun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| data = '' | ||
| output = [json_element for json_element in output.splitlines() if json_element] | ||
| for output_chunk in output: | ||
| output_chunk = json.loads(output_chunk) | ||
| if 'data' in output_chunk.keys(): | ||
jasugun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| data += output_chunk['data'] | ||
| return data | ||
|
|
||
| def get_or_create_changelist(self, description): | ||
| ''' Creates or returns changelist number if it's not already created ''' | ||
| pending_changelists = self.get_pending_changelists() | ||
|
|
@@ -335,6 +376,32 @@ def submit(self, cl_number): | |
|
|
||
| return True | ||
|
|
||
| def submit_default_changelist(self, description=None, revert_unchanged=False, dry_run=False): | ||
| ''' Submits given changelist ''' | ||
| logging.info("Submitting default changelist...") | ||
| command = self._get_p4_command('submit', '-f') | ||
| if revert_unchanged: | ||
| command.append('revertunchanged') | ||
| # descriptions could be too long for perforce limitations | ||
| command_length = len(' '.join(command)) | ||
| perforce_cmd_max_characters_limit = 8191 | ||
| if description: | ||
| d_flag = 4 # accounts for ' -d ' string | ||
| description_max_characters_limit = perforce_cmd_max_characters_limit - command_length - d_flag | ||
| description = description[:description_max_characters_limit] | ||
| command.extend(['-d', description]) | ||
| if dry_run: | ||
| logging.info(command) | ||
jasugun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return True | ||
| else: | ||
| _, _, error = nimp.sys.process.call(command, capture_output=True) | ||
|
|
||
| if error is not None and error != "": | ||
jasugun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| logging.error("%s", error) | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
| def sync(self, *files, cl_number = None): | ||
| ''' Udpate given file ''' | ||
| command = ["sync"] | ||
|
|
@@ -357,8 +424,7 @@ def get_modified_files(self, *cl_numbers, root = '//...'): | |
| for cl_number in cl_numbers: | ||
| for filename, action in self._parse_command_output(["fstat", "-e", cl_number , root], | ||
| r"^\.\.\. depotFile(.*)$", | ||
| r"^\.\.\. headAction(.*)", | ||
| hide_output=True): | ||
jasugun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| r"^\.\.\. headAction(.*)"): | ||
| filename = os.path.normpath(filename) if filename is not None else '' | ||
| yield filename, action | ||
|
|
||
|
|
@@ -370,8 +436,10 @@ def _escape_filename(name): | |
| .replace('#', '%23') \ | ||
| .replace('*', '%2A') | ||
|
|
||
| def _get_p4_command(self, *args): | ||
| def _get_p4_command(self, *args, use_json_format=False): | ||
| command = ['p4', '-z', 'tag'] | ||
| if use_json_format: | ||
| command.append('-Mj') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's recommended to always use |
||
| if self._port is not None: | ||
| command += ['-p', self._port] | ||
| if self._user is not None: | ||
|
|
@@ -384,11 +452,12 @@ def _get_p4_command(self, *args): | |
| command += list(args) | ||
| return command | ||
|
|
||
| def _run(self, *args, stdin=None, hide_output=False): | ||
| command = self._get_p4_command(*args) | ||
| def _run(self, *args, stdin=None, hide_output=False, use_json_format=False): | ||
| command = self._get_p4_command(*args, use_json_format=use_json_format) | ||
|
|
||
| for _ in range(5): | ||
| result, output, error = nimp.sys.process.call(command, stdin=stdin, encoding='cp437', capture_output=True, hide_output=hide_output) | ||
| result, output, error = nimp.sys.process.call( | ||
| command, stdin=stdin, encoding='cp437', capture_output=True, hide_output=hide_output) | ||
|
|
||
| if 'Operation took too long ' in error: | ||
| continue | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.