Sys.hx 4.4 KB

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