convert_label_to_autoreload.py #4

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

USAGE = '''
Usage:
  convert_label_to_autoreload.py <label or file_with_list_of_labels>
'''

import os
import os.path
import re
import subprocess
import sys
import p4lock
import p4unlock
import sdputils

if len(sys.argv) > 1:
  SDP_INSTANCE = str(sys.argv[1])
else:
  SDP_INSTANCE = '1'

utils = sdputils.SDPUtils(SDP_INSTANCE)
p4 = utils.p4
utils.login()

def check_auto(label, auto):
  result = subprocess.run(
    [p4, "label", "-o", label],
    capture_output=True, text=True
  )
  for line in result.stdout.splitlines():
    if re.search("^Revision:", line):
      auto = 1
    if re.search(" autoreload", line):
      auto = 1
  return auto

def droplabellines(labelname):
  with open('%s.labelfiles.tmp' % labelname, 'r') as infile, \
       open('%s.labelfiles' % labelname, 'w') as outfile:
    for line in infile.readlines():
      line = line.rstrip()
      line = re.sub(r'(#\d+) - .*', r'\1', line)
      outfile.write(line + '\n')
  os.remove('%s.labelfiles.tmp' % labelname)

def convertlabel(label):
  auto = 0
  auto = check_auto(label, auto)
  if auto:
    return

  # Replace any illegal file name characters with an underscore and hex value.
  def rep(mo): return '_%s' % hex(ord(mo.group(0)))[-2:]
  labelfile = re.sub(r'[\@=`~+<>/:\|\\ ,\(\)\[\]\$%^&?;{}\'\"]', rep, label)

  # Get label spec and replace noautoreload with autoreload
  result = subprocess.run(
    [p4, "label", "-o", label],
    capture_output=True, text=True
  )
  with open('%s.labelspec' % labelfile, 'w') as f:
    f.write(result.stdout.replace("noautoreload", "autoreload"))

  # Get label files
  result = subprocess.run(
    [p4, "files", "@%s" % label],
    capture_output=True, text=True
  )
  with open('%s.labelfiles.tmp' % labelfile, 'w') as f:
    f.write(result.stdout)
  droplabellines(labelfile)

  if (os.path.isfile('%s.labelspec' % labelfile) and os.path.isfile('%s.labelfiles' % labelfile)):
    subprocess.run([p4, "label", "-f", "-d", label])
    with open('%s.labelspec' % labelfile, 'r') as f:
      subprocess.run([p4, "label", "-i"], input=f.read(), text=True)
    p4unlock.unlock(label)
    subprocess.run([p4, "-x", "%s.labelfiles" % labelfile, "labelsync", "-l", label])
    p4lock.lock(label)
    os.remove('%s.labelspec' % labelfile)
    os.remove('%s.labelfiles' % labelfile)

###############################################################################
# main
if __name__ == '__main__':
  if len(sys.argv) < 2:
    print(USAGE)
    sys.exit(1)

  try:
    with open(sys.argv[1], "r") as labellistfile:
      for line in labellistfile.readlines():
        line = line.strip()
        if line:
          convertlabel(line)
  except FileNotFoundError:
    print("No file " + sys.argv[1] + " available, assuming it is the actual label name")
    convertlabel(sys.argv[1].rstrip())
# 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/convert_label_to_autoreload.py
#2 20731 Russell C. Jackson (Rusty) Corrected usage and search for Revision rather than Version.
#1 20289 Russell C. Jackson (Rusty) Script to convert labels to autoreload labels