def test_opens_for_writing_append(self): # open_for_writing can also open to append. directory = self.makeTemporaryDirectory() filename = os.path.join(directory, 'foo') fp = open_for_writing(filename, 'w') fp.write("Hello world!\n") fp.close() fp = open_for_writing(filename, 'a') fp.write("Next line\n") fp.close() self.assertEqual("Hello world!\nNext line\n", open(filename).read())
def test_opens_for_writing(self): # open_for_writing opens a file for, umm, writing. directory = self.makeTemporaryDirectory() filename = os.path.join(directory, 'foo') fp = open_for_writing(filename, 'w') fp.write("Hello world!\n") fp.close() self.assertEqual("Hello world!\n", open(filename).read())
def test_even_if_directory_doesnt_exist(self): # open_for_writing will open a file for writing even if the directory # doesn't exist. directory = self.makeTemporaryDirectory() filename = os.path.join(directory, 'foo', 'bar', 'baz', 'filename') fp = open_for_writing(filename, 'w') fp.write("Hello world!\n") fp.close() self.assertEqual("Hello world!\n", open(filename).read())
def makeSeedStructure(self, flavour, series_name, seed_names, seed_inherit={}): """Create a simple seed structure file.""" structure_path = self.composeSeedPath( flavour, series_name, "STRUCTURE") with open_for_writing(structure_path, "w") as structure: for seed_name in seed_names: inherit = seed_inherit.get(seed_name, []) line = "%s: %s" % (seed_name, " ".join(inherit)) print >>structure, line.strip()
def makeSeed(self, flavour, series_name, seed_name, entries, headers=None): """Create a simple seed file.""" seed_path = self.composeSeedPath(flavour, series_name, seed_name) with open_for_writing(seed_path, "w") as seed: if headers is not None: for header in headers: print >>seed, header print >>seed for entry in entries: print >>seed, " * %s" % entry
def generate_overrides(self): """Ensure correct config.zcml overrides will be called. Call this method before letting any ZCML processing occur. """ loader_file = os.path.join(self.root, 'zcml/+config-overrides.zcml') loader = open_for_writing(loader_file, 'w') print >> loader, """ <configure xmlns="http://namespaces.zope.org/zope"> <!-- This file automatically generated using lp.services.config.LaunchpadConfig.generate_overrides. DO NOT EDIT. --> <include files="%s/*.zcml" /> </configure>""" % self.config_dir loader.close()
def test_newFromFile(self): root = self.makeTemporaryDirectory() with open_for_writing(os.path.join(root, "dists/foo"), "w") as f: f.write("abc\n") archive = self.factory.makeArchive() with open(os.path.join(root, "dists/foo"), "rb") as f: archive_file = getUtility(IArchiveFileSet).newFromFile( archive, "foo", "dists/foo", f, 4, "text/plain") transaction.commit() self.assertEqual(archive, archive_file.archive) self.assertEqual("foo", archive_file.container) self.assertEqual("dists/foo", archive_file.path) archive_file.library_file.open() try: self.assertEqual("abc\n", archive_file.library_file.read()) finally: archive_file.library_file.close() self.assertIsNone(archive_file.scheduled_deletion_date)
def beforeTraversal(self, request): notify(StartRequestEvent(request)) request._traversalticks_start = tickcount.tickcount() threadid = thread.get_ident() threadrequestfile = open_for_writing( 'logs/thread-%s.request' % threadid, 'w') try: request_txt = unicode(request).encode('UTF-8') except Exception: request_txt = 'Exception converting request to string\n\n' try: request_txt += traceback.format_exc() except: request_txt += 'Unable to render traceback!' threadrequestfile.write(request_txt) threadrequestfile.close() # Tell our custom database adapter that the request has started. da.set_request_started() newInteraction(request) transaction.begin() # Now we are logged in, install the correct IDatabasePolicy for # this request. db_policy = IDatabasePolicy(request) getUtility(IStoreSelector).push(db_policy) getUtility(IOpenLaunchBag).clear() # Set the default layer. adapters = getGlobalSiteManager().adapters layer = adapters.lookup((providedBy(request), ), IDefaultSkin, '') if layer is not None: layers.setAdditionalLayer(request, layer) principal = self.getPrincipal(request) request.setPrincipal(principal) self.maybeRestrictToTeam(request) maybe_block_offsite_form_post(request)
def beforeTraversal(self, request): notify(StartRequestEvent(request)) request._traversalticks_start = tickcount.tickcount() threadid = thread.get_ident() threadrequestfile = open_for_writing( 'logs/thread-%s.request' % threadid, 'w') try: request_txt = unicode(request).encode('UTF-8') except Exception: request_txt = 'Exception converting request to string\n\n' try: request_txt += traceback.format_exc() except: request_txt += 'Unable to render traceback!' threadrequestfile.write(request_txt) threadrequestfile.close() # Tell our custom database adapter that the request has started. da.set_request_started() newInteraction(request) transaction.begin() # Now we are logged in, install the correct IDatabasePolicy for # this request. db_policy = IDatabasePolicy(request) getUtility(IStoreSelector).push(db_policy) getUtility(IOpenLaunchBag).clear() # Set the default layer. adapters = getGlobalSiteManager().adapters layer = adapters.lookup((providedBy(request),), IDefaultSkin, '') if layer is not None: layers.setAdditionalLayer(request, layer) principal = self.getPrincipal(request) request.setPrincipal(principal) self.maybeRestrictToTeam(request) maybe_block_offsite_form_post(request)