Commit b0e8553c authored by Kyoung-chan Lee's avatar Kyoung-chan Lee Committed by Dylan Trotter

Added os.chdir, os.getcwd, os.path.abspath. (#68)

* Implement os.chdir, os.getcwd.
* Implement os.path.abspath. Add _AssertEqual() to check not only values but also types.
parent c6561259
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
from os import path from os import path
import stat as stat_module import stat as stat_module
import sys import sys
from __go__.os import Chmod, Environ, Remove, Stat from __go__.os import Chdir, Chmod, Environ, Getwd, Remove, Stat
from __go__.path.filepath import Separator from __go__.path.filepath import Separator
from __go__.grumpy import NewFileFromFD from __go__.grumpy import NewFileFromFD
from __go__.syscall import Close, SYS_FCNTL, Syscall, F_GETFD from __go__.syscall import Close, SYS_FCNTL, Syscall, F_GETFD
...@@ -32,6 +32,12 @@ for var in Environ(): ...@@ -32,6 +32,12 @@ for var in Environ():
environ[k] = v environ[k] = v
def chdir(path):
err = Chdir(path)
if err:
raise OSError(err.Error())
def chmod(filepath, mode): def chmod(filepath, mode):
# TODO: Support mode flags other than perms. # TODO: Support mode flags other than perms.
err = Chmod(filepath, stat(filepath).st_mode & ~0o777 | mode & 0o777) err = Chmod(filepath, stat(filepath).st_mode & ~0o777 | mode & 0o777)
...@@ -53,6 +59,13 @@ def fdopen(fd, mode='r'): # pylint: disable=unused-argument ...@@ -53,6 +59,13 @@ def fdopen(fd, mode='r'): # pylint: disable=unused-argument
return NewFileFromFD(fd) return NewFileFromFD(fd)
def getcwd():
dir, err = Getwd()
if err:
raise OSError(err.Error())
return dir
def remove(filepath): def remove(filepath):
if stat_module.S_ISDIR(stat(filepath).st_mode): if stat_module.S_ISDIR(stat(filepath).st_mode):
raise OSError('Operation not permitted: ' + filepath) raise OSError('Operation not permitted: ' + filepath)
......
...@@ -15,7 +15,18 @@ ...@@ -15,7 +15,18 @@
""""Utilities for manipulating and inspecting OS paths.""" """"Utilities for manipulating and inspecting OS paths."""
from __go__.os import Stat from __go__.os import Stat
from __go__.path.filepath import Clean, Dir as dirname, IsAbs as isabs, Join # pylint: disable=g-multiple-import,unused-import from __go__.path.filepath import Abs, Clean, Dir as dirname, IsAbs as isabs, Join # pylint: disable=g-multiple-import,unused-import
def abspath(path):
result, err = Abs(path)
if err:
raise OSError(err.Error())
if isinstance(path, unicode):
# Grumpy compiler encoded the string into utf-8, so the result can be
# decoded using utf-8.
return unicode(result, 'utf-8')
return result
def exists(path): def exists(path):
...@@ -60,7 +71,5 @@ def join(*paths): ...@@ -60,7 +71,5 @@ def join(*paths):
def normpath(path): def normpath(path):
result = Clean(path) result = Clean(path)
if isinstance(path, unicode): if isinstance(path, unicode):
# Grumpy compiler encoded the string into utf-8, so the result can be
# decoded using utf-8.
return unicode(result, 'utf-8') return unicode(result, 'utf-8')
return result return result
...@@ -22,6 +22,19 @@ import weetest ...@@ -22,6 +22,19 @@ import weetest
import tempfile import tempfile
def _AssertEqual(a, b):
assert a == b
assert type(a) is type(b)
def TestAbspath():
_AssertEqual(path.abspath('/a/b/c'), '/a/b/c')
_AssertEqual(path.abspath(u'/a/b/c'), u'/a/b/c')
_AssertEqual(path.abspath('/a/b/c/'), '/a/b/c')
_AssertEqual(path.abspath(u'/a/b/c/'), u'/a/b/c')
_AssertEqual(path.abspath('a/b/c'), path.normpath(os.getcwd() + '/a/b/c'))
def TestDirname(): def TestDirname():
assert path.dirname('/a/b/c') == '/a/b' assert path.dirname('/a/b/c') == '/a/b'
assert path.dirname('/a/b/c/') == '/a/b/c' assert path.dirname('/a/b/c/') == '/a/b/c'
...@@ -85,16 +98,16 @@ def TestJoin(): ...@@ -85,16 +98,16 @@ def TestJoin():
def TestNormPath(): def TestNormPath():
assert path.normpath('abc/') == 'abc' _AssertEqual(path.normpath('abc/'), 'abc')
assert path.normpath('/a//b') == '/a/b' _AssertEqual(path.normpath('/a//b'), '/a/b')
assert path.normpath('abc/../123') == '123' _AssertEqual(path.normpath('abc/../123'), '123')
assert path.normpath('../abc/123') == '../abc/123' _AssertEqual(path.normpath('../abc/123'), '../abc/123')
assert path.normpath('x/y/./z') == 'x/y/z' _AssertEqual(path.normpath('x/y/./z'), 'x/y/z')
assert path.normpath(u'abc/') == u'abc' _AssertEqual(path.normpath(u'abc/'), u'abc')
assert path.normpath(u'/a//b') == u'/a/b' _AssertEqual(path.normpath(u'/a//b'), u'/a/b')
assert path.normpath(u'abc/../123') == u'123' _AssertEqual(path.normpath(u'abc/../123'), u'123')
assert path.normpath(u'../abc/123') == u'../abc/123' _AssertEqual(path.normpath(u'../abc/123'), u'../abc/123')
assert path.normpath(u'x/y/./z') == u'x/y/z' _AssertEqual(path.normpath(u'x/y/./z'), u'x/y/z')
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -20,6 +20,20 @@ import tempfile ...@@ -20,6 +20,20 @@ import tempfile
import weetest import weetest
def TestChdirAndGetCwd():
path = os.getcwd()
os.chdir('.')
assert os.getcwd() == path
tempdir = tempfile.mkdtemp()
try:
os.chdir(tempdir)
assert tempdir in os.getcwd()
finally:
os.chdir(path)
os.rmdir(tempdir)
assert os.getcwd() == path
def TestChmod(): def TestChmod():
fd, path = tempfile.mkstemp() fd, path = tempfile.mkstemp()
os.close(fd) os.close(fd)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment