-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy_looper2.py
More file actions
executable file
·60 lines (50 loc) · 2.22 KB
/
py_looper2.py
File metadata and controls
executable file
·60 lines (50 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
# python_template1.sh
# 27 May 2025 | datainscience.com
#
# This file is the bare minimum for a readable and maintainable shell script
# The key elements are:
# 1) A usage/help function
# 2) Command line arguments - miniumon of "-h" for "help/usage"
# 3) Live/Dry-run flag
# - I like the default to be non-live so I can review the commnads
# 4) Input for 'count' how many times to run the command
import argparse
from subprocess import Popen, PIPE
# Create the parser
# https://docs.python.org/3/library/argparse.html
parser = argparse.ArgumentParser(prog="looper", description="Looper run a command some number of times")
# Add the arguments - argparse builds help automatically
# We'll dd two arguments here
parser.add_argument( "-l", "--live",
action="store_true",
required=False,
default=False,
help="Acturally run commands. Default is to just print the command lines")
parser.add_argument( "-c", "--count",
type=int,
required=True,
help="How many times to run the command")
# Parse the arguments
args = parser.parse_args()
cmd_base = "./test_script.py"
# Python needs a "range()" function for 'for loops'
for c in range(0,args.count):
print("++===============================++")
# Build up the command arument list, the Popen command expects
# the command to be in the form of a list, with one arg per list
# element
cmd = [f"{cmd_base}", "--arg1", f"some_text_{c}", "--failpct", "30"]
# Always print the ccommand that will be run - the syntax " ".join(cmd)
# translates to - take the elements of the list cmd and joint them to a string
# separted by spaces
print(f"Running: {" ".join(cmd)}")
# If we're in 'live mode' run the command
if args.live:
proc = Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=None)
std_out, std_err = proc.communicate()
if proc.returncode != 0: # The command failed!
print(f"looper: std_err: {std_err.decode('utf-8')}")
print(f"looper: std_out: {std_out.decode('utf-8')}")
print(f"looper rc: {proc.returncode }")
print("--===============================--\n")