Python __builtin__.file() Examples

The following are 30 code examples of __builtin__.file(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module __builtin__ , or try the search function .
Example #1
Source File: config_model_test.py    From loaner with Apache License 2.0 6 votes vote down vote up
def setUp(self):
    super(ConfigurationTest, self).setUp()
    # Save the real modules for clean up.
    self.real_open = __builtin__.open
    self.real_file = __builtin__.file
    self.fs = fake_filesystem.FakeFilesystem()
    self.os = fake_filesystem.FakeOsModule(self.fs)
    self.open = fake_filesystem.FakeFileOpen(self.fs)
    self.stubs = mox3_stubout.StubOutForTesting()
    self.stubs.SmartSet(__builtin__, 'open', self.open)
    self.stubs.SmartSet(os, 'path', self.os.path)

    config_file = constants.CONFIG_DEFAULTS_PATH
    self.fs.CreateFile(config_file, contents=_config_defaults_yaml)

    config_model.Config(id='string_config', string_value='config value 1').put()
    config_model.Config(id='integer_config', integer_value=1).put()
    config_model.Config(id='bool_config', bool_value=True).put()
    config_model.Config(id='list_config', list_value=['email1', 'email2']).put() 
Example #2
Source File: dev_appserver.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def ReadAppConfig(appinfo_path, parse_app_config=appinfo_includes.Parse):
  """Reads app.yaml file and returns its app id and list of URLMap instances.

  Args:
    appinfo_path: String containing the path to the app.yaml file.
    parse_app_config: Used for dependency injection.

  Returns:
    AppInfoExternal instance.

  Raises:
    If the config file could not be read or the config does not contain any
    URLMap instances, this function will raise an InvalidAppConfigError
    exception.
  """
  try:
    appinfo_file = file(appinfo_path, 'r')
  except IOError, unused_e:
    raise InvalidAppConfigError(
        'Application configuration could not be read from "%s"' % appinfo_path) 
Example #3
Source File: dev_appserver.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def GetVersionObject(isfile=os.path.isfile, open_fn=open):
  """Gets the version of the SDK by parsing the VERSION file.

  Args:
    isfile: used for testing.
    open_fn: Used for testing.

  Returns:
    A Yaml object or None if the VERSION file does not exist.
  """
  version_filename = os.path.join(os.path.dirname(google.appengine.__file__),
                                  VERSION_FILE)
  if not isfile(version_filename):
    logging.error('Could not find version file at %s', version_filename)
    return None

  version_fh = open_fn(version_filename, 'r')
  try:
    version = yaml.safe_load(version_fh)
  finally:
    version_fh.close()

  return version 
Example #4
Source File: dev_appserver.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def GetModuleFile(module, is_file=os.path.isfile):
    """Helper method to try to determine modules source file.

    Args:
      module: Module object to get file for.
      is_file: Function used to determine if a given path is a file.

    Returns:
      Path of the module's corresponding Python source file if it exists, or
      just the module's compiled Python file. If the module has an invalid
      __file__ attribute, None will be returned.
    """
    module_file = getattr(module, '__file__', None)
    if module_file is None:
      return None


    source_file = module_file[:module_file.rfind('py') + 2]

    if is_file(source_file):
      return source_file
    return module.__file__ 
Example #5
Source File: dev_appserver.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def _RemainingDataSize(input_buffer):
  """Computes how much data is remaining in the buffer.

  It leaves the buffer in its initial state.

  Args:
    input_buffer: a file-like object with seek and tell methods.

  Returns:
    integer representing how much data is remaining in the buffer.
  """
  current_position = input_buffer.tell()
  input_buffer.seek(0, 2)
  remaining_data_size = input_buffer.tell() - current_position
  input_buffer.seek(current_position)
  return remaining_data_size 
Example #6
Source File: dev_appserver.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def ReadCronConfig(croninfo_path, parse_cron_config=croninfo.LoadSingleCron):
  """Reads cron.yaml file and returns a list of CronEntry instances.

  Args:
    croninfo_path: String containing the path to the cron.yaml file.
    parse_cron_config: Used for dependency injection.

  Returns:
    A CronInfoExternal object.

  Raises:
    If the config file is unreadable, empty or invalid, this function will
    raise an InvalidAppConfigError or a MalformedCronConfiguration exception.
  """
  try:
    croninfo_file = file(croninfo_path, 'r')
  except IOError, e:
    raise InvalidAppConfigError(
        'Cron configuration could not be read from "%s": %s'
        % (croninfo_path, e)) 
Example #7
Source File: dev_appserver.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def __init__(self, response_file=None, **kwds):
    """Initializer.

    Args:
      response_file: A file-like object that contains the full response
        generated by the user application request handler.  If present
        the headers and body are set from this value, although the values
        may be further overridden by the keyword parameters.
      kwds: All keywords are mapped to attributes of AppServerResponse.
    """
    self.status_code = 200
    self.status_message = 'Good to go'
    self.large_response = False

    if response_file:
      self.SetResponse(response_file)
    else:
      self.headers = mimetools.Message(cStringIO.StringIO())
      self.body = None

    for name, value in kwds.iteritems():
      setattr(self, name, value) 
Example #8
Source File: dev_appserver.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def GetExpiration(self, path):
    """Returns the cache expiration duration to be users for the given file.

    Args:
      path: A string containing the file's path relative to the app.

    Returns:
      Integer number of seconds to be used for browser cache expiration time.
    """

    if self._default_expiration is None:
      return 0

    url_map = self._FirstMatch(path)
    if url_map.expiration is None:
      return self._default_expiration

    return appinfo.ParseExpiration(url_map.expiration) 
Example #9
Source File: dev_appserver.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def GetMimeType(self, path):
    """Returns the mime type that we should use when serving the specified file.

    Args:
      path: A string containing the file's path relative to the app.

    Returns:
      String containing the mime type to use. Will be 'application/octet-stream'
      if we have no idea what it should be.
    """
    url_map = self._FirstMatch(path)
    if url_map.mime_type is not None:
      return url_map.mime_type


    unused_filename, extension = os.path.splitext(path)
    return mimetypes.types_map.get(extension, 'application/octet-stream') 
Example #10
Source File: dev_appserver.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def AdjustPath(self, path):
    """Adjusts application file paths to relative to the application.

    More precisely this method adjusts application file path to paths
    relative to the application or external library directories.

    Handler paths that start with $PYTHON_LIB will be converted to paths
    relative to the google directory.

    Args:
      path: File path that should be adjusted.

    Returns:
      The adjusted path.
    """
    if path.startswith(PYTHON_LIB_VAR):
      path = os.path.join(SDK_ROOT, path[len(PYTHON_LIB_VAR) + 1:])
    else:
      path = os.path.join(self._root_path, path)

    return path 
Example #11
Source File: dev_appserver.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def __init__(self,
               config,
               module_dict,
               root_path,
               path_adjuster,
               setup_env=SetupEnvironment,
               exec_cgi=ExecuteCGI):
    """Initializer.

    Args:
      config: AppInfoExternal instance representing the parsed app.yaml file.
      module_dict: Dictionary in which application-loaded modules should be
        preserved between requests. This dictionary must be separate from the
        sys.modules dictionary.
      path_adjuster: Instance of PathAdjuster to use for finding absolute
        paths of CGI files on disk.
      setup_env, exec_cgi: Used for dependency injection.
    """
    self._config = config
    self._module_dict = module_dict
    self._root_path = root_path
    self._path_adjuster = path_adjuster
    self._setup_env = setup_env
    self._exec_cgi = exec_cgi 
Example #12
Source File: dev_appserver.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def CheckScriptExists(cgi_path, handler_path):
  """Check that the given handler_path is a file that exists on disk.

  Args:
    cgi_path: Absolute path to the CGI script file on disk.
    handler_path: CGI path stored in the application configuration (as a path
      like 'foo/bar/baz.py'). May contain $PYTHON_LIB references.

  Raises:
    CouldNotFindModuleError: if the given handler_path is a file and doesn't
    have the expected extension.
  """
  if handler_path.startswith(PYTHON_LIB_VAR + '/'):

    return

  if (not os.path.isdir(cgi_path) and
      not os.path.isfile(cgi_path) and
      os.path.isfile(cgi_path + '.py')):
    raise CouldNotFindModuleError(
        'Perhaps you meant to have the line "script: %s.py" in your app.yaml' %
        handler_path) 
Example #13
Source File: dev_appserver.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def EndRedirect(self, dispatched_output, original_output):
    """Process the end of an internal redirect.

    This method is called after all subsequent dispatch requests have finished.
    By default the output from the dispatched process is copied to the original.

    This will not be called on dispatchers that do not return an internal
    redirect.

    Args:
      dispatched_output: StringIO buffer containing the results from the
       dispatched
      original_output: The original output file.

    Returns:
      None if request handling is complete.
      A new AppServerRequest instance if internal redirect is required.
    """
    original_output.write(dispatched_output.read()) 
Example #14
Source File: dev_appserver.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def CopyStreamPart(source, destination, content_size):
  """Copy a portion of a stream from one file-like object to another.

  Args:
    source: Source stream to copy from.
    destination: Destination stream to copy to.
    content_size: Maximum bytes to copy.

  Returns:
    Number of bytes actually copied.
  """
  bytes_copied = 0
  bytes_left = content_size
  while bytes_left > 0:
    bytes = source.read(min(bytes_left, COPY_BLOCK_SIZE))
    bytes_read = len(bytes)
    if bytes_read == 0:
      break
    destination.write(bytes)
    bytes_copied += bytes_read
    bytes_left -= bytes_read
  return bytes_copied 
Example #15
Source File: sandbox.py    From browserscope with Apache License 2.0 6 votes vote down vote up
def find_module(self, fullname, path=None):
    if fullname in _WHITE_LIST_C_MODULES:
      return None
    if any(regex.match(fullname) for regex in self._enabled_regexes):
      return None
    _, _, submodule_name = fullname.rpartition('.')
    try:
      result = imp.find_module(submodule_name, path)
    except ImportError:
      return None
    f, _, description = result
    _, _, file_type = description
    if isinstance(f, file):
      f.close()
    if file_type == imp.C_EXTENSION:
      return self
    return None 
Example #16
Source File: install.py    From DDDProxy with Apache License 2.0 5 votes vote down vote up
def setInitFile(tempFilePath, filePath, authFormat):
		f = file(tempFilePath, "r")
		InitFileContent = "" + f.read()
		f.close()

		port = raw_input("Enter Bind Port(empty for default):")
		if not port:
			port = "-1"
		InitFileContent = InitFileContent.replace("{{python}}", sys.executable)
		InitFileContent = InitFileContent.replace("{{path-to-DDDProxy}}", mainPath)
		InitFileContent = InitFileContent.replace("{{port-setting}}", port)
		InitFileContent = InitFileContent.replace("{{entry-point}}", "%s.py" % (server))
		InitFileContent = InitFileContent.replace("{{server-name}}", "dddproxy." + server)
		if server == "remoteServer":
			serverPassword = ""
			while True:
				serverPassword = getpass.getpass("Enter passphrase(empty for random):")
				if not serverPassword:
					serverPassword = pwGen(20)
					print "Server password:", serverPassword
					break
				if serverPassword != getpass.getpass("Enter same passphrase again:"):
					print "Passphrases do not match. try again"
				else:
					break
			InitFileContent = InitFileContent.replace("{{auth}}", authFormat % (serverPassword))
		else:
			InitFileContent = InitFileContent.replace("{{auth}}", "")
		if os.path.exists(filePath):
			overwrite = raw_input(filePath + " already exists.\nOverwrite (y/n)?")
			if overwrite != "y":
				exit(1)
		print "Write file ", filePath
		f = file(filePath, "w+")
		f.write(InitFileContent)
		f.close()
		return port 
Example #17
Source File: win32.py    From pivy with ISC License 5 votes vote down vote up
def spawn(sh, escape, cmd, args, env):
    if not sh:
        sys.stderr.write("scons: Could not find command interpreter, is it in your PATH?\n")
        return 127
    return exec_spawn([sh, '/C', escape(string.join(args))], env)

# Windows does not allow special characters in file names anyway, so no
# need for a complex escape function, we will just quote the arg, except
# that "cmd /c" requires that if an argument ends with a backslash it
# needs to be escaped so as not to interfere with closing double quote
# that we add. 
Example #18
Source File: sandbox.py    From python-compat-runtime with Apache License 2.0 5 votes vote down vote up
def _install_import_hooks(config, path_override_hook):
  """Install runtime's import hooks.

  These hooks customize the import process as per
  https://docs.python.org/2/library/sys.html#sys.meta_path .

  Args:
    config: An apphosting/tools/devappserver2/runtime_config.proto
        for this instance.
    path_override_hook: A hook for importing special appengine
        versions of select libraries from the libraries
        section of the current module's app.yaml file.
  """
  if not config.vm:
    enabled_regexes = THIRD_PARTY_C_MODULES.get_enabled_regexes(config)
    sys.meta_path = [
        StubModuleImportHook(),
        ModuleOverrideImportHook(_MODULE_OVERRIDE_POLICIES),
        CModuleImportHook(enabled_regexes),
        path_override_hook,
        PyCryptoRandomImportHook,
        PathRestrictingImportHook(enabled_regexes)]
  else:
    sys.meta_path = [
        # Picks up custom versions of certain libraries in the libraries section
        #     of app.yaml
        path_override_hook,
        # Picks up a custom version of Crypto.Random.OSRNG.posix.
        # TODO: Investigate removing this as it may not be needed
        #     for vms since they are able to read /dev/urandom, I left it for
        #     consistency.
        PyCryptoRandomImportHook] 
Example #19
Source File: ls_open_file_handles.py    From ReadableWebProxy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def unpatch(self):
		builtins.open = self.oldopen
		try:
			builtins.file = self.oldfile
		except AttributeError:
			pass 
Example #20
Source File: ls_open_file_handles.py    From ReadableWebProxy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def patch(self):
		builtins.open = self.File

		try:
			builtins.file = self.File
		except AttributeError:
			pass

		atexit.register(self.exit_handler) 
Example #21
Source File: ls_open_file_handles.py    From ReadableWebProxy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, print_only_open=True):
		self.openfiles = []
		self.oldopen = builtins.open

		self.oldfile = getattr(builtins, 'file', io.FileIO)

		self.do_print_only_open = print_only_open
		self.in_use = False

		class File(self.oldfile):

			def __init__(int_self, *args, **kwargs):
				path = args[0]

				self.oldfile.__init__(int_self, *args, **kwargs)
				if self.in_use:
					return
				self.in_use = True
				self.openfiles.append((weakref.ref(int_self), path, int_self._stack_trace()))
				self.in_use = False

				# Drop all files where the file itself has been GC'ed
				self.openfiles = [tmp for tmp in self.openfiles if tmp[0]()]
				if len(self.openfiles) > 100:
					print("WARNING")
					print("WARNING")
					print("WARNING")
					print("%s open files!" % len(self.openfiles))


			def close(int_self):
				self.oldfile.close(int_self)

			def _stack_trace(int_self):
				try:
					raise RuntimeError()
				except RuntimeError as e:
					stack = traceback.extract_stack()[:-2]
					return traceback.format_list(stack)

		self.File = File 
