-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts_runner.py
executable file
·65 lines (53 loc) · 2.33 KB
/
scripts_runner.py
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
61
62
63
64
65
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Contains routines to interact with the file system. Currently, it contains the
following routines:
* find_and_execute_scripts(): given a directory, executes all executable
shell scripts (files with names ending in SH) in that directory and its
subdirectories, then runs them.
When run from the command line, the script runs find_and_execute_scripts() in
the current directory.
This program comes with ABSOLUTELY NO WARRANTY. Use at your own risk.
scripts_runner.py is copyright 2015-16 by Patrick Mooney. It is free software,
and you are welcome to redistribute it under certain conditions, according to
the GNU general public license, either version 3 or (at your own option) any
later version. See the file LICENSE.md for details.
"""
import os
import subprocess
import sys
debugging = False
def find_and_execute_scripts(path='.'):
"""Find executable *SH files in the current directory and its subdirectories,
then execute them. Specify the path to walk as PATH; it defaults to the
current directory.
"""
for (dirname, subshere, fileshere) in os.walk(path, topdown=True):
subshere.sort()
print('Looking for scripts in %s' % dirname)
file_list = sorted([ which_script for which_script in fileshere if which_script.endswith('SH') ])
# file_list = [ which_script for which_script in file_list if os.access(which_script, os.X_OK) ]
for which_script in file_list:
try:
olddir = os.getcwd()
os.chdir(dirname)
print('\n\n Running script: %s' % os.path.abspath(which_script))
try:
subprocess.call('nice -n 10 ./' + which_script, shell=True)
os.system('chmod a-x -R %s' % which_script)
except BaseException as e:
print('Unable to execute script: the system said: ' + str(e))
finally:
os.chdir(olddir)
if __name__ == "__main__":
if len(sys.argv) > 1:
if sys.argv[1] == "--help":
print(__doc__)
sys.exit(0)
else:
for whichdir in sys.argv[1:]:
find_and_execute_scripts(path=whichdir)
else:
if debugging: os.chdir('/home/patrick/NeedProcessing/Pictures/PanoramaGroups')
find_and_execute_scripts()