def test_filenameMatchesPackage(self): """ The C{__file__} attribute of the module should match the package name. """ filename = filepath.FilePath(self.parent).child("goodpackage.py") filename.setContent(packages.testModule.encode("utf8")) try: module = runner.filenameToModule(filename.path) self.assertEqual(filename.path, module.__file__) finally: filename.remove()
def test_find_package_modules(self): """ Will filter the found modules excluding the modules listed in L{twisted.python.dist3}. """ distribution = Distribution() distribution.script_name = "setup.py" distribution.script_args = "build_py" builder = BuildPy3(distribution) # Rig the dist3 data so that we can reduce the scope of this test and # reduce the risk of getting false failures, while doing a minimum # level of patching. self.patch( _setup, "notPortedModules", [ "twisted.spread.test.test_pbfailure", ], ) twistedPackageDir = filepath.FilePath(twisted.__file__).parent() packageDir = twistedPackageDir.child("spread").child("test") result = builder.find_package_modules("twisted.spread.test", packageDir.path) self.assertEqual( sorted( [ ( "twisted.spread.test", "__init__", packageDir.child("__init__.py").path, ), ( "twisted.spread.test", "test_banana", packageDir.child("test_banana.py").path, ), ( "twisted.spread.test", "test_jelly", packageDir.child("test_jelly.py").path, ), ( "twisted.spread.test", "test_pb", packageDir.child("test_pb.py").path, ), ] ), sorted(result), )
def rename(self, name, overwrite=False, settings=settings): if name == self.name: return new_prj = self._manager.get_project(name) new_prj.create(overwrite) new_path = filepath.FilePath(new_prj.path) new_path.remove() self._path.moveTo(new_path) self._path = new_path if self == self._manager.current: settings.set("current_project", self.name) settings.VIRTUALBRICKS_HOME = self.path settings.store()
def __init__(self): super(System, self).__init__() self.load() # These might be updated from ltsp_shared_folders, if they're used self.teachers = 'teachers' self.share_groups = [self.teachers] # INotifier for /etc/group and /etc/shadow self.system_event = Event() self.libuser_event = Event() self.system_event.connect(self.on_system_changed) self.mask = inotify.IN_MODIFY self.group_fp = filepath.FilePath( os.path.join(paths.sysconfdir, 'group')) self.shadow_fp = filepath.FilePath( os.path.join(paths.sysconfdir, 'shadow')) self.notifier = inotify.INotify() self.notifier.startReading() self.notifier._addWatch(self.group_fp, self.mask, False, [self.on_fd_changed]) self.notifier._addWatch(self.shadow_fp, self.mask, False, [self.on_fd_changed])
def test_script_raises_on_invalid_file_content(self): tmpdir = tempfile.gettempdir() fp = filepath.FilePath('{}/test.js'.format(tmpdir)) fd = fp.open('w') fd.write('/* -*- mamba-file-type: mamba-javascript */\n' 'function dummy() { console.debug(\'dummy_test\'); }') fd.close() self.assertRaises(script.InvalidFile, script.Script, tmpdir + sep + 'test.js') fp.remove()
def test_directoryNotPackage(self): """ L{runner.filenameToModule} raises a C{ValueError} when the name of an empty directory is passed that isn't considered a valid Python package because it doesn't contain a C{__init__.py} file. """ emptyDir = filepath.FilePath(self.parent).child("emptyDirectory") emptyDir.createDirectory() err = self.assertRaises(ValueError, runner.filenameToModule, emptyDir.path) self.assertEqual(str(err), "%r is not a package directory" % (emptyDir.path, ))
def test_helpMultiword(self): """ The help action supports arguments with spaces in them. """ fakeHelp = filepath.FilePath(self.mktemp()) fakeHelp.makedirs() fakeHelp.child("foo bar").setContent("baz") original = Help.helpContentPath try: Help.helpContentPath = fakeHelp self._test("help foo bar", ["baz"], []) finally: Help.helpContentPath = original
def test_write_file(self): Controller.process = lambda _: 0 with fake_project(): self.config.parseOptions(['controller', 'test_controller']) controller = Controller(self.config) controller._write_controller() controller_file = filepath.FilePath( 'application/controller/test_controller.py' ) self.assertTrue(controller_file.exists()) controller_file.remove()
def requestAvatar(self, avatarId, mind, *interfaces): for iface in interfaces: if iface is ftp.IFTPShell: if avatarId is checkers.ANONYMOUS: avatar = ftp.FTPAnonymousShell(self.anonymousRoot) else: user_dir = self.dir[avatarId] avatar = ftp.FTPShell(filepath.FilePath(user_dir)) return ftp.IFTPShell, avatar, getattr(avatar, 'logout', lambda: None) raise NotImplementedError( "Only IFTPShell interface is supported by this realm")
def test_database_fallback_if_previous_loaded_was_ok(self): bad_file = tempfile.NamedTemporaryFile(delete=False) bad_file.write('[}') bad_file.close() config.Database('../mamba/test/application/config/database.json') config.Database(bad_file.name) self.assertFalse(config.Database().loaded) self.assertEqual(config.Database().uri, 'sqlite:') self.assertEqual(config.Database().min_threads, 5) self.assertEqual(config.Database().max_threads, 20) filepath.FilePath(bad_file.name).remove()
def test_copyToWithSymlink(self): """ Verify that copying with followLinks=True copies symlink targets instead of symlinks """ self.symlink( self.path.child("sub1").path, self.path.child("link1").path) fp = filepath.FilePath(self.mktemp()) self.path.copyTo(fp) self.assertFalse(fp.child("link1").islink()) self.assertEquals([x.basename() for x in fp.child("sub1").children()], [x.basename() for x in fp.child("link1").children()])
def test_memorySubstoreFile(self): """ In-memory substores whose stores have file directories should be able to create files. """ filesdir = filepath.FilePath(self.mktemp()) s = Store(filesdir=filesdir) ss = SubStore.createNew(s, ['account', '*****@*****.**']) s2 = ss.open() f = s2.newFile("test.txt") f.write("yay") f.close() self.assertEqual(open(f.finalpath.path).read(), "yay")
def testCalling(self): """ Test invoking a method on an item in the batch process. """ dbdir = filepath.FilePath(self.mktemp()) s = store.Store(dbdir) ss = substore.SubStore.createNew(s, 'substore') service.IService(s).startService() d = iaxiom.IBatchService(ss).call(BatchCallTestItem(store=ss.open()).callIt) def called(ign): self.failUnless(ss.open().findUnique(BatchCallTestItem).called, "Was not called") return service.IService(s).stopService() return d.addCallback(called)
def _saveKey(key, options): if not options['filename']: kind = keys.objectType(key) kind = {'ssh-rsa': 'rsa', 'ssh-dss': 'dsa'}[kind] filename = os.path.expanduser('~/.ssh/id_%s' % kind) options['filename'] = raw_input( 'Enter file in which to save the key (%s): ' % filename).strip() or filename if os.path.exists(options['filename']): print '%s already exists.' % options['filename'] yn = raw_input('Overwrite (y/n)? ') if yn[0].lower() != 'y': sys.exit() if options.get('no-passphrase'): options['pass'] = b'' elif not options['pass']: while 1: p1 = getpass.getpass( 'Enter passphrase (empty for no passphrase): ') p2 = getpass.getpass('Enter same passphrase again: ') if p1 == p2: break print 'Passphrases do not match. Try again.' options['pass'] = p1 keyObj = keys.Key(key) comment = '%s@%s' % (getpass.getuser(), socket.gethostname()) filepath.FilePath(options['filename']).setContent( keyObj.toString('openssh', options['pass'])) os.chmod(options['filename'], 33152) filepath.FilePath(options['filename'] + '.pub').setContent( keyObj.public().toString('openssh', comment)) print 'Your identification has been saved in %s' % options['filename'] print 'Your public key has been saved in %s.pub' % options['filename'] print 'The key fingerprint is:' print keyObj.fingerprint()
def test_write_file_with_controller(self): View.process = lambda _: 0 with fake_project(): self.config.parseOptions(['view', 'test_view', 'dummy']) view = View(self.config) view._write_view() view_file = filepath.FilePath( 'application/view/Dummy/test_view.html' ) self.assertTrue(view_file.exists()) view_file.remove()
def test_write_file(self): Model.process = lambda _: 0 with fake_project(): self.config.parseOptions(['model', 'test_model', 'test']) model = Model(self.config) model._write_model() model_file = filepath.FilePath( 'application/model/test_model.py' ) self.assertTrue(model_file.exists()) model_file.remove()
def test_keepOriginalAttributes(self): """ Verify that the Unlistable exception raised will preserve the attributes of the previously-raised exception. """ fp = filepath.FilePath(self.mktemp()) ose = self.assertRaises(OSError, fp.children) d1 = ose.__dict__.keys() d1.remove('originalException') d2 = ose.originalException.__dict__.keys() d1.sort() d2.sort() self.assertEquals(d1, d2)
def test_getPublicHTMLChild(self): """ L{UserDirectory.getChild} returns a L{static.File} instance when passed the name of a user with a home directory containing a I{public_html} directory. """ home = filepath.FilePath(self.bob[-2]) public_html = home.child('public_html') public_html.makedirs() request = DummyRequest(['bob']) result = self.directory.getChild('bob', request) self.assertIsInstance(result, static.File) self.assertEqual(result.path, public_html.path)
def test_moveTo(self): """ Verify that moving an entire directory results into another directory with the same content. """ oldPaths = list(self.path.walk()) # Record initial state fp = filepath.FilePath(self.mktemp()) self.path.moveTo(fp) fp.moveTo(self.path) newPaths = list(self.path.walk()) # Record double-move state newPaths.sort() oldPaths.sort() self.assertEquals(newPaths, oldPaths)
def test_sql_create_live(self): result = yield utils.getProcessValue('mamba-admin', [], os.environ) if result == 1: raise unittest.SkipTest('mamba framework is not installed yet') with fake_project(): cfg_file = filepath.FilePath('config/database.json') cfg_file_content = cfg_file.open('r').read() cfg_file_new = cfg_file_content.replace('dummy.db', 'dummy2.db') cfg_file.open('w').write(cfg_file_new) yield utils.getProcessOutput( 'python', ['../../scripts/mamba_admin.py', 'sql', 'create', '-l'], os.environ) db_file = filepath.FilePath('db/dummy2.db') self.assertTrue(db_file.exists) db_file.remove() cfg_file.open('w').write(cfg_file_content)
def test_normalFileStandardOut(self): """ If L{StandardIO} is created with a file descriptor which refers to a normal file (ie, a file from the filesystem), L{StandardIO.write} writes bytes to that file. In particular, it does not immediately consider the file closed or call its protocol's C{connectionLost} method. """ onConnLost = defer.Deferred() proto = ConnectionLostNotifyingProtocol(onConnLost) path = filepath.FilePath(self.mktemp()) self.normal = normal = path.open('w') self.addCleanup(normal.close) kwargs = dict(stdout=normal.fileno()) if not platform.isWindows(): # Make a fake stdin so that StandardIO doesn't mess with the *real* # stdin. r, w = os.pipe() self.addCleanup(os.close, r) self.addCleanup(os.close, w) kwargs['stdin'] = r connection = stdio.StandardIO(proto, **kwargs) # The reactor needs to spin a bit before it might have incorrectly # decided stdout is closed. Use this counter to keep track of how # much we've let it spin. If it closes before we expected, this # counter will have a value that's too small and we'll know. howMany = 5 count = itertools.count() def spin(): for value in count: if value == howMany: connection.loseConnection() return connection.write(str(value)) break reactor.callLater(0, spin) reactor.callLater(0, spin) # Once the connection is lost, make sure the counter is at the # appropriate value. def cbLost(reason): self.assertEquals(count.next(), howMany + 1) self.assertEquals(path.getContent(), ''.join(map(str, range(howMany)))) onConnLost.addCallback(cbLost) return onConnLost
def test_getDistribChild(self): """ L{UserDirectory.getChild} returns a L{ResourceSubscription} instance when passed the name of a user suffixed with C{".twistd"} who has a home directory containing a I{.twistd-web-pb} socket. """ home = filepath.FilePath(self.bob[-2]) home.makedirs() web = home.child('.twistd-web-pb') request = DummyRequest(['bob']) result = self.directory.getChild('bob.twistd', request) self.assertIsInstance(result, distrib.ResourceSubscription) self.assertEqual(result.host, 'unix') self.assertEqual(abspath(result.port), web.path)
def _generate_init_scripts(self): """Generated the needed __init__.py script files """ # application level script = filepath.FilePath('{}/application/__init__.py'.format( self.app_dir)) script.open('w').write('# Mamba application root directory\n') # controller level script = filepath.FilePath( '{}/application/controller/__init__.py'.format(self.app_dir)) script.open('w').write('# Controllers should be placed here\n') # model level script = filepath.FilePath('{}/application/model/__init__.py'.format( self.app_dir)) script.open('w').write('# Models should be placed here\n') # lib level script = filepath.FilePath('{}/application/lib/__init__.py'.format( self.app_dir)) script.open('w').write('# Libs should be placed here\n')
def testNewItemType(self): """ Creating the first instance of a of an Item subclass changes the underlying database schema as well as some Store-private state which tracks that schema. Test to make sure that creating the first instance of an Item subclass in one store is reflected in a second store. """ dbdir = filepath.FilePath(self.mktemp()) firstStore = store.Store(dbdir) secondStore = store.Store(dbdir) ConcurrentItemA(store=firstStore) self.assertEquals(secondStore.query(ConcurrentItemA).count(), 1)
def test_removeSafelyNoTrialMarker(self): """ If a path doesn't contain a node named C{"_trial_marker"}, that path is not removed by L{runner._removeSafely} and a L{runner._NoTrialMarker} exception is raised instead. """ directory = self.mktemp() os.mkdir(directory) dirPath = filepath.FilePath(directory) self.parseOptions([]) myRunner = self.getRunner() self.assertRaises(runner._NoTrialMarker, myRunner._removeSafely, dirPath)
def makeHTTPResourcesServerFactory(): # HTTP Server root = resource.Resource() root.putChild(b"", Root()) root.putChild(b"sketch", Sketch()) root.putChild(b"experiment", Experiment()) root.putChild(b"sketches.json", SketchFind()) root.putChild(b"experiments.json", ExperimentFind()) rootDir = filepath.FilePath(os.path.join(os.path.dirname(__file__), "..")) root.putChild(b"resources", static.File(rootDir.child(b"resources").path)) return server.Site(root)
def test_committedTableCreation(self): """ When tables are created in a transaction which is committed, they should persist in both Axiom's in-memory schema representation and within the on-disk SQL store. """ storedir = filepath.FilePath(self.mktemp()) s1 = store.Store(storedir) s1.transact(A, store=s1) self.assertIn(A, s1.typeToTableNameCache) s1.close() s2 = store.Store(storedir) self.assertIn(A, s2.typeToTableNameCache) s2.close()
def setup(self): logger.info('Setting up Sauron') if self.override_state: # Override previous state of conf self.eye.set_state_conf(self.conf) else: # Merge provided conf with state conf self.eye.set_base_conf(self.conf) # Register File Watcher notifier = inotify.INotify() notifier.startReading() notifier.watch(filepath.FilePath(self.conf_path), callbacks=[self.on_conf_change]) self.notifer = notifier
def _cbRestoreDone(self, ret, path_segments, result_defer): pth = '/'.join(path_segments) lg.out(8, 'ftp_server._cbRestoreDone %s %s' % (ret, pth)) if ret['status'] != 'OK': return result_defer.errback(FileNotFoundError(pth)) if not ret['result']['downloaded']: return result_defer.errback(FileNotFoundError(pth)) fp = filepath.FilePath(os.path.join(ret['local_path'], os.path.basename(ret['remote_path']))) try: fobj = fp.open('r') except: return result_defer.errback(FileNotFoundError(pth)) fr = _FileReader(fobj) return result_defer.callback(fr)
def test_copyToDirectory(self): """ L{FilePath.copyTo} makes a copy of all the contents of the directory named by that L{FilePath} if it is able to do so. """ oldPaths = list(self.path.walk()) # Record initial state fp = filepath.FilePath(self.mktemp()) self.path.copyTo(fp) self.path.remove() fp.copyTo(self.path) newPaths = list(self.path.walk()) # Record double-copy state newPaths.sort() oldPaths.sort() self.assertEquals(newPaths, oldPaths)