Skip to content

Instantly share code, notes, and snippets.

@airekans
Last active August 29, 2015 14:06
Show Gist options
  • Save airekans/082982bcf6ee8d5f5e24 to your computer and use it in GitHub Desktop.
Save airekans/082982bcf6ee8d5f5e24 to your computer and use it in GitHub Desktop.
Script used to sync files from p4 shelve
#! /usr/bin/env python2.7
import pypipe # use pypipe from https://github.com/airekans/Pypipe
import sys
import os
_BACKUP_CL = 123456
def is_p4_different(p4_path, shelve_cl):
return len(pypipe.sh('p4 diff -du %s@=%d' % (p4_path, shelve_cl))) > 2
def p4_revert(p4_path, cl, mode):
if mode == 'edit':
cmd = "p4 revert -c %d %s" % (cl, p4_path)
os.system(cmd)
else:
# get the real file path
res = pypipe.sh('p4 fstat %s' % p4_path).grep(r'clientFile').sh("awk '{print $3}'").list()
assert len(res) == 1
real_path = res[0].strip()
os.system("p4 revert -c %d %s" % (cl, p4_path))
os.system("rm -v %s" % real_path)
def main():
global _BACKUP_CL
if len(sys.argv) < 2:
print 'Usage: %s CL [CL_for_unsh]' % sys.argv[0]
sys.exit(1)
changelist = int(sys.argv[1])
if len(sys.argv) > 2:
_BACKUP_CL = int(sys.argv[2])
shelved_file_list = pypipe.sh('p4 unshelve -s %d -n' % changelist).\
sh("awk '{if ($1 ~ /\.\.\./) print $2; else print $1;}'").\
sh("awk -F '#' '{print $1}'").list()
assert len(shelved_file_list) > 0
shelved_file_list = set(f.strip() for f in shelved_file_list)
#print shelved_file_list
# shelve the _BACKUP_CL first
# get the edited files in _BACKUP_CL
backup_edit_files = [f.strip() for f in pypipe.sh('p4 describe %d' % _BACKUP_CL).\
grep(r'edit$').sh("awk -F '[ #]' '{print $2}'")]
backup_add_files = [f.strip() for f in pypipe.sh('p4 describe %d' % _BACKUP_CL).\
grep(r'add$').sh("awk -F '[ #]' '{print $2}'")]
backup_files = {}
for f in backup_edit_files:
backup_files[f] = 'edit'
for f in backup_add_files:
backup_files[f] = 'add'
# check the files that in the shelve but not in backup
for f in shelved_file_list:
if f not in backup_files:
print '%s not exist in local, unshelve it' % f
cmd = "p4 unshelve -s %d -f -c %d %s" % (changelist, _BACKUP_CL, f)
os.system(cmd)
else: # check if the file is different from the local one
if is_p4_different(f, changelist):
print '%s is diff with local one' % f
p4_revert(f, _BACKUP_CL, backup_files[f])
# unshelve the file
cmd = "p4 unshelve -s %d -f -c %d %s" % (changelist, _BACKUP_CL, f)
os.system(cmd)
for f, mode in backup_files.iteritems():
if f not in shelved_file_list:
p4_revert(f, _BACKUP_CL, mode)
print 'p4 unshelve %d done' % changelist
if __name__ == "__main__":
main()
#! /bin/bash
if [ $# -lt 1 ]; then
echo Usage: $0 changelist
exit 1
fi
cl_num=$1
backup_cl=123456
p4 shelve -f -c $backup_cl
echo "reverting edit file"
for f in $(p4 describe $backup_cl | grep 'edit$' | awk -F '[ #]' '{print $2}');
do
p4 revert -c $backup_cl $f
done
echo "reverting add file"
for f in $(p4 describe $backup_cl | grep 'add$' | awk -F '[ #]' '{print $2}');
do
file_path=$(p4 fstat $f | grep 'clientFile' | awk '{print $3}')
p4 revert -c $backup_cl $f
rm -v $file_path
done
p4 unshelve -s $cl_num -f -c $backup_cl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment