コード例 #1
0
ファイル: FileOps.cs プロジェクト: rudimk/dlr-dotnet
            internal static bool TryCreate(RubyContext /*!*/ context, string /*!*/ path, out FileSystemInfo result)
            {
                PlatformAdaptationLayer pal = context.Platform;

                result = null;
                if (pal.FileExists(path))
                {
                    result = new FileInfo(path);
                }
                else if (pal.DirectoryExists(path))
                {
                    result = new DirectoryInfo(path);
#if !SILVERLIGHT
                }
                else if (path.ToUpperInvariant().Equals(NUL_VALUE))
                {
                    result = new DeviceInfo(NUL_VALUE);
#endif
                }
                else
                {
                    return(false);
                }
                return(true);
            }
コード例 #2
0
ファイル: Glob.cs プロジェクト: ltwlf/IronSP
            private void TestPath(string path, int patternEnd, bool isLastPathSegment)
            {
                if (!isLastPathSegment)
                {
                    DoGlob(path, patternEnd, false);
                    return;
                }

                if (!NoEscapes)
                {
                    path = Unescape(path, _stripTwo ? 2 : 0);
                }
                else if (_stripTwo)
                {
                    path = path.Substring(2);
                }

                if (_pal.DirectoryExists(path))
                {
                    _result.Add(path);
                }
                else if (!_dirOnly && _pal.FileExists(path))
                {
                    _result.Add(path);
                }
            }
コード例 #3
0
 public override bool FileExists(string path)
 {
     if (IsRelativePath(path))
     {
         return(getDirectory().GetFile(path.Substring(2)).Exists);
     }
     else
     {
         return(innerPal.FileExists(path));
     }
 }
コード例 #4
0
 public override bool FileExists(string path)
 {
     if (IsRelativePath(path))
     {
         path = RemoveDotSlash(path);
         return(getDirectory().GetFile(path).Exists);
     }
     else
     {
         return(innerPal.FileExists(path));
     }
 }
コード例 #5
0
ファイル: imp.cs プロジェクト: dearman/iron-python
        private static PythonTuple FindModulePath(CodeContext /*!*/ context, string name, List path)
        {
            Debug.Assert(path != null);

            if (name == null)
            {
                throw PythonOps.TypeError("find_module() argument 1 must be string, not None");
            }

            PlatformAdaptationLayer pal = context.LanguageContext.DomainManager.Platform;

            foreach (object d in path)
            {
                string dir = d as string;
                if (dir == null)
                {
                    continue;               // skip invalid entries
                }
                string pathName = Path.Combine(dir, name);
                if (pal.DirectoryExists(pathName))
                {
                    if (pal.FileExists(Path.Combine(pathName, "__init__.py")))
                    {
                        return(PythonTuple.MakeTuple(null, pathName, PythonTuple.MakeTuple("", "", PackageDirectory)));
                    }
                }

                string fileName = pathName + ".py";
                if (pal.FileExists(fileName))
                {
                    Stream     fs = pal.OpenInputFileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    PythonFile pf = PythonFile.Create(context, fs, fileName, "U");
                    return(PythonTuple.MakeTuple(pf, fileName, PythonTuple.MakeTuple(".py", "U", PythonSource)));
                }
            }
            throw PythonOps.ImportError("No module named {0}", name);
        }
コード例 #6
0
ファイル: Glob.cs プロジェクト: gaybro8777/ironruby
            private void TestPath(string path, int patternEnd, bool isLastPathSegment)
            {
                if (!isLastPathSegment)
                {
                    DoGlob(path, patternEnd, false);
                    return;
                }
                string pathName = path.Replace('\\', '/');

                if (_stripTwo)
                {
                    pathName = pathName.Substring(2);
                }
                if (_pal.DirectoryExists(pathName))
                {
                    _result.Add(pathName);
                }
                else if (!_dirOnly && _pal.FileExists(pathName))
                {
                    _result.Add(pathName);
                }
            }
コード例 #7
0
ファイル: FileOps.cs プロジェクト: gaybro8777/ironruby
            internal static bool TryCreate(RubyContext /*!*/ context, string /*!*/ path, out FileSystemInfo result)
            {
                PlatformAdaptationLayer pal = context.DomainManager.Platform;

                result = null;
                if (pal.FileExists(path))
                {
                    result = new FileInfo(path);
                }
                else if (pal.DirectoryExists(path))
                {
                    result = new DirectoryInfo(path);
                }
                else if (path.ToUpper().Equals(NUL_VALUE))
                {
                    result = null;
                }
                else
                {
                    return(false);
                }
                return(true);
            }
