DeleteStreams.py #3

  • //
  • guest/
  • pascal_soccard/
  • Scripts/
  • client/
  • DeleteStreams.py
  • View
  • Commits
  • Open Download .zip Download (3 KB)
#!/usr/bin/env python3

################################################################################
#
# Copyright (c) 2025, Perforce Software, Inc.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1.  Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#
# 2.  Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# DATE
#   
#   $Date: 2025/07/09 $
#
# SYNOPSIS
#
#   DeleteStreams.py
# 
# DESCRIPTION
#
#   This script removes a list of streams including their streams children and any 
#   workspaces associated with any of the streams being deleted.
#   
#   Proceed with caution!
#
# REQUIREMENTS
#
#   * Python (tested with Python 3.11 on Linux) 
#   * P4Python
#   * P4 variables set in P4CONFIG or set in the environment
#
################################################################################

from __future__ import print_function
from P4 import P4, P4Exception
import argparse

def deleteStream(p4, stream, parent, flag):
    for child in p4.run_streams("-U", "-F", "Parent=" + stream):
        deleteStream(p4, child["Stream"], stream, flag)
    for child in p4.run_streams("-F", "Parent=" + stream):
        deleteStream(p4, child["Stream"], stream, flag)
    try:
      p4.run_stream("-d", flag, stream)
      if parent:
         print("Stream", stream, "child of", parent, "has been deleted")
      else:
         print("Stream", stream, "has been deleted")
    except P4Exception as err:
      if "has active clients; cannot delete until they are removed." in str(err):
         for client in p4.run_clients("-S", stream):
             p4.run_client("-d", flag, client["client"])
             print("Client", client["client"], "of stream", stream, "has been deleted")
         deleteStream(p4, stream, parent, flag)
      else:
         print(err)
      pass 
 
def main():
    parser = argparse.ArgumentParser(prog="DeleteStream", usage="%(prog)s list_of_stream")
    parser.add_argument("streams", nargs="*", help="streams to delete")
    args = parser.parse_args()
    streams = args.streams

    p4 = P4()
    p4.exception_level = 1
    try:
      p4.connect()
    except P4Exception as err:
      exit(err)
    flag = ""
    if "super" in p4.run_protects("-m")[0]["permMax"]:
       flag = "-f"
    for stream in streams:
        deleteStream(p4, stream, "", flag)

if __name__ == "__main__":
    main()
# Change User Description Committed
#3 31742 Pascal Soccard Added deletion of unloaded task streams
#2 31740 Pascal Soccard p4 commands will use the -f flag if the script is run be a super user
#1 31739 Pascal Soccard Script to delete a list of streams, their children stream and
       any associated workspaces.