Example #22
Source File: dev_appserver.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def PortAllEntities(datastore_path):
  """Copies entities from a DatastoreFileStub to an SQLite stub.

  Args:
    datastore_path: Path to the file to store Datastore file stub data is.
  """

  previous_stub = apiproxy_stub_map.apiproxy.GetStub('datastore_v3')

  try:
    app_id = os.environ['APPLICATION_ID']
    apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
    datastore_stub = datastore_file_stub.DatastoreFileStub(
        app_id, datastore_path, trusted=True)
    apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', datastore_stub)

    entities = FetchAllEntitites()
    sqlite_datastore_stub = datastore_sqlite_stub.DatastoreSqliteStub(app_id,
                            datastore_path + '.sqlite', trusted=True)
    apiproxy_stub_map.apiproxy.ReplaceStub('datastore_v3',
                                           sqlite_datastore_stub)
    PutAllEntities(entities)
    sqlite_datastore_stub.Close()
  finally:
    apiproxy_stub_map.apiproxy.ReplaceStub('datastore_v3', previous_stub)

  shutil.copy(datastore_path, datastore_path + '.filestub')
  _RemoveFile(datastore_path)
  shutil.move(datastore_path + '.sqlite', datastore_path) 
Example #23
Source File: sandbox.py    From python-compat-runtime with Apache License 2.0 5 votes vote down vote up
def _install_fake_file(config, python_lib_paths, path_override_hook):
  """Install a stub file implementation to enforce sandbox rules."""
  stubs.FakeFile.set_allowed_paths(config.application_root,
                                   python_lib_paths[1:] +
                                   path_override_hook.extra_accessible_paths)
  stubs.FakeFile.set_skip_files(config.skip_files)
  stubs.FakeFile.set_static_files(config.static_files)
  __builtin__.file = stubs.FakeFile
  __builtin__.open = stubs.FakeFile
  types.FileType = stubs.FakeFile 
