unload_clients_with_delete.py #6

  • //
  • guest/
  • russell_jackson/
  • sdp/
  • Maintenance/
  • unload_clients_with_delete.py
  • View
  • Commits
  • Open Download .zip Download (4 KB)
#!/usr/bin/env python3
"""
Unload or delete Perforce clients that have been inactive for a configured
number of weeks, using the P4Python API.  Clients with files opened for
exclusive locks are deleted rather than unloaded.

"""

import argparse
import logging
import sys
import time
from typing import Iterable, List, Set

from P4 import P4, P4Exception  # type: ignore

import sdputils  # type: ignore


def parse_args(argv: Iterable[str]) -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description="Unload or delete inactive Perforce clients via P4Python",
    )
    parser.add_argument(
        "instance",
        nargs="?",
        default="1",
        help="SDP instance identifier (default: '1')",
    )
    return parser.parse_args(list(argv))


def connect_p4(instance: str) -> P4:
    utils = sdputils.SDPUtils(instance)
    p4 = P4()
    p4.port = utils.server
    try:
        p4.user = utils.p4user  # type: ignore[attr-defined]
    except AttributeError:
        pass
    passwd_path = f"/p4/common/config/.p4passwd.p4_{instance}.admin"
    try:
        with open(passwd_path) as pf:
            p4.password = pf.read().strip()
    except FileNotFoundError:
        pass
    p4.connect()
    if p4.password:
        p4.run_login()
    return p4


def get_clients_with_exclusive_locks(p4: P4) -> Set[str]:
    """Return a set of clients that have files opened with exclusive locks."""
    clients: Set[str] = set()
    try:
        opened = p4.run("opened", "-a")
    except P4Exception as exc:
        logging.error("Failed to list opened files: %s", exc)
        return clients
    for rec in opened:
        filetype = rec.get("type") or rec.get("Type")
        client = rec.get("client") or rec.get("Client")
        if client and filetype and "+l" in filetype:
            clients.add(client)
    return clients


def get_inactive_clients(p4: P4, weeks: int) -> List[str]:
    now = int(time.time())
    threshold = weeks * 7 * 24 * 60 * 60
    try:
        clients = p4.run("clients", "-a")
    except P4Exception as exc:
        logging.error("Failed to list clients: %s", exc)
        return []
    result: List[str] = []
    for rec in clients:
        name = rec.get("client") or rec.get("Client")
        if not name or "swarm-project" in name.lower():
            continue
        access_str = rec.get("Access") or rec.get("access")
        if not access_str:
            continue
        try:
            access = int(access_str)
        except (ValueError, TypeError):
            continue
        if now - access >= threshold:
            result.append(name)
    return result


def process_clients(p4: P4, inactive: List[str], exclusives: Set[str]) -> None:
    for client in inactive:
        try:
            if client in exclusives:
                # Delete clients with exclusive locks (unload will fail)
                p4.run("client", "-f", "-Fd", client)
                logging.info("Deleted client %s", client)
            else:
                p4.run("unload", "-f", "-L", "-z", "-c", client)
                logging.info("Unloaded client %s", client)
        except P4Exception as exc:
            logging.error("Failed to process client %s: %s", client, exc)


def main(argv: Iterable[str] | None = None) -> int:
    args = parse_args(argv or sys.argv[1:])
    logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
    try:
        p4 = connect_p4(args.instance)
    except P4Exception:
        return 1
    utils = sdputils.SDPUtils(args.instance)
    weeks = int(utils.get("weeks"))
    inactive_clients = get_inactive_clients(p4, weeks)
    clients_with_excl = get_clients_with_exclusive_locks(p4)
    process_clients(p4, inactive_clients, clients_with_excl)
    return 0


if __name__ == "__main__":
    sys.exit(main())
# Change User Description Committed
#9 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
#8 32388 Russell C. Jackson (Rusty) Updates using Claude.ai to clean up the code, reduce duplication, enhanace security, and use current standards.
#7 31833 Russell C. Jackson (Rusty) A couple of additional fixes.
#6 31825 Russell C. Jackson (Rusty) Converted to use P4 api and to use only python 3.
#5 30843 Russell C. Jackson (Rusty) Updated to use parameters for the user and client commands.
#4 25066 Russell C. Jackson (Rusty) Added check for swarm clients to skip unloading them.
#3 25025 Russell C. Jackson (Rusty) Fixed typo in sdputils.py and forced cfgweeks to be an int.
#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/unload_clients_with_delete.py
#5 22006 C. Thomas Tyler Removed hard-coding assumption of an Instance named 1.
 Now, one of
two things must be true: P4BIN must be defined, as it would be if the
SDP envrionment is loaded in the usual way (source p4_vars N), or else
the 'p4' executable must be found in the path.

Avoided removal of Swarm workspaces.
#4 16029 C. Thomas Tyler Routine merge to dev from main using:
p4 merge -b perforce_software-sdp-dev
#3 13906 C. Thomas Tyler Normalized P4INSTANCE to SDP_INSTANCE to get Unix/Windows
implementations in sync.

Reasons:
1. Things that interact with SDP in both Unix and Windows
environments shoudn't have to account for this obscure
SDP difference between Unix and Windows.  (I came across
this doing CBD work).

2. The Windows and Unix scripts have different variable
names for defining the same concept, the SDP instance.
Unix uses P4INSTANCE, while Windows uses SDP_INSTANCE.

3. This instance tag, a data set identifier, is an SDP concept.
I prefer the SDP_INSTANCE name over P4INSTANCE, so I prpose
to normalize to SDP_INSTANCE.

4. The P4INSTANCE name makes it look like a setting that might be
recognized by the p4d itself, which it is not.  (There are other
such things such as P4SERVER that could perhaps be renamed as
a separate task; but I'm not sure we want to totally disallow
the P4 prefix for variable names. It looks too right to be wrong
in same cases, like P4BIN and P4DBIN.  That's a discussion for
another day, outside the scope of this task).

Meanwhile:
* Fixed a bug in the Windows 2013.3 upgrade script that
was referencing undefined P4INSTANCE, as the Windows
environment defined only SDP_INSTANCE.

* Had P4INSTANCE been removed completely, this change would
likely cause trouble for users doing updates for existing
SDP installations.  So, though it involves slight technical debt,
I opted to keep a redundant definition of P4INSTANCE
in p4_vars.template, with comments indicating SDP_INSTANCE should be
used in favor of P4INSTANCE, with a warning that P4INSTANCE
may go away in a future release.  This should avoid unnecessary
upgrade pain.

* In mkdirs.sh, the varialbe name was INSTANCE rather than
SDP_INSTANCE.  I changed that as well.  That required manual
change rather than sub/replace to avoid corrupting other similar
varialbe names (e.g.  MASTERINSTANCE).

This is a trivial change technically (a substitute/replace, plus
tweaks in p4_vars.template), but impacts many files.
#2 11477 Russell C. Jackson (Rusty) Updated to use /usr/bin/env python

Added workshop header.

 Changed cfg to config.
#1 11464 Russell C. Jackson (Rusty) Current maintenance scripts.