def test_program_not_found(self): alphabet = 'abcdefghijklmnopqrstuvwxyz' prog_name = ''.join(random.choice(alphabet) for i in range(20)) got_exception = False try: perform._(prog_name) except perform.ProgramNotFoundException: got_exception = True self.assertTrue(got_exception)
#!/bin/bash # Performs an incremental backup to a remote client, and commits # the changes to a git repository. Depends on commit_directory.sh. # # Usage ./backup_to_remote directory_to_backup hostname host_location # Usage ./backup_to_remote ~/documents ben-vps /storage/documents-backup import perform import sys import os from_dir = sys.argv[1] hostname = sys.argv[2] to_dir = sys.argv[3] path = os.path.dirname(os.path.realpath(__file__)) run_script = "cat > /tmp/backup.sh ; chmod 755 /tmp/backup.sh ; /tmp/backup.sh %(to_dir) ; rm /tmp/backup.sh" dir = "" perform.rsync("-az", from_dir, "{}:{}".format(hostname, to_dir)) perform.sync() perform._("cat %(path)/commit_directory.py | ssh %(hostname) %(run_script)
import perform battery_threshold = 200 if len(sys.argv) > 1: battery_threshold = int(sys.argv[1]) # acpi output looks like: # Battery 0: Unknown, 5% # Battery 1: Discharging, 1%, 00:01:15 remaining total_battery_percent = 0 charging = True for line in perform.acpi().split("\n"): battery_percent = int(re.findall("(\d+)%", line)[0]) total_battery_percent += battery_percent if "discharging" in line.lower(): charging = False total_battery_percent //= 2 if total_battery_percent < battery_threshold and not charging: msg = "Low battery: {}%".format(total_battery_percent) perform._("notify-send", msg) perform._("i3-nagbar", "-m", msg, nr=True) nag_pid = perform._("ps aux | grep i3-nagbar | grep -v grep | awk '{ print $2 }'", shell=True) time.sleep(45) perform.kill(nag_pid)
def test_return_object_underscore(self): self.assertEqual(perform._("echo", "hello", "world", return_object=True).stdout, "hello world") self.assertEqual(perform._("echo", "-E", r"hello\thello", return_object=True).stdout, r"hello\thello")
def test_underscore_shell(self): self.assertEqual(perform._("echo 'hello\nworld' | tac", shell=True), 'world\nhello')
def test_underscore(self): self.assertEqual("Hello all", perform._("echo", 'Hello all'))