2019-09-17 08:55:59 +00:00
|
|
|
import json, ntpath, os, sys
|
2017-02-21 13:33:03 +00:00
|
|
|
|
2017-02-18 18:00:40 +00:00
|
|
|
class PBinCLIException(Exception):
|
|
|
|
pass
|
|
|
|
|
2017-02-21 13:33:03 +00:00
|
|
|
|
2019-09-17 08:55:59 +00:00
|
|
|
def PBinCLIError(message):
|
|
|
|
print("PBinCLI Error: {}".format(message), file=sys.stderr)
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
|
2019-06-01 15:07:05 +00:00
|
|
|
def path_leaf(path):
|
|
|
|
head, tail = ntpath.split(path)
|
|
|
|
return tail or ntpath.basename(head)
|
|
|
|
|
|
|
|
|
2017-02-18 18:00:40 +00:00
|
|
|
def check_readable(f):
|
2018-02-12 14:28:18 +00:00
|
|
|
# Checks if path exists and readable
|
2017-02-18 18:00:40 +00:00
|
|
|
if not os.path.exists(f) or not os.access(f, os.R_OK):
|
2019-09-17 08:55:59 +00:00
|
|
|
PBinCLIError("Error accessing path: {}".format(f))
|
2017-02-18 18:00:40 +00:00
|
|
|
|
2017-02-21 13:33:03 +00:00
|
|
|
|
2017-02-18 18:00:40 +00:00
|
|
|
def check_writable(f):
|
2018-02-12 14:28:18 +00:00
|
|
|
# Checks if path is writable
|
2017-02-18 18:00:40 +00:00
|
|
|
if not os.access(os.path.dirname(f) or ".", os.W_OK):
|
2019-09-17 08:55:59 +00:00
|
|
|
PBinCLIError("Path is not writable: {}".format(f))
|
2017-02-20 12:13:42 +00:00
|
|
|
|
2017-02-21 13:33:03 +00:00
|
|
|
|
2019-06-02 14:04:38 +00:00
|
|
|
def json_encode(s):
|
|
|
|
return json.dumps(s, separators=(',',':')).encode()
|
2019-09-09 14:23:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def validate_url(s):
|
|
|
|
if not s.endswith('/'):
|
|
|
|
s = s + "/"
|
|
|
|
return s
|