コード例 #8
0
            public zipimporter(CodeContext /*!*/ context, object pathObj, [ParamDictionary] IDictionary <object, object> kwArgs)
            {
                PlatformAdaptationLayer pal = context.LanguageContext.DomainManager.Platform;
                string prefix, input, path;

                if (pathObj == null)
                {
                    throw PythonOps.TypeError("must be string, not None");
                }

                if (!(pathObj is string))
                {
                    throw PythonOps.TypeError("must be string, not {0}", pathObj.GetType());
                }

                if (kwArgs.Count > 0)
                {
                    throw PythonOps.TypeError("zipimporter() does not take keyword arguments");
                }

                path = pathObj as string;

                if (path.Length == 0)
                {
                    throw MakeError(context, "archive path is empty");
                }

                if (path.Length > MAXPATHLEN)
                {
                    throw MakeError(context, "archive path too long");
                }

                string buf = path.Replace(Path.AltDirectorySeparatorChar,
                                          Path.DirectorySeparatorChar);

                input = buf;

                path   = string.Empty;
                prefix = string.Empty;

                try {
                    while (!string.IsNullOrEmpty(buf))
                    {
                        if (pal.FileExists(buf))
                        {
                            path = buf;
                            break;
                        }
                        buf = Path.GetDirectoryName(buf);
                    }
                } catch {
                    throw MakeError(context, "not a Zip file");
                }

                if (!string.IsNullOrEmpty(path))
                {
                    PythonDictionary zip_directory_cache =
                        context.LanguageContext.GetModuleState(
                            _zip_directory_cache_key) as PythonDictionary;

                    if (zip_directory_cache != null && zip_directory_cache.ContainsKey(path))
                    {
                        _files = zip_directory_cache[path] as PythonDictionary;
                    }
                    else
                    {
                        _files = ReadDirectory(context, path);
                        zip_directory_cache.Add(path, _files);
                    }
                }
                else
                {
                    throw MakeError(context, "not a Zip file");
                }

                _prefix = input.Replace(path, string.Empty);
                // add trailing SEP
                if (!string.IsNullOrEmpty(_prefix) && !_prefix.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal))
                {
                    _prefix  = _prefix.Substring(1);
                    _prefix += Path.DirectorySeparatorChar;
                }
                _archive = path;
            }
コード例 #9
0
        private static void GetExecutable(PlatformAdaptationLayer /*!*/ pal, string /*!*/ command, out string executable, out string arguments)
        {
            command = command.Trim(' ');
            if (command.Length == 0)
            {
                throw RubyExceptions.CreateEINVAL(command);
            }

            // This seems to be quite complicated:
            // 1) If the first part of the command is a shell command (DIR, ECHO, ...) or
            //    if the command contains unquoted <, > or | then
            //    it uses ENV['COMSPEC'] to execute the command: %COMSPEC% /c "COMMAND"
            // 2) It looks for the shortest prefix of command that is separated by space from the rest of the command that is either
            //      a) An absolute path to an executable file.
            //      b) Try prepend paths from ENV['PATH']
            //      c) Try Environment.SpecialFolder.System.
            //      d) Try SHGetFolderPath(CSIDL_WINDOWS) - we can't get this from Environment.SpecialFolder, so we need to use
            //         ENV["SystemRoot"] environment variable.
            //    In each step it tries to append ".exe" or ".com" extension if the path doesn't exist.
            //
            //    For example, if the command is `x/a b/x` and the directory structure is
            //      x\a b\x.exe
            //      x\a.exe
            //    it executes a.exe.
            //
            //    MRI probably calls CreateProcess Win32 API with lpApplicationName it resolves as described above and
            //    lpCommandLine == command. System.Diagnostics.Process also uses this API with lpApplicationName == NULL and
            //    lpCommandLine == '"{ProcessStartInfo.FileName}" {ProcessStartInfo.Arguments}'.
            //
            //    Although CreateProcess does all the searching for an executable if passed no lpApplicationName,
            //    we need to do it ourselves because it is slightly different in MRI (is that a bug?) and also because System.Diagnostics.Process
            //    quotes the FileName :(
            //

            string comspec = pal.GetEnvironmentVariable("COMSPEC");

            if (!pal.FileExists(comspec))
            {
                comspec = null;
            }

            if (comspec != null && IndexOfUnquotedSpecialCharacter(command) >= 0)
            {
                executable = comspec;
                arguments  = "/c \"" + command + "\"";
                return;
            }

            int start = 0;

            while (true)
            {
                int next = command.IndexOf(' ', start);

                executable = (next >= 0) ? command.Substring(0, next) : command;
                arguments  = (next >= 0) ? command.Substring(next + 1) : "";

                if (start == 0 && comspec != null && IsShellCommand(executable))
                {
                    executable = comspec;
                    arguments  = "/c \"" + command + "\"";
                    return;
                }

                try {
                    foreach (var path in GetExecutableFiles(pal, executable))
                    {
                        if (pal.FileExists(path))
                        {
                            // We need to set the path we found as executable. Althought makes command line of the target process
                            // different from when called by MRI it will execute the right process. If we passed the original executable name
                            // CreateProcess might resolve it to a different executable.
                            executable = path;
                            return;
                        }
                    }
                } catch (Exception e) {
                    if (next < 0)
                    {
                        throw RubyExceptions.CreateENOENT(command, e);
                    }
                }

                if (next < 0)
                {
                    throw RubyExceptions.CreateENOENT(command);
                }

                start = next + 1;
                while (start < command.Length && command[start] == ' ')
                {
                    start++;
                }
            }
        }