def run(self, options, **params): app = self.app pid_file = options.pid_file if pid_file: if os.path.isfile(pid_file): pid = Pidfile(pid_file).read() if not pid: raise CommandError('No pid in pid file %s' % pid_file) else: raise CommandError('Could not located pid file %s' % pid_file) else: raise CommandError('Pid file not available') try: self.kill(pid, signal.SIGTERM) except ProcessLookupError: raise CommandError('Process %d does not exist' % pid) from None start = time.time() while time.time() - start < options.timeout: if os.path.isfile(pid_file): time.sleep(0.2) else: app.write('Process %d terminated' % pid) return 0 app.write_err('Could not terminate process after %d seconds' % options.timeout) self.kill(pid, signal.SIGKILL) app.write_err('Processed %d killed' % pid) return 1
def run(self, options): auth_backend = self.app.auth_backend request = self.app.wsgi_request(urlargs={}, app_handler=True) request.cache.auth_backend = auth_backend form_class = get_form_class(request, 'create-application') if not form_class: raise CommandError('Cannot create application') model = self.app.models['applications'] ID = request.config['MASTER_APPLICATION_ID'] if not ID: raise CommandError( 'MASTER_APPLICATION_ID not available in config.\n' 'Create a UUID with the create_uuid command') try: app_domain = model.get_instance(request, id=ID) except Http404: form = form_class(request, data=dict( id=ID, name=slugify(request.config['APP_NAME']), ), model='applications') if form.is_valid(): app_domain = model.create_model(request, data=form.cleaned_data) else: raise CommandError(form.message()) self.write('Successfully created admin application') data = model.tojson(request, app_domain) jwt = model.jwt(request, app_domain) self.write(json.dumps(data, indent=4)) return jwt
def run(self, options): if not options.luxname: raise CommandError("A project name is required") name = options.luxname validate_name(name) target = path.join(os.getcwd(), '%s-project' % name) if path.exists(target): raise CommandError("%r conflicts with an existing path" % target) # Check that the name cannot be imported. try: import_module(name) except ImportError: pass else: raise CommandError("%r conflicts with the name of an " "existing Python module and cannot be " "used as a name.\n" "Please try another name." % name) # # if some directory is given, make sure it's nicely expanded try: os.makedirs(target) except OSError as e: raise CommandError(str(e)) from e self.build(name, target) self.write('Project "%s" created' % name) return target
def run(self, options, **params): auth_backend = self.app.auth_backend request = self.app.wsgi_request(urlargs={}, app_handler=True) request.cache.auth_backend = auth_backend model = self.app.models['applications'] app_id = options.app_id or request.config['MASTER_APPLICATION_ID'] if not app_id: raise CommandError('app_id not available') try: app_domain = model.get_instance(request, id=app_id) except Http404: raise CommandError('app_id %s not available' % app_id) from None token = model.regenerate_token(request, app_domain) self.write(token) return token
def run(self, options, interactive=False): username = options.username if not username: interactive = True request = self.app.wsgi_request() auth_backend = self.app.auth_backend auth_backend.request(request.environ) if interactive: # pragma nocover def_username = get_def_username(request, auth_backend) input_msg = 'Username' if def_username: input_msg += ' (Leave blank to use %s)' % def_username while not username: username = input(input_msg + ': ') if def_username and username == '': username = def_username user = auth_backend.get_user(request, username=username) if user is None: raise CommandError('user %s not available' % username) token = auth_backend.create_token(request, user) self.write('Token: %s' % as_hex(token.id)) return token
def validate_name(name): # If it's not a valid directory name. if not re.search(r'^[_a-zA-Z]\w*$', name): # Provide a smart error message, depending on the error. if not re.search(r'^[_a-zA-Z]', name): message = 'make sure the name begins with a letter or underscore' else: message = 'use only numbers, letters and underscores' raise CommandError("%r is not a valid project name. Please %s." % (name, message))
def run_alembic_cmd(self, opt): ''' Logic for running different Alembic commands. ''' from alembic import command as alembic_cmd config = self.get_config() # command consume any number of parameters but first is command name cmd = opt.command.pop(0) # init command needs to point to lux template, not alembic default if cmd == 'init': dirname = config.get_main_option('script_location') # line 63 will be executed in: # https://github.com/zzzeek/alembic/blob/master/alembic/command.py # since we do not use any *.ini file, we simply silence error # about referenced before assignment as it have no negative impact. try: alembic_cmd.init(config, dirname, template='lux') except UnboundLocalError: # pragma nocover pass # merge required two revision name elif cmd == 'merge': if len(opt.command) != 2: raise CommandError('Command %s required revisions id.' % cmd) alembic_cmd.merge(config, opt.command, message=opt.msg, branch_label=opt.branch) elif cmd == 'revision': alembic_cmd.revision(config, message=opt.msg, branch_label=opt.branch) # auto command is a shortcut for `revision --autogenerate` elif cmd == 'auto': alembic_cmd.revision(config, autogenerate=True, message=opt.msg, branch_label=opt.branch) # this commands required revision name, but do not take any message or # branch labels elif cmd in ('show', 'stamp', 'upgrade', 'downgrade'): if len(opt.command) != 1: raise CommandError('Command %s required revision id' % cmd) getattr(alembic_cmd, cmd)(config, *opt.command) else: # execute commands without any additional params getattr(alembic_cmd, cmd)(config)
def run(self, opt): ''' Run obvious commands and validate more complex. ''' from alembic import util try: if opt.list: available = 'Alembic commands:\n%s' % ', '.join(self.commands) self.write(available) return available if opt.command: cmd = opt.command[0] if cmd not in self.commands: raise CommandError('Unrecognized command %s' % opt.command[0]) if cmd in ('auto', 'revision', 'merge') and not opt.msg: raise CommandError('Missing [-m] parameter for %s' % cmd) self.run_alembic_cmd(opt) return True raise CommandError('Pass [--commands] for available commands') except util.CommandError as exc: raise CommandError(str(exc))
def run(self, options, interactive=False): username = options.username if not username: interactive = True request = self.app.wsgi_request() auth_backend = self.app.auth_backend if interactive: # pragma nocover try: # Get a username username = input('username: '******'username "%s" is not available' % username) token = auth_backend.create_token(request, user) if token: self.write("token created successfully.\n") self.write(token.id.hex) else: raise CommandError("Could not create token") return token.id.hex
async def run(self, options): try: from bs4 import BeautifulSoup as bs except ImportError: raise CommandError( 'Requires BeautifulSoup: pip install beautifulsoup4') from None path = options.static_path if not path: path = os.getcwd() if not options.base_url: raise CommandError('specify base url with --base-url flag') base = options.base_url[0] self.app.config['CACHE_SERVER'] = 'static://' self.bs = bs self.http = HttpTestClient(self.app.callable, wsgi=self.app) self.files = {} urls = ['%s/sitemap.xml' % base] self.app.cms.set_priority = False await self.build_from_rurls(path, base, urls) self.app.cms.set_priority = True self.app.cache_server.clear() self.files = {} await self.build_from_rurls(path, base, urls)