Example #24
Source File: sandbox.py    From python-compat-runtime with Apache License 2.0 5 votes vote down vote up
def _find_module_or_loader(self, submodule_name, fullname, path):
    """Acts like imp.find_module with support for path hooks.

    Args:
      submodule_name: The name of the submodule within its parent package.
      fullname: The full name of the module to load.
      path: A list containing the paths to search for the module.

    Returns:
      A tuple (source_file, path_name, description, loader) where:
        source_file: An open file or None.
        path_name: A str containing the path to the module.
        description: A description tuple like the one imp.find_module returns.
        loader: A PEP 302 compatible path hook. If this is not None, then the
            other elements will be None.

    Raises:
      ImportError: The module could not be imported.
    """
    for path_entry in path + [None]:
      result = self._find_path_hook(submodule_name, fullname, path_entry)
      if result is not None:
        break
    else:
      raise ImportError('No module named %s' % fullname)
    if isinstance(result, tuple):
      return result + (None,)
    else:
      return None, None, None, result.find_module(fullname) 
Example #25
Source File: sandbox.py    From python-compat-runtime with Apache License 2.0 5 votes vote down vote up
def get_code(self, fullname):
    """Returns the code object for the module specified by fullname.

    This implements part of the extensions to the PEP 302 importer protocol.

    Args:
      fullname: The fullname of the module.

    Returns:
      The code object associated the module.
    """
    full_path, _, _, loader = self._get_module_info(fullname)
    if loader:
      return loader.get_code(fullname)
    if full_path is None:
      return None
    source_file = open(full_path)
    try:
      source_code = source_file.read()
    finally:
      source_file.close()

    # Check that coding cookie is correct if present, error if not present and
    # we can't decode with the default of 'ascii'.  According to PEP 263 this
    # coding cookie line must be in the first or second line of the file.
    encoding = DEFAULT_ENCODING
    for line in source_code.split('\n', 2)[:2]:
      matches = CODING_MAGIC_COMMENT_RE.findall(line)
      if matches:
        encoding = matches[0].lower()
    # This may raise up to the user, which is what we want, however we ignore
    # the output because we don't want to return a unicode version of the code.
    source_code.decode(encoding)

    return compile(source_code, full_path, 'exec') 
