p4python create and submit a new file - perforce

How to create and submit a new file using p4python?
create_and_submit_file(full_path_in_depot, new_file_text_content):
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
p4 = get_p4() # implemented
try: # Catch exceptions with try/except
connect_and_login_p4(p4) # implemented
# .. implementation here .. p4.some_call()
LOGGER.info('done')
except P4Exception:
for e in p4.errors: # Display errors
LOGGER.error(e)

If the file already exists on the local filesystem within a workspace, all you need to do is p4.run_add(file) and p4.run_submit('-d', 'this is my awesome file').
If the file doesn't exist, you need to create it, and if you don't have a workspace, you need to create that too. For the sake of brevity, here's how you'd do that from the command line completely from scratch (this maps pretty directly to P4Python but I don't know enough about your environment to give you code that'll work out of the box so I won't attempt the translation):
echo "my awesome file content" > my_awesome_file
p4 set P4CLIENT=my_awesome_client
p4 --field "View=//depot/... //my_awesome_client/..." client -o | p4 client -i
p4 add my_awesome_file
p4 submit -d "this is my awesome file"
Check out the example for p4.save_client to see a simple example of how you can create/modify a client spec with P4Python and modify the fields to suit your environment (similar to how I used the --field flag to set the View such that the root of my_awesome_client corresponds to //depot/...):
https://www.perforce.com/perforce/r14.2/manuals/p4script/python.p4.html#python.p4.save_spectype

Related

Given the Function Name, how to find the Changelist in perforce when that function was added?

I am looking for a way to find the changelist in perforce if I have the Function name and File in which that function is present ?
For Eg: if "A.cpp" has function added "void b()" via changelist 1234
I would like to get back changelist 1234 by giving the Function name only or both function name and file name. I was thinking to use python script for this but not sure how to proceed.
Use p4 annotate, e.g.:
p4 annotate -c A.cpp | grep "void b()"
Check out p4 help annotate for the options available -- you can use the -I flag to find the origin changelist if the line was added by a merge, you can use the -a flag to find text in earlier versions of the file, etc.

Shell reading and saveing to config.cfg file?

I am still very new to shell since I have been using it with linux recently, and I tried to mess with a game called garrysmod and make a little script that will ask for information to run the server, its IP, playerslots, etc. Now I somewhat got this working, but I want to be able to save and load this to a config file. I figure out how to get it load from the config, but I want to be able to edit it from the command prompt when it asks you if you would like to edit it.
This is what I have so far as an example:
setup.sh
source config.cfg
echo "Servers current name is $name"
echo
echo "What would you like the name to be?"
read $name
read "The new name is $name"\
config.cfg
name='ServerName'
address=127.0.0.1
port=27015
map='ttt_mapnamehere.bsp'
playercap=32
Now it works after you change it, but I don't know how to get it to save to the .cfg file. The reason of this is because several .sh files will run in order as you go through the steps, and at the end the file one will pull data from the config file being the IP, port, map name, player count, etc. If someone can show me how to do this (examples as Im a visual learner) that would be great!
Simply write it back:
...
printf "name='%s'\naddress='%s'\nport=%s\nmap='%s'\nplayercap=%s\n" \
"$name" "$address" "$port" "$map" "$playercap" > config.cfg

Can't add files in perforce

As far as I can tell, my client is setup correctly:
$ p4 client -o
# A Perforce Client Specification.
# ...
Client: stephen-dev1-stephen
Update: 2014/06/26 17:41:14
Access: 2014/06/26 17:45:47
Owner: StephenRasku
Host: stephen-dev1
Description:
Created by StephenRasku.
Root: /home/stephen/Code
Options: noallwrite noclobber nocompress unlocked nomodtime rmdir
SubmitOptions: submitunchanged
LineEnd: local
View:
//depot/labs/products/component/SpamView-URI/... //stephen-dev1-stephen/SpamView-URI/...
//version/... //stephen-dev1-stephen/version/...
//thirdparty/... //stephen-dev1-stephen/thirdparty/...
//starteam/... //stephen-dev1-stephen/starteam/...
//specs/... //stephen-dev1-stephen/specs/...
//release/... //stephen-dev1-stephen/release/...
//projects/... //stephen-dev1-stephen/projects/...
//main/... //stephen-dev1-stephen/main/...
//features/... //stephen-dev1-stephen/features/...
//dev/... //stephen-dev1-stephen/dev/...
//depot/... //stephen-dev1-stephen/depot/...
The files exist:
$ pwd
/home/stephen/Code/SpamView-URI
$ ls mainline/EBUILD_VERSION mainline/package.sh mainline/ebuild
mainline/ebuild mainline/EBUILD_VERSION mainline/package.sh
But it complains when I try and add them:
$ p4 add mainline/EBUILD_VERSION mainline/package.sh mainline/ebuild
mainline/EBUILD_VERSION - file(s) not in client view.
mainline/package.sh - file(s) not in client view.
mainline/ebuild - file(s) not in client view.
What's the problem? I checked out the file using git p4 clone if that makes a difference.
Check the "View" lines in the client workspace specification to confirm
that the file specification used in your Perforce command (or appearing in the error message)
falls within your workspace view. If you see an error attempting to add a file,
for example, you might want to check your mapping to confirm that the
file resides in a directory that is within your client view.
See the section under 'Client Workspace View':
http://answers.perforce.com/articles/KB_Article/Common-Permissions-and-File-Access-Problems
Are the files under this exact directory structure below?
/home/stephen/Code/SpamView-URI/mainline/EBUILD_VERSION
/home/stephen/Code/SpamView-URI/mainline/package.sh
/home/stephen/Code/SpamView-URI/mainline/ebuild
Judging by the first View mapping line of:
//depot/labs/products/component/SpamView-URI/... //stephen-dev1-stephen/SpamView-URI/...
I would guess that is the path they should be under.
If you 'cd' into the '/home/stephen/Code/SpamView-URI/mainline'
directory are you able to add these files?
Your client spec isn't right, as you noticed. With these 2 lines,
//depot/labs/products/component/SpamView-URI/... //stephen-dev1-stephen/SpamView-URI/...
//depot/... //stephen-dev1-stephen/depot/...
Your trying to map the files under //depot/labs/products/component/SpamView-URI/... to both //stephen-dev1-stephen/SpamView-URI/... and //stephen-dev1-stephen/depot/labs/products/component/SpamView-URI/... Since perforce reads top to bottom, it will overwrite your first mapping with the second mapping, basically removing the second mapping. \
Move your //depot/labs/products/component/SpamView-URI/... //stephen-dev1-stephen/SpamView-URI/... to the last line, and you should be ok.

Piping with multiple commands

Assume you have a file called “heading” as follows
echo "Permissions^V<TAB>^V<TAB>Size^V<TAB>^V<TAB>File Name" > heading
echo "-------------------------------------------------------" >> heading
Write a (single) set of commands that will create a report as follows:
make a list of the names, permissions and size of all the files in your current directory,
matching (roughly) the format of the heading you just created,
put the list of files directly following the heading, and
save it all into a file called “file.list”.
All this is to be done without destroying the heading file.
I need to be able to do this all in a pipleline without altering the file. I can't seem to do this without destroying the file. Can somebody please make a pipe for me?
You can use command group:
{ cat heading; ls -l | sed 's/:/^V<tab>^V<tab>/g'; } > file.list

Perforce: How to find the original number of a change list

In perforce changelists get renumbered on submission. So for e.g. when the changelist was created it would be numbered 777 , but on submission of changelist it would get renumbered to say 790.
My question is how do I get the new CL number (790) , if I know the old CL number 777 , or vice versa ?
If you really want the original changelist number, that can be retrieved from Perforce without having to embed the original changelist number in the description. You can use the -ztag command line option to get at it. And you can only get at it through the 'changes' command (as far as I know):
d:\sandbox>p4 submit -c 24510
Submitting change 24510.
Locking 1 files ...
edit //depot/testfile.txt#2
Change 24510 renamed change 24512 and submitted.
d:\sandbox>p4 -ztag changes -m1 //depot/testfile.txt
... change 24512
... time 1294249178
... user test.user
... client client-test.user
... status submitted
... oldChange 24510
... desc <enter description here>
<saved
As pointed out, it's probably not that useful. However, I did want to note that it's possible to get at it.
The only way I can think of is adding the original changelist number as part of the changelist description field. First, you'll need a script to store the original changelist number:
#!/bin/env perl
$id = $ARGV[0];
open (CHANGE_IN, "p4 change -o $id|");
open (CHANGE_OUT, "|p4 change -i $id");
while (<CHANGE_IN>)
{
if (/^Description:/ and not /ORIGID/)
{
s/(^Description:)(.*)$/$1 ORIGID $id. $2/;
}
print CHANGE_OUT $_;
}
close (CHANGE_IN);
close (CHANGE_OUT);
Save this as origid.pl on the Perforce server with the executable bit set. Then setup a trigger with p4 triggers.
Triggers:
add_origid change-submit //depot/... /usr/bin/origid.pl %change%
Version 2012.1 of Perforce introduced the -O (capital oh) argument to p4 describe, which allows you to query a changelist by its original number (before being renumbered by p4 submit).
I find this very helpful, since I often find myself keeping notes about a changeset before it is submitted, then forgetting to note what it was renumbered to on submission.
So if I have a note talking about change 12300, I can now see what it refers to by typing:
p4 describe -s -O 12300
and having Perforce tell me:
Change 12345 by me#myhost on 2013/10/31 00:00:00
Fix that thing I wrote that note about
Affected files ...
... //Proj/MAIN/foo.c
The ztag method mentioned earlier can be used to find the old changelist number of a submitted change:
> p4 -ztag describe -s 12345 | grep oldChange
... oldChange 12300
Adding to Eric Miller's reply, because I can't comment (not enough points):
to just emit the 1 number
p4 -ztag describe $ORIG | sed -e 's/^\.\.\. oldChange //;t-ok;d;:-ok'
e.g.
OLD=$(p4 -ztag describe $ORIG | sed -e 's/^\.\.\. oldChange //;t-ok;d;:-ok')
or if you want to look up many numbers, this will output a map of new old on each line (may be useful with the "join" command). If there is no old commit, then it re-emits the new commit.
p4 -ztag describe 782546 782547 ... | sed -e '${x;p};s/^\.\.\. change //;t-keep;b-next;:-keep;x;/./p;g;G;s/\n/ /;x;d;:-next;s/^\.\.\. oldChange //;t-ok;d;:-ok;H;x;s/ .*\n/ /;x;d;'
I tried to avoid using GNU extensions to sed.
Unless you do something like Tim suggests the old change list number will be lost on submission.
Change list numbers are only temporary until you actually submit. So if you create a new change list (777 say) and then decide to delete it, the next change list you create will be 778.
It can be a bit more elegant if you use the P4 Python module.
i.e.
import P4
p4 = P4.P4()
p4.connect() # having a valid p4 workspace/connection is on you :)
c = p4.run_describe('969696') # describe a Submitted, renumbered changelist, i.e. 969696
old_pending_cl_number = c['oldChange'] # print out prior/pending CL# if this exists.
Cheers

Resources