Skip to content

Commit

Permalink
[refactor] Rename core/{passwd => pyos}.py
Browse files Browse the repository at this point in the history
pyos.py is like pyutil and pyerror: It's not translated to C++.
  • Loading branch information
Andy Chu committed Jun 21, 2020
1 parent 830214a commit 881409c
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 40 deletions.
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -211,3 +211,14 @@ This is very different than the **developer build** of Oil.
* Subscribe for updates:
* [/r/oilshell on Reddit](https://www.reddit.com/r/oilshell/)
* [@oilshellblog on Twitter](https://twitter.com/oilshellblog)


## Python Files Not Translated to C++

mycpp/
mylib.py # statically typed equivalents of Python's data structures
pylib/ # copied from Python stdlib
core/
py{error,os,util}.py # too complicated to translate
*/*_def.py # abstract definitions
*/*_gen.py # code generators
2 changes: 1 addition & 1 deletion build/mycpp.sh
Expand Up @@ -313,7 +313,7 @@ osh-eval() {
# os_path.py: crashes on path += '/' + b
# pgen2/parse.py: prefer hand-written C

local exclude='_devbuild/|pybase.py|optview.py|option_def.py|id_kind_def.py|match.py|lexer_def.py|/meta.py|pretty.py|process.py|pyerror.py|pyutil.py|os_path.py|path_stat.py|bool_stat.py|passwd.py|builtin_def.py|consts.py|pgen2/parse.py|oil_lang/objects.py|flag_spec.py|flag_def.py|builtin_process'
local exclude='_devbuild/|pybase.py|optview.py|option_def.py|id_kind_def.py|match.py|lexer_def.py|/meta.py|pretty.py|process.py|pyerror.py|pyos.py|pyutil.py|os_path.py|path_stat.py|bool_stat.py|builtin_def.py|consts.py|pgen2/parse.py|oil_lang/objects.py|flag_spec.py|flag_def.py|builtin_process.py'

# TODO: Should we have a --header-file option?
# And then list the classes and functions that have to be exported
Expand Down
19 changes: 3 additions & 16 deletions core/passwd.py → core/pyos.py
@@ -1,21 +1,8 @@
#!/usr/bin/env python2
"""
passwd.py
Wrapper around the 'pwd' module. Its API uses large tuples e.g. Tuple7, which
can't be translated directly by mycpp.
TODO: Rename this to io.py? sys_.py?
Python files that won't be translated:
pylib/ # directly copied from stdlib
mycpp/
mylib.py # data structures
core/
sys_.py
*/*_def.py # abstract definitions with their own code generators
pyos.py -- Wrappers for the operating system.
Like py{error,util}.py, it won't be translated to C++.
"""
from __future__ import print_function

Expand All @@ -33,7 +20,7 @@

def GetMyHomeDir():
# type: () -> Optional[str]
"""Get the user's home directory from the /etc/passwd.
"""Get the user's home directory from the /etc/pyos.
Used by $HOME initialization in osh/state.py. Tilde expansion and readline
initialization use mem.GetVar('HOME').
Expand Down
4 changes: 2 additions & 2 deletions core/shell.py
Expand Up @@ -21,7 +21,7 @@
from core import completion
from core import main_loop
from core import meta
from core import passwd
from core import pyos
from core import process
from core import pure
from core import pyutil
Expand Down Expand Up @@ -601,7 +601,7 @@ def Main(lang, arg_r, environ, login_shell, loader, line_input):
# users may want to ln -s ~/.config/oil/oshrc ~/oshrc or ~/.oshrc.

# https://unix.stackexchange.com/questions/24347/why-do-some-applications-use-config-appname-for-their-config-data-while-other
home_dir = passwd.GetMyHomeDir()
home_dir = pyos.GetMyHomeDir()
assert home_dir is not None
history_filename = os_path.join(home_dir, '.config/oil', 'history_' + lang)

Expand Down
4 changes: 2 additions & 2 deletions core/state.py
Expand Up @@ -21,7 +21,7 @@
from _devbuild.gen import runtime_asdl # for cell
from asdl import runtime
from core import error
from core import passwd
from core import pyos
from core import pyutil
from core.pyerror import e_usage
from core.pyutil import stderr_line
Expand Down Expand Up @@ -618,7 +618,7 @@ def _InitDefaults(mem):
# In bash, this looks like 'linux-gnu', 'linux-musl', etc. Scripts test
# for 'darwin' and 'freebsd' too. They generally don't like at 'gnu' or
# 'musl'. We don't have that info, so just make it 'linux'.
SetGlobalString(mem, 'OSTYPE', passwd.OsType())
SetGlobalString(mem, 'OSTYPE', pyos.OsType())

# For getopts builtin
SetGlobalString(mem, 'OPTIND', '1')
Expand Down
12 changes: 6 additions & 6 deletions cpp/core_passwd.h → cpp/core_pyos.h
@@ -1,12 +1,12 @@
// core_passwd.h: Replacement for core/passwd.py
// core_pyos.h: Replacement for core/pyos.py

#ifndef CORE_PASSWD_H
#define CORE_PASSWD_H
#ifndef CORE_PYOS_H
#define CORE_PYOS_H

#include "mylib.h"
#include "syntax_asdl.h"

namespace passwd {
namespace pyos {

inline Str* GetMyHomeDir() {
assert(0);
Expand Down Expand Up @@ -37,6 +37,6 @@ inline Str* ReadBytesFromTerminal(int fd, int n) {
assert(0);
}

} // namespace passwd
} // namespace pyos

#endif // CORE_PASSWD_H
#endif // CORE_PYOS_H
2 changes: 1 addition & 1 deletion cpp/preamble.h
Expand Up @@ -32,7 +32,7 @@ using id_kind_asdl::Kind_t;
#include "pgen2_parse.h"
#include "qsn_qsn.h"
// added for osh_eval
#include "core_passwd.h"
#include "core_pyos.h"
#include "libc.h"
#include "osh_bool_stat.h"
#include "posix.h"
Expand Down
6 changes: 3 additions & 3 deletions osh/builtin_misc.py
Expand Up @@ -14,7 +14,7 @@
from _devbuild.gen.runtime_asdl import span_e, cmd_value__Argv
from asdl import runtime
from core import error
from core import passwd
from core import pyos
from core.pyerror import e_usage
from core import pyutil # strerror_OS
from core import state
Expand Down Expand Up @@ -47,7 +47,7 @@
class Times(vm._Builtin):
def Run(self, cmd_val):
# type: (cmd_value__Argv) -> int
passwd.PrintTimes()
pyos.PrintTimes()
return 0


Expand Down Expand Up @@ -177,7 +177,7 @@ def Run(self, cmd_val):
status = 0
stdin_fd = self.stdin.fileno()
if self.stdin.isatty(): # set stdin to read in unbuffered mode
s = passwd.ReadBytesFromTerminal(stdin_fd, arg.n)
s = pyos.ReadBytesFromTerminal(stdin_fd, arg.n)
else:
chunks = [] # type: List[str]
n = arg.n
Expand Down
6 changes: 3 additions & 3 deletions osh/cmd_eval.py
Expand Up @@ -67,7 +67,7 @@
from asdl import runtime
from core import error
from core.error import _ControlFlow
from core import passwd # Time(). TODO: rename
from core import pyos # Time(). TODO: rename
from core import state
from core import ui
from core import util
Expand Down Expand Up @@ -1267,9 +1267,9 @@ def _Dispatch(self, node):
# $'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'
# "A trailing newline is added when the format string is displayed."

s_real, s_user, s_sys = passwd.Time()
s_real, s_user, s_sys = pyos.Time()
status = self._Execute(node.pipeline)
e_real, e_user, e_sys = passwd.Time()
e_real, e_user, e_sys = pyos.Time()
# note: mycpp doesn't support %.3f
libc.print_time(e_real - s_real, e_user - s_user, e_sys - s_sys)

Expand Down
4 changes: 2 additions & 2 deletions osh/prompt.py
Expand Up @@ -13,7 +13,7 @@
from asdl import runtime
from core import main_loop
from core import error
from core import passwd
from core import pyos
from core import state
from core import ui
from frontend import consts
Expand Down Expand Up @@ -64,7 +64,7 @@ def Get(self, name):
elif name == 'hostname': # for \h and \H
value = libc.gethostname()
elif name == 'user': # for \u
value = passwd.GetUserName(self._GetEuid()) # recursive call for caching
value = pyos.GetUserName(self._GetEuid()) # recursive call for caching
else:
raise AssertionError(name)

Expand Down
6 changes: 3 additions & 3 deletions osh/word_eval.py
Expand Up @@ -28,7 +28,7 @@
scope_e,
)
from core import error
from core import passwd
from core import pyos
from core import pyutil
from qsn_ import qsn
from core.pyerror import log, e_die
Expand Down Expand Up @@ -420,9 +420,9 @@ def _EvalTildeSub(self, token):
if val.tag_() == value_e.Str:
val = cast(value__Str, UP_val)
return val.s
return passwd.GetMyHomeDir()
return pyos.GetMyHomeDir()

return passwd.GetHomeDir(token)
return pyos.GetHomeDir(token)

def _EvalVarNum(self, var_num):
# type: (int) -> value_t
Expand Down
2 changes: 1 addition & 1 deletion types/osh-eval-manifest.txt
Expand Up @@ -9,9 +9,9 @@
./core/main_loop.py
./core/meta.py
./core/optview.py
./core/passwd.py
./core/pure.py
./core/pyerror.py
./core/pyos.py
./core/pyutil.py
./core/state.py
./core/ui.py
Expand Down

0 comments on commit 881409c

Please sign in to comment.