p4lock.py #4

  • //
  • guest/
  • russell_jackson/
  • sdp/
  • Maintenance/
  • p4lock.py
  • View
  • Commits
  • Open Download .zip Download (2 KB)
#!/usr/bin/env python3

"""
This program should lock a perforce label.

The only command line parameter is the label
p4lock.py LABEL
"""

import isitalabel
import os
import re
import subprocess
import sys

def lock (label):
  x = isitalabel.check(label)
  if x == 0:
    print("There is no label %s." % label)
    sys.exit(1)

  label_pattern = re.compile(r"^Label:\s+.*")
  lock_pattern = re.compile(r"^Options:\s+unlocked")

  linelist = []

  # owner.txt is a one line file generated by the p4unlock.py script. It saves
  # the name of the label owner so it can be put back by this script.
  # The owner line is removed by p4unlock.py because Perforce doesn't allow
  # anyone except the owner of a label to update the label.

  found_owner = True
  owner = ""
  if os.path.isfile("owner.txt"):
    with open("owner.txt", "r") as ownerfile:
      for line in ownerfile.readlines():
        owner = line
    os.remove("owner.txt")
  else:
    found_owner = False

  result = subprocess.run(
    ["p4", "label", "-o", label],
    capture_output=True, text=True
  )
  for line in result.stdout.splitlines(keepends=True):
    if found_owner:
      match = label_pattern.match(line)
      if match is not None:
        linelist.append("Owner:\t%s\n" % owner)
    line = lock_pattern.sub("Options:\tlocked", line)
    linelist.append(line)

  spec_text = "".join(linelist)
  subprocess.run(["p4", "label", "-i"], input=spec_text, text=True)

def main():
  if len(sys.argv) < 2:
    print('Invalid input')
    print('You must specify a label')
    print('usage: p4lock.py LABELNAME')
  else:
    label = sys.argv[1]
    lock(label)

###########################################################################
# main
if __name__ == '__main__':
  main()
# Change User Description Committed
#4 32423 Russell C. Jackson (Rusty) Modernize SDP maintenance scripts: security, correctness, and Python 3

- Replace all os.system() and os.popen() calls with subprocess.run() using
  argument lists to eliminate shell injection vulnerabilities
- Fix critical bugs: broken indentation in convert_label_to_autoreload.py,
  malformed print() in p4lock/p4unlock, wrong variable in isitalabel,
  format string typo in maintain_user_from_groups
- Add p4 property alias and shared SKIP_USERS constant to sdputils.py
- Add try/finally with p4.disconnect() to all P4Python scripts
- Replace bare except: with specific exception types throughout
- Update all shebangs to python3, remove unnecessary __future__ imports
- Use context managers for all file handle operations
- Replace from subprocess import * with explicit imports
#3 32388 Russell C. Jackson (Rusty) Updates using Claude.ai to clean up the code, reduce duplication, enhanace security, and use current standards.
#2 24675 Russell C. Jackson (Rusty) Fixed bugs in sdputils.py and scripts using it.
Converted to standard 2 space spacing, removed copyright stuff.
#1 22693 Russell C. Jackson (Rusty) Branched a Unix only version of the SDP.
Removed extra items to create a cleaner tree.
Moved a few items around to make more sense without Windows in the mix.
//guest/perforce_software/sdp/dev/Maintenance/p4lock.py
#1 20289 Russell C. Jackson (Rusty) Script to convert labels to autoreload labels