Sys.hx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. public static inline function time():Float {
  30. return Time.time();
  31. }
  32. public static function exit(code:Int):Void {
  33. python.lib.Sys.exit(code);
  34. }
  35. public static inline function print(v:Dynamic):Void {
  36. python.Lib.print(v);
  37. }
  38. public static inline function println(v:Dynamic):Void {
  39. python.Lib.println(v);
  40. }
  41. public static function args():Array<String> {
  42. var argv = python.lib.Sys.argv;
  43. return argv.slice(1);
  44. }
  45. public static function getEnv(s:String):String {
  46. return Os.environ.get(s, null);
  47. }
  48. public static function putEnv(s:String, v:Null<String>):Void {
  49. if (v == null) {
  50. try {
  51. Os.environ.remove(s);
  52. } catch(e:python.Exceptions.KeyError) {
  53. // the variable didn't exist
  54. }
  55. return;
  56. }
  57. Os.environ.set(s, v);
  58. }
  59. public static function environment():Map<String, String> {
  60. final environ = new StringMap();
  61. final env = Os.environ;
  62. for (key in env.keys()) {
  63. environ.set(key, env.get(key, null));
  64. }
  65. return environ;
  66. }
  67. public static function sleep(seconds:Float):Void {
  68. python.lib.Time.sleep(seconds);
  69. }
  70. public static function setTimeLocale(loc:String):Bool {
  71. return false;
  72. }
  73. public static function getCwd():String {
  74. return haxe.io.Path.addTrailingSlash(python.lib.Os.getcwd());
  75. }
  76. public static function setCwd(s:String):Void {
  77. python.lib.Os.chdir(s);
  78. }
  79. public static function systemName():String {
  80. return switch (python.lib.Sys.platform) {
  81. case var x if (StringTools.startsWith(x, "linux")):
  82. "Linux";
  83. case "darwin": "Mac";
  84. case "win32" | "cygwin": "Windows";
  85. case _:
  86. throw "not supported platform";
  87. }
  88. }
  89. public static function command(cmd:String, ?args:Array<String>):Int {
  90. return if (args == null)
  91. python.lib.Subprocess.call(cmd, {shell: true});
  92. else
  93. python.lib.Subprocess.call([cmd].concat(args));
  94. }
  95. public static inline function cpuTime():Float {
  96. return python.lib.Timeit.default_timer();
  97. }
  98. @:deprecated("Use programPath instead") public static function executablePath():String {
  99. return python.lib.Sys.argv[0];
  100. }
  101. // It has to be initialized before any call to Sys.setCwd()...
  102. static var _programPath = sys.FileSystem.fullPath(python.lib.Inspect.getsourcefile(Sys));
  103. public static function programPath():String {
  104. return _programPath;
  105. }
  106. public static function getChar(echo:Bool):Int {
  107. var ch = switch (systemName()) {
  108. case "Linux" | "Mac":
  109. var fd = python.lib.Sys.stdin.fileno();
  110. var old = python.lib.Termios.tcgetattr(fd);
  111. var restore = python.lib.Termios.tcsetattr.bind(fd, python.lib.Termios.TCSADRAIN, old);
  112. try {
  113. python.lib.Tty.setraw(fd);
  114. var x = python.lib.Sys.stdin.read(1);
  115. restore();
  116. x.charCodeAt(0);
  117. } catch (e:Dynamic) {
  118. restore();
  119. throw e;
  120. }
  121. case "Windows":
  122. // python.lib.Msvcrt.getch().decode("utf-8").charCodeAt(0);
  123. python.lib.Msvcrt.getwch().charCodeAt(0);
  124. case var x:
  125. throw "platform " + x + " not supported";
  126. }
  127. if (echo) {
  128. python.Lib.print(String.fromCharCode(ch));
  129. }
  130. return ch;
  131. }
  132. public static function stdin():haxe.io.Input {
  133. return python.io.IoTools.createFileInputFromText(python.lib.Sys.stdin);
  134. }
  135. public static function stdout():haxe.io.Output {
  136. return python.io.IoTools.createFileOutputFromText(python.lib.Sys.stdout);
  137. }
  138. public static function stderr():haxe.io.Output {
  139. return python.io.IoTools.createFileOutputFromText(python.lib.Sys.stderr);
  140. }
  141. }