2014-05-14 14:52:37 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
|
2015-12-09 20:29:03 +00:00
|
|
|
sep = "/"
|
|
|
|
|
2021-05-27 05:50:04 +00:00
|
|
|
|
2014-05-09 20:34:23 +00:00
|
|
|
def normcase(s):
|
|
|
|
return s
|
2014-05-14 14:46:23 +00:00
|
|
|
|
2021-05-27 05:50:04 +00:00
|
|
|
|
2014-05-14 14:46:23 +00:00
|
|
|
def normpath(s):
|
|
|
|
return s
|
|
|
|
|
2021-05-27 05:50:04 +00:00
|
|
|
|
2014-05-14 18:16:46 +00:00
|
|
|
def abspath(s):
|
2015-06-28 21:05:01 +00:00
|
|
|
if s[0] != "/":
|
|
|
|
return os.getcwd() + "/" + s
|
|
|
|
return s
|
2014-05-14 18:16:46 +00:00
|
|
|
|
2021-05-27 05:50:04 +00:00
|
|
|
|
2014-05-14 14:46:23 +00:00
|
|
|
def join(*args):
|
|
|
|
# TODO: this is non-compliant
|
2014-05-25 22:59:41 +00:00
|
|
|
if type(args[0]) is bytes:
|
|
|
|
return b"/".join(args)
|
|
|
|
else:
|
|
|
|
return "/".join(args)
|
2014-05-14 14:46:23 +00:00
|
|
|
|
2021-05-27 05:50:04 +00:00
|
|
|
|
2014-05-14 14:46:23 +00:00
|
|
|
def split(path):
|
|
|
|
if path == "":
|
|
|
|
return ("", "")
|
|
|
|
r = path.rsplit("/", 1)
|
|
|
|
if len(r) == 1:
|
|
|
|
return ("", path)
|
2021-05-27 05:50:04 +00:00
|
|
|
head = r[0] # .rstrip("/")
|
2014-05-14 14:46:23 +00:00
|
|
|
if not head:
|
|
|
|
head = "/"
|
|
|
|
return (head, r[1])
|
2014-05-14 14:52:37 +00:00
|
|
|
|
2021-05-27 05:50:04 +00:00
|
|
|
|
2015-01-27 02:25:20 +00:00
|
|
|
def dirname(path):
|
|
|
|
return split(path)[0]
|
|
|
|
|
2021-05-27 05:50:04 +00:00
|
|
|
|
2015-01-27 02:25:20 +00:00
|
|
|
def basename(path):
|
|
|
|
return split(path)[1]
|
|
|
|
|
2021-05-27 05:50:04 +00:00
|
|
|
|
2014-05-14 14:52:37 +00:00
|
|
|
def exists(path):
|
2022-09-30 07:50:21 +00:00
|
|
|
try:
|
|
|
|
os.stat(path)
|
|
|
|
return True
|
|
|
|
except OSError:
|
|
|
|
return False
|
2014-05-14 17:04:44 +00:00
|
|
|
|
2021-05-27 05:50:04 +00:00
|
|
|
|
2014-05-14 17:04:44 +00:00
|
|
|
# TODO
|
|
|
|
lexists = exists
|
2014-05-14 18:16:13 +00:00
|
|
|
|
2021-05-27 05:50:04 +00:00
|
|
|
|
2014-05-14 18:16:13 +00:00
|
|
|
def isdir(path):
|
|
|
|
try:
|
|
|
|
mode = os.stat(path)[0]
|
2022-09-30 07:50:21 +00:00
|
|
|
return mode & 0o040000
|
2014-05-14 18:16:13 +00:00
|
|
|
except OSError:
|
|
|
|
return False
|
2015-05-07 22:01:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def expanduser(s):
|
|
|
|
if s == "~" or s.startswith("~/"):
|
|
|
|
h = os.getenv("HOME")
|
|
|
|
return h + s[1:]
|
|
|
|
if s[0] == "~":
|
|
|
|
# Sorry folks, follow conventions
|
|
|
|
return "/home/" + s[1:]
|
|
|
|
return s
|