Sys.hx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. import python.lib.Time;
  23. import python.lib.Os;
  24. import sys.io.FileInput;
  25. import sys.io.FileOutput;
  26. import haxe.ds.StringMap;
  27. @:coreApi
  28. class Sys {
  29. static var environ(get,default):StringMap<String>;
  30. static function get_environ():StringMap<String> {
  31. return switch environ {
  32. case null:
  33. var environ = new StringMap();
  34. var env = Os.environ;
  35. for (key in env.keys()) {
  36. environ.set(key, env.get(key, null));
  37. }
  38. Sys.environ = environ;
  39. case env: env;
  40. }
  41. }
  42. public static inline function time():Float {
  43. return Time.time();
  44. }
  45. public static function exit(code:Int):Void {
  46. python.lib.Sys.exit(code);
  47. }
  48. public static inline function print(v:Dynamic):Void {
  49. python.Lib.print(v);
  50. }
  51. public static inline function println(v:Dynamic):Void {
  52. python.Lib.println(v);
  53. }
  54. public static function args():Array<String> {
  55. var argv = python.lib.Sys.argv;
  56. return argv.slice(1);
  57. }
  58. public static function getEnv(s:String):String {
  59. return environ.get(s);
  60. }
  61. public static function putEnv(s:String, v:String):Void {
  62. python.lib.Os.putenv(s, v);
  63. environ.set(s, v);
  64. }
  65. public static function environment():Map<String, String> {
  66. return environ;
  67. }
  68. public static function sleep(seconds:Float):Void {
  69. python.lib.Time.sleep(seconds);
  70. }
  71. public static function setTimeLocale(loc:String):Bool {
  72. return false;
  73. }
  74. public static function getCwd():String {
  75. return python.lib.Os.getcwd();
  76. }
  77. public static function setCwd(s:String):Void {
  78. python.lib.Os.chdir(s);
  79. }
  80. public static function systemName():String {
  81. return switch (python.lib.Sys.platform) {
  82. case var x if (StringTools.startsWith(x, "linux")):
  83. "Linux";
  84. case "darwin": "Mac";
  85. case "win32" | "cygwin": "Windows";
  86. case _:
  87. throw "not supported platform";
  88. }
  89. }
  90. public static function command(cmd:String, ?args:Array<String>):Int {
  91. return if (args == null)
  92. python.lib.Subprocess.call(cmd, {shell: true});
  93. else
  94. python.lib.Subprocess.call([cmd].concat(args));
  95. }
  96. public static inline function cpuTime():Float {
  97. return python.lib.Timeit.default_timer();
  98. }
  99. @:deprecated("Use programPath instead") public static function executablePath():String {
  100. return python.lib.Sys.argv[0];
  101. }
  102. // It has to be initialized before any call to Sys.setCwd()...
  103. static var _programPath = sys.FileSystem.fullPath(python.lib.Inspect.getsourcefile(Sys));
  104. public static function programPath():String {
  105. return _programPath;
  106. }
  107. public static function getChar(echo:Bool):Int {
  108. var ch = switch (systemName()) {
  109. case "Linux" | "Mac":
  110. var fd = python.lib.Sys.stdin.fileno();
  111. var old = python.lib.Termios.tcgetattr(fd);
  112. var restore = python.lib.Termios.tcsetattr.bind(fd, python.lib.Termios.TCSADRAIN, old);
  113. try {
  114. python.lib.Tty.setraw(fd);
  115. var x = python.lib.Sys.stdin.read(1);
  116. restore();
  117. x.charCodeAt(0);
  118. } catch (e:Dynamic) {
  119. restore();
  120. throw e;
  121. }
  122. case "Windows":
  123. // python.lib.Msvcrt.getch().decode("utf-8").charCodeAt(0);
  124. python.lib.Msvcrt.getwch().charCodeAt(0);
  125. case var x:
  126. throw "platform " + x + " not supported";
  127. }
  128. if (echo) {
  129. python.Lib.print(String.fromCharCode(ch));
  130. }
  131. return ch;
  132. }
  133. public static function stdin():haxe.io.Input {
  134. return python.io.IoTools.createFileInputFromText(python.lib.Sys.stdin);
  135. }
  136. public static function stdout():haxe.io.Output {
  137. return python.io.IoTools.createFileOutputFromText(python.lib.Sys.stdout);
  138. }
  139. public static function stderr():haxe.io.Output {
  140. return python.io.IoTools.createFileOutputFromText(python.lib.Sys.stderr);
  141. }
  142. }