77 lines
1.6 KiB
Python
77 lines
1.6 KiB
Python
import getopt
|
|
import sys
|
|
from core.sheerka import Sheerka
|
|
import logging
|
|
|
|
|
|
def usage():
|
|
print("Sheerka v0.1\n")
|
|
print("usage:")
|
|
print(sys.argv[0] + "[-hd] command ")
|
|
|
|
|
|
def sysarg_to_string(argv):
|
|
"""
|
|
Transform a list of strings into a single string
|
|
Add quotes if needed
|
|
:return:
|
|
"""
|
|
if argv is None or not argv:
|
|
return ""
|
|
|
|
result = ""
|
|
first = True
|
|
for s in argv:
|
|
if not first:
|
|
result += " "
|
|
|
|
result += '"' + s + '"' if " " in s else s
|
|
first = False
|
|
|
|
if result[0] in ('"', "'"):
|
|
result = result[1:-1] # strip quotes
|
|
return result
|
|
|
|
|
|
def init_logging(debug):
|
|
if debug:
|
|
log_format = "%(asctime)s %(name)s [%(levelname)s] %(message)s"
|
|
log_level = logging.DEBUG
|
|
else:
|
|
log_format = "%(message)s"
|
|
log_level = logging.INFO
|
|
|
|
logging.basicConfig(format=log_format, level=log_level)
|
|
|
|
|
|
def main(argv):
|
|
try:
|
|
opts, args = getopt.getopt(argv, "hd", ["help", "debug"])
|
|
debug = False
|
|
for o, a in opts:
|
|
if o in ('-h', "--help"):
|
|
usage()
|
|
return True
|
|
if o in ('-d', "--debug"):
|
|
debug = True
|
|
|
|
# init_logging(debug)
|
|
sheerka = Sheerka(debug=debug)
|
|
sheerka.initialize()
|
|
|
|
_in = sysarg_to_string(args)
|
|
result = sheerka.eval(_in)
|
|
|
|
for res in result:
|
|
logging.info(res)
|
|
|
|
return result[-1].status if len(result) > 0 else 1
|
|
except getopt.GetoptError:
|
|
usage()
|
|
sys.exit(2)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
res = main(sys.argv[1:])
|
|
exit(0 if res else 1)
|