PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Backend/Modules/dotnet.cs

https://bitbucket.org/AdamMil/boaold
C# | 181 lines | 130 code | 31 blank | 20 comment | 14 complexity | e7f147b8874fbd1e147fea497ad9764d MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. Boa is the reference implementation for a language similar to Python,
  3. also called Boa. This implementation is both interpreted and compiled,
  4. targeting the Microsoft .NET Framework.
  5. http://www.adammil.net/
  6. Copyright (C) 2004-2005 Adam Milazzo
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License
  9. as published by the Free Software Foundation; either version 2
  10. of the License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. using System;
  20. using System.Collections;
  21. using System.Diagnostics;
  22. using System.IO;
  23. using System.Reflection;
  24. using Boa.AST;
  25. using Boa.Runtime;
  26. namespace Boa.Modules
  27. {
  28. [BoaType("module")]
  29. public sealed class dotnet
  30. { dotnet() { }
  31. public static string __repr__() { return "<module 'dotnet' (built-in)>"; }
  32. public static string __str__() { return __repr__(); }
  33. public static bool access(string path, int mode)
  34. { throw new NotImplementedException();
  35. }
  36. public static void chdir(string path)
  37. { if(path==null || path=="") throw Ops.ValueError("chdir(): path cannot be null or empty");
  38. try { Environment.CurrentDirectory = path; }
  39. catch(FileNotFoundException) { throw NotFound(path); }
  40. catch(DirectoryNotFoundException) { throw NotFound(path); }
  41. catch(IOException e) { throw Ops.OSError(e.Message); }
  42. }
  43. public static void chmod(string path, int mode)
  44. { throw new NotImplementedException();
  45. }
  46. public static string getcwd() { return Environment.CurrentDirectory; }
  47. public static string getcwdu() { return Environment.CurrentDirectory; }
  48. public static int getpid() { return Process.GetCurrentProcess().Id; }
  49. public static object getenv(string name) { return environ.get(name); }
  50. public static object getenv(string name, object defaultValue) { return environ.get(name, defaultValue); }
  51. public static List listdir(string path)
  52. { if(path==null || path=="") throw Ops.ValueError("listdir(): path cannot be null or empty");
  53. List ret = new List();
  54. try
  55. { foreach(string s in Directory.GetDirectories(path)) ret.append(Path.GetFileName(s));
  56. foreach(string s in Directory.GetFiles(path)) ret.append(Path.GetFileName(s));
  57. }
  58. catch(DirectoryNotFoundException) { throw NotFound(path); }
  59. catch(IOException e) { throw Ops.OSError(e.Message); }
  60. return ret;
  61. }
  62. public static void makedirs(string path) { makedirs(path, 0777); }
  63. public static void makedirs(string path, int mode)
  64. { if(path==null || path=="") throw Ops.ValueError("makedirs(): path cannot be null or empty");
  65. try { Directory.CreateDirectory(path); }
  66. catch(DirectoryNotFoundException) { throw Invalid(path); }
  67. catch(IOException e) { throw Ops.OSError(e.Message); }
  68. }
  69. public static void mkdir(string path) { makedirs(path, 0777); }
  70. public static void mkdir(string path, int mode) { makedirs(path, mode); }
  71. public static BoaFile popen(string command) { return popen(command, "t", -1); }
  72. public static BoaFile popen(string command, string mode) { return popen(command, mode, -1); }
  73. public static BoaFile popen(string command, string mode, int bufsize)
  74. { throw new NotImplementedException();
  75. }
  76. public static Tuple popen2(string command) { return popen2(command, "t", -1); }
  77. public static Tuple popen2(string command, string mode) { return popen2(command, mode, -1); }
  78. public static Tuple popen2(string command, string mode, int bufsize)
  79. { throw new NotImplementedException();
  80. }
  81. public static Tuple popen3(string command) { return popen3(command, "t", -1); }
  82. public static Tuple popen3(string command, string mode) { return popen3(command, mode, -1); }
  83. public static Tuple popen3(string command, string mode, int bufsize)
  84. { throw new NotImplementedException();
  85. }
  86. public static Tuple popen4(string command) { return popen4(command, "t", -1); }
  87. public static Tuple popen4(string command, string mode) { return popen4(command, mode, -1); }
  88. public static Tuple popen4(string command, string mode, int bufsize)
  89. { throw new NotImplementedException();
  90. }
  91. public static void remove(string path)
  92. { if(path==null || path=="") throw Ops.ValueError("remove(): path cannot be null or empty");
  93. try { File.Delete(path); }
  94. catch(DirectoryNotFoundException) { throw Invalid(path); }
  95. catch(UnauthorizedAccessException) { throw Ops.OSError("{0} is a directory", Ops.Repr(path)); }
  96. catch(IOException e) { throw Ops.OSError(e.Message); }
  97. }
  98. public static void removedirs(string path)
  99. { if(path==null || path=="") throw Ops.ValueError("removedirs(): path cannot be null or empty");
  100. DirectoryInfo d;
  101. try { d = new DirectoryInfo(path); }
  102. catch(ArgumentException) { throw Invalid(path); }
  103. catch(IOException e) { throw Ops.OSError(e.Message); }
  104. try { d.Delete(); } catch(IOException e) { throw Ops.OSError(e.Message); }
  105. try { while((d=d.Parent)!=null) d.Delete(); } catch { }
  106. }
  107. public static void rmdir(string path)
  108. { if(path==null || path=="") throw Ops.ValueError("rmdir(): path cannot be null or empty");
  109. try { Directory.Delete(path); }
  110. catch(DirectoryNotFoundException) { throw NotFound(path); }
  111. catch(IOException e) { throw Ops.IOError(e.Message); }
  112. }
  113. public static byte[] urandom(int bytes)
  114. { if(bytes<0) throw Ops.ValueError("urandom(): 'bytes' must be >= 0");
  115. if(urng==null) urng = new System.Security.Cryptography.RNGCryptoServiceProvider();
  116. byte[] ret = new byte[bytes];
  117. lock(urng) urng.GetBytes(ret);
  118. return ret;
  119. }
  120. public static BoaFile tmpfile()
  121. { throw new NotImplementedException();
  122. }
  123. public static string tmpnam() { return Path.GetTempFileName(); }
  124. public static IEnumerator walk(string path) { return walk(path, true, null); }
  125. public static IEnumerator walk(string path, bool topDown) { return walk(path, topDown, null); }
  126. public static IEnumerator walk(string path, bool topDown, object onError)
  127. { throw new NotImplementedException();
  128. }
  129. public static void unlink(string path) { remove(path); }
  130. public const int F_OK=1, R_OK=2, W_OK=4, X_OK=8;
  131. public static string altsep = System.IO.Path.AltDirectorySeparatorChar.ToString();
  132. public static string curdir = ".";
  133. public static string defpath = ".";
  134. public static Dict environ = new Dict(Environment.GetEnvironmentVariables());
  135. public static string extsep = ".";
  136. public static string linesep = "\n";
  137. public static string pardir = "..";
  138. public static string pathsep = ";";
  139. public static string sep = System.IO.Path.DirectorySeparatorChar.ToString();
  140. internal static OSErrorException NotFound(string path)
  141. { return Ops.OSError("no such file or directory {0}", Ops.Repr(path));
  142. }
  143. internal static OSErrorException Invalid(string path)
  144. { return Ops.OSError("invalid component in path {0}", Ops.Repr(path));
  145. }
  146. static System.Security.Cryptography.RandomNumberGenerator urng;
  147. }
  148. } // namespace Boa.Modules