def getpermissions(self, req, repos): session_user = req.session['user'] asking_user = user.User(session_user['name']) path = req.post.get('getPermissions') branch_or_path = Path(path) if not repos.has_path_permissions: branch_or_path = branch_or_path.lstrip('/') perms = permissions.list_by_path(repos.name, repos.vcs_type, path) usernames = [] if 'userlist' in req.post: usernames = user.list(asking_user) groupnames = [] if 'grouplist' in req.post: groupnames = group.list(asking_user) templatevars = { 'perms': perms, 'repository': repos.name, 'path': branch_or_path, 'usernames': usernames, 'groupnames': groupnames } return XMLTemplateResponse('ajax/repositoryperms.xml', templatevars)
def set_init_var(self, key, val): defval = self.defaults[key] if isinstance(Path(''), type(defval)): p = Path(str(val), append_slash=defval.append_slash) self.init_vars[key] = p return self.init_vars[key] = val
def env_path(key=None, default=None): from submin.bootstrap import settings # for base_dir base = Path(os.path.normpath(settings.base_dir)) if key == None: return base path = Path(value(key, default=default)) if path.absolute: return path return base + path
def _fix(self, unixuser): base_dir = Path(self.sa.env) apache = www_user(unixuser) directory = str(base_dir) user = apache.pw_uid group = apache.pw_gid self.fix_mode_paths = [ os.path.join(directory, x) for x in self.fix_mode_dirs ] if not self._change_item(directory, user, group): return for root, dirs, files in os.walk(directory): for f in files: path = os.path.join(root, f) self._change_item(path, user, group) # make a copy of dirs, because we might change it while we # iterate over it. for d in list(dirs): path = os.path.join(root, d) if path in self.ignore_dirs: dirs.remove(d) continue self._change_item(path, user, group)
def setUp(self): self.submin_env = Path(tempfile.mkdtemp(prefix='submin-unittest')) conf_dir = self.submin_env + 'conf' os.mkdir(conf_dir) mock_settings.base_dir = self.submin_env storage.open(mock_settings) storage.database_evolve() self.sa = MagicMock() self.sa.env = self.submin_env self.saved_stdout, sys.stdout = sys.stdout, StringIO()
def setUp(self): self.submin_env = Path(tempfile.mkdtemp(prefix='submin-unittest')) conf_dir = self.submin_env + 'conf' os.mkdir(conf_dir) mock_settings.base_dir = self.submin_env storage.open(mock_settings) storage.database_evolve() options.set_value('svn_authz_file', conf_dir + 'authz') # needed for export options.set_value('svn_dir', self.submin_env + 'svn') # needed for export options.set_value('git_dir', self.submin_env + 'git') options.set_value('vcs_plugins', 'svn, git')
def _set_systemdirs(self): basefile = Path(__file__) if not basefile.absolute: basefile = Path(os.getcwd()) + basefile # Basefile will contain <basedir>/lib/subminadmin/__init__.py subminadmin_basedir = basefile.dirname() basedir_lib = os.path.dirname(subminadmin_basedir) basedir = Path(basedir_lib) + 'static' basedir_www = basedir + 'www' self.basedir = basedir self.basedir_lib = Path(basedir_lib) self.basedir_www = basedir_www
def getpermissions(self, req, repos): session_user = req.session['user'] asking_user = user.User(session_user['name']) path = req.post.get('getPermissions') branch_or_path = Path(path) if not repos.has_path_permissions: branch_or_path = branch_or_path.lstrip('/') perms = permissions.list_by_path(repos.name, repos.vcs_type, path) usernames = [] if 'userlist' in req.post: usernames = user.list(asking_user) groupnames = [] if 'grouplist' in req.post: groupnames = group.list(asking_user) templatevars = {'perms': perms, 'repository': repos.name, 'path': branch_or_path, 'usernames': usernames, 'groupnames': groupnames} return XMLTemplateResponse('ajax/repositoryperms.xml', templatevars)
def subcmd_hook(self, argv): """This is hidden from help because it is not meant to be run, except from commit/receive hooks """ from submin.path.path import Path from submin.models import hookjobs from submin.models import options from submin.subminadmin import trac #import urllib2 from urllib import request, error if argv[0] != 'queue' or len(argv) != 4: print('Unknown command') return vcs_type, repository, hooktype = argv[1:] content = ''.join(sys.stdin.readlines()) print('Notifying Trac of changes...') if hooktype == 'trac-sync' and 'refs/tags' in content: print('Skipping tag (no sync needed)') return hookjobs.queue(vcs_type, repository, hooktype, content) baseurl = Path(options.http_vhost() + options.url_path('base_url_submin')) joburl = str(baseurl + 'hooks' + hooktype + vcs_type + repository) try: response = urllib2.urlopen(joburl) except urllib2.HTTPError as e: print('Job queued, but could not sync to "%s", HTTP error %u' % ( joburl, e.code, )) except urllib2.URLError as e: print('Job queued, but URL invalid %s: %s' % (joburl, str(e))) else: xml = response.read() if 'success="True"' not in xml: print('Failed to sync:\n%s' % xml) # TODO: don't process XML with regexps... messages = re.sub('.*<errormsgs>(.*)</errormsgs>.*', '\\1', xml, flags=re.DOTALL) messages = re.sub('<msg>(.*)</msg>', '\\1\n', messages) if "" != messages: print('WARNING: Synced, but got some messages:\n%s' % messages)
def convert(self, old_config_file): config = self.read_ini(old_config_file) self.init_storage() self.write_options(config) self.write_users(config) self.write_groups(config) self.write_permissions(config) # final initialize (for convenience) self.sa.execute(['upgrade', 'hooks', 'no-fix-unixperms']) self.sa.execute(['unixperms', 'fix']) confdir = Path(self.sa.env) + 'conf' cgiconf = confdir + 'apache.cgi.conf' wsgiconf = confdir + 'apache.wsgi.conf' self.sa.execute(['apacheconf', 'create', 'cgi', str(cgiconf)]) self.sa.execute(['apacheconf', 'create', 'wsgi', str(wsgiconf)])
def generate_cgi(self): common.create_dir(self.env, Path('cgi-bin')) fname = self.env + "cgi-bin" + "submin.cgi" fp = open(str(fname), "w+") suggestion = '/path/to/submin' if 'PYTHONPATH' in os.environ: suggestion = os.path.abspath(os.environ["PYTHONPATH"].split(":")[0]) fp.write("""#!/usr/bin/env python2 # If you installed submin in a non-standard path, uncomment the two lines below # and insert your submin path. #import sys #sys.path.append("%s") from submin.dispatch.cgirunner import run run() """ % suggestion) fp.close() os.chmod(str(fname), 0o755)
def have_trac_sync_access(): baseurl = Path(options.http_vhost() + options.url_path('base_url_submin')) # because we don't specify a full path, this will never succeed, but # it will set the 'inacl' attribute to True/False joburl = str(baseurl + 'hooks' + 'trac-sync') try: response = urllib2.urlopen(joburl, timeout=2) except urllib2.HTTPError as e: raise SyncError('HTTP error: %s' % str(e)) except urllib2.URLError as e: raise SyncError('URL invalid %u: %s' % (e.reason[0], e.reason[1])) except socket.timeout as e: raise SyncError('Timeout: are we running a single-threaded server?') root = ET.fromstring(response.read()) command = root.find('./command') if command is None: raise SyncError(root) if 'inacl' not in command.attrib or command.attrib['inacl'].lower( ) == 'false': msgnodes = root.findall('./command/errormsgs/msg') raise SyncError('\n'.join([x.text for x in msgnodes]))
def __init__(self, sa, argv): self.sa = sa self.env = Path(self.sa.env) self.argv = argv self.settings_path = str(Path(self.sa.env) + 'conf' + 'settings.py')
def __init__(self, sa, argv): self.sa = sa self.env = Path(sa.env) self.argv = argv
def url_path(key): return Path(value(key), append_slash=True)
def __init__(self, sa, argv): import socket self.sa = sa self.env = Path(self.sa.env) self.argv = argv self.defaults = { 'svn_dir': Path('svn'), 'git_dir': Path('git'), 'trac_dir': Path('trac'), 'http_base': Path('/'), 'http_vhost': socket.getfqdn(), 'trac_url': Path('trac'), 'submin_url': Path('submin'), 'svn_url': Path('svn'), 'create_user': '******', 'enable_features': 'svn, git, apache, nginx', 'smtp_from': 'Submin <root@%s>' % socket.getfqdn(), } self.init_vars = { 'conf_dir': Path('conf'), 'hooks_dir': Path('hooks'), } self.init_vars.update({ 'authz': self.init_vars['conf_dir'] + Path('authz'), 'htpasswd': self.init_vars['conf_dir'] + Path('htpasswd'), }) self.email = None
def create_env(self): """This is called when all info is gathered""" for key, value in self.defaults.items(): if key not in self.init_vars: self.init_vars[key] = value try: self.create_dir(self.env) self.create_dir(self.init_vars['svn_dir']) self.create_dir(self.init_vars['git_dir']) self.create_dir(self.init_vars['conf_dir']) self.create_dir(self.init_vars['trac_dir']) self.create_dir(self.init_vars['hooks_dir']) self.create_dir(Path('auth')) except OSError: return # already printed error message self.sa.execute(['config', 'defaults']) # check http_base p = self.init_vars['http_base'] if str(p) == "": self.init_vars['http_base'] = Path("/") # write changes to config from submin.models import options default_options = { 'base_url_submin': self._get_url('submin_url'), 'base_url_svn': self._get_url('svn_url'), 'base_url_trac': self._get_url('trac_url'), 'http_vhost': self.init_vars['http_vhost'], 'auth_type': 'sql', 'svn_dir': str(self.init_vars['svn_dir']), 'git_dir': str(self.init_vars['git_dir']), 'trac_dir': str(self.init_vars['trac_dir']), 'svn_authz_file': str(self.init_vars['authz']), 'smtp_from': self.init_vars['smtp_from'], 'commit_email_from': self.init_vars['commit_email_from'], } for (key, value) in default_options.items(): options.set_value(key, value) # add a user from submin.models import user if self.init_vars['create_user'] == "yes": # add an admin user u = user.add('admin', self.email, send_mail=False) u.is_admin = True try: u.prepare_password_reset('submin2-admin') except SendEmailError as e: print( 'WARNING: Could not send an e-mail, please install a mail server' ) print( 'WARNING: You can request a password reset for "admin" on the login page' ) self.sa.execute(['upgrade', 'hooks', 'no-fix-unixperms']) self.sa.execute(['unixperms', 'fix']) if 'apache' in self.init_vars['enable_features']: self.sa.execute(['apacheconf', 'create', 'all']) if 'nginx' in self.init_vars['enable_features']: self.sa.execute(['nginxconf', 'create', 'all']) if 'trac' in self.init_vars['enable_features']: self.sa.execute(['trac', 'init'])
def lib_path(): # __file__ returns <submin-static-dir>/lib/models/options.py # by calling dirname() twice, we get the lib dir return Path(os.path.dirname(os.path.dirname(__file__)))