Example #26
Source File: dev_appserver.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def _StaticFilePathRe(url_map):
  """Returns a regular expression string that matches static file paths.

  Args:
    url_map: A fully initialized static_files or static_dir appinfo.URLMap
      instance.

  Returns:
    The regular expression matches paths, relative to the application's root
    directory, of files that this static handler serves. re.compile should
    accept the returned string.

  Raises:
    AssertionError: The url_map argument was not an URLMap for a static handler.
  """
  handler_type = url_map.GetHandlerType()


  if handler_type == 'static_files':
    return url_map.upload + '$'

  elif handler_type == 'static_dir':
    path = url_map.static_dir.rstrip(os.path.sep)
    return path + re.escape(os.path.sep) + r'(.*)'

  assert False, 'This property only applies to static handlers.' 
Example #27
Source File: dev_appserver.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def SetResponse(self, response_file):
    """Sets headers and body from the response file.

    Args:
      response_file: File like object to set body and headers from.
    """
    self.headers = mimetools.Message(response_file)
    self.body = response_file 
Example #28
Source File: dev_appserver.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def CreateEtag(data):
    """Returns string of hash of file content, unique per URL."""
    data_crc = zlib.crc32(data)
    return base64.b64encode(str(data_crc)) 
Example #29
Source File: dev_appserver.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def ReadDataFile(data_path, openfile=file):
  """Reads a file on disk, returning a corresponding HTTP status and data.

  Args:
    data_path: Path to the file on disk to read.
    openfile: Used for dependency injection.

  Returns:
    Tuple (status, data) where status is an HTTP response code, and data is
      the data read; will be an empty string if an error occurred or the
      file was empty.
  """
  status = httplib.INTERNAL_SERVER_ERROR
  data = ""

  try:
    data_file = openfile(data_path, 'rb')
    try:
      data = data_file.read()
    finally:
      data_file.close()
      status = httplib.OK
  except (OSError, IOError), e:
    logging.error('Error encountered reading file "%s":\n%s', data_path, e)
    if e.errno in FILE_MISSING_EXCEPTIONS:
      status = httplib.NOT_FOUND
    else:
      status = httplib.FORBIDDEN 
Example #30
Source File: dev_appserver.py    From browserscope with Apache License 2.0 5 votes vote down vote up
def GetHttpHeaders(self, path):
    """Returns http_headers of the matching appinfo.URLMap, or an empty one.

    Args:
      path: A string containing the file's path relative to the app.

    Returns:
      A user-specified HTTP headers to be used in static content response. These
      headers are contained in an appinfo.HttpHeadersDict, which maps header
      names to values (both strings).
    """
    return self._FirstMatch(path).http_headers or appinfo.HttpHeadersDict()