sdputils.py #5

  • //
  • guest/
  • russell_jackson/
  • sdp/
  • Maintenance/
  • sdputils.py
  • View
  • Commits
  • Open Download .zip Download (1 KB)
#!/usr/bin/env python

"""
This module is a convenience for other modules in this directory
"""

# Python 2.7/3.3 compatibility.
from __future__ import print_function

import os
import sys
import platform

class SDPUtils:
  def __init__(self, sdp_instance):
    self.sdp_instance = sdp_instance

    # Python 2.7/3.3 compatibility.
    if sys.version_info[0] >= 3:
      import configparser
      self.rawconfig = configparser.RawConfigParser()
    else:
      import ConfigParser
      self.rawconfig = ConfigParser.RawConfigParser()

    ##########################################################################
    #####                                #####
    #####  CONFIGURATION VARIABLES: Modify in maintenance.cfg as needed. #####
    #####                                #####
    self.rawconfig.read('maintenance.cfg')
    self.server = os.environ.get('P4PORT')
    self.p4user = os.environ.get('P4USER')

    if platform.system() == "Windows":
      self.p4 = "p4.exe -p %s -u %s" % (self.server, self.p4user)
    else:
      self.p4 = "/p4/%s/bin/p4_%s -u %s -p %s" % (self.sdp_instance, self.sdp_instance, self.p4user, self.server)
      self.p4_noport = "/p4/%s/bin/p4_%s -u %s" % (self.sdp_instance, self.sdp_instance, self.p4user)

  def get(self, varname):
    return self.rawconfig.get(self.sdp_instance, varname)

  def login(self, port=os.environ.get('P4PORT')):
    os.system('%s -p %s login -a < /p4/common/config/.p4passwd.p4_%s.admin' % (self.p4_noport, port, self.sdp_instance))

# Change User Description Committed
#9 32424 Russell C. Jackson (Rusty) Remove unnecessary 'from __future__ import annotations' from sdputils.py
#8 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
#7 32388 Russell C. Jackson (Rusty) Updates using Claude.ai to clean up the code, reduce duplication, enhanace security, and use current standards.
#6 31825 Russell C. Jackson (Rusty) Converted to use P4 api and to use only python 3.
#5 25025 Russell C. Jackson (Rusty) Fixed typo in sdputils.py and forced cfgweeks to be an int.
#4 24964 Russell C. Jackson (Rusty) Corrected type and ordering issue.
#3 24675 Russell C. Jackson (Rusty) Fixed bugs in sdputils.py and scripts using it.
Converted to standard 2 space spacing, removed copyright stuff.
#2 24648 Russell C. Jackson (Rusty) Updated p4deleteuser to support deleting workspaces off edge servers.
Added -a to login in sdputils.
#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/sdputils.py
#2 22337 Russell C. Jackson (Rusty) Added +x to file types.
#1 16638 C. Thomas Tyler Routine merge down to dev from main using:
p4 merge -b perforce_software-sdp-dev
//guest/perforce_software/sdp/main/Maintenance/sdputils.py
#1 16581 Robert Cowham Standardised processing and formatting.
Made python 2/3 compatible.
These need automated testing!