def test_branches(self): confRegEx = mock.MagicMock(return_value=re.compile('^(.*\\-command|Git|DEFAULT|Database)$')) with mock.patch('gitdh.module.ModuleLoader.getConfRegEx', new=confRegEx): c = Config() c.read_string(self.cStr) self.assertTrue(isinstance(c.branches, ConfigBranches)) self.assertEqual(len(c.branches), 2) self.assertNotIn('feature-xyz', c.branches) self.assertNotIn('Git', c.branches) self.assertNotIn('Database', c.branches) self.assertNotIn('DEFAULT', c.branches) self.assertNotIn('crunch-command', c.branches) self.assertIn('development', c.branches) self.assertIn('master', c.branches) self.assertRaises(KeyError, lambda: c.branches['feature-xyz']) self.assertRaises(KeyError, lambda: c.branches['Git']) self.assertRaises(KeyError, lambda: c.branches['Database']) self.assertRaises(KeyError, lambda: c.branches['DEFAULT']) self.assertRaises(KeyError, lambda: c.branches['crunch-command']) self.assertSetEqual(set((i for i in c.branches)), set(('development', 'master'))) self.assertIsInstance(c.branches['development'], SectionProxy) self.assertIn('Path', c.branches['development']) self.assertNotIn('External', c.branches['development']) c['development']['External'] = 'False' self.assertIn('External', c.branches['development']) confRegEx.assert_called_once_with()
def test_branches(self): confRegEx = mock.MagicMock(return_value=re.compile("^(.*\\-command|Git|DEFAULT|Database)$")) with mock.patch("gitdh.module.ModuleLoader.getConfRegEx", new=confRegEx): c = Config() c.read_string(self.cStr) self.assertTrue(isinstance(c.branches, ConfigBranches)) self.assertEqual(len(c.branches), 2) self.assertNotIn("feature-xyz", c.branches) self.assertNotIn("Git", c.branches) self.assertNotIn("Database", c.branches) self.assertNotIn("DEFAULT", c.branches) self.assertNotIn("crunch-command", c.branches) self.assertIn("development", c.branches) self.assertIn("master", c.branches) self.assertRaises(KeyError, lambda: c.branches["feature-xyz"]) self.assertRaises(KeyError, lambda: c.branches["Git"]) self.assertRaises(KeyError, lambda: c.branches["Database"]) self.assertRaises(KeyError, lambda: c.branches["DEFAULT"]) self.assertRaises(KeyError, lambda: c.branches["crunch-command"]) self.assertSetEqual(set((i for i in c.branches)), set(("development", "master"))) self.assertIsInstance(c.branches["development"], SectionProxy) self.assertIn("Path", c.branches["development"]) self.assertNotIn("External", c.branches["development"]) c["development"]["External"] = "False" self.assertIn("External", c.branches["development"]) confRegEx.assert_called_once_with()
def test_command(self): c = Config() c.read_string(self.cStr) self.assertTrue("crunch-command" in c) sct = c["crunch-command"] self.assertIsInstance(sct, SectionProxy) self.assertEqual(sct["Mode"], "perfile") self.assertEqual(sct["RegExp"], "\.php$") self.assertEqual(sct["Command"], "eff_php_crunch ${f}")
def test_command(self): c = Config() c.read_string(self.cStr) self.assertTrue('crunch-command' in c) sct = c['crunch-command'] self.assertIsInstance(sct, SectionProxy) self.assertEqual(sct['Mode'], 'perfile') self.assertEqual(sct['RegExp'], '\.php$') self.assertEqual(sct['Command'], 'eff_php_crunch ${f}')
def test_bareGitRepo(self): d = tempfile.TemporaryDirectory() self._createBareGitRepo(d.name) c = Config.fromGitRepo(d.name) self.assertTrue("Database" in c) self.assertTrue("master" in c) self.assertEqual(c["Database"]["Engine"], "sqlite") self.assertEqual(c.repoPath, d.name) c = Config.fromPath(d.name) self.assertTrue("Database" in c) self.assertTrue("master" in c) self.assertEqual(c["Database"]["Engine"], "sqlite") self.assertEqual(c.repoPath, d.name) d.cleanup()
def test_bareGitRepo(self): d = tempfile.TemporaryDirectory() self._createBareGitRepo(d.name) c = Config.fromGitRepo(d.name) self.assertTrue('Database' in c) self.assertTrue('master' in c) self.assertEqual(c['Database']['Engine'], 'sqlite') self.assertEqual(c.repoPath, d.name) c = Config.fromPath(d.name) self.assertTrue('Database' in c) self.assertTrue('master' in c) self.assertEqual(c['Database']['Engine'], 'sqlite') self.assertEqual(c.repoPath, d.name) d.cleanup()
def test_filePath(self): f = tempfile.NamedTemporaryFile() f.write(self.cStr.encode()) f.flush() c = Config.fromFilePath(f.name) self.assertTrue("Database" in c) self.assertTrue("master" in c) self.assertEqual(c["Database"]["Engine"], "sqlite") self.assertEqual(c.repoPath, "/var/lib/gitolite/repositories/test.git") c = Config.fromPath(f.name) self.assertTrue("Database" in c) self.assertTrue("master" in c) self.assertEqual(c["Database"]["Engine"], "sqlite") self.assertEqual(c.repoPath, "/var/lib/gitolite/repositories/test.git") f.close()
def test_filePath(self): f = tempfile.NamedTemporaryFile() f.write(self.cStr.encode()) f.flush() c = Config.fromFilePath(f.name) self.assertTrue('Database' in c) self.assertTrue('master' in c) self.assertEqual(c['Database']['Engine'], 'sqlite') self.assertEqual(c.repoPath, '/var/lib/gitolite/repositories/test.git') c = Config.fromPath(f.name) self.assertTrue('Database' in c) self.assertTrue('master' in c) self.assertEqual(c['Database']['Engine'], 'sqlite') self.assertEqual(c.repoPath, '/var/lib/gitolite/repositories/test.git') f.close()
def test_gitRepo(self): path = "/home/git/repositories/test.git" isdir = mock.MagicMock(return_value=True) git = mock.MagicMock() git.return_value = git git.getBranches.return_value = ["master", "development", "gitdh"] confFile = mock.MagicMock() confFile.getFileName.return_value = "gitdh.conf" confFile.getFileContent.return_value = self.cStr git.getFiles.return_value = [confFile] with mock.patch("gitdh.git.Git", new=git): c = Config.fromGitRepo(path) self.assertTrue("Database" in c) self.assertTrue("master" in c) self.assertEqual(c["Database"]["Engine"], "sqlite") self.assertEqual(c.repoPath, path) git.assert_called_once_with(path) git.getBranches.assert_called_once_with() git.getFiles.assert_called_once_with(branch="gitdh") confFile.getFileName.assert_called_once_with() confFile.getFileContent.assert_called_once_with() git.reset_mock() confFile.reset_mock() with mock.patch("os.path.isdir", new=isdir): c = Config.fromPath(path) self.assertTrue("Database" in c) self.assertTrue("master" in c) self.assertEqual(c["Database"]["Engine"], "sqlite") self.assertEqual(c.repoPath, path) git.assert_called_once_with(path) git.getBranches.assert_called_once_with() git.getFiles.assert_called_once_with(branch="gitdh") confFile.getFileName.assert_called_once_with() confFile.getFileContent.assert_called_once_with() isdir.assert_called_once_with(path)
def test_gitRepo(self): path = "/home/git/repositories/test.git" isdir = mock.MagicMock(return_value=True) git = mock.MagicMock() git.return_value = git git.getBranches.return_value = ["master", "development", "gitdh"] confFile = mock.MagicMock() confFile.getFileName.return_value = "gitdh.conf" confFile.getFileContent.return_value = self.cStr git.getFiles.return_value = [confFile] with mock.patch('gitdh.git.Git', new=git): c = Config.fromGitRepo(path) self.assertTrue('Database' in c) self.assertTrue('master' in c) self.assertEqual(c['Database']['Engine'], 'sqlite') self.assertEqual(c.repoPath, path) git.assert_called_once_with(path) git.getBranches.assert_called_once_with() git.getFiles.assert_called_once_with(branch='gitdh') confFile.getFileName.assert_called_once_with() confFile.getFileContent.assert_called_once_with() git.reset_mock() confFile.reset_mock() with mock.patch('os.path.isdir', new=isdir): c = Config.fromPath(path) self.assertTrue('Database' in c) self.assertTrue('master' in c) self.assertEqual(c['Database']['Engine'], 'sqlite') self.assertEqual(c.repoPath, path) git.assert_called_once_with(path) git.getBranches.assert_called_once_with() git.getFiles.assert_called_once_with(branch='gitdh') confFile.getFileName.assert_called_once_with() confFile.getFileContent.assert_called_once_with() isdir.assert_called_once_with(path)
def test_file(self): f = tempfile.TemporaryFile(mode="w+") f.write(self.cStr) f.flush() f.seek(0) c = Config.fromFile(f) self.assertTrue("Database" in c) self.assertTrue("master" in c) self.assertEqual(c["Database"]["Engine"], "sqlite") self.assertEqual(c.repoPath, "/var/lib/gitolite/repositories/test.git") f.close()
def test_file(self): f = tempfile.TemporaryFile(mode='w+') f.write(self.cStr) f.flush() f.seek(0) c = Config.fromFile(f) self.assertTrue('Database' in c) self.assertTrue('master' in c) self.assertEqual(c['Database']['Engine'], 'sqlite') self.assertEqual(c.repoPath, '/var/lib/gitolite/repositories/test.git') f.close()
def installPostreceive(printOnly=False, force=False, quiet=False, mode="755", *target): "Install post-receive hooks" if force and printOnly: print("Invalid options: --printOnly and --force both set", file=sys.stderr) sys.exit(1) if len(target) == 0: print("Please provide at least one target, for more info call with -h", file=sys.stderr) sys.exit(1) filesToWrite = {} virtEnvStr = "" if "VIRTUAL_ENV" in os.environ: virtEnvStr = ". %s/bin/activate\n" % (quote(os.environ["VIRTUAL_ENV"]),) for t in target: try: c = Config.fromFilePath(t) if c.repoPath is None: raise Exception("Missing RepositoryPath for '%s'" % (t)) fPath = os.path.join(c.repoPath, "hooks/post-receive") if not printOnly and not os.access(os.path.dirname(fPath), os.W_OK): raise Exception("Can't write to '%s'" % (fPath,)) if not printOnly and os.path.exists(fPath) and not force: raise Exception("'%s' exists already, use --force to overwrite" % (fPath,)) configFile = os.path.abspath(t) filesToWrite[fPath] = postreceiveContent.format(configFile, virtEnvStr) except Exception as e: print(e, file=sys.stderr) sys.exit(1) for (path, content) in filesToWrite.items(): if printOnly: yield "# File '%s'" % path yield content else: try: if not quiet: yield "Writing post-receive hook '%s'" % (path,) with open(path, "w") as f: f.write(content) # Only on UNIX # os.fchmod(f, mode) os.chmod(fPath, int(mode, 8)) except (IOError, OSError) as e: print(e, file=sys.stderr) print("Please check '%s'" % (path,), file=sys.stderr) sys.exit(1)
def gitDhMain(target, action, args, dbBe=None): config = Config.fromPath(target) if config.repoPath is None: raise Exception("Missing RepositoryPath in '%s'" % (target,)) if dbBe is None and "Database" in config: dbBe = DatabaseBackend.getDatabaseBackend(config) enabledModules = [] modules = ModuleLoader().initModuleObjects(config, args, dbBe) for module in modules: if module.isEnabled(action): enabledModules.append(module) commitCycle(enabledModules)
def installCron( name, user=None, printOnly=False, force=False, quiet=False, mailto="root", unixPath=None, interval="*/5 * * * *", mode="644", *target ): "Install a cron job in /etc/conf.d" if force and printOnly: print("Invalid options: --printOnly and --force both set", file=sys.stderr) sys.exit(1) if len(target) == 0: print("Please provide at least one target, for more info call with -h", file=sys.stderr) sys.exit(1) if user is None: user = pwd.getpwuid(os.getuid())[0] fPath = os.path.join("/etc/cron.d", name) try: for t in target: Config.fromPath(t) if not printOnly and not os.access(os.path.dirname(fPath), os.W_OK): raise Exception("Can't write to '%s'" % (fPath,)) if not printOnly and os.path.exists(fPath) and not force: raise Exception("'%s' already exists, use --force to overwrite" % (fPath,)) except Exception as e: print(e, file=sys.stderr) sys.exit(1) cmdStr = "git-dh cron" for t in target: cmdStr += " " + quote(os.path.abspath(t)) if "VIRTUAL_ENV" in os.environ: virtEnvPath = os.path.join(os.environ["VIRTUAL_ENV"], "bin", "activate") cmdStr = ". %s; %s" % (quote(virtEnvPath), cmdStr) cmdStr = "bash -c %s" % (quote(cmdStr),) if unixPath is None: unixPath = os.environ.get("PATH", "") content = cronContent.format(fPath, unixPath, mailto, interval, user, cmdStr) if printOnly: yield "# File '%s'" % fPath yield content else: try: if not quiet: yield "Writing cron job '%s'" % (fPath,) with open(fPath, "w") as f: f.write(content) # Only on UNIX # os.fchmod(f, mode) os.chmod(fPath, int(mode, 8)) except (IOError, OSError) as e: print(e, file=sys.stderr) print("Please check '%s'" % (fPath,), file=sys.stderr) sys.exit(1)