repo stringlengths 7 55 | path stringlengths 4 223 | func_name stringlengths 1 134 | original_string stringlengths 75 104k | language stringclasses 1 value | code stringlengths 75 104k | code_tokens listlengths 19 28.4k | docstring stringlengths 1 46.9k | docstring_tokens listlengths 1 1.97k | sha stringlengths 40 40 | url stringlengths 87 315 | partition stringclasses 1 value |
|---|---|---|---|---|---|---|---|---|---|---|---|
Miserlou/Zappa | zappa/letsencrypt.py | sign_certificate | def sign_certificate():
"""
Get the new certificate.
Returns the signed bytes.
"""
LOGGER.info("Signing certificate...")
cmd = [
'openssl', 'req',
'-in', os.path.join(gettempdir(), 'domain.csr'),
'-outform', 'DER'
]
devnull = open(os.devnull, 'wb')
csr_der = subprocess.check_output(cmd, stderr=devnull)
code, result = _send_signed_request(DEFAULT_CA + "/acme/new-cert", {
"resource": "new-cert",
"csr": _b64(csr_der),
})
if code != 201:
raise ValueError("Error signing certificate: {0} {1}".format(code, result))
LOGGER.info("Certificate signed!")
return result | python | def sign_certificate():
"""
Get the new certificate.
Returns the signed bytes.
"""
LOGGER.info("Signing certificate...")
cmd = [
'openssl', 'req',
'-in', os.path.join(gettempdir(), 'domain.csr'),
'-outform', 'DER'
]
devnull = open(os.devnull, 'wb')
csr_der = subprocess.check_output(cmd, stderr=devnull)
code, result = _send_signed_request(DEFAULT_CA + "/acme/new-cert", {
"resource": "new-cert",
"csr": _b64(csr_der),
})
if code != 201:
raise ValueError("Error signing certificate: {0} {1}".format(code, result))
LOGGER.info("Certificate signed!")
return result | [
"def",
"sign_certificate",
"(",
")",
":",
"LOGGER",
".",
"info",
"(",
"\"Signing certificate...\"",
")",
"cmd",
"=",
"[",
"'openssl'",
",",
"'req'",
",",
"'-in'",
",",
"os",
".",
"path",
".",
"join",
"(",
"gettempdir",
"(",
")",
",",
"'domain.csr'",
")",
",",
"'-outform'",
",",
"'DER'",
"]",
"devnull",
"=",
"open",
"(",
"os",
".",
"devnull",
",",
"'wb'",
")",
"csr_der",
"=",
"subprocess",
".",
"check_output",
"(",
"cmd",
",",
"stderr",
"=",
"devnull",
")",
"code",
",",
"result",
"=",
"_send_signed_request",
"(",
"DEFAULT_CA",
"+",
"\"/acme/new-cert\"",
",",
"{",
"\"resource\"",
":",
"\"new-cert\"",
",",
"\"csr\"",
":",
"_b64",
"(",
"csr_der",
")",
",",
"}",
")",
"if",
"code",
"!=",
"201",
":",
"raise",
"ValueError",
"(",
"\"Error signing certificate: {0} {1}\"",
".",
"format",
"(",
"code",
",",
"result",
")",
")",
"LOGGER",
".",
"info",
"(",
"\"Certificate signed!\"",
")",
"return",
"result"
] | Get the new certificate.
Returns the signed bytes. | [
"Get",
"the",
"new",
"certificate",
".",
"Returns",
"the",
"signed",
"bytes",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/letsencrypt.py#L317-L339 | train |
Miserlou/Zappa | zappa/letsencrypt.py | encode_certificate | def encode_certificate(result):
"""
Encode cert bytes to PEM encoded cert file.
"""
cert_body = """-----BEGIN CERTIFICATE-----\n{0}\n-----END CERTIFICATE-----\n""".format(
"\n".join(textwrap.wrap(base64.b64encode(result).decode('utf8'), 64)))
signed_crt = open("{}/signed.crt".format(gettempdir()), "w")
signed_crt.write(cert_body)
signed_crt.close()
return True | python | def encode_certificate(result):
"""
Encode cert bytes to PEM encoded cert file.
"""
cert_body = """-----BEGIN CERTIFICATE-----\n{0}\n-----END CERTIFICATE-----\n""".format(
"\n".join(textwrap.wrap(base64.b64encode(result).decode('utf8'), 64)))
signed_crt = open("{}/signed.crt".format(gettempdir()), "w")
signed_crt.write(cert_body)
signed_crt.close()
return True | [
"def",
"encode_certificate",
"(",
"result",
")",
":",
"cert_body",
"=",
"\"\"\"-----BEGIN CERTIFICATE-----\\n{0}\\n-----END CERTIFICATE-----\\n\"\"\"",
".",
"format",
"(",
"\"\\n\"",
".",
"join",
"(",
"textwrap",
".",
"wrap",
"(",
"base64",
".",
"b64encode",
"(",
"result",
")",
".",
"decode",
"(",
"'utf8'",
")",
",",
"64",
")",
")",
")",
"signed_crt",
"=",
"open",
"(",
"\"{}/signed.crt\"",
".",
"format",
"(",
"gettempdir",
"(",
")",
")",
",",
"\"w\"",
")",
"signed_crt",
".",
"write",
"(",
"cert_body",
")",
"signed_crt",
".",
"close",
"(",
")",
"return",
"True"
] | Encode cert bytes to PEM encoded cert file. | [
"Encode",
"cert",
"bytes",
"to",
"PEM",
"encoded",
"cert",
"file",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/letsencrypt.py#L342-L352 | train |
Miserlou/Zappa | zappa/letsencrypt.py | _send_signed_request | def _send_signed_request(url, payload):
"""
Helper function to make signed requests to Boulder
"""
payload64 = _b64(json.dumps(payload).encode('utf8'))
out = parse_account_key()
header = get_boulder_header(out)
protected = copy.deepcopy(header)
protected["nonce"] = urlopen(DEFAULT_CA + "/directory").headers['Replay-Nonce']
protected64 = _b64(json.dumps(protected).encode('utf8'))
cmd = [
'openssl', 'dgst',
'-sha256',
'-sign', os.path.join(gettempdir(), 'account.key')
]
proc = subprocess.Popen(
cmd,
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
out, err = proc.communicate("{0}.{1}".format(protected64, payload64).encode('utf8'))
if proc.returncode != 0: # pragma: no cover
raise IOError("OpenSSL Error: {0}".format(err))
data = json.dumps({
"header": header, "protected": protected64,
"payload": payload64, "signature": _b64(out),
})
try:
resp = urlopen(url, data.encode('utf8'))
return resp.getcode(), resp.read()
except IOError as e:
return getattr(e, "code", None), getattr(e, "read", e.__str__)() | python | def _send_signed_request(url, payload):
"""
Helper function to make signed requests to Boulder
"""
payload64 = _b64(json.dumps(payload).encode('utf8'))
out = parse_account_key()
header = get_boulder_header(out)
protected = copy.deepcopy(header)
protected["nonce"] = urlopen(DEFAULT_CA + "/directory").headers['Replay-Nonce']
protected64 = _b64(json.dumps(protected).encode('utf8'))
cmd = [
'openssl', 'dgst',
'-sha256',
'-sign', os.path.join(gettempdir(), 'account.key')
]
proc = subprocess.Popen(
cmd,
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
out, err = proc.communicate("{0}.{1}".format(protected64, payload64).encode('utf8'))
if proc.returncode != 0: # pragma: no cover
raise IOError("OpenSSL Error: {0}".format(err))
data = json.dumps({
"header": header, "protected": protected64,
"payload": payload64, "signature": _b64(out),
})
try:
resp = urlopen(url, data.encode('utf8'))
return resp.getcode(), resp.read()
except IOError as e:
return getattr(e, "code", None), getattr(e, "read", e.__str__)() | [
"def",
"_send_signed_request",
"(",
"url",
",",
"payload",
")",
":",
"payload64",
"=",
"_b64",
"(",
"json",
".",
"dumps",
"(",
"payload",
")",
".",
"encode",
"(",
"'utf8'",
")",
")",
"out",
"=",
"parse_account_key",
"(",
")",
"header",
"=",
"get_boulder_header",
"(",
"out",
")",
"protected",
"=",
"copy",
".",
"deepcopy",
"(",
"header",
")",
"protected",
"[",
"\"nonce\"",
"]",
"=",
"urlopen",
"(",
"DEFAULT_CA",
"+",
"\"/directory\"",
")",
".",
"headers",
"[",
"'Replay-Nonce'",
"]",
"protected64",
"=",
"_b64",
"(",
"json",
".",
"dumps",
"(",
"protected",
")",
".",
"encode",
"(",
"'utf8'",
")",
")",
"cmd",
"=",
"[",
"'openssl'",
",",
"'dgst'",
",",
"'-sha256'",
",",
"'-sign'",
",",
"os",
".",
"path",
".",
"join",
"(",
"gettempdir",
"(",
")",
",",
"'account.key'",
")",
"]",
"proc",
"=",
"subprocess",
".",
"Popen",
"(",
"cmd",
",",
"stdin",
"=",
"subprocess",
".",
"PIPE",
",",
"stdout",
"=",
"subprocess",
".",
"PIPE",
",",
"stderr",
"=",
"subprocess",
".",
"PIPE",
")",
"out",
",",
"err",
"=",
"proc",
".",
"communicate",
"(",
"\"{0}.{1}\"",
".",
"format",
"(",
"protected64",
",",
"payload64",
")",
".",
"encode",
"(",
"'utf8'",
")",
")",
"if",
"proc",
".",
"returncode",
"!=",
"0",
":",
"# pragma: no cover",
"raise",
"IOError",
"(",
"\"OpenSSL Error: {0}\"",
".",
"format",
"(",
"err",
")",
")",
"data",
"=",
"json",
".",
"dumps",
"(",
"{",
"\"header\"",
":",
"header",
",",
"\"protected\"",
":",
"protected64",
",",
"\"payload\"",
":",
"payload64",
",",
"\"signature\"",
":",
"_b64",
"(",
"out",
")",
",",
"}",
")",
"try",
":",
"resp",
"=",
"urlopen",
"(",
"url",
",",
"data",
".",
"encode",
"(",
"'utf8'",
")",
")",
"return",
"resp",
".",
"getcode",
"(",
")",
",",
"resp",
".",
"read",
"(",
")",
"except",
"IOError",
"as",
"e",
":",
"return",
"getattr",
"(",
"e",
",",
"\"code\"",
",",
"None",
")",
",",
"getattr",
"(",
"e",
",",
"\"read\"",
",",
"e",
".",
"__str__",
")",
"(",
")"
] | Helper function to make signed requests to Boulder | [
"Helper",
"function",
"to",
"make",
"signed",
"requests",
"to",
"Boulder"
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/letsencrypt.py#L366-L398 | train |
Miserlou/Zappa | zappa/cli.py | shamelessly_promote | def shamelessly_promote():
"""
Shamelessly promote our little community.
"""
click.echo("Need " + click.style("help", fg='green', bold=True) +
"? Found a " + click.style("bug", fg='green', bold=True) +
"? Let us " + click.style("know", fg='green', bold=True) + "! :D")
click.echo("File bug reports on " + click.style("GitHub", bold=True) + " here: "
+ click.style("https://github.com/Miserlou/Zappa", fg='cyan', bold=True))
click.echo("And join our " + click.style("Slack", bold=True) + " channel here: "
+ click.style("https://slack.zappa.io", fg='cyan', bold=True))
click.echo("Love!,")
click.echo(" ~ Team " + click.style("Zappa", bold=True) + "!") | python | def shamelessly_promote():
"""
Shamelessly promote our little community.
"""
click.echo("Need " + click.style("help", fg='green', bold=True) +
"? Found a " + click.style("bug", fg='green', bold=True) +
"? Let us " + click.style("know", fg='green', bold=True) + "! :D")
click.echo("File bug reports on " + click.style("GitHub", bold=True) + " here: "
+ click.style("https://github.com/Miserlou/Zappa", fg='cyan', bold=True))
click.echo("And join our " + click.style("Slack", bold=True) + " channel here: "
+ click.style("https://slack.zappa.io", fg='cyan', bold=True))
click.echo("Love!,")
click.echo(" ~ Team " + click.style("Zappa", bold=True) + "!") | [
"def",
"shamelessly_promote",
"(",
")",
":",
"click",
".",
"echo",
"(",
"\"Need \"",
"+",
"click",
".",
"style",
"(",
"\"help\"",
",",
"fg",
"=",
"'green'",
",",
"bold",
"=",
"True",
")",
"+",
"\"? Found a \"",
"+",
"click",
".",
"style",
"(",
"\"bug\"",
",",
"fg",
"=",
"'green'",
",",
"bold",
"=",
"True",
")",
"+",
"\"? Let us \"",
"+",
"click",
".",
"style",
"(",
"\"know\"",
",",
"fg",
"=",
"'green'",
",",
"bold",
"=",
"True",
")",
"+",
"\"! :D\"",
")",
"click",
".",
"echo",
"(",
"\"File bug reports on \"",
"+",
"click",
".",
"style",
"(",
"\"GitHub\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" here: \"",
"+",
"click",
".",
"style",
"(",
"\"https://github.com/Miserlou/Zappa\"",
",",
"fg",
"=",
"'cyan'",
",",
"bold",
"=",
"True",
")",
")",
"click",
".",
"echo",
"(",
"\"And join our \"",
"+",
"click",
".",
"style",
"(",
"\"Slack\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" channel here: \"",
"+",
"click",
".",
"style",
"(",
"\"https://slack.zappa.io\"",
",",
"fg",
"=",
"'cyan'",
",",
"bold",
"=",
"True",
")",
")",
"click",
".",
"echo",
"(",
"\"Love!,\"",
")",
"click",
".",
"echo",
"(",
"\" ~ Team \"",
"+",
"click",
".",
"style",
"(",
"\"Zappa\"",
",",
"bold",
"=",
"True",
")",
"+",
"\"!\"",
")"
] | Shamelessly promote our little community. | [
"Shamelessly",
"promote",
"our",
"little",
"community",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L2744-L2757 | train |
Miserlou/Zappa | zappa/cli.py | handle | def handle(): # pragma: no cover
"""
Main program execution handler.
"""
try:
cli = ZappaCLI()
sys.exit(cli.handle())
except SystemExit as e: # pragma: no cover
cli.on_exit()
sys.exit(e.code)
except KeyboardInterrupt: # pragma: no cover
cli.on_exit()
sys.exit(130)
except Exception as e:
cli.on_exit()
click.echo("Oh no! An " + click.style("error occurred", fg='red', bold=True) + "! :(")
click.echo("\n==============\n")
import traceback
traceback.print_exc()
click.echo("\n==============\n")
shamelessly_promote()
sys.exit(-1) | python | def handle(): # pragma: no cover
"""
Main program execution handler.
"""
try:
cli = ZappaCLI()
sys.exit(cli.handle())
except SystemExit as e: # pragma: no cover
cli.on_exit()
sys.exit(e.code)
except KeyboardInterrupt: # pragma: no cover
cli.on_exit()
sys.exit(130)
except Exception as e:
cli.on_exit()
click.echo("Oh no! An " + click.style("error occurred", fg='red', bold=True) + "! :(")
click.echo("\n==============\n")
import traceback
traceback.print_exc()
click.echo("\n==============\n")
shamelessly_promote()
sys.exit(-1) | [
"def",
"handle",
"(",
")",
":",
"# pragma: no cover",
"try",
":",
"cli",
"=",
"ZappaCLI",
"(",
")",
"sys",
".",
"exit",
"(",
"cli",
".",
"handle",
"(",
")",
")",
"except",
"SystemExit",
"as",
"e",
":",
"# pragma: no cover",
"cli",
".",
"on_exit",
"(",
")",
"sys",
".",
"exit",
"(",
"e",
".",
"code",
")",
"except",
"KeyboardInterrupt",
":",
"# pragma: no cover",
"cli",
".",
"on_exit",
"(",
")",
"sys",
".",
"exit",
"(",
"130",
")",
"except",
"Exception",
"as",
"e",
":",
"cli",
".",
"on_exit",
"(",
")",
"click",
".",
"echo",
"(",
"\"Oh no! An \"",
"+",
"click",
".",
"style",
"(",
"\"error occurred\"",
",",
"fg",
"=",
"'red'",
",",
"bold",
"=",
"True",
")",
"+",
"\"! :(\"",
")",
"click",
".",
"echo",
"(",
"\"\\n==============\\n\"",
")",
"import",
"traceback",
"traceback",
".",
"print_exc",
"(",
")",
"click",
".",
"echo",
"(",
"\"\\n==============\\n\"",
")",
"shamelessly_promote",
"(",
")",
"sys",
".",
"exit",
"(",
"-",
"1",
")"
] | Main program execution handler. | [
"Main",
"program",
"execution",
"handler",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L2772-L2797 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.stage_config | def stage_config(self):
"""
A shortcut property for settings of a stage.
"""
def get_stage_setting(stage, extended_stages=None):
if extended_stages is None:
extended_stages = []
if stage in extended_stages:
raise RuntimeError(stage + " has already been extended to these settings. "
"There is a circular extends within the settings file.")
extended_stages.append(stage)
try:
stage_settings = dict(self.zappa_settings[stage].copy())
except KeyError:
raise ClickException("Cannot extend settings for undefined stage '" + stage + "'.")
extends_stage = self.zappa_settings[stage].get('extends', None)
if not extends_stage:
return stage_settings
extended_settings = get_stage_setting(stage=extends_stage, extended_stages=extended_stages)
extended_settings.update(stage_settings)
return extended_settings
settings = get_stage_setting(stage=self.api_stage)
# Backwards compatible for delete_zip setting that was more explicitly named delete_local_zip
if u'delete_zip' in settings:
settings[u'delete_local_zip'] = settings.get(u'delete_zip')
settings.update(self.stage_config_overrides)
return settings | python | def stage_config(self):
"""
A shortcut property for settings of a stage.
"""
def get_stage_setting(stage, extended_stages=None):
if extended_stages is None:
extended_stages = []
if stage in extended_stages:
raise RuntimeError(stage + " has already been extended to these settings. "
"There is a circular extends within the settings file.")
extended_stages.append(stage)
try:
stage_settings = dict(self.zappa_settings[stage].copy())
except KeyError:
raise ClickException("Cannot extend settings for undefined stage '" + stage + "'.")
extends_stage = self.zappa_settings[stage].get('extends', None)
if not extends_stage:
return stage_settings
extended_settings = get_stage_setting(stage=extends_stage, extended_stages=extended_stages)
extended_settings.update(stage_settings)
return extended_settings
settings = get_stage_setting(stage=self.api_stage)
# Backwards compatible for delete_zip setting that was more explicitly named delete_local_zip
if u'delete_zip' in settings:
settings[u'delete_local_zip'] = settings.get(u'delete_zip')
settings.update(self.stage_config_overrides)
return settings | [
"def",
"stage_config",
"(",
"self",
")",
":",
"def",
"get_stage_setting",
"(",
"stage",
",",
"extended_stages",
"=",
"None",
")",
":",
"if",
"extended_stages",
"is",
"None",
":",
"extended_stages",
"=",
"[",
"]",
"if",
"stage",
"in",
"extended_stages",
":",
"raise",
"RuntimeError",
"(",
"stage",
"+",
"\" has already been extended to these settings. \"",
"\"There is a circular extends within the settings file.\"",
")",
"extended_stages",
".",
"append",
"(",
"stage",
")",
"try",
":",
"stage_settings",
"=",
"dict",
"(",
"self",
".",
"zappa_settings",
"[",
"stage",
"]",
".",
"copy",
"(",
")",
")",
"except",
"KeyError",
":",
"raise",
"ClickException",
"(",
"\"Cannot extend settings for undefined stage '\"",
"+",
"stage",
"+",
"\"'.\"",
")",
"extends_stage",
"=",
"self",
".",
"zappa_settings",
"[",
"stage",
"]",
".",
"get",
"(",
"'extends'",
",",
"None",
")",
"if",
"not",
"extends_stage",
":",
"return",
"stage_settings",
"extended_settings",
"=",
"get_stage_setting",
"(",
"stage",
"=",
"extends_stage",
",",
"extended_stages",
"=",
"extended_stages",
")",
"extended_settings",
".",
"update",
"(",
"stage_settings",
")",
"return",
"extended_settings",
"settings",
"=",
"get_stage_setting",
"(",
"stage",
"=",
"self",
".",
"api_stage",
")",
"# Backwards compatible for delete_zip setting that was more explicitly named delete_local_zip",
"if",
"u'delete_zip'",
"in",
"settings",
":",
"settings",
"[",
"u'delete_local_zip'",
"]",
"=",
"settings",
".",
"get",
"(",
"u'delete_zip'",
")",
"settings",
".",
"update",
"(",
"self",
".",
"stage_config_overrides",
")",
"return",
"settings"
] | A shortcut property for settings of a stage. | [
"A",
"shortcut",
"property",
"for",
"settings",
"of",
"a",
"stage",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L127-L161 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.override_stage_config_setting | def override_stage_config_setting(self, key, val):
"""
Forcefully override a setting set by zappa_settings (for the current stage only)
:param key: settings key
:param val: value
"""
self._stage_config_overrides = getattr(self, '_stage_config_overrides', {})
self._stage_config_overrides.setdefault(self.api_stage, {})[key] = val | python | def override_stage_config_setting(self, key, val):
"""
Forcefully override a setting set by zappa_settings (for the current stage only)
:param key: settings key
:param val: value
"""
self._stage_config_overrides = getattr(self, '_stage_config_overrides', {})
self._stage_config_overrides.setdefault(self.api_stage, {})[key] = val | [
"def",
"override_stage_config_setting",
"(",
"self",
",",
"key",
",",
"val",
")",
":",
"self",
".",
"_stage_config_overrides",
"=",
"getattr",
"(",
"self",
",",
"'_stage_config_overrides'",
",",
"{",
"}",
")",
"self",
".",
"_stage_config_overrides",
".",
"setdefault",
"(",
"self",
".",
"api_stage",
",",
"{",
"}",
")",
"[",
"key",
"]",
"=",
"val"
] | Forcefully override a setting set by zappa_settings (for the current stage only)
:param key: settings key
:param val: value | [
"Forcefully",
"override",
"a",
"setting",
"set",
"by",
"zappa_settings",
"(",
"for",
"the",
"current",
"stage",
"only",
")",
":",
"param",
"key",
":",
"settings",
"key",
":",
"param",
"val",
":",
"value"
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L171-L178 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.handle | def handle(self, argv=None):
"""
Main function.
Parses command, load settings and dispatches accordingly.
"""
desc = ('Zappa - Deploy Python applications to AWS Lambda'
' and API Gateway.\n')
parser = argparse.ArgumentParser(description=desc)
parser.add_argument(
'-v', '--version', action='version',
version=pkg_resources.get_distribution("zappa").version,
help='Print the zappa version'
)
parser.add_argument(
'--color', default='auto', choices=['auto','never','always']
)
env_parser = argparse.ArgumentParser(add_help=False)
me_group = env_parser.add_mutually_exclusive_group()
all_help = ('Execute this command for all of our defined '
'Zappa stages.')
me_group.add_argument('--all', action='store_true', help=all_help)
me_group.add_argument('stage_env', nargs='?')
group = env_parser.add_argument_group()
group.add_argument(
'-a', '--app_function', help='The WSGI application function.'
)
group.add_argument(
'-s', '--settings_file', help='The path to a Zappa settings file.'
)
group.add_argument(
'-q', '--quiet', action='store_true', help='Silence all output.'
)
# https://github.com/Miserlou/Zappa/issues/407
# Moved when 'template' command added.
# Fuck Terraform.
group.add_argument(
'-j', '--json', action='store_true', help='Make the output of this command be machine readable.'
)
# https://github.com/Miserlou/Zappa/issues/891
group.add_argument(
'--disable_progress', action='store_true', help='Disable progress bars.'
)
##
# Certify
##
subparsers = parser.add_subparsers(title='subcommands', dest='command')
cert_parser = subparsers.add_parser(
'certify', parents=[env_parser],
help='Create and install SSL certificate'
)
cert_parser.add_argument(
'--manual', action='store_true',
help=("Gets new Let's Encrypt certificates, but prints them to console."
"Does not update API Gateway domains.")
)
cert_parser.add_argument(
'-y', '--yes', action='store_true', help='Auto confirm yes.'
)
##
# Deploy
##
deploy_parser = subparsers.add_parser(
'deploy', parents=[env_parser], help='Deploy application.'
)
deploy_parser.add_argument(
'-z', '--zip', help='Deploy Lambda with specific local or S3 hosted zip package'
)
##
# Init
##
init_parser = subparsers.add_parser('init', help='Initialize Zappa app.')
##
# Package
##
package_parser = subparsers.add_parser(
'package', parents=[env_parser], help='Build the application zip package locally.'
)
package_parser.add_argument(
'-o', '--output', help='Name of file to output the package to.'
)
##
# Template
##
template_parser = subparsers.add_parser(
'template', parents=[env_parser], help='Create a CloudFormation template for this API Gateway.'
)
template_parser.add_argument(
'-l', '--lambda-arn', required=True, help='ARN of the Lambda function to template to.'
)
template_parser.add_argument(
'-r', '--role-arn', required=True, help='ARN of the Role to template with.'
)
template_parser.add_argument(
'-o', '--output', help='Name of file to output the template to.'
)
##
# Invocation
##
invoke_parser = subparsers.add_parser(
'invoke', parents=[env_parser],
help='Invoke remote function.'
)
invoke_parser.add_argument(
'--raw', action='store_true',
help=('When invoking remotely, invoke this python as a string,'
' not as a modular path.')
)
invoke_parser.add_argument(
'--no-color', action='store_true',
help=("Don't color the output")
)
invoke_parser.add_argument('command_rest')
##
# Manage
##
manage_parser = subparsers.add_parser(
'manage',
help='Invoke remote Django manage.py commands.'
)
rest_help = ("Command in the form of <env> <command>. <env> is not "
"required if --all is specified")
manage_parser.add_argument('--all', action='store_true', help=all_help)
manage_parser.add_argument('command_rest', nargs='+', help=rest_help)
manage_parser.add_argument(
'--no-color', action='store_true',
help=("Don't color the output")
)
# This is explicitly added here because this is the only subcommand that doesn't inherit from env_parser
# https://github.com/Miserlou/Zappa/issues/1002
manage_parser.add_argument(
'-s', '--settings_file', help='The path to a Zappa settings file.'
)
##
# Rollback
##
def positive_int(s):
""" Ensure an arg is positive """
i = int(s)
if i < 0:
msg = "This argument must be positive (got {})".format(s)
raise argparse.ArgumentTypeError(msg)
return i
rollback_parser = subparsers.add_parser(
'rollback', parents=[env_parser],
help='Rollback deployed code to a previous version.'
)
rollback_parser.add_argument(
'-n', '--num-rollback', type=positive_int, default=1,
help='The number of versions to rollback.'
)
##
# Scheduling
##
subparsers.add_parser(
'schedule', parents=[env_parser],
help='Schedule functions to occur at regular intervals.'
)
##
# Status
##
status_parser = subparsers.add_parser(
'status', parents=[env_parser],
help='Show deployment status and event schedules.'
)
##
# Log Tailing
##
tail_parser = subparsers.add_parser(
'tail', parents=[env_parser], help='Tail deployment logs.'
)
tail_parser.add_argument(
'--no-color', action='store_true',
help="Don't color log tail output."
)
tail_parser.add_argument(
'--http', action='store_true',
help='Only show HTTP requests in tail output.'
)
tail_parser.add_argument(
'--non-http', action='store_true',
help='Only show non-HTTP requests in tail output.'
)
tail_parser.add_argument(
'--since', type=str, default="100000s",
help="Only show lines since a certain timeframe."
)
tail_parser.add_argument(
'--filter', type=str, default="",
help="Apply a filter pattern to the logs."
)
tail_parser.add_argument(
'--force-color', action='store_true',
help='Force coloring log tail output even if coloring support is not auto-detected. (example: piping)'
)
tail_parser.add_argument(
'--disable-keep-open', action='store_true',
help="Exit after printing the last available log, rather than keeping the log open."
)
##
# Undeploy
##
undeploy_parser = subparsers.add_parser(
'undeploy', parents=[env_parser], help='Undeploy application.'
)
undeploy_parser.add_argument(
'--remove-logs', action='store_true',
help=('Removes log groups of api gateway and lambda task'
' during the undeployment.'),
)
undeploy_parser.add_argument(
'-y', '--yes', action='store_true', help='Auto confirm yes.'
)
##
# Unschedule
##
subparsers.add_parser('unschedule', parents=[env_parser],
help='Unschedule functions.')
##
# Updating
##
update_parser = subparsers.add_parser(
'update', parents=[env_parser], help='Update deployed application.'
)
update_parser.add_argument(
'-z', '--zip', help='Update Lambda with specific local or S3 hosted zip package'
)
update_parser.add_argument(
'-n', '--no-upload', help="Update configuration where appropriate, but don't upload new code"
)
##
# Debug
##
subparsers.add_parser(
'shell', parents=[env_parser], help='A debug shell with a loaded Zappa object.'
)
argcomplete.autocomplete(parser)
args = parser.parse_args(argv)
self.vargs = vars(args)
if args.color == 'never':
disable_click_colors()
elif args.color == 'always':
#TODO: Support aggressive coloring like "--force-color" on all commands
pass
elif args.color == 'auto':
pass
# Parse the input
# NOTE(rmoe): Special case for manage command
# The manage command can't have both stage_env and command_rest
# arguments. Since they are both positional arguments argparse can't
# differentiate the two. This causes problems when used with --all.
# (e.g. "manage --all showmigrations admin" argparse thinks --all has
# been specified AND that stage_env='showmigrations')
# By having command_rest collect everything but --all we can split it
# apart here instead of relying on argparse.
if not args.command:
parser.print_help()
return
if args.command == 'manage' and not self.vargs.get('all'):
self.stage_env = self.vargs['command_rest'].pop(0)
else:
self.stage_env = self.vargs.get('stage_env')
if args.command == 'package':
self.load_credentials = False
self.command = args.command
self.disable_progress = self.vargs.get('disable_progress')
if self.vargs.get('quiet'):
self.silence()
# We don't have any settings yet, so make those first!
# (Settings-based interactions will fail
# before a project has been initialized.)
if self.command == 'init':
self.init()
return
# Make sure there isn't a new version available
if not self.vargs.get('json'):
self.check_for_update()
# Load and Validate Settings File
self.load_settings_file(self.vargs.get('settings_file'))
# Should we execute this for all stages, or just one?
all_stages = self.vargs.get('all')
stages = []
if all_stages: # All stages!
stages = self.zappa_settings.keys()
else: # Just one env.
if not self.stage_env:
# If there's only one stage defined in the settings,
# use that as the default.
if len(self.zappa_settings.keys()) == 1:
stages.append(list(self.zappa_settings.keys())[0])
else:
parser.error("Please supply a stage to interact with.")
else:
stages.append(self.stage_env)
for stage in stages:
try:
self.dispatch_command(self.command, stage)
except ClickException as e:
# Discussion on exit codes: https://github.com/Miserlou/Zappa/issues/407
e.show()
sys.exit(e.exit_code) | python | def handle(self, argv=None):
"""
Main function.
Parses command, load settings and dispatches accordingly.
"""
desc = ('Zappa - Deploy Python applications to AWS Lambda'
' and API Gateway.\n')
parser = argparse.ArgumentParser(description=desc)
parser.add_argument(
'-v', '--version', action='version',
version=pkg_resources.get_distribution("zappa").version,
help='Print the zappa version'
)
parser.add_argument(
'--color', default='auto', choices=['auto','never','always']
)
env_parser = argparse.ArgumentParser(add_help=False)
me_group = env_parser.add_mutually_exclusive_group()
all_help = ('Execute this command for all of our defined '
'Zappa stages.')
me_group.add_argument('--all', action='store_true', help=all_help)
me_group.add_argument('stage_env', nargs='?')
group = env_parser.add_argument_group()
group.add_argument(
'-a', '--app_function', help='The WSGI application function.'
)
group.add_argument(
'-s', '--settings_file', help='The path to a Zappa settings file.'
)
group.add_argument(
'-q', '--quiet', action='store_true', help='Silence all output.'
)
# https://github.com/Miserlou/Zappa/issues/407
# Moved when 'template' command added.
# Fuck Terraform.
group.add_argument(
'-j', '--json', action='store_true', help='Make the output of this command be machine readable.'
)
# https://github.com/Miserlou/Zappa/issues/891
group.add_argument(
'--disable_progress', action='store_true', help='Disable progress bars.'
)
##
# Certify
##
subparsers = parser.add_subparsers(title='subcommands', dest='command')
cert_parser = subparsers.add_parser(
'certify', parents=[env_parser],
help='Create and install SSL certificate'
)
cert_parser.add_argument(
'--manual', action='store_true',
help=("Gets new Let's Encrypt certificates, but prints them to console."
"Does not update API Gateway domains.")
)
cert_parser.add_argument(
'-y', '--yes', action='store_true', help='Auto confirm yes.'
)
##
# Deploy
##
deploy_parser = subparsers.add_parser(
'deploy', parents=[env_parser], help='Deploy application.'
)
deploy_parser.add_argument(
'-z', '--zip', help='Deploy Lambda with specific local or S3 hosted zip package'
)
##
# Init
##
init_parser = subparsers.add_parser('init', help='Initialize Zappa app.')
##
# Package
##
package_parser = subparsers.add_parser(
'package', parents=[env_parser], help='Build the application zip package locally.'
)
package_parser.add_argument(
'-o', '--output', help='Name of file to output the package to.'
)
##
# Template
##
template_parser = subparsers.add_parser(
'template', parents=[env_parser], help='Create a CloudFormation template for this API Gateway.'
)
template_parser.add_argument(
'-l', '--lambda-arn', required=True, help='ARN of the Lambda function to template to.'
)
template_parser.add_argument(
'-r', '--role-arn', required=True, help='ARN of the Role to template with.'
)
template_parser.add_argument(
'-o', '--output', help='Name of file to output the template to.'
)
##
# Invocation
##
invoke_parser = subparsers.add_parser(
'invoke', parents=[env_parser],
help='Invoke remote function.'
)
invoke_parser.add_argument(
'--raw', action='store_true',
help=('When invoking remotely, invoke this python as a string,'
' not as a modular path.')
)
invoke_parser.add_argument(
'--no-color', action='store_true',
help=("Don't color the output")
)
invoke_parser.add_argument('command_rest')
##
# Manage
##
manage_parser = subparsers.add_parser(
'manage',
help='Invoke remote Django manage.py commands.'
)
rest_help = ("Command in the form of <env> <command>. <env> is not "
"required if --all is specified")
manage_parser.add_argument('--all', action='store_true', help=all_help)
manage_parser.add_argument('command_rest', nargs='+', help=rest_help)
manage_parser.add_argument(
'--no-color', action='store_true',
help=("Don't color the output")
)
# This is explicitly added here because this is the only subcommand that doesn't inherit from env_parser
# https://github.com/Miserlou/Zappa/issues/1002
manage_parser.add_argument(
'-s', '--settings_file', help='The path to a Zappa settings file.'
)
##
# Rollback
##
def positive_int(s):
""" Ensure an arg is positive """
i = int(s)
if i < 0:
msg = "This argument must be positive (got {})".format(s)
raise argparse.ArgumentTypeError(msg)
return i
rollback_parser = subparsers.add_parser(
'rollback', parents=[env_parser],
help='Rollback deployed code to a previous version.'
)
rollback_parser.add_argument(
'-n', '--num-rollback', type=positive_int, default=1,
help='The number of versions to rollback.'
)
##
# Scheduling
##
subparsers.add_parser(
'schedule', parents=[env_parser],
help='Schedule functions to occur at regular intervals.'
)
##
# Status
##
status_parser = subparsers.add_parser(
'status', parents=[env_parser],
help='Show deployment status and event schedules.'
)
##
# Log Tailing
##
tail_parser = subparsers.add_parser(
'tail', parents=[env_parser], help='Tail deployment logs.'
)
tail_parser.add_argument(
'--no-color', action='store_true',
help="Don't color log tail output."
)
tail_parser.add_argument(
'--http', action='store_true',
help='Only show HTTP requests in tail output.'
)
tail_parser.add_argument(
'--non-http', action='store_true',
help='Only show non-HTTP requests in tail output.'
)
tail_parser.add_argument(
'--since', type=str, default="100000s",
help="Only show lines since a certain timeframe."
)
tail_parser.add_argument(
'--filter', type=str, default="",
help="Apply a filter pattern to the logs."
)
tail_parser.add_argument(
'--force-color', action='store_true',
help='Force coloring log tail output even if coloring support is not auto-detected. (example: piping)'
)
tail_parser.add_argument(
'--disable-keep-open', action='store_true',
help="Exit after printing the last available log, rather than keeping the log open."
)
##
# Undeploy
##
undeploy_parser = subparsers.add_parser(
'undeploy', parents=[env_parser], help='Undeploy application.'
)
undeploy_parser.add_argument(
'--remove-logs', action='store_true',
help=('Removes log groups of api gateway and lambda task'
' during the undeployment.'),
)
undeploy_parser.add_argument(
'-y', '--yes', action='store_true', help='Auto confirm yes.'
)
##
# Unschedule
##
subparsers.add_parser('unschedule', parents=[env_parser],
help='Unschedule functions.')
##
# Updating
##
update_parser = subparsers.add_parser(
'update', parents=[env_parser], help='Update deployed application.'
)
update_parser.add_argument(
'-z', '--zip', help='Update Lambda with specific local or S3 hosted zip package'
)
update_parser.add_argument(
'-n', '--no-upload', help="Update configuration where appropriate, but don't upload new code"
)
##
# Debug
##
subparsers.add_parser(
'shell', parents=[env_parser], help='A debug shell with a loaded Zappa object.'
)
argcomplete.autocomplete(parser)
args = parser.parse_args(argv)
self.vargs = vars(args)
if args.color == 'never':
disable_click_colors()
elif args.color == 'always':
#TODO: Support aggressive coloring like "--force-color" on all commands
pass
elif args.color == 'auto':
pass
# Parse the input
# NOTE(rmoe): Special case for manage command
# The manage command can't have both stage_env and command_rest
# arguments. Since they are both positional arguments argparse can't
# differentiate the two. This causes problems when used with --all.
# (e.g. "manage --all showmigrations admin" argparse thinks --all has
# been specified AND that stage_env='showmigrations')
# By having command_rest collect everything but --all we can split it
# apart here instead of relying on argparse.
if not args.command:
parser.print_help()
return
if args.command == 'manage' and not self.vargs.get('all'):
self.stage_env = self.vargs['command_rest'].pop(0)
else:
self.stage_env = self.vargs.get('stage_env')
if args.command == 'package':
self.load_credentials = False
self.command = args.command
self.disable_progress = self.vargs.get('disable_progress')
if self.vargs.get('quiet'):
self.silence()
# We don't have any settings yet, so make those first!
# (Settings-based interactions will fail
# before a project has been initialized.)
if self.command == 'init':
self.init()
return
# Make sure there isn't a new version available
if not self.vargs.get('json'):
self.check_for_update()
# Load and Validate Settings File
self.load_settings_file(self.vargs.get('settings_file'))
# Should we execute this for all stages, or just one?
all_stages = self.vargs.get('all')
stages = []
if all_stages: # All stages!
stages = self.zappa_settings.keys()
else: # Just one env.
if not self.stage_env:
# If there's only one stage defined in the settings,
# use that as the default.
if len(self.zappa_settings.keys()) == 1:
stages.append(list(self.zappa_settings.keys())[0])
else:
parser.error("Please supply a stage to interact with.")
else:
stages.append(self.stage_env)
for stage in stages:
try:
self.dispatch_command(self.command, stage)
except ClickException as e:
# Discussion on exit codes: https://github.com/Miserlou/Zappa/issues/407
e.show()
sys.exit(e.exit_code) | [
"def",
"handle",
"(",
"self",
",",
"argv",
"=",
"None",
")",
":",
"desc",
"=",
"(",
"'Zappa - Deploy Python applications to AWS Lambda'",
"' and API Gateway.\\n'",
")",
"parser",
"=",
"argparse",
".",
"ArgumentParser",
"(",
"description",
"=",
"desc",
")",
"parser",
".",
"add_argument",
"(",
"'-v'",
",",
"'--version'",
",",
"action",
"=",
"'version'",
",",
"version",
"=",
"pkg_resources",
".",
"get_distribution",
"(",
"\"zappa\"",
")",
".",
"version",
",",
"help",
"=",
"'Print the zappa version'",
")",
"parser",
".",
"add_argument",
"(",
"'--color'",
",",
"default",
"=",
"'auto'",
",",
"choices",
"=",
"[",
"'auto'",
",",
"'never'",
",",
"'always'",
"]",
")",
"env_parser",
"=",
"argparse",
".",
"ArgumentParser",
"(",
"add_help",
"=",
"False",
")",
"me_group",
"=",
"env_parser",
".",
"add_mutually_exclusive_group",
"(",
")",
"all_help",
"=",
"(",
"'Execute this command for all of our defined '",
"'Zappa stages.'",
")",
"me_group",
".",
"add_argument",
"(",
"'--all'",
",",
"action",
"=",
"'store_true'",
",",
"help",
"=",
"all_help",
")",
"me_group",
".",
"add_argument",
"(",
"'stage_env'",
",",
"nargs",
"=",
"'?'",
")",
"group",
"=",
"env_parser",
".",
"add_argument_group",
"(",
")",
"group",
".",
"add_argument",
"(",
"'-a'",
",",
"'--app_function'",
",",
"help",
"=",
"'The WSGI application function.'",
")",
"group",
".",
"add_argument",
"(",
"'-s'",
",",
"'--settings_file'",
",",
"help",
"=",
"'The path to a Zappa settings file.'",
")",
"group",
".",
"add_argument",
"(",
"'-q'",
",",
"'--quiet'",
",",
"action",
"=",
"'store_true'",
",",
"help",
"=",
"'Silence all output.'",
")",
"# https://github.com/Miserlou/Zappa/issues/407",
"# Moved when 'template' command added.",
"# Fuck Terraform.",
"group",
".",
"add_argument",
"(",
"'-j'",
",",
"'--json'",
",",
"action",
"=",
"'store_true'",
",",
"help",
"=",
"'Make the output of this command be machine readable.'",
")",
"# https://github.com/Miserlou/Zappa/issues/891",
"group",
".",
"add_argument",
"(",
"'--disable_progress'",
",",
"action",
"=",
"'store_true'",
",",
"help",
"=",
"'Disable progress bars.'",
")",
"##",
"# Certify",
"##",
"subparsers",
"=",
"parser",
".",
"add_subparsers",
"(",
"title",
"=",
"'subcommands'",
",",
"dest",
"=",
"'command'",
")",
"cert_parser",
"=",
"subparsers",
".",
"add_parser",
"(",
"'certify'",
",",
"parents",
"=",
"[",
"env_parser",
"]",
",",
"help",
"=",
"'Create and install SSL certificate'",
")",
"cert_parser",
".",
"add_argument",
"(",
"'--manual'",
",",
"action",
"=",
"'store_true'",
",",
"help",
"=",
"(",
"\"Gets new Let's Encrypt certificates, but prints them to console.\"",
"\"Does not update API Gateway domains.\"",
")",
")",
"cert_parser",
".",
"add_argument",
"(",
"'-y'",
",",
"'--yes'",
",",
"action",
"=",
"'store_true'",
",",
"help",
"=",
"'Auto confirm yes.'",
")",
"##",
"# Deploy",
"##",
"deploy_parser",
"=",
"subparsers",
".",
"add_parser",
"(",
"'deploy'",
",",
"parents",
"=",
"[",
"env_parser",
"]",
",",
"help",
"=",
"'Deploy application.'",
")",
"deploy_parser",
".",
"add_argument",
"(",
"'-z'",
",",
"'--zip'",
",",
"help",
"=",
"'Deploy Lambda with specific local or S3 hosted zip package'",
")",
"##",
"# Init",
"##",
"init_parser",
"=",
"subparsers",
".",
"add_parser",
"(",
"'init'",
",",
"help",
"=",
"'Initialize Zappa app.'",
")",
"##",
"# Package",
"##",
"package_parser",
"=",
"subparsers",
".",
"add_parser",
"(",
"'package'",
",",
"parents",
"=",
"[",
"env_parser",
"]",
",",
"help",
"=",
"'Build the application zip package locally.'",
")",
"package_parser",
".",
"add_argument",
"(",
"'-o'",
",",
"'--output'",
",",
"help",
"=",
"'Name of file to output the package to.'",
")",
"##",
"# Template",
"##",
"template_parser",
"=",
"subparsers",
".",
"add_parser",
"(",
"'template'",
",",
"parents",
"=",
"[",
"env_parser",
"]",
",",
"help",
"=",
"'Create a CloudFormation template for this API Gateway.'",
")",
"template_parser",
".",
"add_argument",
"(",
"'-l'",
",",
"'--lambda-arn'",
",",
"required",
"=",
"True",
",",
"help",
"=",
"'ARN of the Lambda function to template to.'",
")",
"template_parser",
".",
"add_argument",
"(",
"'-r'",
",",
"'--role-arn'",
",",
"required",
"=",
"True",
",",
"help",
"=",
"'ARN of the Role to template with.'",
")",
"template_parser",
".",
"add_argument",
"(",
"'-o'",
",",
"'--output'",
",",
"help",
"=",
"'Name of file to output the template to.'",
")",
"##",
"# Invocation",
"##",
"invoke_parser",
"=",
"subparsers",
".",
"add_parser",
"(",
"'invoke'",
",",
"parents",
"=",
"[",
"env_parser",
"]",
",",
"help",
"=",
"'Invoke remote function.'",
")",
"invoke_parser",
".",
"add_argument",
"(",
"'--raw'",
",",
"action",
"=",
"'store_true'",
",",
"help",
"=",
"(",
"'When invoking remotely, invoke this python as a string,'",
"' not as a modular path.'",
")",
")",
"invoke_parser",
".",
"add_argument",
"(",
"'--no-color'",
",",
"action",
"=",
"'store_true'",
",",
"help",
"=",
"(",
"\"Don't color the output\"",
")",
")",
"invoke_parser",
".",
"add_argument",
"(",
"'command_rest'",
")",
"##",
"# Manage",
"##",
"manage_parser",
"=",
"subparsers",
".",
"add_parser",
"(",
"'manage'",
",",
"help",
"=",
"'Invoke remote Django manage.py commands.'",
")",
"rest_help",
"=",
"(",
"\"Command in the form of <env> <command>. <env> is not \"",
"\"required if --all is specified\"",
")",
"manage_parser",
".",
"add_argument",
"(",
"'--all'",
",",
"action",
"=",
"'store_true'",
",",
"help",
"=",
"all_help",
")",
"manage_parser",
".",
"add_argument",
"(",
"'command_rest'",
",",
"nargs",
"=",
"'+'",
",",
"help",
"=",
"rest_help",
")",
"manage_parser",
".",
"add_argument",
"(",
"'--no-color'",
",",
"action",
"=",
"'store_true'",
",",
"help",
"=",
"(",
"\"Don't color the output\"",
")",
")",
"# This is explicitly added here because this is the only subcommand that doesn't inherit from env_parser",
"# https://github.com/Miserlou/Zappa/issues/1002",
"manage_parser",
".",
"add_argument",
"(",
"'-s'",
",",
"'--settings_file'",
",",
"help",
"=",
"'The path to a Zappa settings file.'",
")",
"##",
"# Rollback",
"##",
"def",
"positive_int",
"(",
"s",
")",
":",
"\"\"\" Ensure an arg is positive \"\"\"",
"i",
"=",
"int",
"(",
"s",
")",
"if",
"i",
"<",
"0",
":",
"msg",
"=",
"\"This argument must be positive (got {})\"",
".",
"format",
"(",
"s",
")",
"raise",
"argparse",
".",
"ArgumentTypeError",
"(",
"msg",
")",
"return",
"i",
"rollback_parser",
"=",
"subparsers",
".",
"add_parser",
"(",
"'rollback'",
",",
"parents",
"=",
"[",
"env_parser",
"]",
",",
"help",
"=",
"'Rollback deployed code to a previous version.'",
")",
"rollback_parser",
".",
"add_argument",
"(",
"'-n'",
",",
"'--num-rollback'",
",",
"type",
"=",
"positive_int",
",",
"default",
"=",
"1",
",",
"help",
"=",
"'The number of versions to rollback.'",
")",
"##",
"# Scheduling",
"##",
"subparsers",
".",
"add_parser",
"(",
"'schedule'",
",",
"parents",
"=",
"[",
"env_parser",
"]",
",",
"help",
"=",
"'Schedule functions to occur at regular intervals.'",
")",
"##",
"# Status",
"##",
"status_parser",
"=",
"subparsers",
".",
"add_parser",
"(",
"'status'",
",",
"parents",
"=",
"[",
"env_parser",
"]",
",",
"help",
"=",
"'Show deployment status and event schedules.'",
")",
"##",
"# Log Tailing",
"##",
"tail_parser",
"=",
"subparsers",
".",
"add_parser",
"(",
"'tail'",
",",
"parents",
"=",
"[",
"env_parser",
"]",
",",
"help",
"=",
"'Tail deployment logs.'",
")",
"tail_parser",
".",
"add_argument",
"(",
"'--no-color'",
",",
"action",
"=",
"'store_true'",
",",
"help",
"=",
"\"Don't color log tail output.\"",
")",
"tail_parser",
".",
"add_argument",
"(",
"'--http'",
",",
"action",
"=",
"'store_true'",
",",
"help",
"=",
"'Only show HTTP requests in tail output.'",
")",
"tail_parser",
".",
"add_argument",
"(",
"'--non-http'",
",",
"action",
"=",
"'store_true'",
",",
"help",
"=",
"'Only show non-HTTP requests in tail output.'",
")",
"tail_parser",
".",
"add_argument",
"(",
"'--since'",
",",
"type",
"=",
"str",
",",
"default",
"=",
"\"100000s\"",
",",
"help",
"=",
"\"Only show lines since a certain timeframe.\"",
")",
"tail_parser",
".",
"add_argument",
"(",
"'--filter'",
",",
"type",
"=",
"str",
",",
"default",
"=",
"\"\"",
",",
"help",
"=",
"\"Apply a filter pattern to the logs.\"",
")",
"tail_parser",
".",
"add_argument",
"(",
"'--force-color'",
",",
"action",
"=",
"'store_true'",
",",
"help",
"=",
"'Force coloring log tail output even if coloring support is not auto-detected. (example: piping)'",
")",
"tail_parser",
".",
"add_argument",
"(",
"'--disable-keep-open'",
",",
"action",
"=",
"'store_true'",
",",
"help",
"=",
"\"Exit after printing the last available log, rather than keeping the log open.\"",
")",
"##",
"# Undeploy",
"##",
"undeploy_parser",
"=",
"subparsers",
".",
"add_parser",
"(",
"'undeploy'",
",",
"parents",
"=",
"[",
"env_parser",
"]",
",",
"help",
"=",
"'Undeploy application.'",
")",
"undeploy_parser",
".",
"add_argument",
"(",
"'--remove-logs'",
",",
"action",
"=",
"'store_true'",
",",
"help",
"=",
"(",
"'Removes log groups of api gateway and lambda task'",
"' during the undeployment.'",
")",
",",
")",
"undeploy_parser",
".",
"add_argument",
"(",
"'-y'",
",",
"'--yes'",
",",
"action",
"=",
"'store_true'",
",",
"help",
"=",
"'Auto confirm yes.'",
")",
"##",
"# Unschedule",
"##",
"subparsers",
".",
"add_parser",
"(",
"'unschedule'",
",",
"parents",
"=",
"[",
"env_parser",
"]",
",",
"help",
"=",
"'Unschedule functions.'",
")",
"##",
"# Updating",
"##",
"update_parser",
"=",
"subparsers",
".",
"add_parser",
"(",
"'update'",
",",
"parents",
"=",
"[",
"env_parser",
"]",
",",
"help",
"=",
"'Update deployed application.'",
")",
"update_parser",
".",
"add_argument",
"(",
"'-z'",
",",
"'--zip'",
",",
"help",
"=",
"'Update Lambda with specific local or S3 hosted zip package'",
")",
"update_parser",
".",
"add_argument",
"(",
"'-n'",
",",
"'--no-upload'",
",",
"help",
"=",
"\"Update configuration where appropriate, but don't upload new code\"",
")",
"##",
"# Debug",
"##",
"subparsers",
".",
"add_parser",
"(",
"'shell'",
",",
"parents",
"=",
"[",
"env_parser",
"]",
",",
"help",
"=",
"'A debug shell with a loaded Zappa object.'",
")",
"argcomplete",
".",
"autocomplete",
"(",
"parser",
")",
"args",
"=",
"parser",
".",
"parse_args",
"(",
"argv",
")",
"self",
".",
"vargs",
"=",
"vars",
"(",
"args",
")",
"if",
"args",
".",
"color",
"==",
"'never'",
":",
"disable_click_colors",
"(",
")",
"elif",
"args",
".",
"color",
"==",
"'always'",
":",
"#TODO: Support aggressive coloring like \"--force-color\" on all commands",
"pass",
"elif",
"args",
".",
"color",
"==",
"'auto'",
":",
"pass",
"# Parse the input",
"# NOTE(rmoe): Special case for manage command",
"# The manage command can't have both stage_env and command_rest",
"# arguments. Since they are both positional arguments argparse can't",
"# differentiate the two. This causes problems when used with --all.",
"# (e.g. \"manage --all showmigrations admin\" argparse thinks --all has",
"# been specified AND that stage_env='showmigrations')",
"# By having command_rest collect everything but --all we can split it",
"# apart here instead of relying on argparse.",
"if",
"not",
"args",
".",
"command",
":",
"parser",
".",
"print_help",
"(",
")",
"return",
"if",
"args",
".",
"command",
"==",
"'manage'",
"and",
"not",
"self",
".",
"vargs",
".",
"get",
"(",
"'all'",
")",
":",
"self",
".",
"stage_env",
"=",
"self",
".",
"vargs",
"[",
"'command_rest'",
"]",
".",
"pop",
"(",
"0",
")",
"else",
":",
"self",
".",
"stage_env",
"=",
"self",
".",
"vargs",
".",
"get",
"(",
"'stage_env'",
")",
"if",
"args",
".",
"command",
"==",
"'package'",
":",
"self",
".",
"load_credentials",
"=",
"False",
"self",
".",
"command",
"=",
"args",
".",
"command",
"self",
".",
"disable_progress",
"=",
"self",
".",
"vargs",
".",
"get",
"(",
"'disable_progress'",
")",
"if",
"self",
".",
"vargs",
".",
"get",
"(",
"'quiet'",
")",
":",
"self",
".",
"silence",
"(",
")",
"# We don't have any settings yet, so make those first!",
"# (Settings-based interactions will fail",
"# before a project has been initialized.)",
"if",
"self",
".",
"command",
"==",
"'init'",
":",
"self",
".",
"init",
"(",
")",
"return",
"# Make sure there isn't a new version available",
"if",
"not",
"self",
".",
"vargs",
".",
"get",
"(",
"'json'",
")",
":",
"self",
".",
"check_for_update",
"(",
")",
"# Load and Validate Settings File",
"self",
".",
"load_settings_file",
"(",
"self",
".",
"vargs",
".",
"get",
"(",
"'settings_file'",
")",
")",
"# Should we execute this for all stages, or just one?",
"all_stages",
"=",
"self",
".",
"vargs",
".",
"get",
"(",
"'all'",
")",
"stages",
"=",
"[",
"]",
"if",
"all_stages",
":",
"# All stages!",
"stages",
"=",
"self",
".",
"zappa_settings",
".",
"keys",
"(",
")",
"else",
":",
"# Just one env.",
"if",
"not",
"self",
".",
"stage_env",
":",
"# If there's only one stage defined in the settings,",
"# use that as the default.",
"if",
"len",
"(",
"self",
".",
"zappa_settings",
".",
"keys",
"(",
")",
")",
"==",
"1",
":",
"stages",
".",
"append",
"(",
"list",
"(",
"self",
".",
"zappa_settings",
".",
"keys",
"(",
")",
")",
"[",
"0",
"]",
")",
"else",
":",
"parser",
".",
"error",
"(",
"\"Please supply a stage to interact with.\"",
")",
"else",
":",
"stages",
".",
"append",
"(",
"self",
".",
"stage_env",
")",
"for",
"stage",
"in",
"stages",
":",
"try",
":",
"self",
".",
"dispatch_command",
"(",
"self",
".",
"command",
",",
"stage",
")",
"except",
"ClickException",
"as",
"e",
":",
"# Discussion on exit codes: https://github.com/Miserlou/Zappa/issues/407",
"e",
".",
"show",
"(",
")",
"sys",
".",
"exit",
"(",
"e",
".",
"exit_code",
")"
] | Main function.
Parses command, load settings and dispatches accordingly. | [
"Main",
"function",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L180-L513 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.dispatch_command | def dispatch_command(self, command, stage):
"""
Given a command to execute and stage,
execute that command.
"""
self.api_stage = stage
if command not in ['status', 'manage']:
if not self.vargs.get('json', None):
click.echo("Calling " + click.style(command, fg="green", bold=True) + " for stage " +
click.style(self.api_stage, bold=True) + ".." )
# Explicitly define the app function.
# Related: https://github.com/Miserlou/Zappa/issues/832
if self.vargs.get('app_function', None):
self.app_function = self.vargs['app_function']
# Load our settings, based on api_stage.
try:
self.load_settings(self.vargs.get('settings_file'))
except ValueError as e:
if hasattr(e, 'message'):
print("Error: {}".format(e.message))
else:
print(str(e))
sys.exit(-1)
self.callback('settings')
# Hand it off
if command == 'deploy': # pragma: no cover
self.deploy(self.vargs['zip'])
if command == 'package': # pragma: no cover
self.package(self.vargs['output'])
if command == 'template': # pragma: no cover
self.template( self.vargs['lambda_arn'],
self.vargs['role_arn'],
output=self.vargs['output'],
json=self.vargs['json']
)
elif command == 'update': # pragma: no cover
self.update(self.vargs['zip'], self.vargs['no_upload'])
elif command == 'rollback': # pragma: no cover
self.rollback(self.vargs['num_rollback'])
elif command == 'invoke': # pragma: no cover
if not self.vargs.get('command_rest'):
print("Please enter the function to invoke.")
return
self.invoke(
self.vargs['command_rest'],
raw_python=self.vargs['raw'],
no_color=self.vargs['no_color'],
)
elif command == 'manage': # pragma: no cover
if not self.vargs.get('command_rest'):
print("Please enter the management command to invoke.")
return
if not self.django_settings:
print("This command is for Django projects only!")
print("If this is a Django project, please define django_settings in your zappa_settings.")
return
command_tail = self.vargs.get('command_rest')
if len(command_tail) > 1:
command = " ".join(command_tail) # ex: zappa manage dev "shell --version"
else:
command = command_tail[0] # ex: zappa manage dev showmigrations admin
self.invoke(
command,
command="manage",
no_color=self.vargs['no_color'],
)
elif command == 'tail': # pragma: no cover
self.tail(
colorize=(not self.vargs['no_color']),
http=self.vargs['http'],
non_http=self.vargs['non_http'],
since=self.vargs['since'],
filter_pattern=self.vargs['filter'],
force_colorize=self.vargs['force_color'] or None,
keep_open=not self.vargs['disable_keep_open']
)
elif command == 'undeploy': # pragma: no cover
self.undeploy(
no_confirm=self.vargs['yes'],
remove_logs=self.vargs['remove_logs']
)
elif command == 'schedule': # pragma: no cover
self.schedule()
elif command == 'unschedule': # pragma: no cover
self.unschedule()
elif command == 'status': # pragma: no cover
self.status(return_json=self.vargs['json'])
elif command == 'certify': # pragma: no cover
self.certify(
no_confirm=self.vargs['yes'],
manual=self.vargs['manual']
)
elif command == 'shell': # pragma: no cover
self.shell() | python | def dispatch_command(self, command, stage):
"""
Given a command to execute and stage,
execute that command.
"""
self.api_stage = stage
if command not in ['status', 'manage']:
if not self.vargs.get('json', None):
click.echo("Calling " + click.style(command, fg="green", bold=True) + " for stage " +
click.style(self.api_stage, bold=True) + ".." )
# Explicitly define the app function.
# Related: https://github.com/Miserlou/Zappa/issues/832
if self.vargs.get('app_function', None):
self.app_function = self.vargs['app_function']
# Load our settings, based on api_stage.
try:
self.load_settings(self.vargs.get('settings_file'))
except ValueError as e:
if hasattr(e, 'message'):
print("Error: {}".format(e.message))
else:
print(str(e))
sys.exit(-1)
self.callback('settings')
# Hand it off
if command == 'deploy': # pragma: no cover
self.deploy(self.vargs['zip'])
if command == 'package': # pragma: no cover
self.package(self.vargs['output'])
if command == 'template': # pragma: no cover
self.template( self.vargs['lambda_arn'],
self.vargs['role_arn'],
output=self.vargs['output'],
json=self.vargs['json']
)
elif command == 'update': # pragma: no cover
self.update(self.vargs['zip'], self.vargs['no_upload'])
elif command == 'rollback': # pragma: no cover
self.rollback(self.vargs['num_rollback'])
elif command == 'invoke': # pragma: no cover
if not self.vargs.get('command_rest'):
print("Please enter the function to invoke.")
return
self.invoke(
self.vargs['command_rest'],
raw_python=self.vargs['raw'],
no_color=self.vargs['no_color'],
)
elif command == 'manage': # pragma: no cover
if not self.vargs.get('command_rest'):
print("Please enter the management command to invoke.")
return
if not self.django_settings:
print("This command is for Django projects only!")
print("If this is a Django project, please define django_settings in your zappa_settings.")
return
command_tail = self.vargs.get('command_rest')
if len(command_tail) > 1:
command = " ".join(command_tail) # ex: zappa manage dev "shell --version"
else:
command = command_tail[0] # ex: zappa manage dev showmigrations admin
self.invoke(
command,
command="manage",
no_color=self.vargs['no_color'],
)
elif command == 'tail': # pragma: no cover
self.tail(
colorize=(not self.vargs['no_color']),
http=self.vargs['http'],
non_http=self.vargs['non_http'],
since=self.vargs['since'],
filter_pattern=self.vargs['filter'],
force_colorize=self.vargs['force_color'] or None,
keep_open=not self.vargs['disable_keep_open']
)
elif command == 'undeploy': # pragma: no cover
self.undeploy(
no_confirm=self.vargs['yes'],
remove_logs=self.vargs['remove_logs']
)
elif command == 'schedule': # pragma: no cover
self.schedule()
elif command == 'unschedule': # pragma: no cover
self.unschedule()
elif command == 'status': # pragma: no cover
self.status(return_json=self.vargs['json'])
elif command == 'certify': # pragma: no cover
self.certify(
no_confirm=self.vargs['yes'],
manual=self.vargs['manual']
)
elif command == 'shell': # pragma: no cover
self.shell() | [
"def",
"dispatch_command",
"(",
"self",
",",
"command",
",",
"stage",
")",
":",
"self",
".",
"api_stage",
"=",
"stage",
"if",
"command",
"not",
"in",
"[",
"'status'",
",",
"'manage'",
"]",
":",
"if",
"not",
"self",
".",
"vargs",
".",
"get",
"(",
"'json'",
",",
"None",
")",
":",
"click",
".",
"echo",
"(",
"\"Calling \"",
"+",
"click",
".",
"style",
"(",
"command",
",",
"fg",
"=",
"\"green\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" for stage \"",
"+",
"click",
".",
"style",
"(",
"self",
".",
"api_stage",
",",
"bold",
"=",
"True",
")",
"+",
"\"..\"",
")",
"# Explicitly define the app function.",
"# Related: https://github.com/Miserlou/Zappa/issues/832",
"if",
"self",
".",
"vargs",
".",
"get",
"(",
"'app_function'",
",",
"None",
")",
":",
"self",
".",
"app_function",
"=",
"self",
".",
"vargs",
"[",
"'app_function'",
"]",
"# Load our settings, based on api_stage.",
"try",
":",
"self",
".",
"load_settings",
"(",
"self",
".",
"vargs",
".",
"get",
"(",
"'settings_file'",
")",
")",
"except",
"ValueError",
"as",
"e",
":",
"if",
"hasattr",
"(",
"e",
",",
"'message'",
")",
":",
"print",
"(",
"\"Error: {}\"",
".",
"format",
"(",
"e",
".",
"message",
")",
")",
"else",
":",
"print",
"(",
"str",
"(",
"e",
")",
")",
"sys",
".",
"exit",
"(",
"-",
"1",
")",
"self",
".",
"callback",
"(",
"'settings'",
")",
"# Hand it off",
"if",
"command",
"==",
"'deploy'",
":",
"# pragma: no cover",
"self",
".",
"deploy",
"(",
"self",
".",
"vargs",
"[",
"'zip'",
"]",
")",
"if",
"command",
"==",
"'package'",
":",
"# pragma: no cover",
"self",
".",
"package",
"(",
"self",
".",
"vargs",
"[",
"'output'",
"]",
")",
"if",
"command",
"==",
"'template'",
":",
"# pragma: no cover",
"self",
".",
"template",
"(",
"self",
".",
"vargs",
"[",
"'lambda_arn'",
"]",
",",
"self",
".",
"vargs",
"[",
"'role_arn'",
"]",
",",
"output",
"=",
"self",
".",
"vargs",
"[",
"'output'",
"]",
",",
"json",
"=",
"self",
".",
"vargs",
"[",
"'json'",
"]",
")",
"elif",
"command",
"==",
"'update'",
":",
"# pragma: no cover",
"self",
".",
"update",
"(",
"self",
".",
"vargs",
"[",
"'zip'",
"]",
",",
"self",
".",
"vargs",
"[",
"'no_upload'",
"]",
")",
"elif",
"command",
"==",
"'rollback'",
":",
"# pragma: no cover",
"self",
".",
"rollback",
"(",
"self",
".",
"vargs",
"[",
"'num_rollback'",
"]",
")",
"elif",
"command",
"==",
"'invoke'",
":",
"# pragma: no cover",
"if",
"not",
"self",
".",
"vargs",
".",
"get",
"(",
"'command_rest'",
")",
":",
"print",
"(",
"\"Please enter the function to invoke.\"",
")",
"return",
"self",
".",
"invoke",
"(",
"self",
".",
"vargs",
"[",
"'command_rest'",
"]",
",",
"raw_python",
"=",
"self",
".",
"vargs",
"[",
"'raw'",
"]",
",",
"no_color",
"=",
"self",
".",
"vargs",
"[",
"'no_color'",
"]",
",",
")",
"elif",
"command",
"==",
"'manage'",
":",
"# pragma: no cover",
"if",
"not",
"self",
".",
"vargs",
".",
"get",
"(",
"'command_rest'",
")",
":",
"print",
"(",
"\"Please enter the management command to invoke.\"",
")",
"return",
"if",
"not",
"self",
".",
"django_settings",
":",
"print",
"(",
"\"This command is for Django projects only!\"",
")",
"print",
"(",
"\"If this is a Django project, please define django_settings in your zappa_settings.\"",
")",
"return",
"command_tail",
"=",
"self",
".",
"vargs",
".",
"get",
"(",
"'command_rest'",
")",
"if",
"len",
"(",
"command_tail",
")",
">",
"1",
":",
"command",
"=",
"\" \"",
".",
"join",
"(",
"command_tail",
")",
"# ex: zappa manage dev \"shell --version\"",
"else",
":",
"command",
"=",
"command_tail",
"[",
"0",
"]",
"# ex: zappa manage dev showmigrations admin",
"self",
".",
"invoke",
"(",
"command",
",",
"command",
"=",
"\"manage\"",
",",
"no_color",
"=",
"self",
".",
"vargs",
"[",
"'no_color'",
"]",
",",
")",
"elif",
"command",
"==",
"'tail'",
":",
"# pragma: no cover",
"self",
".",
"tail",
"(",
"colorize",
"=",
"(",
"not",
"self",
".",
"vargs",
"[",
"'no_color'",
"]",
")",
",",
"http",
"=",
"self",
".",
"vargs",
"[",
"'http'",
"]",
",",
"non_http",
"=",
"self",
".",
"vargs",
"[",
"'non_http'",
"]",
",",
"since",
"=",
"self",
".",
"vargs",
"[",
"'since'",
"]",
",",
"filter_pattern",
"=",
"self",
".",
"vargs",
"[",
"'filter'",
"]",
",",
"force_colorize",
"=",
"self",
".",
"vargs",
"[",
"'force_color'",
"]",
"or",
"None",
",",
"keep_open",
"=",
"not",
"self",
".",
"vargs",
"[",
"'disable_keep_open'",
"]",
")",
"elif",
"command",
"==",
"'undeploy'",
":",
"# pragma: no cover",
"self",
".",
"undeploy",
"(",
"no_confirm",
"=",
"self",
".",
"vargs",
"[",
"'yes'",
"]",
",",
"remove_logs",
"=",
"self",
".",
"vargs",
"[",
"'remove_logs'",
"]",
")",
"elif",
"command",
"==",
"'schedule'",
":",
"# pragma: no cover",
"self",
".",
"schedule",
"(",
")",
"elif",
"command",
"==",
"'unschedule'",
":",
"# pragma: no cover",
"self",
".",
"unschedule",
"(",
")",
"elif",
"command",
"==",
"'status'",
":",
"# pragma: no cover",
"self",
".",
"status",
"(",
"return_json",
"=",
"self",
".",
"vargs",
"[",
"'json'",
"]",
")",
"elif",
"command",
"==",
"'certify'",
":",
"# pragma: no cover",
"self",
".",
"certify",
"(",
"no_confirm",
"=",
"self",
".",
"vargs",
"[",
"'yes'",
"]",
",",
"manual",
"=",
"self",
".",
"vargs",
"[",
"'manual'",
"]",
")",
"elif",
"command",
"==",
"'shell'",
":",
"# pragma: no cover",
"self",
".",
"shell",
"(",
")"
] | Given a command to execute and stage,
execute that command. | [
"Given",
"a",
"command",
"to",
"execute",
"and",
"stage",
"execute",
"that",
"command",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L515-L620 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.package | def package(self, output=None):
"""
Only build the package
"""
# Make sure we're in a venv.
self.check_venv()
# force not to delete the local zip
self.override_stage_config_setting('delete_local_zip', False)
# Execute the prebuild script
if self.prebuild_script:
self.execute_prebuild_script()
# Create the Lambda Zip
self.create_package(output)
self.callback('zip')
size = human_size(os.path.getsize(self.zip_path))
click.echo(click.style("Package created", fg="green", bold=True) + ": " + click.style(self.zip_path, bold=True) + " (" + size + ")") | python | def package(self, output=None):
"""
Only build the package
"""
# Make sure we're in a venv.
self.check_venv()
# force not to delete the local zip
self.override_stage_config_setting('delete_local_zip', False)
# Execute the prebuild script
if self.prebuild_script:
self.execute_prebuild_script()
# Create the Lambda Zip
self.create_package(output)
self.callback('zip')
size = human_size(os.path.getsize(self.zip_path))
click.echo(click.style("Package created", fg="green", bold=True) + ": " + click.style(self.zip_path, bold=True) + " (" + size + ")") | [
"def",
"package",
"(",
"self",
",",
"output",
"=",
"None",
")",
":",
"# Make sure we're in a venv.",
"self",
".",
"check_venv",
"(",
")",
"# force not to delete the local zip",
"self",
".",
"override_stage_config_setting",
"(",
"'delete_local_zip'",
",",
"False",
")",
"# Execute the prebuild script",
"if",
"self",
".",
"prebuild_script",
":",
"self",
".",
"execute_prebuild_script",
"(",
")",
"# Create the Lambda Zip",
"self",
".",
"create_package",
"(",
"output",
")",
"self",
".",
"callback",
"(",
"'zip'",
")",
"size",
"=",
"human_size",
"(",
"os",
".",
"path",
".",
"getsize",
"(",
"self",
".",
"zip_path",
")",
")",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"Package created\"",
",",
"fg",
"=",
"\"green\"",
",",
"bold",
"=",
"True",
")",
"+",
"\": \"",
"+",
"click",
".",
"style",
"(",
"self",
".",
"zip_path",
",",
"bold",
"=",
"True",
")",
"+",
"\" (\"",
"+",
"size",
"+",
"\")\"",
")"
] | Only build the package | [
"Only",
"build",
"the",
"package"
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L626-L642 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.template | def template(self, lambda_arn, role_arn, output=None, json=False):
"""
Only build the template file.
"""
if not lambda_arn:
raise ClickException("Lambda ARN is required to template.")
if not role_arn:
raise ClickException("Role ARN is required to template.")
self.zappa.credentials_arn = role_arn
# Create the template!
template = self.zappa.create_stack_template(
lambda_arn=lambda_arn,
lambda_name=self.lambda_name,
api_key_required=self.api_key_required,
iam_authorization=self.iam_authorization,
authorizer=self.authorizer,
cors_options=self.cors,
description=self.apigateway_description,
policy=self.apigateway_policy,
endpoint_configuration=self.endpoint_configuration
)
if not output:
template_file = self.lambda_name + '-template-' + str(int(time.time())) + '.json'
else:
template_file = output
with open(template_file, 'wb') as out:
out.write(bytes(template.to_json(indent=None, separators=(',',':')), "utf-8"))
if not json:
click.echo(click.style("Template created", fg="green", bold=True) + ": " + click.style(template_file, bold=True))
else:
with open(template_file, 'r') as out:
print(out.read()) | python | def template(self, lambda_arn, role_arn, output=None, json=False):
"""
Only build the template file.
"""
if not lambda_arn:
raise ClickException("Lambda ARN is required to template.")
if not role_arn:
raise ClickException("Role ARN is required to template.")
self.zappa.credentials_arn = role_arn
# Create the template!
template = self.zappa.create_stack_template(
lambda_arn=lambda_arn,
lambda_name=self.lambda_name,
api_key_required=self.api_key_required,
iam_authorization=self.iam_authorization,
authorizer=self.authorizer,
cors_options=self.cors,
description=self.apigateway_description,
policy=self.apigateway_policy,
endpoint_configuration=self.endpoint_configuration
)
if not output:
template_file = self.lambda_name + '-template-' + str(int(time.time())) + '.json'
else:
template_file = output
with open(template_file, 'wb') as out:
out.write(bytes(template.to_json(indent=None, separators=(',',':')), "utf-8"))
if not json:
click.echo(click.style("Template created", fg="green", bold=True) + ": " + click.style(template_file, bold=True))
else:
with open(template_file, 'r') as out:
print(out.read()) | [
"def",
"template",
"(",
"self",
",",
"lambda_arn",
",",
"role_arn",
",",
"output",
"=",
"None",
",",
"json",
"=",
"False",
")",
":",
"if",
"not",
"lambda_arn",
":",
"raise",
"ClickException",
"(",
"\"Lambda ARN is required to template.\"",
")",
"if",
"not",
"role_arn",
":",
"raise",
"ClickException",
"(",
"\"Role ARN is required to template.\"",
")",
"self",
".",
"zappa",
".",
"credentials_arn",
"=",
"role_arn",
"# Create the template!",
"template",
"=",
"self",
".",
"zappa",
".",
"create_stack_template",
"(",
"lambda_arn",
"=",
"lambda_arn",
",",
"lambda_name",
"=",
"self",
".",
"lambda_name",
",",
"api_key_required",
"=",
"self",
".",
"api_key_required",
",",
"iam_authorization",
"=",
"self",
".",
"iam_authorization",
",",
"authorizer",
"=",
"self",
".",
"authorizer",
",",
"cors_options",
"=",
"self",
".",
"cors",
",",
"description",
"=",
"self",
".",
"apigateway_description",
",",
"policy",
"=",
"self",
".",
"apigateway_policy",
",",
"endpoint_configuration",
"=",
"self",
".",
"endpoint_configuration",
")",
"if",
"not",
"output",
":",
"template_file",
"=",
"self",
".",
"lambda_name",
"+",
"'-template-'",
"+",
"str",
"(",
"int",
"(",
"time",
".",
"time",
"(",
")",
")",
")",
"+",
"'.json'",
"else",
":",
"template_file",
"=",
"output",
"with",
"open",
"(",
"template_file",
",",
"'wb'",
")",
"as",
"out",
":",
"out",
".",
"write",
"(",
"bytes",
"(",
"template",
".",
"to_json",
"(",
"indent",
"=",
"None",
",",
"separators",
"=",
"(",
"','",
",",
"':'",
")",
")",
",",
"\"utf-8\"",
")",
")",
"if",
"not",
"json",
":",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"Template created\"",
",",
"fg",
"=",
"\"green\"",
",",
"bold",
"=",
"True",
")",
"+",
"\": \"",
"+",
"click",
".",
"style",
"(",
"template_file",
",",
"bold",
"=",
"True",
")",
")",
"else",
":",
"with",
"open",
"(",
"template_file",
",",
"'r'",
")",
"as",
"out",
":",
"print",
"(",
"out",
".",
"read",
"(",
")",
")"
] | Only build the template file. | [
"Only",
"build",
"the",
"template",
"file",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L644-L681 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.deploy | def deploy(self, source_zip=None):
"""
Package your project, upload it to S3, register the Lambda function
and create the API Gateway routes.
"""
if not source_zip:
# Make sure we're in a venv.
self.check_venv()
# Execute the prebuild script
if self.prebuild_script:
self.execute_prebuild_script()
# Make sure this isn't already deployed.
deployed_versions = self.zappa.get_lambda_function_versions(self.lambda_name)
if len(deployed_versions) > 0:
raise ClickException("This application is " + click.style("already deployed", fg="red") +
" - did you mean to call " + click.style("update", bold=True) + "?")
# Make sure the necessary IAM execution roles are available
if self.manage_roles:
try:
self.zappa.create_iam_roles()
except botocore.client.ClientError:
raise ClickException(
click.style("Failed", fg="red") + " to " + click.style("manage IAM roles", bold=True) + "!\n" +
"You may " + click.style("lack the necessary AWS permissions", bold=True) +
" to automatically manage a Zappa execution role.\n" +
"To fix this, see here: " +
click.style("https://github.com/Miserlou/Zappa#custom-aws-iam-roles-and-policies-for-deployment", bold=True)
+ '\n')
# Create the Lambda Zip
self.create_package()
self.callback('zip')
# Upload it to S3
success = self.zappa.upload_to_s3(
self.zip_path, self.s3_bucket_name, disable_progress=self.disable_progress)
if not success: # pragma: no cover
raise ClickException("Unable to upload to S3. Quitting.")
# If using a slim handler, upload it to S3 and tell lambda to use this slim handler zip
if self.stage_config.get('slim_handler', False):
# https://github.com/Miserlou/Zappa/issues/510
success = self.zappa.upload_to_s3(self.handler_path, self.s3_bucket_name, disable_progress=self.disable_progress)
if not success: # pragma: no cover
raise ClickException("Unable to upload handler to S3. Quitting.")
# Copy the project zip to the current project zip
current_project_name = '{0!s}_{1!s}_current_project.tar.gz'.format(self.api_stage, self.project_name)
success = self.zappa.copy_on_s3(src_file_name=self.zip_path, dst_file_name=current_project_name,
bucket_name=self.s3_bucket_name)
if not success: # pragma: no cover
raise ClickException("Unable to copy the zip to be the current project. Quitting.")
handler_file = self.handler_path
else:
handler_file = self.zip_path
# Fixes https://github.com/Miserlou/Zappa/issues/613
try:
self.lambda_arn = self.zappa.get_lambda_function(
function_name=self.lambda_name)
except botocore.client.ClientError:
# Register the Lambda function with that zip as the source
# You'll also need to define the path to your lambda_handler code.
kwargs = dict(
handler=self.lambda_handler,
description=self.lambda_description,
vpc_config=self.vpc_config,
dead_letter_config=self.dead_letter_config,
timeout=self.timeout_seconds,
memory_size=self.memory_size,
runtime=self.runtime,
aws_environment_variables=self.aws_environment_variables,
aws_kms_key_arn=self.aws_kms_key_arn,
use_alb=self.use_alb
)
if source_zip and source_zip.startswith('s3://'):
bucket, key_name = parse_s3_url(source_zip)
kwargs['function_name'] = self.lambda_name
kwargs['bucket'] = bucket
kwargs['s3_key'] = key_name
elif source_zip and not source_zip.startswith('s3://'):
with open(source_zip, mode='rb') as fh:
byte_stream = fh.read()
kwargs['function_name'] = self.lambda_name
kwargs['local_zip'] = byte_stream
else:
kwargs['function_name'] = self.lambda_name
kwargs['bucket'] = self.s3_bucket_name
kwargs['s3_key'] = handler_file
self.lambda_arn = self.zappa.create_lambda_function(**kwargs)
# Schedule events for this deployment
self.schedule()
endpoint_url = ''
deployment_string = click.style("Deployment complete", fg="green", bold=True) + "!"
if self.use_alb:
kwargs = dict(
lambda_arn=self.lambda_arn,
lambda_name=self.lambda_name,
alb_vpc_config=self.alb_vpc_config,
timeout=self.timeout_seconds
)
self.zappa.deploy_lambda_alb(**kwargs)
if self.use_apigateway:
# Create and configure the API Gateway
template = self.zappa.create_stack_template(
lambda_arn=self.lambda_arn,
lambda_name=self.lambda_name,
api_key_required=self.api_key_required,
iam_authorization=self.iam_authorization,
authorizer=self.authorizer,
cors_options=self.cors,
description=self.apigateway_description,
endpoint_configuration=self.endpoint_configuration
)
self.zappa.update_stack(
self.lambda_name,
self.s3_bucket_name,
wait=True,
disable_progress=self.disable_progress
)
api_id = self.zappa.get_api_id(self.lambda_name)
# Add binary support
if self.binary_support:
self.zappa.add_binary_support(api_id=api_id, cors=self.cors)
# Add payload compression
if self.stage_config.get('payload_compression', True):
self.zappa.add_api_compression(
api_id=api_id,
min_compression_size=self.stage_config.get('payload_minimum_compression_size', 0))
# Deploy the API!
endpoint_url = self.deploy_api_gateway(api_id)
deployment_string = deployment_string + ": {}".format(endpoint_url)
# Create/link API key
if self.api_key_required:
if self.api_key is None:
self.zappa.create_api_key(api_id=api_id, stage_name=self.api_stage)
else:
self.zappa.add_api_stage_to_api_key(api_key=self.api_key, api_id=api_id, stage_name=self.api_stage)
if self.stage_config.get('touch', True):
self.touch_endpoint(endpoint_url)
# Finally, delete the local copy our zip package
if not source_zip:
if self.stage_config.get('delete_local_zip', True):
self.remove_local_zip()
# Remove the project zip from S3.
if not source_zip:
self.remove_uploaded_zip()
self.callback('post')
click.echo(deployment_string) | python | def deploy(self, source_zip=None):
"""
Package your project, upload it to S3, register the Lambda function
and create the API Gateway routes.
"""
if not source_zip:
# Make sure we're in a venv.
self.check_venv()
# Execute the prebuild script
if self.prebuild_script:
self.execute_prebuild_script()
# Make sure this isn't already deployed.
deployed_versions = self.zappa.get_lambda_function_versions(self.lambda_name)
if len(deployed_versions) > 0:
raise ClickException("This application is " + click.style("already deployed", fg="red") +
" - did you mean to call " + click.style("update", bold=True) + "?")
# Make sure the necessary IAM execution roles are available
if self.manage_roles:
try:
self.zappa.create_iam_roles()
except botocore.client.ClientError:
raise ClickException(
click.style("Failed", fg="red") + " to " + click.style("manage IAM roles", bold=True) + "!\n" +
"You may " + click.style("lack the necessary AWS permissions", bold=True) +
" to automatically manage a Zappa execution role.\n" +
"To fix this, see here: " +
click.style("https://github.com/Miserlou/Zappa#custom-aws-iam-roles-and-policies-for-deployment", bold=True)
+ '\n')
# Create the Lambda Zip
self.create_package()
self.callback('zip')
# Upload it to S3
success = self.zappa.upload_to_s3(
self.zip_path, self.s3_bucket_name, disable_progress=self.disable_progress)
if not success: # pragma: no cover
raise ClickException("Unable to upload to S3. Quitting.")
# If using a slim handler, upload it to S3 and tell lambda to use this slim handler zip
if self.stage_config.get('slim_handler', False):
# https://github.com/Miserlou/Zappa/issues/510
success = self.zappa.upload_to_s3(self.handler_path, self.s3_bucket_name, disable_progress=self.disable_progress)
if not success: # pragma: no cover
raise ClickException("Unable to upload handler to S3. Quitting.")
# Copy the project zip to the current project zip
current_project_name = '{0!s}_{1!s}_current_project.tar.gz'.format(self.api_stage, self.project_name)
success = self.zappa.copy_on_s3(src_file_name=self.zip_path, dst_file_name=current_project_name,
bucket_name=self.s3_bucket_name)
if not success: # pragma: no cover
raise ClickException("Unable to copy the zip to be the current project. Quitting.")
handler_file = self.handler_path
else:
handler_file = self.zip_path
# Fixes https://github.com/Miserlou/Zappa/issues/613
try:
self.lambda_arn = self.zappa.get_lambda_function(
function_name=self.lambda_name)
except botocore.client.ClientError:
# Register the Lambda function with that zip as the source
# You'll also need to define the path to your lambda_handler code.
kwargs = dict(
handler=self.lambda_handler,
description=self.lambda_description,
vpc_config=self.vpc_config,
dead_letter_config=self.dead_letter_config,
timeout=self.timeout_seconds,
memory_size=self.memory_size,
runtime=self.runtime,
aws_environment_variables=self.aws_environment_variables,
aws_kms_key_arn=self.aws_kms_key_arn,
use_alb=self.use_alb
)
if source_zip and source_zip.startswith('s3://'):
bucket, key_name = parse_s3_url(source_zip)
kwargs['function_name'] = self.lambda_name
kwargs['bucket'] = bucket
kwargs['s3_key'] = key_name
elif source_zip and not source_zip.startswith('s3://'):
with open(source_zip, mode='rb') as fh:
byte_stream = fh.read()
kwargs['function_name'] = self.lambda_name
kwargs['local_zip'] = byte_stream
else:
kwargs['function_name'] = self.lambda_name
kwargs['bucket'] = self.s3_bucket_name
kwargs['s3_key'] = handler_file
self.lambda_arn = self.zappa.create_lambda_function(**kwargs)
# Schedule events for this deployment
self.schedule()
endpoint_url = ''
deployment_string = click.style("Deployment complete", fg="green", bold=True) + "!"
if self.use_alb:
kwargs = dict(
lambda_arn=self.lambda_arn,
lambda_name=self.lambda_name,
alb_vpc_config=self.alb_vpc_config,
timeout=self.timeout_seconds
)
self.zappa.deploy_lambda_alb(**kwargs)
if self.use_apigateway:
# Create and configure the API Gateway
template = self.zappa.create_stack_template(
lambda_arn=self.lambda_arn,
lambda_name=self.lambda_name,
api_key_required=self.api_key_required,
iam_authorization=self.iam_authorization,
authorizer=self.authorizer,
cors_options=self.cors,
description=self.apigateway_description,
endpoint_configuration=self.endpoint_configuration
)
self.zappa.update_stack(
self.lambda_name,
self.s3_bucket_name,
wait=True,
disable_progress=self.disable_progress
)
api_id = self.zappa.get_api_id(self.lambda_name)
# Add binary support
if self.binary_support:
self.zappa.add_binary_support(api_id=api_id, cors=self.cors)
# Add payload compression
if self.stage_config.get('payload_compression', True):
self.zappa.add_api_compression(
api_id=api_id,
min_compression_size=self.stage_config.get('payload_minimum_compression_size', 0))
# Deploy the API!
endpoint_url = self.deploy_api_gateway(api_id)
deployment_string = deployment_string + ": {}".format(endpoint_url)
# Create/link API key
if self.api_key_required:
if self.api_key is None:
self.zappa.create_api_key(api_id=api_id, stage_name=self.api_stage)
else:
self.zappa.add_api_stage_to_api_key(api_key=self.api_key, api_id=api_id, stage_name=self.api_stage)
if self.stage_config.get('touch', True):
self.touch_endpoint(endpoint_url)
# Finally, delete the local copy our zip package
if not source_zip:
if self.stage_config.get('delete_local_zip', True):
self.remove_local_zip()
# Remove the project zip from S3.
if not source_zip:
self.remove_uploaded_zip()
self.callback('post')
click.echo(deployment_string) | [
"def",
"deploy",
"(",
"self",
",",
"source_zip",
"=",
"None",
")",
":",
"if",
"not",
"source_zip",
":",
"# Make sure we're in a venv.",
"self",
".",
"check_venv",
"(",
")",
"# Execute the prebuild script",
"if",
"self",
".",
"prebuild_script",
":",
"self",
".",
"execute_prebuild_script",
"(",
")",
"# Make sure this isn't already deployed.",
"deployed_versions",
"=",
"self",
".",
"zappa",
".",
"get_lambda_function_versions",
"(",
"self",
".",
"lambda_name",
")",
"if",
"len",
"(",
"deployed_versions",
")",
">",
"0",
":",
"raise",
"ClickException",
"(",
"\"This application is \"",
"+",
"click",
".",
"style",
"(",
"\"already deployed\"",
",",
"fg",
"=",
"\"red\"",
")",
"+",
"\" - did you mean to call \"",
"+",
"click",
".",
"style",
"(",
"\"update\"",
",",
"bold",
"=",
"True",
")",
"+",
"\"?\"",
")",
"# Make sure the necessary IAM execution roles are available",
"if",
"self",
".",
"manage_roles",
":",
"try",
":",
"self",
".",
"zappa",
".",
"create_iam_roles",
"(",
")",
"except",
"botocore",
".",
"client",
".",
"ClientError",
":",
"raise",
"ClickException",
"(",
"click",
".",
"style",
"(",
"\"Failed\"",
",",
"fg",
"=",
"\"red\"",
")",
"+",
"\" to \"",
"+",
"click",
".",
"style",
"(",
"\"manage IAM roles\"",
",",
"bold",
"=",
"True",
")",
"+",
"\"!\\n\"",
"+",
"\"You may \"",
"+",
"click",
".",
"style",
"(",
"\"lack the necessary AWS permissions\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" to automatically manage a Zappa execution role.\\n\"",
"+",
"\"To fix this, see here: \"",
"+",
"click",
".",
"style",
"(",
"\"https://github.com/Miserlou/Zappa#custom-aws-iam-roles-and-policies-for-deployment\"",
",",
"bold",
"=",
"True",
")",
"+",
"'\\n'",
")",
"# Create the Lambda Zip",
"self",
".",
"create_package",
"(",
")",
"self",
".",
"callback",
"(",
"'zip'",
")",
"# Upload it to S3",
"success",
"=",
"self",
".",
"zappa",
".",
"upload_to_s3",
"(",
"self",
".",
"zip_path",
",",
"self",
".",
"s3_bucket_name",
",",
"disable_progress",
"=",
"self",
".",
"disable_progress",
")",
"if",
"not",
"success",
":",
"# pragma: no cover",
"raise",
"ClickException",
"(",
"\"Unable to upload to S3. Quitting.\"",
")",
"# If using a slim handler, upload it to S3 and tell lambda to use this slim handler zip",
"if",
"self",
".",
"stage_config",
".",
"get",
"(",
"'slim_handler'",
",",
"False",
")",
":",
"# https://github.com/Miserlou/Zappa/issues/510",
"success",
"=",
"self",
".",
"zappa",
".",
"upload_to_s3",
"(",
"self",
".",
"handler_path",
",",
"self",
".",
"s3_bucket_name",
",",
"disable_progress",
"=",
"self",
".",
"disable_progress",
")",
"if",
"not",
"success",
":",
"# pragma: no cover",
"raise",
"ClickException",
"(",
"\"Unable to upload handler to S3. Quitting.\"",
")",
"# Copy the project zip to the current project zip",
"current_project_name",
"=",
"'{0!s}_{1!s}_current_project.tar.gz'",
".",
"format",
"(",
"self",
".",
"api_stage",
",",
"self",
".",
"project_name",
")",
"success",
"=",
"self",
".",
"zappa",
".",
"copy_on_s3",
"(",
"src_file_name",
"=",
"self",
".",
"zip_path",
",",
"dst_file_name",
"=",
"current_project_name",
",",
"bucket_name",
"=",
"self",
".",
"s3_bucket_name",
")",
"if",
"not",
"success",
":",
"# pragma: no cover",
"raise",
"ClickException",
"(",
"\"Unable to copy the zip to be the current project. Quitting.\"",
")",
"handler_file",
"=",
"self",
".",
"handler_path",
"else",
":",
"handler_file",
"=",
"self",
".",
"zip_path",
"# Fixes https://github.com/Miserlou/Zappa/issues/613",
"try",
":",
"self",
".",
"lambda_arn",
"=",
"self",
".",
"zappa",
".",
"get_lambda_function",
"(",
"function_name",
"=",
"self",
".",
"lambda_name",
")",
"except",
"botocore",
".",
"client",
".",
"ClientError",
":",
"# Register the Lambda function with that zip as the source",
"# You'll also need to define the path to your lambda_handler code.",
"kwargs",
"=",
"dict",
"(",
"handler",
"=",
"self",
".",
"lambda_handler",
",",
"description",
"=",
"self",
".",
"lambda_description",
",",
"vpc_config",
"=",
"self",
".",
"vpc_config",
",",
"dead_letter_config",
"=",
"self",
".",
"dead_letter_config",
",",
"timeout",
"=",
"self",
".",
"timeout_seconds",
",",
"memory_size",
"=",
"self",
".",
"memory_size",
",",
"runtime",
"=",
"self",
".",
"runtime",
",",
"aws_environment_variables",
"=",
"self",
".",
"aws_environment_variables",
",",
"aws_kms_key_arn",
"=",
"self",
".",
"aws_kms_key_arn",
",",
"use_alb",
"=",
"self",
".",
"use_alb",
")",
"if",
"source_zip",
"and",
"source_zip",
".",
"startswith",
"(",
"'s3://'",
")",
":",
"bucket",
",",
"key_name",
"=",
"parse_s3_url",
"(",
"source_zip",
")",
"kwargs",
"[",
"'function_name'",
"]",
"=",
"self",
".",
"lambda_name",
"kwargs",
"[",
"'bucket'",
"]",
"=",
"bucket",
"kwargs",
"[",
"'s3_key'",
"]",
"=",
"key_name",
"elif",
"source_zip",
"and",
"not",
"source_zip",
".",
"startswith",
"(",
"'s3://'",
")",
":",
"with",
"open",
"(",
"source_zip",
",",
"mode",
"=",
"'rb'",
")",
"as",
"fh",
":",
"byte_stream",
"=",
"fh",
".",
"read",
"(",
")",
"kwargs",
"[",
"'function_name'",
"]",
"=",
"self",
".",
"lambda_name",
"kwargs",
"[",
"'local_zip'",
"]",
"=",
"byte_stream",
"else",
":",
"kwargs",
"[",
"'function_name'",
"]",
"=",
"self",
".",
"lambda_name",
"kwargs",
"[",
"'bucket'",
"]",
"=",
"self",
".",
"s3_bucket_name",
"kwargs",
"[",
"'s3_key'",
"]",
"=",
"handler_file",
"self",
".",
"lambda_arn",
"=",
"self",
".",
"zappa",
".",
"create_lambda_function",
"(",
"*",
"*",
"kwargs",
")",
"# Schedule events for this deployment",
"self",
".",
"schedule",
"(",
")",
"endpoint_url",
"=",
"''",
"deployment_string",
"=",
"click",
".",
"style",
"(",
"\"Deployment complete\"",
",",
"fg",
"=",
"\"green\"",
",",
"bold",
"=",
"True",
")",
"+",
"\"!\"",
"if",
"self",
".",
"use_alb",
":",
"kwargs",
"=",
"dict",
"(",
"lambda_arn",
"=",
"self",
".",
"lambda_arn",
",",
"lambda_name",
"=",
"self",
".",
"lambda_name",
",",
"alb_vpc_config",
"=",
"self",
".",
"alb_vpc_config",
",",
"timeout",
"=",
"self",
".",
"timeout_seconds",
")",
"self",
".",
"zappa",
".",
"deploy_lambda_alb",
"(",
"*",
"*",
"kwargs",
")",
"if",
"self",
".",
"use_apigateway",
":",
"# Create and configure the API Gateway",
"template",
"=",
"self",
".",
"zappa",
".",
"create_stack_template",
"(",
"lambda_arn",
"=",
"self",
".",
"lambda_arn",
",",
"lambda_name",
"=",
"self",
".",
"lambda_name",
",",
"api_key_required",
"=",
"self",
".",
"api_key_required",
",",
"iam_authorization",
"=",
"self",
".",
"iam_authorization",
",",
"authorizer",
"=",
"self",
".",
"authorizer",
",",
"cors_options",
"=",
"self",
".",
"cors",
",",
"description",
"=",
"self",
".",
"apigateway_description",
",",
"endpoint_configuration",
"=",
"self",
".",
"endpoint_configuration",
")",
"self",
".",
"zappa",
".",
"update_stack",
"(",
"self",
".",
"lambda_name",
",",
"self",
".",
"s3_bucket_name",
",",
"wait",
"=",
"True",
",",
"disable_progress",
"=",
"self",
".",
"disable_progress",
")",
"api_id",
"=",
"self",
".",
"zappa",
".",
"get_api_id",
"(",
"self",
".",
"lambda_name",
")",
"# Add binary support",
"if",
"self",
".",
"binary_support",
":",
"self",
".",
"zappa",
".",
"add_binary_support",
"(",
"api_id",
"=",
"api_id",
",",
"cors",
"=",
"self",
".",
"cors",
")",
"# Add payload compression",
"if",
"self",
".",
"stage_config",
".",
"get",
"(",
"'payload_compression'",
",",
"True",
")",
":",
"self",
".",
"zappa",
".",
"add_api_compression",
"(",
"api_id",
"=",
"api_id",
",",
"min_compression_size",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'payload_minimum_compression_size'",
",",
"0",
")",
")",
"# Deploy the API!",
"endpoint_url",
"=",
"self",
".",
"deploy_api_gateway",
"(",
"api_id",
")",
"deployment_string",
"=",
"deployment_string",
"+",
"\": {}\"",
".",
"format",
"(",
"endpoint_url",
")",
"# Create/link API key",
"if",
"self",
".",
"api_key_required",
":",
"if",
"self",
".",
"api_key",
"is",
"None",
":",
"self",
".",
"zappa",
".",
"create_api_key",
"(",
"api_id",
"=",
"api_id",
",",
"stage_name",
"=",
"self",
".",
"api_stage",
")",
"else",
":",
"self",
".",
"zappa",
".",
"add_api_stage_to_api_key",
"(",
"api_key",
"=",
"self",
".",
"api_key",
",",
"api_id",
"=",
"api_id",
",",
"stage_name",
"=",
"self",
".",
"api_stage",
")",
"if",
"self",
".",
"stage_config",
".",
"get",
"(",
"'touch'",
",",
"True",
")",
":",
"self",
".",
"touch_endpoint",
"(",
"endpoint_url",
")",
"# Finally, delete the local copy our zip package",
"if",
"not",
"source_zip",
":",
"if",
"self",
".",
"stage_config",
".",
"get",
"(",
"'delete_local_zip'",
",",
"True",
")",
":",
"self",
".",
"remove_local_zip",
"(",
")",
"# Remove the project zip from S3.",
"if",
"not",
"source_zip",
":",
"self",
".",
"remove_uploaded_zip",
"(",
")",
"self",
".",
"callback",
"(",
"'post'",
")",
"click",
".",
"echo",
"(",
"deployment_string",
")"
] | Package your project, upload it to S3, register the Lambda function
and create the API Gateway routes. | [
"Package",
"your",
"project",
"upload",
"it",
"to",
"S3",
"register",
"the",
"Lambda",
"function",
"and",
"create",
"the",
"API",
"Gateway",
"routes",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L683-L854 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.update | def update(self, source_zip=None, no_upload=False):
"""
Repackage and update the function code.
"""
if not source_zip:
# Make sure we're in a venv.
self.check_venv()
# Execute the prebuild script
if self.prebuild_script:
self.execute_prebuild_script()
# Temporary version check
try:
updated_time = 1472581018
function_response = self.zappa.lambda_client.get_function(FunctionName=self.lambda_name)
conf = function_response['Configuration']
last_updated = parser.parse(conf['LastModified'])
last_updated_unix = time.mktime(last_updated.timetuple())
except botocore.exceptions.BotoCoreError as e:
click.echo(click.style(type(e).__name__, fg="red") + ": " + e.args[0])
sys.exit(-1)
except Exception as e:
click.echo(click.style("Warning!", fg="red") + " Couldn't get function " + self.lambda_name +
" in " + self.zappa.aws_region + " - have you deployed yet?")
sys.exit(-1)
if last_updated_unix <= updated_time:
click.echo(click.style("Warning!", fg="red") +
" You may have upgraded Zappa since deploying this application. You will need to " +
click.style("redeploy", bold=True) + " for this deployment to work properly!")
# Make sure the necessary IAM execution roles are available
if self.manage_roles:
try:
self.zappa.create_iam_roles()
except botocore.client.ClientError:
click.echo(click.style("Failed", fg="red") + " to " + click.style("manage IAM roles", bold=True) + "!")
click.echo("You may " + click.style("lack the necessary AWS permissions", bold=True) +
" to automatically manage a Zappa execution role.")
click.echo("To fix this, see here: " +
click.style("https://github.com/Miserlou/Zappa#custom-aws-iam-roles-and-policies-for-deployment",
bold=True))
sys.exit(-1)
# Create the Lambda Zip,
if not no_upload:
self.create_package()
self.callback('zip')
# Upload it to S3
if not no_upload:
success = self.zappa.upload_to_s3(self.zip_path, self.s3_bucket_name, disable_progress=self.disable_progress)
if not success: # pragma: no cover
raise ClickException("Unable to upload project to S3. Quitting.")
# If using a slim handler, upload it to S3 and tell lambda to use this slim handler zip
if self.stage_config.get('slim_handler', False):
# https://github.com/Miserlou/Zappa/issues/510
success = self.zappa.upload_to_s3(self.handler_path, self.s3_bucket_name, disable_progress=self.disable_progress)
if not success: # pragma: no cover
raise ClickException("Unable to upload handler to S3. Quitting.")
# Copy the project zip to the current project zip
current_project_name = '{0!s}_{1!s}_current_project.tar.gz'.format(self.api_stage, self.project_name)
success = self.zappa.copy_on_s3(src_file_name=self.zip_path, dst_file_name=current_project_name,
bucket_name=self.s3_bucket_name)
if not success: # pragma: no cover
raise ClickException("Unable to copy the zip to be the current project. Quitting.")
handler_file = self.handler_path
else:
handler_file = self.zip_path
# Register the Lambda function with that zip as the source
# You'll also need to define the path to your lambda_handler code.
kwargs = dict(
bucket=self.s3_bucket_name,
function_name=self.lambda_name,
num_revisions=self.num_retained_versions
)
if source_zip and source_zip.startswith('s3://'):
bucket, key_name = parse_s3_url(source_zip)
kwargs.update(dict(
bucket=bucket,
s3_key=key_name
))
self.lambda_arn = self.zappa.update_lambda_function(**kwargs)
elif source_zip and not source_zip.startswith('s3://'):
with open(source_zip, mode='rb') as fh:
byte_stream = fh.read()
kwargs['local_zip'] = byte_stream
self.lambda_arn = self.zappa.update_lambda_function(**kwargs)
else:
if not no_upload:
kwargs['s3_key'] = handler_file
self.lambda_arn = self.zappa.update_lambda_function(**kwargs)
# Remove the uploaded zip from S3, because it is now registered..
if not source_zip and not no_upload:
self.remove_uploaded_zip()
# Update the configuration, in case there are changes.
self.lambda_arn = self.zappa.update_lambda_configuration(
lambda_arn=self.lambda_arn,
function_name=self.lambda_name,
handler=self.lambda_handler,
description=self.lambda_description,
vpc_config=self.vpc_config,
timeout=self.timeout_seconds,
memory_size=self.memory_size,
runtime=self.runtime,
aws_environment_variables=self.aws_environment_variables,
aws_kms_key_arn=self.aws_kms_key_arn
)
# Finally, delete the local copy our zip package
if not source_zip and not no_upload:
if self.stage_config.get('delete_local_zip', True):
self.remove_local_zip()
if self.use_apigateway:
self.zappa.create_stack_template(
lambda_arn=self.lambda_arn,
lambda_name=self.lambda_name,
api_key_required=self.api_key_required,
iam_authorization=self.iam_authorization,
authorizer=self.authorizer,
cors_options=self.cors,
description=self.apigateway_description,
endpoint_configuration=self.endpoint_configuration
)
self.zappa.update_stack(
self.lambda_name,
self.s3_bucket_name,
wait=True,
update_only=True,
disable_progress=self.disable_progress)
api_id = self.zappa.get_api_id(self.lambda_name)
# Update binary support
if self.binary_support:
self.zappa.add_binary_support(api_id=api_id, cors=self.cors)
else:
self.zappa.remove_binary_support(api_id=api_id, cors=self.cors)
if self.stage_config.get('payload_compression', True):
self.zappa.add_api_compression(
api_id=api_id,
min_compression_size=self.stage_config.get('payload_minimum_compression_size', 0))
else:
self.zappa.remove_api_compression(api_id=api_id)
# It looks a bit like we might actually be using this just to get the URL,
# but we're also updating a few of the APIGW settings.
endpoint_url = self.deploy_api_gateway(api_id)
if self.stage_config.get('domain', None):
endpoint_url = self.stage_config.get('domain')
else:
endpoint_url = None
self.schedule()
# Update any cognito pool with the lambda arn
# do this after schedule as schedule clears the lambda policy and we need to add one
self.update_cognito_triggers()
self.callback('post')
if endpoint_url and 'https://' not in endpoint_url:
endpoint_url = 'https://' + endpoint_url
if self.base_path:
endpoint_url += '/' + self.base_path
deployed_string = "Your updated Zappa deployment is " + click.style("live", fg='green', bold=True) + "!"
if self.use_apigateway:
deployed_string = deployed_string + ": " + click.style("{}".format(endpoint_url), bold=True)
api_url = None
if endpoint_url and 'amazonaws.com' not in endpoint_url:
api_url = self.zappa.get_api_url(
self.lambda_name,
self.api_stage)
if endpoint_url != api_url:
deployed_string = deployed_string + " (" + api_url + ")"
if self.stage_config.get('touch', True):
if api_url:
self.touch_endpoint(api_url)
elif endpoint_url:
self.touch_endpoint(endpoint_url)
click.echo(deployed_string) | python | def update(self, source_zip=None, no_upload=False):
"""
Repackage and update the function code.
"""
if not source_zip:
# Make sure we're in a venv.
self.check_venv()
# Execute the prebuild script
if self.prebuild_script:
self.execute_prebuild_script()
# Temporary version check
try:
updated_time = 1472581018
function_response = self.zappa.lambda_client.get_function(FunctionName=self.lambda_name)
conf = function_response['Configuration']
last_updated = parser.parse(conf['LastModified'])
last_updated_unix = time.mktime(last_updated.timetuple())
except botocore.exceptions.BotoCoreError as e:
click.echo(click.style(type(e).__name__, fg="red") + ": " + e.args[0])
sys.exit(-1)
except Exception as e:
click.echo(click.style("Warning!", fg="red") + " Couldn't get function " + self.lambda_name +
" in " + self.zappa.aws_region + " - have you deployed yet?")
sys.exit(-1)
if last_updated_unix <= updated_time:
click.echo(click.style("Warning!", fg="red") +
" You may have upgraded Zappa since deploying this application. You will need to " +
click.style("redeploy", bold=True) + " for this deployment to work properly!")
# Make sure the necessary IAM execution roles are available
if self.manage_roles:
try:
self.zappa.create_iam_roles()
except botocore.client.ClientError:
click.echo(click.style("Failed", fg="red") + " to " + click.style("manage IAM roles", bold=True) + "!")
click.echo("You may " + click.style("lack the necessary AWS permissions", bold=True) +
" to automatically manage a Zappa execution role.")
click.echo("To fix this, see here: " +
click.style("https://github.com/Miserlou/Zappa#custom-aws-iam-roles-and-policies-for-deployment",
bold=True))
sys.exit(-1)
# Create the Lambda Zip,
if not no_upload:
self.create_package()
self.callback('zip')
# Upload it to S3
if not no_upload:
success = self.zappa.upload_to_s3(self.zip_path, self.s3_bucket_name, disable_progress=self.disable_progress)
if not success: # pragma: no cover
raise ClickException("Unable to upload project to S3. Quitting.")
# If using a slim handler, upload it to S3 and tell lambda to use this slim handler zip
if self.stage_config.get('slim_handler', False):
# https://github.com/Miserlou/Zappa/issues/510
success = self.zappa.upload_to_s3(self.handler_path, self.s3_bucket_name, disable_progress=self.disable_progress)
if not success: # pragma: no cover
raise ClickException("Unable to upload handler to S3. Quitting.")
# Copy the project zip to the current project zip
current_project_name = '{0!s}_{1!s}_current_project.tar.gz'.format(self.api_stage, self.project_name)
success = self.zappa.copy_on_s3(src_file_name=self.zip_path, dst_file_name=current_project_name,
bucket_name=self.s3_bucket_name)
if not success: # pragma: no cover
raise ClickException("Unable to copy the zip to be the current project. Quitting.")
handler_file = self.handler_path
else:
handler_file = self.zip_path
# Register the Lambda function with that zip as the source
# You'll also need to define the path to your lambda_handler code.
kwargs = dict(
bucket=self.s3_bucket_name,
function_name=self.lambda_name,
num_revisions=self.num_retained_versions
)
if source_zip and source_zip.startswith('s3://'):
bucket, key_name = parse_s3_url(source_zip)
kwargs.update(dict(
bucket=bucket,
s3_key=key_name
))
self.lambda_arn = self.zappa.update_lambda_function(**kwargs)
elif source_zip and not source_zip.startswith('s3://'):
with open(source_zip, mode='rb') as fh:
byte_stream = fh.read()
kwargs['local_zip'] = byte_stream
self.lambda_arn = self.zappa.update_lambda_function(**kwargs)
else:
if not no_upload:
kwargs['s3_key'] = handler_file
self.lambda_arn = self.zappa.update_lambda_function(**kwargs)
# Remove the uploaded zip from S3, because it is now registered..
if not source_zip and not no_upload:
self.remove_uploaded_zip()
# Update the configuration, in case there are changes.
self.lambda_arn = self.zappa.update_lambda_configuration(
lambda_arn=self.lambda_arn,
function_name=self.lambda_name,
handler=self.lambda_handler,
description=self.lambda_description,
vpc_config=self.vpc_config,
timeout=self.timeout_seconds,
memory_size=self.memory_size,
runtime=self.runtime,
aws_environment_variables=self.aws_environment_variables,
aws_kms_key_arn=self.aws_kms_key_arn
)
# Finally, delete the local copy our zip package
if not source_zip and not no_upload:
if self.stage_config.get('delete_local_zip', True):
self.remove_local_zip()
if self.use_apigateway:
self.zappa.create_stack_template(
lambda_arn=self.lambda_arn,
lambda_name=self.lambda_name,
api_key_required=self.api_key_required,
iam_authorization=self.iam_authorization,
authorizer=self.authorizer,
cors_options=self.cors,
description=self.apigateway_description,
endpoint_configuration=self.endpoint_configuration
)
self.zappa.update_stack(
self.lambda_name,
self.s3_bucket_name,
wait=True,
update_only=True,
disable_progress=self.disable_progress)
api_id = self.zappa.get_api_id(self.lambda_name)
# Update binary support
if self.binary_support:
self.zappa.add_binary_support(api_id=api_id, cors=self.cors)
else:
self.zappa.remove_binary_support(api_id=api_id, cors=self.cors)
if self.stage_config.get('payload_compression', True):
self.zappa.add_api_compression(
api_id=api_id,
min_compression_size=self.stage_config.get('payload_minimum_compression_size', 0))
else:
self.zappa.remove_api_compression(api_id=api_id)
# It looks a bit like we might actually be using this just to get the URL,
# but we're also updating a few of the APIGW settings.
endpoint_url = self.deploy_api_gateway(api_id)
if self.stage_config.get('domain', None):
endpoint_url = self.stage_config.get('domain')
else:
endpoint_url = None
self.schedule()
# Update any cognito pool with the lambda arn
# do this after schedule as schedule clears the lambda policy and we need to add one
self.update_cognito_triggers()
self.callback('post')
if endpoint_url and 'https://' not in endpoint_url:
endpoint_url = 'https://' + endpoint_url
if self.base_path:
endpoint_url += '/' + self.base_path
deployed_string = "Your updated Zappa deployment is " + click.style("live", fg='green', bold=True) + "!"
if self.use_apigateway:
deployed_string = deployed_string + ": " + click.style("{}".format(endpoint_url), bold=True)
api_url = None
if endpoint_url and 'amazonaws.com' not in endpoint_url:
api_url = self.zappa.get_api_url(
self.lambda_name,
self.api_stage)
if endpoint_url != api_url:
deployed_string = deployed_string + " (" + api_url + ")"
if self.stage_config.get('touch', True):
if api_url:
self.touch_endpoint(api_url)
elif endpoint_url:
self.touch_endpoint(endpoint_url)
click.echo(deployed_string) | [
"def",
"update",
"(",
"self",
",",
"source_zip",
"=",
"None",
",",
"no_upload",
"=",
"False",
")",
":",
"if",
"not",
"source_zip",
":",
"# Make sure we're in a venv.",
"self",
".",
"check_venv",
"(",
")",
"# Execute the prebuild script",
"if",
"self",
".",
"prebuild_script",
":",
"self",
".",
"execute_prebuild_script",
"(",
")",
"# Temporary version check",
"try",
":",
"updated_time",
"=",
"1472581018",
"function_response",
"=",
"self",
".",
"zappa",
".",
"lambda_client",
".",
"get_function",
"(",
"FunctionName",
"=",
"self",
".",
"lambda_name",
")",
"conf",
"=",
"function_response",
"[",
"'Configuration'",
"]",
"last_updated",
"=",
"parser",
".",
"parse",
"(",
"conf",
"[",
"'LastModified'",
"]",
")",
"last_updated_unix",
"=",
"time",
".",
"mktime",
"(",
"last_updated",
".",
"timetuple",
"(",
")",
")",
"except",
"botocore",
".",
"exceptions",
".",
"BotoCoreError",
"as",
"e",
":",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"type",
"(",
"e",
")",
".",
"__name__",
",",
"fg",
"=",
"\"red\"",
")",
"+",
"\": \"",
"+",
"e",
".",
"args",
"[",
"0",
"]",
")",
"sys",
".",
"exit",
"(",
"-",
"1",
")",
"except",
"Exception",
"as",
"e",
":",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"Warning!\"",
",",
"fg",
"=",
"\"red\"",
")",
"+",
"\" Couldn't get function \"",
"+",
"self",
".",
"lambda_name",
"+",
"\" in \"",
"+",
"self",
".",
"zappa",
".",
"aws_region",
"+",
"\" - have you deployed yet?\"",
")",
"sys",
".",
"exit",
"(",
"-",
"1",
")",
"if",
"last_updated_unix",
"<=",
"updated_time",
":",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"Warning!\"",
",",
"fg",
"=",
"\"red\"",
")",
"+",
"\" You may have upgraded Zappa since deploying this application. You will need to \"",
"+",
"click",
".",
"style",
"(",
"\"redeploy\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" for this deployment to work properly!\"",
")",
"# Make sure the necessary IAM execution roles are available",
"if",
"self",
".",
"manage_roles",
":",
"try",
":",
"self",
".",
"zappa",
".",
"create_iam_roles",
"(",
")",
"except",
"botocore",
".",
"client",
".",
"ClientError",
":",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"Failed\"",
",",
"fg",
"=",
"\"red\"",
")",
"+",
"\" to \"",
"+",
"click",
".",
"style",
"(",
"\"manage IAM roles\"",
",",
"bold",
"=",
"True",
")",
"+",
"\"!\"",
")",
"click",
".",
"echo",
"(",
"\"You may \"",
"+",
"click",
".",
"style",
"(",
"\"lack the necessary AWS permissions\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" to automatically manage a Zappa execution role.\"",
")",
"click",
".",
"echo",
"(",
"\"To fix this, see here: \"",
"+",
"click",
".",
"style",
"(",
"\"https://github.com/Miserlou/Zappa#custom-aws-iam-roles-and-policies-for-deployment\"",
",",
"bold",
"=",
"True",
")",
")",
"sys",
".",
"exit",
"(",
"-",
"1",
")",
"# Create the Lambda Zip,",
"if",
"not",
"no_upload",
":",
"self",
".",
"create_package",
"(",
")",
"self",
".",
"callback",
"(",
"'zip'",
")",
"# Upload it to S3",
"if",
"not",
"no_upload",
":",
"success",
"=",
"self",
".",
"zappa",
".",
"upload_to_s3",
"(",
"self",
".",
"zip_path",
",",
"self",
".",
"s3_bucket_name",
",",
"disable_progress",
"=",
"self",
".",
"disable_progress",
")",
"if",
"not",
"success",
":",
"# pragma: no cover",
"raise",
"ClickException",
"(",
"\"Unable to upload project to S3. Quitting.\"",
")",
"# If using a slim handler, upload it to S3 and tell lambda to use this slim handler zip",
"if",
"self",
".",
"stage_config",
".",
"get",
"(",
"'slim_handler'",
",",
"False",
")",
":",
"# https://github.com/Miserlou/Zappa/issues/510",
"success",
"=",
"self",
".",
"zappa",
".",
"upload_to_s3",
"(",
"self",
".",
"handler_path",
",",
"self",
".",
"s3_bucket_name",
",",
"disable_progress",
"=",
"self",
".",
"disable_progress",
")",
"if",
"not",
"success",
":",
"# pragma: no cover",
"raise",
"ClickException",
"(",
"\"Unable to upload handler to S3. Quitting.\"",
")",
"# Copy the project zip to the current project zip",
"current_project_name",
"=",
"'{0!s}_{1!s}_current_project.tar.gz'",
".",
"format",
"(",
"self",
".",
"api_stage",
",",
"self",
".",
"project_name",
")",
"success",
"=",
"self",
".",
"zappa",
".",
"copy_on_s3",
"(",
"src_file_name",
"=",
"self",
".",
"zip_path",
",",
"dst_file_name",
"=",
"current_project_name",
",",
"bucket_name",
"=",
"self",
".",
"s3_bucket_name",
")",
"if",
"not",
"success",
":",
"# pragma: no cover",
"raise",
"ClickException",
"(",
"\"Unable to copy the zip to be the current project. Quitting.\"",
")",
"handler_file",
"=",
"self",
".",
"handler_path",
"else",
":",
"handler_file",
"=",
"self",
".",
"zip_path",
"# Register the Lambda function with that zip as the source",
"# You'll also need to define the path to your lambda_handler code.",
"kwargs",
"=",
"dict",
"(",
"bucket",
"=",
"self",
".",
"s3_bucket_name",
",",
"function_name",
"=",
"self",
".",
"lambda_name",
",",
"num_revisions",
"=",
"self",
".",
"num_retained_versions",
")",
"if",
"source_zip",
"and",
"source_zip",
".",
"startswith",
"(",
"'s3://'",
")",
":",
"bucket",
",",
"key_name",
"=",
"parse_s3_url",
"(",
"source_zip",
")",
"kwargs",
".",
"update",
"(",
"dict",
"(",
"bucket",
"=",
"bucket",
",",
"s3_key",
"=",
"key_name",
")",
")",
"self",
".",
"lambda_arn",
"=",
"self",
".",
"zappa",
".",
"update_lambda_function",
"(",
"*",
"*",
"kwargs",
")",
"elif",
"source_zip",
"and",
"not",
"source_zip",
".",
"startswith",
"(",
"'s3://'",
")",
":",
"with",
"open",
"(",
"source_zip",
",",
"mode",
"=",
"'rb'",
")",
"as",
"fh",
":",
"byte_stream",
"=",
"fh",
".",
"read",
"(",
")",
"kwargs",
"[",
"'local_zip'",
"]",
"=",
"byte_stream",
"self",
".",
"lambda_arn",
"=",
"self",
".",
"zappa",
".",
"update_lambda_function",
"(",
"*",
"*",
"kwargs",
")",
"else",
":",
"if",
"not",
"no_upload",
":",
"kwargs",
"[",
"'s3_key'",
"]",
"=",
"handler_file",
"self",
".",
"lambda_arn",
"=",
"self",
".",
"zappa",
".",
"update_lambda_function",
"(",
"*",
"*",
"kwargs",
")",
"# Remove the uploaded zip from S3, because it is now registered..",
"if",
"not",
"source_zip",
"and",
"not",
"no_upload",
":",
"self",
".",
"remove_uploaded_zip",
"(",
")",
"# Update the configuration, in case there are changes.",
"self",
".",
"lambda_arn",
"=",
"self",
".",
"zappa",
".",
"update_lambda_configuration",
"(",
"lambda_arn",
"=",
"self",
".",
"lambda_arn",
",",
"function_name",
"=",
"self",
".",
"lambda_name",
",",
"handler",
"=",
"self",
".",
"lambda_handler",
",",
"description",
"=",
"self",
".",
"lambda_description",
",",
"vpc_config",
"=",
"self",
".",
"vpc_config",
",",
"timeout",
"=",
"self",
".",
"timeout_seconds",
",",
"memory_size",
"=",
"self",
".",
"memory_size",
",",
"runtime",
"=",
"self",
".",
"runtime",
",",
"aws_environment_variables",
"=",
"self",
".",
"aws_environment_variables",
",",
"aws_kms_key_arn",
"=",
"self",
".",
"aws_kms_key_arn",
")",
"# Finally, delete the local copy our zip package",
"if",
"not",
"source_zip",
"and",
"not",
"no_upload",
":",
"if",
"self",
".",
"stage_config",
".",
"get",
"(",
"'delete_local_zip'",
",",
"True",
")",
":",
"self",
".",
"remove_local_zip",
"(",
")",
"if",
"self",
".",
"use_apigateway",
":",
"self",
".",
"zappa",
".",
"create_stack_template",
"(",
"lambda_arn",
"=",
"self",
".",
"lambda_arn",
",",
"lambda_name",
"=",
"self",
".",
"lambda_name",
",",
"api_key_required",
"=",
"self",
".",
"api_key_required",
",",
"iam_authorization",
"=",
"self",
".",
"iam_authorization",
",",
"authorizer",
"=",
"self",
".",
"authorizer",
",",
"cors_options",
"=",
"self",
".",
"cors",
",",
"description",
"=",
"self",
".",
"apigateway_description",
",",
"endpoint_configuration",
"=",
"self",
".",
"endpoint_configuration",
")",
"self",
".",
"zappa",
".",
"update_stack",
"(",
"self",
".",
"lambda_name",
",",
"self",
".",
"s3_bucket_name",
",",
"wait",
"=",
"True",
",",
"update_only",
"=",
"True",
",",
"disable_progress",
"=",
"self",
".",
"disable_progress",
")",
"api_id",
"=",
"self",
".",
"zappa",
".",
"get_api_id",
"(",
"self",
".",
"lambda_name",
")",
"# Update binary support",
"if",
"self",
".",
"binary_support",
":",
"self",
".",
"zappa",
".",
"add_binary_support",
"(",
"api_id",
"=",
"api_id",
",",
"cors",
"=",
"self",
".",
"cors",
")",
"else",
":",
"self",
".",
"zappa",
".",
"remove_binary_support",
"(",
"api_id",
"=",
"api_id",
",",
"cors",
"=",
"self",
".",
"cors",
")",
"if",
"self",
".",
"stage_config",
".",
"get",
"(",
"'payload_compression'",
",",
"True",
")",
":",
"self",
".",
"zappa",
".",
"add_api_compression",
"(",
"api_id",
"=",
"api_id",
",",
"min_compression_size",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'payload_minimum_compression_size'",
",",
"0",
")",
")",
"else",
":",
"self",
".",
"zappa",
".",
"remove_api_compression",
"(",
"api_id",
"=",
"api_id",
")",
"# It looks a bit like we might actually be using this just to get the URL,",
"# but we're also updating a few of the APIGW settings.",
"endpoint_url",
"=",
"self",
".",
"deploy_api_gateway",
"(",
"api_id",
")",
"if",
"self",
".",
"stage_config",
".",
"get",
"(",
"'domain'",
",",
"None",
")",
":",
"endpoint_url",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'domain'",
")",
"else",
":",
"endpoint_url",
"=",
"None",
"self",
".",
"schedule",
"(",
")",
"# Update any cognito pool with the lambda arn",
"# do this after schedule as schedule clears the lambda policy and we need to add one",
"self",
".",
"update_cognito_triggers",
"(",
")",
"self",
".",
"callback",
"(",
"'post'",
")",
"if",
"endpoint_url",
"and",
"'https://'",
"not",
"in",
"endpoint_url",
":",
"endpoint_url",
"=",
"'https://'",
"+",
"endpoint_url",
"if",
"self",
".",
"base_path",
":",
"endpoint_url",
"+=",
"'/'",
"+",
"self",
".",
"base_path",
"deployed_string",
"=",
"\"Your updated Zappa deployment is \"",
"+",
"click",
".",
"style",
"(",
"\"live\"",
",",
"fg",
"=",
"'green'",
",",
"bold",
"=",
"True",
")",
"+",
"\"!\"",
"if",
"self",
".",
"use_apigateway",
":",
"deployed_string",
"=",
"deployed_string",
"+",
"\": \"",
"+",
"click",
".",
"style",
"(",
"\"{}\"",
".",
"format",
"(",
"endpoint_url",
")",
",",
"bold",
"=",
"True",
")",
"api_url",
"=",
"None",
"if",
"endpoint_url",
"and",
"'amazonaws.com'",
"not",
"in",
"endpoint_url",
":",
"api_url",
"=",
"self",
".",
"zappa",
".",
"get_api_url",
"(",
"self",
".",
"lambda_name",
",",
"self",
".",
"api_stage",
")",
"if",
"endpoint_url",
"!=",
"api_url",
":",
"deployed_string",
"=",
"deployed_string",
"+",
"\" (\"",
"+",
"api_url",
"+",
"\")\"",
"if",
"self",
".",
"stage_config",
".",
"get",
"(",
"'touch'",
",",
"True",
")",
":",
"if",
"api_url",
":",
"self",
".",
"touch_endpoint",
"(",
"api_url",
")",
"elif",
"endpoint_url",
":",
"self",
".",
"touch_endpoint",
"(",
"endpoint_url",
")",
"click",
".",
"echo",
"(",
"deployed_string",
")"
] | Repackage and update the function code. | [
"Repackage",
"and",
"update",
"the",
"function",
"code",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L856-L1055 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.rollback | def rollback(self, revision):
"""
Rollsback the currently deploy lambda code to a previous revision.
"""
print("Rolling back..")
self.zappa.rollback_lambda_function_version(
self.lambda_name, versions_back=revision)
print("Done!") | python | def rollback(self, revision):
"""
Rollsback the currently deploy lambda code to a previous revision.
"""
print("Rolling back..")
self.zappa.rollback_lambda_function_version(
self.lambda_name, versions_back=revision)
print("Done!") | [
"def",
"rollback",
"(",
"self",
",",
"revision",
")",
":",
"print",
"(",
"\"Rolling back..\"",
")",
"self",
".",
"zappa",
".",
"rollback_lambda_function_version",
"(",
"self",
".",
"lambda_name",
",",
"versions_back",
"=",
"revision",
")",
"print",
"(",
"\"Done!\"",
")"
] | Rollsback the currently deploy lambda code to a previous revision. | [
"Rollsback",
"the",
"currently",
"deploy",
"lambda",
"code",
"to",
"a",
"previous",
"revision",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L1057-L1066 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.tail | def tail(self, since, filter_pattern, limit=10000, keep_open=True, colorize=True, http=False, non_http=False, force_colorize=False):
"""
Tail this function's logs.
if keep_open, do so repeatedly, printing any new logs
"""
try:
since_stamp = string_to_timestamp(since)
last_since = since_stamp
while True:
new_logs = self.zappa.fetch_logs(
self.lambda_name,
start_time=since_stamp,
limit=limit,
filter_pattern=filter_pattern,
)
new_logs = [ e for e in new_logs if e['timestamp'] > last_since ]
self.print_logs(new_logs, colorize, http, non_http, force_colorize)
if not keep_open:
break
if new_logs:
last_since = new_logs[-1]['timestamp']
time.sleep(1)
except KeyboardInterrupt: # pragma: no cover
# Die gracefully
try:
sys.exit(0)
except SystemExit:
os._exit(130) | python | def tail(self, since, filter_pattern, limit=10000, keep_open=True, colorize=True, http=False, non_http=False, force_colorize=False):
"""
Tail this function's logs.
if keep_open, do so repeatedly, printing any new logs
"""
try:
since_stamp = string_to_timestamp(since)
last_since = since_stamp
while True:
new_logs = self.zappa.fetch_logs(
self.lambda_name,
start_time=since_stamp,
limit=limit,
filter_pattern=filter_pattern,
)
new_logs = [ e for e in new_logs if e['timestamp'] > last_since ]
self.print_logs(new_logs, colorize, http, non_http, force_colorize)
if not keep_open:
break
if new_logs:
last_since = new_logs[-1]['timestamp']
time.sleep(1)
except KeyboardInterrupt: # pragma: no cover
# Die gracefully
try:
sys.exit(0)
except SystemExit:
os._exit(130) | [
"def",
"tail",
"(",
"self",
",",
"since",
",",
"filter_pattern",
",",
"limit",
"=",
"10000",
",",
"keep_open",
"=",
"True",
",",
"colorize",
"=",
"True",
",",
"http",
"=",
"False",
",",
"non_http",
"=",
"False",
",",
"force_colorize",
"=",
"False",
")",
":",
"try",
":",
"since_stamp",
"=",
"string_to_timestamp",
"(",
"since",
")",
"last_since",
"=",
"since_stamp",
"while",
"True",
":",
"new_logs",
"=",
"self",
".",
"zappa",
".",
"fetch_logs",
"(",
"self",
".",
"lambda_name",
",",
"start_time",
"=",
"since_stamp",
",",
"limit",
"=",
"limit",
",",
"filter_pattern",
"=",
"filter_pattern",
",",
")",
"new_logs",
"=",
"[",
"e",
"for",
"e",
"in",
"new_logs",
"if",
"e",
"[",
"'timestamp'",
"]",
">",
"last_since",
"]",
"self",
".",
"print_logs",
"(",
"new_logs",
",",
"colorize",
",",
"http",
",",
"non_http",
",",
"force_colorize",
")",
"if",
"not",
"keep_open",
":",
"break",
"if",
"new_logs",
":",
"last_since",
"=",
"new_logs",
"[",
"-",
"1",
"]",
"[",
"'timestamp'",
"]",
"time",
".",
"sleep",
"(",
"1",
")",
"except",
"KeyboardInterrupt",
":",
"# pragma: no cover",
"# Die gracefully",
"try",
":",
"sys",
".",
"exit",
"(",
"0",
")",
"except",
"SystemExit",
":",
"os",
".",
"_exit",
"(",
"130",
")"
] | Tail this function's logs.
if keep_open, do so repeatedly, printing any new logs | [
"Tail",
"this",
"function",
"s",
"logs",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L1068-L1100 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.undeploy | def undeploy(self, no_confirm=False, remove_logs=False):
"""
Tear down an existing deployment.
"""
if not no_confirm: # pragma: no cover
confirm = input("Are you sure you want to undeploy? [y/n] ")
if confirm != 'y':
return
if self.use_alb:
self.zappa.undeploy_lambda_alb(self.lambda_name)
if self.use_apigateway:
if remove_logs:
self.zappa.remove_api_gateway_logs(self.lambda_name)
domain_name = self.stage_config.get('domain', None)
base_path = self.stage_config.get('base_path', None)
# Only remove the api key when not specified
if self.api_key_required and self.api_key is None:
api_id = self.zappa.get_api_id(self.lambda_name)
self.zappa.remove_api_key(api_id, self.api_stage)
gateway_id = self.zappa.undeploy_api_gateway(
self.lambda_name,
domain_name=domain_name,
base_path=base_path
)
self.unschedule() # removes event triggers, including warm up event.
self.zappa.delete_lambda_function(self.lambda_name)
if remove_logs:
self.zappa.remove_lambda_function_logs(self.lambda_name)
click.echo(click.style("Done", fg="green", bold=True) + "!") | python | def undeploy(self, no_confirm=False, remove_logs=False):
"""
Tear down an existing deployment.
"""
if not no_confirm: # pragma: no cover
confirm = input("Are you sure you want to undeploy? [y/n] ")
if confirm != 'y':
return
if self.use_alb:
self.zappa.undeploy_lambda_alb(self.lambda_name)
if self.use_apigateway:
if remove_logs:
self.zappa.remove_api_gateway_logs(self.lambda_name)
domain_name = self.stage_config.get('domain', None)
base_path = self.stage_config.get('base_path', None)
# Only remove the api key when not specified
if self.api_key_required and self.api_key is None:
api_id = self.zappa.get_api_id(self.lambda_name)
self.zappa.remove_api_key(api_id, self.api_stage)
gateway_id = self.zappa.undeploy_api_gateway(
self.lambda_name,
domain_name=domain_name,
base_path=base_path
)
self.unschedule() # removes event triggers, including warm up event.
self.zappa.delete_lambda_function(self.lambda_name)
if remove_logs:
self.zappa.remove_lambda_function_logs(self.lambda_name)
click.echo(click.style("Done", fg="green", bold=True) + "!") | [
"def",
"undeploy",
"(",
"self",
",",
"no_confirm",
"=",
"False",
",",
"remove_logs",
"=",
"False",
")",
":",
"if",
"not",
"no_confirm",
":",
"# pragma: no cover",
"confirm",
"=",
"input",
"(",
"\"Are you sure you want to undeploy? [y/n] \"",
")",
"if",
"confirm",
"!=",
"'y'",
":",
"return",
"if",
"self",
".",
"use_alb",
":",
"self",
".",
"zappa",
".",
"undeploy_lambda_alb",
"(",
"self",
".",
"lambda_name",
")",
"if",
"self",
".",
"use_apigateway",
":",
"if",
"remove_logs",
":",
"self",
".",
"zappa",
".",
"remove_api_gateway_logs",
"(",
"self",
".",
"lambda_name",
")",
"domain_name",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'domain'",
",",
"None",
")",
"base_path",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'base_path'",
",",
"None",
")",
"# Only remove the api key when not specified",
"if",
"self",
".",
"api_key_required",
"and",
"self",
".",
"api_key",
"is",
"None",
":",
"api_id",
"=",
"self",
".",
"zappa",
".",
"get_api_id",
"(",
"self",
".",
"lambda_name",
")",
"self",
".",
"zappa",
".",
"remove_api_key",
"(",
"api_id",
",",
"self",
".",
"api_stage",
")",
"gateway_id",
"=",
"self",
".",
"zappa",
".",
"undeploy_api_gateway",
"(",
"self",
".",
"lambda_name",
",",
"domain_name",
"=",
"domain_name",
",",
"base_path",
"=",
"base_path",
")",
"self",
".",
"unschedule",
"(",
")",
"# removes event triggers, including warm up event.",
"self",
".",
"zappa",
".",
"delete_lambda_function",
"(",
"self",
".",
"lambda_name",
")",
"if",
"remove_logs",
":",
"self",
".",
"zappa",
".",
"remove_lambda_function_logs",
"(",
"self",
".",
"lambda_name",
")",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"Done\"",
",",
"fg",
"=",
"\"green\"",
",",
"bold",
"=",
"True",
")",
"+",
"\"!\"",
")"
] | Tear down an existing deployment. | [
"Tear",
"down",
"an",
"existing",
"deployment",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L1102-L1139 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.update_cognito_triggers | def update_cognito_triggers(self):
"""
Update any cognito triggers
"""
if self.cognito:
user_pool = self.cognito.get('user_pool')
triggers = self.cognito.get('triggers', [])
lambda_configs = set()
for trigger in triggers:
lambda_configs.add(trigger['source'].split('_')[0])
self.zappa.update_cognito(self.lambda_name, user_pool, lambda_configs, self.lambda_arn) | python | def update_cognito_triggers(self):
"""
Update any cognito triggers
"""
if self.cognito:
user_pool = self.cognito.get('user_pool')
triggers = self.cognito.get('triggers', [])
lambda_configs = set()
for trigger in triggers:
lambda_configs.add(trigger['source'].split('_')[0])
self.zappa.update_cognito(self.lambda_name, user_pool, lambda_configs, self.lambda_arn) | [
"def",
"update_cognito_triggers",
"(",
"self",
")",
":",
"if",
"self",
".",
"cognito",
":",
"user_pool",
"=",
"self",
".",
"cognito",
".",
"get",
"(",
"'user_pool'",
")",
"triggers",
"=",
"self",
".",
"cognito",
".",
"get",
"(",
"'triggers'",
",",
"[",
"]",
")",
"lambda_configs",
"=",
"set",
"(",
")",
"for",
"trigger",
"in",
"triggers",
":",
"lambda_configs",
".",
"add",
"(",
"trigger",
"[",
"'source'",
"]",
".",
"split",
"(",
"'_'",
")",
"[",
"0",
"]",
")",
"self",
".",
"zappa",
".",
"update_cognito",
"(",
"self",
".",
"lambda_name",
",",
"user_pool",
",",
"lambda_configs",
",",
"self",
".",
"lambda_arn",
")"
] | Update any cognito triggers | [
"Update",
"any",
"cognito",
"triggers"
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L1141-L1151 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.schedule | def schedule(self):
"""
Given a a list of functions and a schedule to execute them,
setup up regular execution.
"""
events = self.stage_config.get('events', [])
if events:
if not isinstance(events, list): # pragma: no cover
print("Events must be supplied as a list.")
return
for event in events:
self.collision_warning(event.get('function'))
if self.stage_config.get('keep_warm', True):
if not events:
events = []
keep_warm_rate = self.stage_config.get('keep_warm_expression', "rate(4 minutes)")
events.append({'name': 'zappa-keep-warm',
'function': 'handler.keep_warm_callback',
'expression': keep_warm_rate,
'description': 'Zappa Keep Warm - {}'.format(self.lambda_name)})
if events:
try:
function_response = self.zappa.lambda_client.get_function(FunctionName=self.lambda_name)
except botocore.exceptions.ClientError as e: # pragma: no cover
click.echo(click.style("Function does not exist", fg="yellow") + ", please " +
click.style("deploy", bold=True) + "first. Ex:" +
click.style("zappa deploy {}.".format(self.api_stage), bold=True))
sys.exit(-1)
print("Scheduling..")
self.zappa.schedule_events(
lambda_arn=function_response['Configuration']['FunctionArn'],
lambda_name=self.lambda_name,
events=events
)
# Add async tasks SNS
if self.stage_config.get('async_source', None) == 'sns' \
and self.stage_config.get('async_resources', True):
self.lambda_arn = self.zappa.get_lambda_function(
function_name=self.lambda_name)
topic_arn = self.zappa.create_async_sns_topic(
lambda_name=self.lambda_name,
lambda_arn=self.lambda_arn
)
click.echo('SNS Topic created: %s' % topic_arn)
# Add async tasks DynamoDB
table_name = self.stage_config.get('async_response_table', False)
read_capacity = self.stage_config.get('async_response_table_read_capacity', 1)
write_capacity = self.stage_config.get('async_response_table_write_capacity', 1)
if table_name and self.stage_config.get('async_resources', True):
created, response_table = self.zappa.create_async_dynamodb_table(
table_name, read_capacity, write_capacity)
if created:
click.echo('DynamoDB table created: %s' % table_name)
else:
click.echo('DynamoDB table exists: %s' % table_name)
provisioned_throughput = response_table['Table']['ProvisionedThroughput']
if provisioned_throughput['ReadCapacityUnits'] != read_capacity or \
provisioned_throughput['WriteCapacityUnits'] != write_capacity:
click.echo(click.style(
"\nWarning! Existing DynamoDB table ({}) does not match configured capacity.\n".format(table_name),
fg='red'
)) | python | def schedule(self):
"""
Given a a list of functions and a schedule to execute them,
setup up regular execution.
"""
events = self.stage_config.get('events', [])
if events:
if not isinstance(events, list): # pragma: no cover
print("Events must be supplied as a list.")
return
for event in events:
self.collision_warning(event.get('function'))
if self.stage_config.get('keep_warm', True):
if not events:
events = []
keep_warm_rate = self.stage_config.get('keep_warm_expression', "rate(4 minutes)")
events.append({'name': 'zappa-keep-warm',
'function': 'handler.keep_warm_callback',
'expression': keep_warm_rate,
'description': 'Zappa Keep Warm - {}'.format(self.lambda_name)})
if events:
try:
function_response = self.zappa.lambda_client.get_function(FunctionName=self.lambda_name)
except botocore.exceptions.ClientError as e: # pragma: no cover
click.echo(click.style("Function does not exist", fg="yellow") + ", please " +
click.style("deploy", bold=True) + "first. Ex:" +
click.style("zappa deploy {}.".format(self.api_stage), bold=True))
sys.exit(-1)
print("Scheduling..")
self.zappa.schedule_events(
lambda_arn=function_response['Configuration']['FunctionArn'],
lambda_name=self.lambda_name,
events=events
)
# Add async tasks SNS
if self.stage_config.get('async_source', None) == 'sns' \
and self.stage_config.get('async_resources', True):
self.lambda_arn = self.zappa.get_lambda_function(
function_name=self.lambda_name)
topic_arn = self.zappa.create_async_sns_topic(
lambda_name=self.lambda_name,
lambda_arn=self.lambda_arn
)
click.echo('SNS Topic created: %s' % topic_arn)
# Add async tasks DynamoDB
table_name = self.stage_config.get('async_response_table', False)
read_capacity = self.stage_config.get('async_response_table_read_capacity', 1)
write_capacity = self.stage_config.get('async_response_table_write_capacity', 1)
if table_name and self.stage_config.get('async_resources', True):
created, response_table = self.zappa.create_async_dynamodb_table(
table_name, read_capacity, write_capacity)
if created:
click.echo('DynamoDB table created: %s' % table_name)
else:
click.echo('DynamoDB table exists: %s' % table_name)
provisioned_throughput = response_table['Table']['ProvisionedThroughput']
if provisioned_throughput['ReadCapacityUnits'] != read_capacity or \
provisioned_throughput['WriteCapacityUnits'] != write_capacity:
click.echo(click.style(
"\nWarning! Existing DynamoDB table ({}) does not match configured capacity.\n".format(table_name),
fg='red'
)) | [
"def",
"schedule",
"(",
"self",
")",
":",
"events",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'events'",
",",
"[",
"]",
")",
"if",
"events",
":",
"if",
"not",
"isinstance",
"(",
"events",
",",
"list",
")",
":",
"# pragma: no cover",
"print",
"(",
"\"Events must be supplied as a list.\"",
")",
"return",
"for",
"event",
"in",
"events",
":",
"self",
".",
"collision_warning",
"(",
"event",
".",
"get",
"(",
"'function'",
")",
")",
"if",
"self",
".",
"stage_config",
".",
"get",
"(",
"'keep_warm'",
",",
"True",
")",
":",
"if",
"not",
"events",
":",
"events",
"=",
"[",
"]",
"keep_warm_rate",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'keep_warm_expression'",
",",
"\"rate(4 minutes)\"",
")",
"events",
".",
"append",
"(",
"{",
"'name'",
":",
"'zappa-keep-warm'",
",",
"'function'",
":",
"'handler.keep_warm_callback'",
",",
"'expression'",
":",
"keep_warm_rate",
",",
"'description'",
":",
"'Zappa Keep Warm - {}'",
".",
"format",
"(",
"self",
".",
"lambda_name",
")",
"}",
")",
"if",
"events",
":",
"try",
":",
"function_response",
"=",
"self",
".",
"zappa",
".",
"lambda_client",
".",
"get_function",
"(",
"FunctionName",
"=",
"self",
".",
"lambda_name",
")",
"except",
"botocore",
".",
"exceptions",
".",
"ClientError",
"as",
"e",
":",
"# pragma: no cover",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"Function does not exist\"",
",",
"fg",
"=",
"\"yellow\"",
")",
"+",
"\", please \"",
"+",
"click",
".",
"style",
"(",
"\"deploy\"",
",",
"bold",
"=",
"True",
")",
"+",
"\"first. Ex:\"",
"+",
"click",
".",
"style",
"(",
"\"zappa deploy {}.\"",
".",
"format",
"(",
"self",
".",
"api_stage",
")",
",",
"bold",
"=",
"True",
")",
")",
"sys",
".",
"exit",
"(",
"-",
"1",
")",
"print",
"(",
"\"Scheduling..\"",
")",
"self",
".",
"zappa",
".",
"schedule_events",
"(",
"lambda_arn",
"=",
"function_response",
"[",
"'Configuration'",
"]",
"[",
"'FunctionArn'",
"]",
",",
"lambda_name",
"=",
"self",
".",
"lambda_name",
",",
"events",
"=",
"events",
")",
"# Add async tasks SNS",
"if",
"self",
".",
"stage_config",
".",
"get",
"(",
"'async_source'",
",",
"None",
")",
"==",
"'sns'",
"and",
"self",
".",
"stage_config",
".",
"get",
"(",
"'async_resources'",
",",
"True",
")",
":",
"self",
".",
"lambda_arn",
"=",
"self",
".",
"zappa",
".",
"get_lambda_function",
"(",
"function_name",
"=",
"self",
".",
"lambda_name",
")",
"topic_arn",
"=",
"self",
".",
"zappa",
".",
"create_async_sns_topic",
"(",
"lambda_name",
"=",
"self",
".",
"lambda_name",
",",
"lambda_arn",
"=",
"self",
".",
"lambda_arn",
")",
"click",
".",
"echo",
"(",
"'SNS Topic created: %s'",
"%",
"topic_arn",
")",
"# Add async tasks DynamoDB",
"table_name",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'async_response_table'",
",",
"False",
")",
"read_capacity",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'async_response_table_read_capacity'",
",",
"1",
")",
"write_capacity",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'async_response_table_write_capacity'",
",",
"1",
")",
"if",
"table_name",
"and",
"self",
".",
"stage_config",
".",
"get",
"(",
"'async_resources'",
",",
"True",
")",
":",
"created",
",",
"response_table",
"=",
"self",
".",
"zappa",
".",
"create_async_dynamodb_table",
"(",
"table_name",
",",
"read_capacity",
",",
"write_capacity",
")",
"if",
"created",
":",
"click",
".",
"echo",
"(",
"'DynamoDB table created: %s'",
"%",
"table_name",
")",
"else",
":",
"click",
".",
"echo",
"(",
"'DynamoDB table exists: %s'",
"%",
"table_name",
")",
"provisioned_throughput",
"=",
"response_table",
"[",
"'Table'",
"]",
"[",
"'ProvisionedThroughput'",
"]",
"if",
"provisioned_throughput",
"[",
"'ReadCapacityUnits'",
"]",
"!=",
"read_capacity",
"or",
"provisioned_throughput",
"[",
"'WriteCapacityUnits'",
"]",
"!=",
"write_capacity",
":",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"\\nWarning! Existing DynamoDB table ({}) does not match configured capacity.\\n\"",
".",
"format",
"(",
"table_name",
")",
",",
"fg",
"=",
"'red'",
")",
")"
] | Given a a list of functions and a schedule to execute them,
setup up regular execution. | [
"Given",
"a",
"a",
"list",
"of",
"functions",
"and",
"a",
"schedule",
"to",
"execute",
"them",
"setup",
"up",
"regular",
"execution",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L1153-L1223 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.unschedule | def unschedule(self):
"""
Given a a list of scheduled functions,
tear down their regular execution.
"""
# Run even if events are not defined to remove previously existing ones (thus default to []).
events = self.stage_config.get('events', [])
if not isinstance(events, list): # pragma: no cover
print("Events must be supplied as a list.")
return
function_arn = None
try:
function_response = self.zappa.lambda_client.get_function(FunctionName=self.lambda_name)
function_arn = function_response['Configuration']['FunctionArn']
except botocore.exceptions.ClientError as e: # pragma: no cover
raise ClickException("Function does not exist, you should deploy first. Ex: zappa deploy {}. "
"Proceeding to unschedule CloudWatch based events.".format(self.api_stage))
print("Unscheduling..")
self.zappa.unschedule_events(
lambda_name=self.lambda_name,
lambda_arn=function_arn,
events=events,
)
# Remove async task SNS
if self.stage_config.get('async_source', None) == 'sns' \
and self.stage_config.get('async_resources', True):
removed_arns = self.zappa.remove_async_sns_topic(self.lambda_name)
click.echo('SNS Topic removed: %s' % ', '.join(removed_arns)) | python | def unschedule(self):
"""
Given a a list of scheduled functions,
tear down their regular execution.
"""
# Run even if events are not defined to remove previously existing ones (thus default to []).
events = self.stage_config.get('events', [])
if not isinstance(events, list): # pragma: no cover
print("Events must be supplied as a list.")
return
function_arn = None
try:
function_response = self.zappa.lambda_client.get_function(FunctionName=self.lambda_name)
function_arn = function_response['Configuration']['FunctionArn']
except botocore.exceptions.ClientError as e: # pragma: no cover
raise ClickException("Function does not exist, you should deploy first. Ex: zappa deploy {}. "
"Proceeding to unschedule CloudWatch based events.".format(self.api_stage))
print("Unscheduling..")
self.zappa.unschedule_events(
lambda_name=self.lambda_name,
lambda_arn=function_arn,
events=events,
)
# Remove async task SNS
if self.stage_config.get('async_source', None) == 'sns' \
and self.stage_config.get('async_resources', True):
removed_arns = self.zappa.remove_async_sns_topic(self.lambda_name)
click.echo('SNS Topic removed: %s' % ', '.join(removed_arns)) | [
"def",
"unschedule",
"(",
"self",
")",
":",
"# Run even if events are not defined to remove previously existing ones (thus default to []).",
"events",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'events'",
",",
"[",
"]",
")",
"if",
"not",
"isinstance",
"(",
"events",
",",
"list",
")",
":",
"# pragma: no cover",
"print",
"(",
"\"Events must be supplied as a list.\"",
")",
"return",
"function_arn",
"=",
"None",
"try",
":",
"function_response",
"=",
"self",
".",
"zappa",
".",
"lambda_client",
".",
"get_function",
"(",
"FunctionName",
"=",
"self",
".",
"lambda_name",
")",
"function_arn",
"=",
"function_response",
"[",
"'Configuration'",
"]",
"[",
"'FunctionArn'",
"]",
"except",
"botocore",
".",
"exceptions",
".",
"ClientError",
"as",
"e",
":",
"# pragma: no cover",
"raise",
"ClickException",
"(",
"\"Function does not exist, you should deploy first. Ex: zappa deploy {}. \"",
"\"Proceeding to unschedule CloudWatch based events.\"",
".",
"format",
"(",
"self",
".",
"api_stage",
")",
")",
"print",
"(",
"\"Unscheduling..\"",
")",
"self",
".",
"zappa",
".",
"unschedule_events",
"(",
"lambda_name",
"=",
"self",
".",
"lambda_name",
",",
"lambda_arn",
"=",
"function_arn",
",",
"events",
"=",
"events",
",",
")",
"# Remove async task SNS",
"if",
"self",
".",
"stage_config",
".",
"get",
"(",
"'async_source'",
",",
"None",
")",
"==",
"'sns'",
"and",
"self",
".",
"stage_config",
".",
"get",
"(",
"'async_resources'",
",",
"True",
")",
":",
"removed_arns",
"=",
"self",
".",
"zappa",
".",
"remove_async_sns_topic",
"(",
"self",
".",
"lambda_name",
")",
"click",
".",
"echo",
"(",
"'SNS Topic removed: %s'",
"%",
"', '",
".",
"join",
"(",
"removed_arns",
")",
")"
] | Given a a list of scheduled functions,
tear down their regular execution. | [
"Given",
"a",
"a",
"list",
"of",
"scheduled",
"functions",
"tear",
"down",
"their",
"regular",
"execution",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L1225-L1258 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.invoke | def invoke(self, function_name, raw_python=False, command=None, no_color=False):
"""
Invoke a remote function.
"""
# There are three likely scenarios for 'command' here:
# command, which is a modular function path
# raw_command, which is a string of python to execute directly
# manage, which is a Django-specific management command invocation
key = command if command is not None else 'command'
if raw_python:
command = {'raw_command': function_name}
else:
command = {key: function_name}
# Can't use hjson
import json as json
response = self.zappa.invoke_lambda_function(
self.lambda_name,
json.dumps(command),
invocation_type='RequestResponse',
)
if 'LogResult' in response:
if no_color:
print(base64.b64decode(response['LogResult']))
else:
decoded = base64.b64decode(response['LogResult']).decode()
formatted = self.format_invoke_command(decoded)
colorized = self.colorize_invoke_command(formatted)
print(colorized)
else:
print(response)
# For a successful request FunctionError is not in response.
# https://github.com/Miserlou/Zappa/pull/1254/
if 'FunctionError' in response:
raise ClickException(
"{} error occurred while invoking command.".format(response['FunctionError'])
) | python | def invoke(self, function_name, raw_python=False, command=None, no_color=False):
"""
Invoke a remote function.
"""
# There are three likely scenarios for 'command' here:
# command, which is a modular function path
# raw_command, which is a string of python to execute directly
# manage, which is a Django-specific management command invocation
key = command if command is not None else 'command'
if raw_python:
command = {'raw_command': function_name}
else:
command = {key: function_name}
# Can't use hjson
import json as json
response = self.zappa.invoke_lambda_function(
self.lambda_name,
json.dumps(command),
invocation_type='RequestResponse',
)
if 'LogResult' in response:
if no_color:
print(base64.b64decode(response['LogResult']))
else:
decoded = base64.b64decode(response['LogResult']).decode()
formatted = self.format_invoke_command(decoded)
colorized = self.colorize_invoke_command(formatted)
print(colorized)
else:
print(response)
# For a successful request FunctionError is not in response.
# https://github.com/Miserlou/Zappa/pull/1254/
if 'FunctionError' in response:
raise ClickException(
"{} error occurred while invoking command.".format(response['FunctionError'])
) | [
"def",
"invoke",
"(",
"self",
",",
"function_name",
",",
"raw_python",
"=",
"False",
",",
"command",
"=",
"None",
",",
"no_color",
"=",
"False",
")",
":",
"# There are three likely scenarios for 'command' here:",
"# command, which is a modular function path",
"# raw_command, which is a string of python to execute directly",
"# manage, which is a Django-specific management command invocation",
"key",
"=",
"command",
"if",
"command",
"is",
"not",
"None",
"else",
"'command'",
"if",
"raw_python",
":",
"command",
"=",
"{",
"'raw_command'",
":",
"function_name",
"}",
"else",
":",
"command",
"=",
"{",
"key",
":",
"function_name",
"}",
"# Can't use hjson",
"import",
"json",
"as",
"json",
"response",
"=",
"self",
".",
"zappa",
".",
"invoke_lambda_function",
"(",
"self",
".",
"lambda_name",
",",
"json",
".",
"dumps",
"(",
"command",
")",
",",
"invocation_type",
"=",
"'RequestResponse'",
",",
")",
"if",
"'LogResult'",
"in",
"response",
":",
"if",
"no_color",
":",
"print",
"(",
"base64",
".",
"b64decode",
"(",
"response",
"[",
"'LogResult'",
"]",
")",
")",
"else",
":",
"decoded",
"=",
"base64",
".",
"b64decode",
"(",
"response",
"[",
"'LogResult'",
"]",
")",
".",
"decode",
"(",
")",
"formatted",
"=",
"self",
".",
"format_invoke_command",
"(",
"decoded",
")",
"colorized",
"=",
"self",
".",
"colorize_invoke_command",
"(",
"formatted",
")",
"print",
"(",
"colorized",
")",
"else",
":",
"print",
"(",
"response",
")",
"# For a successful request FunctionError is not in response.",
"# https://github.com/Miserlou/Zappa/pull/1254/",
"if",
"'FunctionError'",
"in",
"response",
":",
"raise",
"ClickException",
"(",
"\"{} error occurred while invoking command.\"",
".",
"format",
"(",
"response",
"[",
"'FunctionError'",
"]",
")",
")"
] | Invoke a remote function. | [
"Invoke",
"a",
"remote",
"function",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L1260-L1300 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.format_invoke_command | def format_invoke_command(self, string):
"""
Formats correctly the string output from the invoke() method,
replacing line breaks and tabs when necessary.
"""
string = string.replace('\\n', '\n')
formated_response = ''
for line in string.splitlines():
if line.startswith('REPORT'):
line = line.replace('\t', '\n')
if line.startswith('[DEBUG]'):
line = line.replace('\t', ' ')
formated_response += line + '\n'
formated_response = formated_response.replace('\n\n', '\n')
return formated_response | python | def format_invoke_command(self, string):
"""
Formats correctly the string output from the invoke() method,
replacing line breaks and tabs when necessary.
"""
string = string.replace('\\n', '\n')
formated_response = ''
for line in string.splitlines():
if line.startswith('REPORT'):
line = line.replace('\t', '\n')
if line.startswith('[DEBUG]'):
line = line.replace('\t', ' ')
formated_response += line + '\n'
formated_response = formated_response.replace('\n\n', '\n')
return formated_response | [
"def",
"format_invoke_command",
"(",
"self",
",",
"string",
")",
":",
"string",
"=",
"string",
".",
"replace",
"(",
"'\\\\n'",
",",
"'\\n'",
")",
"formated_response",
"=",
"''",
"for",
"line",
"in",
"string",
".",
"splitlines",
"(",
")",
":",
"if",
"line",
".",
"startswith",
"(",
"'REPORT'",
")",
":",
"line",
"=",
"line",
".",
"replace",
"(",
"'\\t'",
",",
"'\\n'",
")",
"if",
"line",
".",
"startswith",
"(",
"'[DEBUG]'",
")",
":",
"line",
"=",
"line",
".",
"replace",
"(",
"'\\t'",
",",
"' '",
")",
"formated_response",
"+=",
"line",
"+",
"'\\n'",
"formated_response",
"=",
"formated_response",
".",
"replace",
"(",
"'\\n\\n'",
",",
"'\\n'",
")",
"return",
"formated_response"
] | Formats correctly the string output from the invoke() method,
replacing line breaks and tabs when necessary. | [
"Formats",
"correctly",
"the",
"string",
"output",
"from",
"the",
"invoke",
"()",
"method",
"replacing",
"line",
"breaks",
"and",
"tabs",
"when",
"necessary",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L1302-L1319 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.colorize_invoke_command | def colorize_invoke_command(self, string):
"""
Apply various heuristics to return a colorized version the invoke
command string. If these fail, simply return the string in plaintext.
Inspired by colorize_log_entry().
"""
final_string = string
try:
# Line headers
try:
for token in ['START', 'END', 'REPORT', '[DEBUG]']:
if token in final_string:
format_string = '[{}]'
# match whole words only
pattern = r'\b{}\b'
if token == '[DEBUG]':
format_string = '{}'
pattern = re.escape(token)
repl = click.style(
format_string.format(token),
bold=True,
fg='cyan'
)
final_string = re.sub(
pattern.format(token), repl, final_string
)
except Exception: # pragma: no cover
pass
# Green bold Tokens
try:
for token in [
'Zappa Event:',
'RequestId:',
'Version:',
'Duration:',
'Billed',
'Memory Size:',
'Max Memory Used:'
]:
if token in final_string:
final_string = final_string.replace(token, click.style(
token,
bold=True,
fg='green'
))
except Exception: # pragma: no cover
pass
# UUIDs
for token in final_string.replace('\t', ' ').split(' '):
try:
if token.count('-') is 4 and token.replace('-', '').isalnum():
final_string = final_string.replace(
token,
click.style(token, fg='magenta')
)
except Exception: # pragma: no cover
pass
return final_string
except Exception:
return string | python | def colorize_invoke_command(self, string):
"""
Apply various heuristics to return a colorized version the invoke
command string. If these fail, simply return the string in plaintext.
Inspired by colorize_log_entry().
"""
final_string = string
try:
# Line headers
try:
for token in ['START', 'END', 'REPORT', '[DEBUG]']:
if token in final_string:
format_string = '[{}]'
# match whole words only
pattern = r'\b{}\b'
if token == '[DEBUG]':
format_string = '{}'
pattern = re.escape(token)
repl = click.style(
format_string.format(token),
bold=True,
fg='cyan'
)
final_string = re.sub(
pattern.format(token), repl, final_string
)
except Exception: # pragma: no cover
pass
# Green bold Tokens
try:
for token in [
'Zappa Event:',
'RequestId:',
'Version:',
'Duration:',
'Billed',
'Memory Size:',
'Max Memory Used:'
]:
if token in final_string:
final_string = final_string.replace(token, click.style(
token,
bold=True,
fg='green'
))
except Exception: # pragma: no cover
pass
# UUIDs
for token in final_string.replace('\t', ' ').split(' '):
try:
if token.count('-') is 4 and token.replace('-', '').isalnum():
final_string = final_string.replace(
token,
click.style(token, fg='magenta')
)
except Exception: # pragma: no cover
pass
return final_string
except Exception:
return string | [
"def",
"colorize_invoke_command",
"(",
"self",
",",
"string",
")",
":",
"final_string",
"=",
"string",
"try",
":",
"# Line headers",
"try",
":",
"for",
"token",
"in",
"[",
"'START'",
",",
"'END'",
",",
"'REPORT'",
",",
"'[DEBUG]'",
"]",
":",
"if",
"token",
"in",
"final_string",
":",
"format_string",
"=",
"'[{}]'",
"# match whole words only",
"pattern",
"=",
"r'\\b{}\\b'",
"if",
"token",
"==",
"'[DEBUG]'",
":",
"format_string",
"=",
"'{}'",
"pattern",
"=",
"re",
".",
"escape",
"(",
"token",
")",
"repl",
"=",
"click",
".",
"style",
"(",
"format_string",
".",
"format",
"(",
"token",
")",
",",
"bold",
"=",
"True",
",",
"fg",
"=",
"'cyan'",
")",
"final_string",
"=",
"re",
".",
"sub",
"(",
"pattern",
".",
"format",
"(",
"token",
")",
",",
"repl",
",",
"final_string",
")",
"except",
"Exception",
":",
"# pragma: no cover",
"pass",
"# Green bold Tokens",
"try",
":",
"for",
"token",
"in",
"[",
"'Zappa Event:'",
",",
"'RequestId:'",
",",
"'Version:'",
",",
"'Duration:'",
",",
"'Billed'",
",",
"'Memory Size:'",
",",
"'Max Memory Used:'",
"]",
":",
"if",
"token",
"in",
"final_string",
":",
"final_string",
"=",
"final_string",
".",
"replace",
"(",
"token",
",",
"click",
".",
"style",
"(",
"token",
",",
"bold",
"=",
"True",
",",
"fg",
"=",
"'green'",
")",
")",
"except",
"Exception",
":",
"# pragma: no cover",
"pass",
"# UUIDs",
"for",
"token",
"in",
"final_string",
".",
"replace",
"(",
"'\\t'",
",",
"' '",
")",
".",
"split",
"(",
"' '",
")",
":",
"try",
":",
"if",
"token",
".",
"count",
"(",
"'-'",
")",
"is",
"4",
"and",
"token",
".",
"replace",
"(",
"'-'",
",",
"''",
")",
".",
"isalnum",
"(",
")",
":",
"final_string",
"=",
"final_string",
".",
"replace",
"(",
"token",
",",
"click",
".",
"style",
"(",
"token",
",",
"fg",
"=",
"'magenta'",
")",
")",
"except",
"Exception",
":",
"# pragma: no cover",
"pass",
"return",
"final_string",
"except",
"Exception",
":",
"return",
"string"
] | Apply various heuristics to return a colorized version the invoke
command string. If these fail, simply return the string in plaintext.
Inspired by colorize_log_entry(). | [
"Apply",
"various",
"heuristics",
"to",
"return",
"a",
"colorized",
"version",
"the",
"invoke",
"command",
"string",
".",
"If",
"these",
"fail",
"simply",
"return",
"the",
"string",
"in",
"plaintext",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L1321-L1387 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.status | def status(self, return_json=False):
"""
Describe the status of the current deployment.
"""
def tabular_print(title, value):
"""
Convenience function for priting formatted table items.
"""
click.echo('%-*s%s' % (32, click.style("\t" + title, fg='green') + ':', str(value)))
return
# Lambda Env Details
lambda_versions = self.zappa.get_lambda_function_versions(self.lambda_name)
if not lambda_versions:
raise ClickException(click.style("No Lambda %s detected in %s - have you deployed yet?" %
(self.lambda_name, self.zappa.aws_region), fg='red'))
status_dict = collections.OrderedDict()
status_dict["Lambda Versions"] = len(lambda_versions)
function_response = self.zappa.lambda_client.get_function(FunctionName=self.lambda_name)
conf = function_response['Configuration']
self.lambda_arn = conf['FunctionArn']
status_dict["Lambda Name"] = self.lambda_name
status_dict["Lambda ARN"] = self.lambda_arn
status_dict["Lambda Role ARN"] = conf['Role']
status_dict["Lambda Handler"] = conf['Handler']
status_dict["Lambda Code Size"] = conf['CodeSize']
status_dict["Lambda Version"] = conf['Version']
status_dict["Lambda Last Modified"] = conf['LastModified']
status_dict["Lambda Memory Size"] = conf['MemorySize']
status_dict["Lambda Timeout"] = conf['Timeout']
status_dict["Lambda Runtime"] = conf['Runtime']
if 'VpcConfig' in conf.keys():
status_dict["Lambda VPC ID"] = conf.get('VpcConfig', {}).get('VpcId', 'Not assigned')
else:
status_dict["Lambda VPC ID"] = None
# Calculated statistics
try:
function_invocations = self.zappa.cloudwatch.get_metric_statistics(
Namespace='AWS/Lambda',
MetricName='Invocations',
StartTime=datetime.utcnow()-timedelta(days=1),
EndTime=datetime.utcnow(),
Period=1440,
Statistics=['Sum'],
Dimensions=[{'Name': 'FunctionName',
'Value': '{}'.format(self.lambda_name)}]
)['Datapoints'][0]['Sum']
except Exception as e:
function_invocations = 0
try:
function_errors = self.zappa.cloudwatch.get_metric_statistics(
Namespace='AWS/Lambda',
MetricName='Errors',
StartTime=datetime.utcnow()-timedelta(days=1),
EndTime=datetime.utcnow(),
Period=1440,
Statistics=['Sum'],
Dimensions=[{'Name': 'FunctionName',
'Value': '{}'.format(self.lambda_name)}]
)['Datapoints'][0]['Sum']
except Exception as e:
function_errors = 0
try:
error_rate = "{0:.2f}%".format(function_errors / function_invocations * 100)
except:
error_rate = "Error calculating"
status_dict["Invocations (24h)"] = int(function_invocations)
status_dict["Errors (24h)"] = int(function_errors)
status_dict["Error Rate (24h)"] = error_rate
# URLs
if self.use_apigateway:
api_url = self.zappa.get_api_url(
self.lambda_name,
self.api_stage)
status_dict["API Gateway URL"] = api_url
# Api Keys
api_id = self.zappa.get_api_id(self.lambda_name)
for api_key in self.zappa.get_api_keys(api_id, self.api_stage):
status_dict["API Gateway x-api-key"] = api_key
# There literally isn't a better way to do this.
# AWS provides no way to tie a APIGW domain name to its Lambda function.
domain_url = self.stage_config.get('domain', None)
base_path = self.stage_config.get('base_path', None)
if domain_url:
status_dict["Domain URL"] = 'https://' + domain_url
if base_path:
status_dict["Domain URL"] += '/' + base_path
else:
status_dict["Domain URL"] = "None Supplied"
# Scheduled Events
event_rules = self.zappa.get_event_rules_for_lambda(lambda_arn=self.lambda_arn)
status_dict["Num. Event Rules"] = len(event_rules)
if len(event_rules) > 0:
status_dict['Events'] = []
for rule in event_rules:
event_dict = {}
rule_name = rule['Name']
event_dict["Event Rule Name"] = rule_name
event_dict["Event Rule Schedule"] = rule.get(u'ScheduleExpression', None)
event_dict["Event Rule State"] = rule.get(u'State', None).title()
event_dict["Event Rule ARN"] = rule.get(u'Arn', None)
status_dict['Events'].append(event_dict)
if return_json:
# Putting the status in machine readable format
# https://github.com/Miserlou/Zappa/issues/407
print(json.dumpsJSON(status_dict))
else:
click.echo("Status for " + click.style(self.lambda_name, bold=True) + ": ")
for k, v in status_dict.items():
if k == 'Events':
# Events are a list of dicts
for event in v:
for item_k, item_v in event.items():
tabular_print(item_k, item_v)
else:
tabular_print(k, v)
# TODO: S3/SQS/etc. type events?
return True | python | def status(self, return_json=False):
"""
Describe the status of the current deployment.
"""
def tabular_print(title, value):
"""
Convenience function for priting formatted table items.
"""
click.echo('%-*s%s' % (32, click.style("\t" + title, fg='green') + ':', str(value)))
return
# Lambda Env Details
lambda_versions = self.zappa.get_lambda_function_versions(self.lambda_name)
if not lambda_versions:
raise ClickException(click.style("No Lambda %s detected in %s - have you deployed yet?" %
(self.lambda_name, self.zappa.aws_region), fg='red'))
status_dict = collections.OrderedDict()
status_dict["Lambda Versions"] = len(lambda_versions)
function_response = self.zappa.lambda_client.get_function(FunctionName=self.lambda_name)
conf = function_response['Configuration']
self.lambda_arn = conf['FunctionArn']
status_dict["Lambda Name"] = self.lambda_name
status_dict["Lambda ARN"] = self.lambda_arn
status_dict["Lambda Role ARN"] = conf['Role']
status_dict["Lambda Handler"] = conf['Handler']
status_dict["Lambda Code Size"] = conf['CodeSize']
status_dict["Lambda Version"] = conf['Version']
status_dict["Lambda Last Modified"] = conf['LastModified']
status_dict["Lambda Memory Size"] = conf['MemorySize']
status_dict["Lambda Timeout"] = conf['Timeout']
status_dict["Lambda Runtime"] = conf['Runtime']
if 'VpcConfig' in conf.keys():
status_dict["Lambda VPC ID"] = conf.get('VpcConfig', {}).get('VpcId', 'Not assigned')
else:
status_dict["Lambda VPC ID"] = None
# Calculated statistics
try:
function_invocations = self.zappa.cloudwatch.get_metric_statistics(
Namespace='AWS/Lambda',
MetricName='Invocations',
StartTime=datetime.utcnow()-timedelta(days=1),
EndTime=datetime.utcnow(),
Period=1440,
Statistics=['Sum'],
Dimensions=[{'Name': 'FunctionName',
'Value': '{}'.format(self.lambda_name)}]
)['Datapoints'][0]['Sum']
except Exception as e:
function_invocations = 0
try:
function_errors = self.zappa.cloudwatch.get_metric_statistics(
Namespace='AWS/Lambda',
MetricName='Errors',
StartTime=datetime.utcnow()-timedelta(days=1),
EndTime=datetime.utcnow(),
Period=1440,
Statistics=['Sum'],
Dimensions=[{'Name': 'FunctionName',
'Value': '{}'.format(self.lambda_name)}]
)['Datapoints'][0]['Sum']
except Exception as e:
function_errors = 0
try:
error_rate = "{0:.2f}%".format(function_errors / function_invocations * 100)
except:
error_rate = "Error calculating"
status_dict["Invocations (24h)"] = int(function_invocations)
status_dict["Errors (24h)"] = int(function_errors)
status_dict["Error Rate (24h)"] = error_rate
# URLs
if self.use_apigateway:
api_url = self.zappa.get_api_url(
self.lambda_name,
self.api_stage)
status_dict["API Gateway URL"] = api_url
# Api Keys
api_id = self.zappa.get_api_id(self.lambda_name)
for api_key in self.zappa.get_api_keys(api_id, self.api_stage):
status_dict["API Gateway x-api-key"] = api_key
# There literally isn't a better way to do this.
# AWS provides no way to tie a APIGW domain name to its Lambda function.
domain_url = self.stage_config.get('domain', None)
base_path = self.stage_config.get('base_path', None)
if domain_url:
status_dict["Domain URL"] = 'https://' + domain_url
if base_path:
status_dict["Domain URL"] += '/' + base_path
else:
status_dict["Domain URL"] = "None Supplied"
# Scheduled Events
event_rules = self.zappa.get_event_rules_for_lambda(lambda_arn=self.lambda_arn)
status_dict["Num. Event Rules"] = len(event_rules)
if len(event_rules) > 0:
status_dict['Events'] = []
for rule in event_rules:
event_dict = {}
rule_name = rule['Name']
event_dict["Event Rule Name"] = rule_name
event_dict["Event Rule Schedule"] = rule.get(u'ScheduleExpression', None)
event_dict["Event Rule State"] = rule.get(u'State', None).title()
event_dict["Event Rule ARN"] = rule.get(u'Arn', None)
status_dict['Events'].append(event_dict)
if return_json:
# Putting the status in machine readable format
# https://github.com/Miserlou/Zappa/issues/407
print(json.dumpsJSON(status_dict))
else:
click.echo("Status for " + click.style(self.lambda_name, bold=True) + ": ")
for k, v in status_dict.items():
if k == 'Events':
# Events are a list of dicts
for event in v:
for item_k, item_v in event.items():
tabular_print(item_k, item_v)
else:
tabular_print(k, v)
# TODO: S3/SQS/etc. type events?
return True | [
"def",
"status",
"(",
"self",
",",
"return_json",
"=",
"False",
")",
":",
"def",
"tabular_print",
"(",
"title",
",",
"value",
")",
":",
"\"\"\"\n Convenience function for priting formatted table items.\n \"\"\"",
"click",
".",
"echo",
"(",
"'%-*s%s'",
"%",
"(",
"32",
",",
"click",
".",
"style",
"(",
"\"\\t\"",
"+",
"title",
",",
"fg",
"=",
"'green'",
")",
"+",
"':'",
",",
"str",
"(",
"value",
")",
")",
")",
"return",
"# Lambda Env Details",
"lambda_versions",
"=",
"self",
".",
"zappa",
".",
"get_lambda_function_versions",
"(",
"self",
".",
"lambda_name",
")",
"if",
"not",
"lambda_versions",
":",
"raise",
"ClickException",
"(",
"click",
".",
"style",
"(",
"\"No Lambda %s detected in %s - have you deployed yet?\"",
"%",
"(",
"self",
".",
"lambda_name",
",",
"self",
".",
"zappa",
".",
"aws_region",
")",
",",
"fg",
"=",
"'red'",
")",
")",
"status_dict",
"=",
"collections",
".",
"OrderedDict",
"(",
")",
"status_dict",
"[",
"\"Lambda Versions\"",
"]",
"=",
"len",
"(",
"lambda_versions",
")",
"function_response",
"=",
"self",
".",
"zappa",
".",
"lambda_client",
".",
"get_function",
"(",
"FunctionName",
"=",
"self",
".",
"lambda_name",
")",
"conf",
"=",
"function_response",
"[",
"'Configuration'",
"]",
"self",
".",
"lambda_arn",
"=",
"conf",
"[",
"'FunctionArn'",
"]",
"status_dict",
"[",
"\"Lambda Name\"",
"]",
"=",
"self",
".",
"lambda_name",
"status_dict",
"[",
"\"Lambda ARN\"",
"]",
"=",
"self",
".",
"lambda_arn",
"status_dict",
"[",
"\"Lambda Role ARN\"",
"]",
"=",
"conf",
"[",
"'Role'",
"]",
"status_dict",
"[",
"\"Lambda Handler\"",
"]",
"=",
"conf",
"[",
"'Handler'",
"]",
"status_dict",
"[",
"\"Lambda Code Size\"",
"]",
"=",
"conf",
"[",
"'CodeSize'",
"]",
"status_dict",
"[",
"\"Lambda Version\"",
"]",
"=",
"conf",
"[",
"'Version'",
"]",
"status_dict",
"[",
"\"Lambda Last Modified\"",
"]",
"=",
"conf",
"[",
"'LastModified'",
"]",
"status_dict",
"[",
"\"Lambda Memory Size\"",
"]",
"=",
"conf",
"[",
"'MemorySize'",
"]",
"status_dict",
"[",
"\"Lambda Timeout\"",
"]",
"=",
"conf",
"[",
"'Timeout'",
"]",
"status_dict",
"[",
"\"Lambda Runtime\"",
"]",
"=",
"conf",
"[",
"'Runtime'",
"]",
"if",
"'VpcConfig'",
"in",
"conf",
".",
"keys",
"(",
")",
":",
"status_dict",
"[",
"\"Lambda VPC ID\"",
"]",
"=",
"conf",
".",
"get",
"(",
"'VpcConfig'",
",",
"{",
"}",
")",
".",
"get",
"(",
"'VpcId'",
",",
"'Not assigned'",
")",
"else",
":",
"status_dict",
"[",
"\"Lambda VPC ID\"",
"]",
"=",
"None",
"# Calculated statistics",
"try",
":",
"function_invocations",
"=",
"self",
".",
"zappa",
".",
"cloudwatch",
".",
"get_metric_statistics",
"(",
"Namespace",
"=",
"'AWS/Lambda'",
",",
"MetricName",
"=",
"'Invocations'",
",",
"StartTime",
"=",
"datetime",
".",
"utcnow",
"(",
")",
"-",
"timedelta",
"(",
"days",
"=",
"1",
")",
",",
"EndTime",
"=",
"datetime",
".",
"utcnow",
"(",
")",
",",
"Period",
"=",
"1440",
",",
"Statistics",
"=",
"[",
"'Sum'",
"]",
",",
"Dimensions",
"=",
"[",
"{",
"'Name'",
":",
"'FunctionName'",
",",
"'Value'",
":",
"'{}'",
".",
"format",
"(",
"self",
".",
"lambda_name",
")",
"}",
"]",
")",
"[",
"'Datapoints'",
"]",
"[",
"0",
"]",
"[",
"'Sum'",
"]",
"except",
"Exception",
"as",
"e",
":",
"function_invocations",
"=",
"0",
"try",
":",
"function_errors",
"=",
"self",
".",
"zappa",
".",
"cloudwatch",
".",
"get_metric_statistics",
"(",
"Namespace",
"=",
"'AWS/Lambda'",
",",
"MetricName",
"=",
"'Errors'",
",",
"StartTime",
"=",
"datetime",
".",
"utcnow",
"(",
")",
"-",
"timedelta",
"(",
"days",
"=",
"1",
")",
",",
"EndTime",
"=",
"datetime",
".",
"utcnow",
"(",
")",
",",
"Period",
"=",
"1440",
",",
"Statistics",
"=",
"[",
"'Sum'",
"]",
",",
"Dimensions",
"=",
"[",
"{",
"'Name'",
":",
"'FunctionName'",
",",
"'Value'",
":",
"'{}'",
".",
"format",
"(",
"self",
".",
"lambda_name",
")",
"}",
"]",
")",
"[",
"'Datapoints'",
"]",
"[",
"0",
"]",
"[",
"'Sum'",
"]",
"except",
"Exception",
"as",
"e",
":",
"function_errors",
"=",
"0",
"try",
":",
"error_rate",
"=",
"\"{0:.2f}%\"",
".",
"format",
"(",
"function_errors",
"/",
"function_invocations",
"*",
"100",
")",
"except",
":",
"error_rate",
"=",
"\"Error calculating\"",
"status_dict",
"[",
"\"Invocations (24h)\"",
"]",
"=",
"int",
"(",
"function_invocations",
")",
"status_dict",
"[",
"\"Errors (24h)\"",
"]",
"=",
"int",
"(",
"function_errors",
")",
"status_dict",
"[",
"\"Error Rate (24h)\"",
"]",
"=",
"error_rate",
"# URLs",
"if",
"self",
".",
"use_apigateway",
":",
"api_url",
"=",
"self",
".",
"zappa",
".",
"get_api_url",
"(",
"self",
".",
"lambda_name",
",",
"self",
".",
"api_stage",
")",
"status_dict",
"[",
"\"API Gateway URL\"",
"]",
"=",
"api_url",
"# Api Keys",
"api_id",
"=",
"self",
".",
"zappa",
".",
"get_api_id",
"(",
"self",
".",
"lambda_name",
")",
"for",
"api_key",
"in",
"self",
".",
"zappa",
".",
"get_api_keys",
"(",
"api_id",
",",
"self",
".",
"api_stage",
")",
":",
"status_dict",
"[",
"\"API Gateway x-api-key\"",
"]",
"=",
"api_key",
"# There literally isn't a better way to do this.",
"# AWS provides no way to tie a APIGW domain name to its Lambda function.",
"domain_url",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'domain'",
",",
"None",
")",
"base_path",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'base_path'",
",",
"None",
")",
"if",
"domain_url",
":",
"status_dict",
"[",
"\"Domain URL\"",
"]",
"=",
"'https://'",
"+",
"domain_url",
"if",
"base_path",
":",
"status_dict",
"[",
"\"Domain URL\"",
"]",
"+=",
"'/'",
"+",
"base_path",
"else",
":",
"status_dict",
"[",
"\"Domain URL\"",
"]",
"=",
"\"None Supplied\"",
"# Scheduled Events",
"event_rules",
"=",
"self",
".",
"zappa",
".",
"get_event_rules_for_lambda",
"(",
"lambda_arn",
"=",
"self",
".",
"lambda_arn",
")",
"status_dict",
"[",
"\"Num. Event Rules\"",
"]",
"=",
"len",
"(",
"event_rules",
")",
"if",
"len",
"(",
"event_rules",
")",
">",
"0",
":",
"status_dict",
"[",
"'Events'",
"]",
"=",
"[",
"]",
"for",
"rule",
"in",
"event_rules",
":",
"event_dict",
"=",
"{",
"}",
"rule_name",
"=",
"rule",
"[",
"'Name'",
"]",
"event_dict",
"[",
"\"Event Rule Name\"",
"]",
"=",
"rule_name",
"event_dict",
"[",
"\"Event Rule Schedule\"",
"]",
"=",
"rule",
".",
"get",
"(",
"u'ScheduleExpression'",
",",
"None",
")",
"event_dict",
"[",
"\"Event Rule State\"",
"]",
"=",
"rule",
".",
"get",
"(",
"u'State'",
",",
"None",
")",
".",
"title",
"(",
")",
"event_dict",
"[",
"\"Event Rule ARN\"",
"]",
"=",
"rule",
".",
"get",
"(",
"u'Arn'",
",",
"None",
")",
"status_dict",
"[",
"'Events'",
"]",
".",
"append",
"(",
"event_dict",
")",
"if",
"return_json",
":",
"# Putting the status in machine readable format",
"# https://github.com/Miserlou/Zappa/issues/407",
"print",
"(",
"json",
".",
"dumpsJSON",
"(",
"status_dict",
")",
")",
"else",
":",
"click",
".",
"echo",
"(",
"\"Status for \"",
"+",
"click",
".",
"style",
"(",
"self",
".",
"lambda_name",
",",
"bold",
"=",
"True",
")",
"+",
"\": \"",
")",
"for",
"k",
",",
"v",
"in",
"status_dict",
".",
"items",
"(",
")",
":",
"if",
"k",
"==",
"'Events'",
":",
"# Events are a list of dicts",
"for",
"event",
"in",
"v",
":",
"for",
"item_k",
",",
"item_v",
"in",
"event",
".",
"items",
"(",
")",
":",
"tabular_print",
"(",
"item_k",
",",
"item_v",
")",
"else",
":",
"tabular_print",
"(",
"k",
",",
"v",
")",
"# TODO: S3/SQS/etc. type events?",
"return",
"True"
] | Describe the status of the current deployment. | [
"Describe",
"the",
"status",
"of",
"the",
"current",
"deployment",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L1389-L1519 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.check_environment | def check_environment(self, environment):
"""
Make sure the environment contains only strings
(since putenv needs a string)
"""
non_strings = []
for (k,v) in environment.items():
if not isinstance(v, basestring):
non_strings.append(k)
if non_strings:
raise ValueError("The following environment variables are not strings: {}".format(", ".join(non_strings)))
else:
return True | python | def check_environment(self, environment):
"""
Make sure the environment contains only strings
(since putenv needs a string)
"""
non_strings = []
for (k,v) in environment.items():
if not isinstance(v, basestring):
non_strings.append(k)
if non_strings:
raise ValueError("The following environment variables are not strings: {}".format(", ".join(non_strings)))
else:
return True | [
"def",
"check_environment",
"(",
"self",
",",
"environment",
")",
":",
"non_strings",
"=",
"[",
"]",
"for",
"(",
"k",
",",
"v",
")",
"in",
"environment",
".",
"items",
"(",
")",
":",
"if",
"not",
"isinstance",
"(",
"v",
",",
"basestring",
")",
":",
"non_strings",
".",
"append",
"(",
"k",
")",
"if",
"non_strings",
":",
"raise",
"ValueError",
"(",
"\"The following environment variables are not strings: {}\"",
".",
"format",
"(",
"\", \"",
".",
"join",
"(",
"non_strings",
")",
")",
")",
"else",
":",
"return",
"True"
] | Make sure the environment contains only strings
(since putenv needs a string) | [
"Make",
"sure",
"the",
"environment",
"contains",
"only",
"strings"
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L1534-L1548 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.init | def init(self, settings_file="zappa_settings.json"):
"""
Initialize a new Zappa project by creating a new zappa_settings.json in a guided process.
This should probably be broken up into few separate componants once it's stable.
Testing these inputs requires monkeypatching with mock, which isn't pretty.
"""
# Make sure we're in a venv.
self.check_venv()
# Ensure that we don't already have a zappa_settings file.
if os.path.isfile(settings_file):
raise ClickException("This project already has a " + click.style("{0!s} file".format(settings_file), fg="red", bold=True) + "!")
# Explain system.
click.echo(click.style(u"""\n███████╗ █████╗ ██████╗ ██████╗ █████╗
╚══███╔╝██╔══██╗██╔══██╗██╔══██╗██╔══██╗
███╔╝ ███████║██████╔╝██████╔╝███████║
███╔╝ ██╔══██║██╔═══╝ ██╔═══╝ ██╔══██║
███████╗██║ ██║██║ ██║ ██║ ██║
╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝\n""", fg='green', bold=True))
click.echo(click.style("Welcome to ", bold=True) + click.style("Zappa", fg='green', bold=True) + click.style("!\n", bold=True))
click.echo(click.style("Zappa", bold=True) + " is a system for running server-less Python web applications"
" on AWS Lambda and AWS API Gateway.")
click.echo("This `init` command will help you create and configure your new Zappa deployment.")
click.echo("Let's get started!\n")
# Create Env
while True:
click.echo("Your Zappa configuration can support multiple production stages, like '" +
click.style("dev", bold=True) + "', '" + click.style("staging", bold=True) + "', and '" +
click.style("production", bold=True) + "'.")
env = input("What do you want to call this environment (default 'dev'): ") or "dev"
try:
self.check_stage_name(env)
break
except ValueError:
click.echo(click.style("Stage names must match a-zA-Z0-9_", fg="red"))
# Detect AWS profiles and regions
# If anyone knows a more straightforward way to easily detect and parse AWS profiles I'm happy to change this, feels like a hack
session = botocore.session.Session()
config = session.full_config
profiles = config.get("profiles", {})
profile_names = list(profiles.keys())
click.echo("\nAWS Lambda and API Gateway are only available in certain regions. "\
"Let's check to make sure you have a profile set up in one that will work.")
if not profile_names:
profile_name, profile = None, None
click.echo("We couldn't find an AWS profile to use. Before using Zappa, you'll need to set one up. See here for more info: {}"
.format(click.style(BOTO3_CONFIG_DOCS_URL, fg="blue", underline=True)))
elif len(profile_names) == 1:
profile_name = profile_names[0]
profile = profiles[profile_name]
click.echo("Okay, using profile {}!".format(click.style(profile_name, bold=True)))
else:
if "default" in profile_names:
default_profile = [p for p in profile_names if p == "default"][0]
else:
default_profile = profile_names[0]
while True:
profile_name = input("We found the following profiles: {}, and {}. "\
"Which would you like us to use? (default '{}'): "
.format(
', '.join(profile_names[:-1]),
profile_names[-1],
default_profile
)) or default_profile
if profile_name in profiles:
profile = profiles[profile_name]
break
else:
click.echo("Please enter a valid name for your AWS profile.")
profile_region = profile.get("region") if profile else None
# Create Bucket
click.echo("\nYour Zappa deployments will need to be uploaded to a " + click.style("private S3 bucket", bold=True) + ".")
click.echo("If you don't have a bucket yet, we'll create one for you too.")
default_bucket = "zappa-" + ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(9))
while True:
bucket = input("What do you want to call your bucket? (default '%s'): " % default_bucket) or default_bucket
if is_valid_bucket_name(bucket):
break
click.echo(click.style("Invalid bucket name!", bold=True))
click.echo("S3 buckets must be named according to the following rules:")
click.echo("""* Bucket names must be unique across all existing bucket names in Amazon S3.
* Bucket names must comply with DNS naming conventions.
* Bucket names must be at least 3 and no more than 63 characters long.
* Bucket names must not contain uppercase characters or underscores.
* Bucket names must start with a lowercase letter or number.
* Bucket names must be a series of one or more labels. Adjacent labels are separated
by a single period (.). Bucket names can contain lowercase letters, numbers, and
hyphens. Each label must start and end with a lowercase letter or a number.
* Bucket names must not be formatted as an IP address (for example, 192.168.5.4).
* When you use virtual hosted–style buckets with Secure Sockets Layer (SSL), the SSL
wildcard certificate only matches buckets that don't contain periods. To work around
this, use HTTP or write your own certificate verification logic. We recommend that
you do not use periods (".") in bucket names when using virtual hosted–style buckets.
""")
# Detect Django/Flask
try: # pragma: no cover
import django
has_django = True
except ImportError as e:
has_django = False
try: # pragma: no cover
import flask
has_flask = True
except ImportError as e:
has_flask = False
print('')
# App-specific
if has_django: # pragma: no cover
click.echo("It looks like this is a " + click.style("Django", bold=True) + " application!")
click.echo("What is the " + click.style("module path", bold=True) + " to your projects's Django settings?")
django_settings = None
matches = detect_django_settings()
while django_settings in [None, '']:
if matches:
click.echo("We discovered: " + click.style(', '.join('{}'.format(i) for v, i in enumerate(matches)), bold=True))
django_settings = input("Where are your project's settings? (default '%s'): " % matches[0]) or matches[0]
else:
click.echo("(This will likely be something like 'your_project.settings')")
django_settings = input("Where are your project's settings?: ")
django_settings = django_settings.replace("'", "")
django_settings = django_settings.replace('"', "")
else:
matches = None
if has_flask:
click.echo("It looks like this is a " + click.style("Flask", bold=True) + " application.")
matches = detect_flask_apps()
click.echo("What's the " + click.style("modular path", bold=True) + " to your app's function?")
click.echo("This will likely be something like 'your_module.app'.")
app_function = None
while app_function in [None, '']:
if matches:
click.echo("We discovered: " + click.style(', '.join('{}'.format(i) for v, i in enumerate(matches)), bold=True))
app_function = input("Where is your app's function? (default '%s'): " % matches[0]) or matches[0]
else:
app_function = input("Where is your app's function?: ")
app_function = app_function.replace("'", "")
app_function = app_function.replace('"', "")
# TODO: Create VPC?
# Memory size? Time limit?
# Domain? LE keys? Region?
# 'Advanced Settings' mode?
# Globalize
click.echo("\nYou can optionally deploy to " + click.style("all available regions", bold=True) + " in order to provide fast global service.")
click.echo("If you are using Zappa for the first time, you probably don't want to do this!")
global_deployment = False
while True:
global_type = input("Would you like to deploy this application " + click.style("globally", bold=True) + "? (default 'n') [y/n/(p)rimary]: ")
if not global_type:
break
if global_type.lower() in ["y", "yes", "p", "primary"]:
global_deployment = True
break
if global_type.lower() in ["n", "no"]:
global_deployment = False
break
# The given environment name
zappa_settings = {
env: {
'profile_name': profile_name,
's3_bucket': bucket,
'runtime': get_venv_from_python_version(),
'project_name': self.get_project_name()
}
}
if profile_region:
zappa_settings[env]['aws_region'] = profile_region
if has_django:
zappa_settings[env]['django_settings'] = django_settings
else:
zappa_settings[env]['app_function'] = app_function
# Global Region Deployment
if global_deployment:
additional_regions = [r for r in API_GATEWAY_REGIONS if r != profile_region]
# Create additional stages
if global_type.lower() in ["p", "primary"]:
additional_regions = [r for r in additional_regions if '-1' in r]
for region in additional_regions:
env_name = env + '_' + region.replace('-', '_')
g_env = {
env_name: {
'extends': env,
'aws_region': region
}
}
zappa_settings.update(g_env)
import json as json # hjson is fine for loading, not fine for writing.
zappa_settings_json = json.dumps(zappa_settings, sort_keys=True, indent=4)
click.echo("\nOkay, here's your " + click.style("zappa_settings.json", bold=True) + ":\n")
click.echo(click.style(zappa_settings_json, fg="yellow", bold=False))
confirm = input("\nDoes this look " + click.style("okay", bold=True, fg="green") + "? (default 'y') [y/n]: ") or 'yes'
if confirm[0] not in ['y', 'Y', 'yes', 'YES']:
click.echo("" + click.style("Sorry", bold=True, fg='red') + " to hear that! Please init again.")
return
# Write
with open("zappa_settings.json", "w") as zappa_settings_file:
zappa_settings_file.write(zappa_settings_json)
if global_deployment:
click.echo("\n" + click.style("Done", bold=True) + "! You can also " + click.style("deploy all", bold=True) + " by executing:\n")
click.echo(click.style("\t$ zappa deploy --all", bold=True))
click.echo("\nAfter that, you can " + click.style("update", bold=True) + " your application code with:\n")
click.echo(click.style("\t$ zappa update --all", bold=True))
else:
click.echo("\n" + click.style("Done", bold=True) + "! Now you can " + click.style("deploy", bold=True) + " your Zappa application by executing:\n")
click.echo(click.style("\t$ zappa deploy %s" % env, bold=True))
click.echo("\nAfter that, you can " + click.style("update", bold=True) + " your application code with:\n")
click.echo(click.style("\t$ zappa update %s" % env, bold=True))
click.echo("\nTo learn more, check out our project page on " + click.style("GitHub", bold=True) +
" here: " + click.style("https://github.com/Miserlou/Zappa", fg="cyan", bold=True))
click.echo("and stop by our " + click.style("Slack", bold=True) + " channel here: " +
click.style("https://slack.zappa.io", fg="cyan", bold=True))
click.echo("\nEnjoy!,")
click.echo(" ~ Team " + click.style("Zappa", bold=True) + "!")
return | python | def init(self, settings_file="zappa_settings.json"):
"""
Initialize a new Zappa project by creating a new zappa_settings.json in a guided process.
This should probably be broken up into few separate componants once it's stable.
Testing these inputs requires monkeypatching with mock, which isn't pretty.
"""
# Make sure we're in a venv.
self.check_venv()
# Ensure that we don't already have a zappa_settings file.
if os.path.isfile(settings_file):
raise ClickException("This project already has a " + click.style("{0!s} file".format(settings_file), fg="red", bold=True) + "!")
# Explain system.
click.echo(click.style(u"""\n███████╗ █████╗ ██████╗ ██████╗ █████╗
╚══███╔╝██╔══██╗██╔══██╗██╔══██╗██╔══██╗
███╔╝ ███████║██████╔╝██████╔╝███████║
███╔╝ ██╔══██║██╔═══╝ ██╔═══╝ ██╔══██║
███████╗██║ ██║██║ ██║ ██║ ██║
╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝\n""", fg='green', bold=True))
click.echo(click.style("Welcome to ", bold=True) + click.style("Zappa", fg='green', bold=True) + click.style("!\n", bold=True))
click.echo(click.style("Zappa", bold=True) + " is a system for running server-less Python web applications"
" on AWS Lambda and AWS API Gateway.")
click.echo("This `init` command will help you create and configure your new Zappa deployment.")
click.echo("Let's get started!\n")
# Create Env
while True:
click.echo("Your Zappa configuration can support multiple production stages, like '" +
click.style("dev", bold=True) + "', '" + click.style("staging", bold=True) + "', and '" +
click.style("production", bold=True) + "'.")
env = input("What do you want to call this environment (default 'dev'): ") or "dev"
try:
self.check_stage_name(env)
break
except ValueError:
click.echo(click.style("Stage names must match a-zA-Z0-9_", fg="red"))
# Detect AWS profiles and regions
# If anyone knows a more straightforward way to easily detect and parse AWS profiles I'm happy to change this, feels like a hack
session = botocore.session.Session()
config = session.full_config
profiles = config.get("profiles", {})
profile_names = list(profiles.keys())
click.echo("\nAWS Lambda and API Gateway are only available in certain regions. "\
"Let's check to make sure you have a profile set up in one that will work.")
if not profile_names:
profile_name, profile = None, None
click.echo("We couldn't find an AWS profile to use. Before using Zappa, you'll need to set one up. See here for more info: {}"
.format(click.style(BOTO3_CONFIG_DOCS_URL, fg="blue", underline=True)))
elif len(profile_names) == 1:
profile_name = profile_names[0]
profile = profiles[profile_name]
click.echo("Okay, using profile {}!".format(click.style(profile_name, bold=True)))
else:
if "default" in profile_names:
default_profile = [p for p in profile_names if p == "default"][0]
else:
default_profile = profile_names[0]
while True:
profile_name = input("We found the following profiles: {}, and {}. "\
"Which would you like us to use? (default '{}'): "
.format(
', '.join(profile_names[:-1]),
profile_names[-1],
default_profile
)) or default_profile
if profile_name in profiles:
profile = profiles[profile_name]
break
else:
click.echo("Please enter a valid name for your AWS profile.")
profile_region = profile.get("region") if profile else None
# Create Bucket
click.echo("\nYour Zappa deployments will need to be uploaded to a " + click.style("private S3 bucket", bold=True) + ".")
click.echo("If you don't have a bucket yet, we'll create one for you too.")
default_bucket = "zappa-" + ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(9))
while True:
bucket = input("What do you want to call your bucket? (default '%s'): " % default_bucket) or default_bucket
if is_valid_bucket_name(bucket):
break
click.echo(click.style("Invalid bucket name!", bold=True))
click.echo("S3 buckets must be named according to the following rules:")
click.echo("""* Bucket names must be unique across all existing bucket names in Amazon S3.
* Bucket names must comply with DNS naming conventions.
* Bucket names must be at least 3 and no more than 63 characters long.
* Bucket names must not contain uppercase characters or underscores.
* Bucket names must start with a lowercase letter or number.
* Bucket names must be a series of one or more labels. Adjacent labels are separated
by a single period (.). Bucket names can contain lowercase letters, numbers, and
hyphens. Each label must start and end with a lowercase letter or a number.
* Bucket names must not be formatted as an IP address (for example, 192.168.5.4).
* When you use virtual hosted–style buckets with Secure Sockets Layer (SSL), the SSL
wildcard certificate only matches buckets that don't contain periods. To work around
this, use HTTP or write your own certificate verification logic. We recommend that
you do not use periods (".") in bucket names when using virtual hosted–style buckets.
""")
# Detect Django/Flask
try: # pragma: no cover
import django
has_django = True
except ImportError as e:
has_django = False
try: # pragma: no cover
import flask
has_flask = True
except ImportError as e:
has_flask = False
print('')
# App-specific
if has_django: # pragma: no cover
click.echo("It looks like this is a " + click.style("Django", bold=True) + " application!")
click.echo("What is the " + click.style("module path", bold=True) + " to your projects's Django settings?")
django_settings = None
matches = detect_django_settings()
while django_settings in [None, '']:
if matches:
click.echo("We discovered: " + click.style(', '.join('{}'.format(i) for v, i in enumerate(matches)), bold=True))
django_settings = input("Where are your project's settings? (default '%s'): " % matches[0]) or matches[0]
else:
click.echo("(This will likely be something like 'your_project.settings')")
django_settings = input("Where are your project's settings?: ")
django_settings = django_settings.replace("'", "")
django_settings = django_settings.replace('"', "")
else:
matches = None
if has_flask:
click.echo("It looks like this is a " + click.style("Flask", bold=True) + " application.")
matches = detect_flask_apps()
click.echo("What's the " + click.style("modular path", bold=True) + " to your app's function?")
click.echo("This will likely be something like 'your_module.app'.")
app_function = None
while app_function in [None, '']:
if matches:
click.echo("We discovered: " + click.style(', '.join('{}'.format(i) for v, i in enumerate(matches)), bold=True))
app_function = input("Where is your app's function? (default '%s'): " % matches[0]) or matches[0]
else:
app_function = input("Where is your app's function?: ")
app_function = app_function.replace("'", "")
app_function = app_function.replace('"', "")
# TODO: Create VPC?
# Memory size? Time limit?
# Domain? LE keys? Region?
# 'Advanced Settings' mode?
# Globalize
click.echo("\nYou can optionally deploy to " + click.style("all available regions", bold=True) + " in order to provide fast global service.")
click.echo("If you are using Zappa for the first time, you probably don't want to do this!")
global_deployment = False
while True:
global_type = input("Would you like to deploy this application " + click.style("globally", bold=True) + "? (default 'n') [y/n/(p)rimary]: ")
if not global_type:
break
if global_type.lower() in ["y", "yes", "p", "primary"]:
global_deployment = True
break
if global_type.lower() in ["n", "no"]:
global_deployment = False
break
# The given environment name
zappa_settings = {
env: {
'profile_name': profile_name,
's3_bucket': bucket,
'runtime': get_venv_from_python_version(),
'project_name': self.get_project_name()
}
}
if profile_region:
zappa_settings[env]['aws_region'] = profile_region
if has_django:
zappa_settings[env]['django_settings'] = django_settings
else:
zappa_settings[env]['app_function'] = app_function
# Global Region Deployment
if global_deployment:
additional_regions = [r for r in API_GATEWAY_REGIONS if r != profile_region]
# Create additional stages
if global_type.lower() in ["p", "primary"]:
additional_regions = [r for r in additional_regions if '-1' in r]
for region in additional_regions:
env_name = env + '_' + region.replace('-', '_')
g_env = {
env_name: {
'extends': env,
'aws_region': region
}
}
zappa_settings.update(g_env)
import json as json # hjson is fine for loading, not fine for writing.
zappa_settings_json = json.dumps(zappa_settings, sort_keys=True, indent=4)
click.echo("\nOkay, here's your " + click.style("zappa_settings.json", bold=True) + ":\n")
click.echo(click.style(zappa_settings_json, fg="yellow", bold=False))
confirm = input("\nDoes this look " + click.style("okay", bold=True, fg="green") + "? (default 'y') [y/n]: ") or 'yes'
if confirm[0] not in ['y', 'Y', 'yes', 'YES']:
click.echo("" + click.style("Sorry", bold=True, fg='red') + " to hear that! Please init again.")
return
# Write
with open("zappa_settings.json", "w") as zappa_settings_file:
zappa_settings_file.write(zappa_settings_json)
if global_deployment:
click.echo("\n" + click.style("Done", bold=True) + "! You can also " + click.style("deploy all", bold=True) + " by executing:\n")
click.echo(click.style("\t$ zappa deploy --all", bold=True))
click.echo("\nAfter that, you can " + click.style("update", bold=True) + " your application code with:\n")
click.echo(click.style("\t$ zappa update --all", bold=True))
else:
click.echo("\n" + click.style("Done", bold=True) + "! Now you can " + click.style("deploy", bold=True) + " your Zappa application by executing:\n")
click.echo(click.style("\t$ zappa deploy %s" % env, bold=True))
click.echo("\nAfter that, you can " + click.style("update", bold=True) + " your application code with:\n")
click.echo(click.style("\t$ zappa update %s" % env, bold=True))
click.echo("\nTo learn more, check out our project page on " + click.style("GitHub", bold=True) +
" here: " + click.style("https://github.com/Miserlou/Zappa", fg="cyan", bold=True))
click.echo("and stop by our " + click.style("Slack", bold=True) + " channel here: " +
click.style("https://slack.zappa.io", fg="cyan", bold=True))
click.echo("\nEnjoy!,")
click.echo(" ~ Team " + click.style("Zappa", bold=True) + "!")
return | [
"def",
"init",
"(",
"self",
",",
"settings_file",
"=",
"\"zappa_settings.json\"",
")",
":",
"# Make sure we're in a venv.",
"self",
".",
"check_venv",
"(",
")",
"# Ensure that we don't already have a zappa_settings file.",
"if",
"os",
".",
"path",
".",
"isfile",
"(",
"settings_file",
")",
":",
"raise",
"ClickException",
"(",
"\"This project already has a \"",
"+",
"click",
".",
"style",
"(",
"\"{0!s} file\"",
".",
"format",
"(",
"settings_file",
")",
",",
"fg",
"=",
"\"red\"",
",",
"bold",
"=",
"True",
")",
"+",
"\"!\"",
")",
"# Explain system.",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"u\"\"\"\\n███████╗ █████╗ ██████╗ ██████╗ █████╗\n╚══███╔╝██╔══██╗██╔══██╗██╔══██╗██╔══██╗\n ███╔╝ ███████║██████╔╝██████╔╝███████║\n ███╔╝ ██╔══██║██╔═══╝ ██╔═══╝ ██╔══██║\n███████╗██║ ██║██║ ██║ ██║ ██║\n╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝\\n\"\"\", fg='green', bold=True))",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"Welcome to \"",
",",
"bold",
"=",
"True",
")",
"+",
"click",
".",
"style",
"(",
"\"Zappa\"",
",",
"fg",
"=",
"'green'",
",",
"bold",
"=",
"True",
")",
"+",
"click",
".",
"style",
"(",
"\"!\\n\"",
",",
"bold",
"=",
"True",
")",
")",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"Zappa\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" is a system for running server-less Python web applications\"",
"\" on AWS Lambda and AWS API Gateway.\"",
")",
"click",
".",
"echo",
"(",
"\"This `init` command will help you create and configure your new Zappa deployment.\"",
")",
"click",
".",
"echo",
"(",
"\"Let's get started!\\n\"",
")",
"# Create Env",
"while",
"True",
":",
"click",
".",
"echo",
"(",
"\"Your Zappa configuration can support multiple production stages, like '\"",
"+",
"click",
".",
"style",
"(",
"\"dev\"",
",",
"bold",
"=",
"True",
")",
"+",
"\"', '\"",
"+",
"click",
".",
"style",
"(",
"\"staging\"",
",",
"bold",
"=",
"True",
")",
"+",
"\"', and '\"",
"+",
"click",
".",
"style",
"(",
"\"production\"",
",",
"bold",
"=",
"True",
")",
"+",
"\"'.\"",
")",
"env",
"=",
"input",
"(",
"\"What do you want to call this environment (default 'dev'): \"",
")",
"or",
"\"dev\"",
"try",
":",
"self",
".",
"check_stage_name",
"(",
"env",
")",
"break",
"except",
"ValueError",
":",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"Stage names must match a-zA-Z0-9_\"",
",",
"fg",
"=",
"\"red\"",
")",
")",
"# Detect AWS profiles and regions",
"# If anyone knows a more straightforward way to easily detect and parse AWS profiles I'm happy to change this, feels like a hack",
"session",
"=",
"botocore",
".",
"session",
".",
"Session",
"(",
")",
"config",
"=",
"session",
".",
"full_config",
"profiles",
"=",
"config",
".",
"get",
"(",
"\"profiles\"",
",",
"{",
"}",
")",
"profile_names",
"=",
"list",
"(",
"profiles",
".",
"keys",
"(",
")",
")",
"click",
".",
"echo",
"(",
"\"\\nAWS Lambda and API Gateway are only available in certain regions. \"",
"\"Let's check to make sure you have a profile set up in one that will work.\"",
")",
"if",
"not",
"profile_names",
":",
"profile_name",
",",
"profile",
"=",
"None",
",",
"None",
"click",
".",
"echo",
"(",
"\"We couldn't find an AWS profile to use. Before using Zappa, you'll need to set one up. See here for more info: {}\"",
".",
"format",
"(",
"click",
".",
"style",
"(",
"BOTO3_CONFIG_DOCS_URL",
",",
"fg",
"=",
"\"blue\"",
",",
"underline",
"=",
"True",
")",
")",
")",
"elif",
"len",
"(",
"profile_names",
")",
"==",
"1",
":",
"profile_name",
"=",
"profile_names",
"[",
"0",
"]",
"profile",
"=",
"profiles",
"[",
"profile_name",
"]",
"click",
".",
"echo",
"(",
"\"Okay, using profile {}!\"",
".",
"format",
"(",
"click",
".",
"style",
"(",
"profile_name",
",",
"bold",
"=",
"True",
")",
")",
")",
"else",
":",
"if",
"\"default\"",
"in",
"profile_names",
":",
"default_profile",
"=",
"[",
"p",
"for",
"p",
"in",
"profile_names",
"if",
"p",
"==",
"\"default\"",
"]",
"[",
"0",
"]",
"else",
":",
"default_profile",
"=",
"profile_names",
"[",
"0",
"]",
"while",
"True",
":",
"profile_name",
"=",
"input",
"(",
"\"We found the following profiles: {}, and {}. \"",
"\"Which would you like us to use? (default '{}'): \"",
".",
"format",
"(",
"', '",
".",
"join",
"(",
"profile_names",
"[",
":",
"-",
"1",
"]",
")",
",",
"profile_names",
"[",
"-",
"1",
"]",
",",
"default_profile",
")",
")",
"or",
"default_profile",
"if",
"profile_name",
"in",
"profiles",
":",
"profile",
"=",
"profiles",
"[",
"profile_name",
"]",
"break",
"else",
":",
"click",
".",
"echo",
"(",
"\"Please enter a valid name for your AWS profile.\"",
")",
"profile_region",
"=",
"profile",
".",
"get",
"(",
"\"region\"",
")",
"if",
"profile",
"else",
"None",
"# Create Bucket",
"click",
".",
"echo",
"(",
"\"\\nYour Zappa deployments will need to be uploaded to a \"",
"+",
"click",
".",
"style",
"(",
"\"private S3 bucket\"",
",",
"bold",
"=",
"True",
")",
"+",
"\".\"",
")",
"click",
".",
"echo",
"(",
"\"If you don't have a bucket yet, we'll create one for you too.\"",
")",
"default_bucket",
"=",
"\"zappa-\"",
"+",
"''",
".",
"join",
"(",
"random",
".",
"choice",
"(",
"string",
".",
"ascii_lowercase",
"+",
"string",
".",
"digits",
")",
"for",
"_",
"in",
"range",
"(",
"9",
")",
")",
"while",
"True",
":",
"bucket",
"=",
"input",
"(",
"\"What do you want to call your bucket? (default '%s'): \"",
"%",
"default_bucket",
")",
"or",
"default_bucket",
"if",
"is_valid_bucket_name",
"(",
"bucket",
")",
":",
"break",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"Invalid bucket name!\"",
",",
"bold",
"=",
"True",
")",
")",
"click",
".",
"echo",
"(",
"\"S3 buckets must be named according to the following rules:\"",
")",
"click",
".",
"echo",
"(",
"\"\"\"* Bucket names must be unique across all existing bucket names in Amazon S3.\n* Bucket names must comply with DNS naming conventions.\n* Bucket names must be at least 3 and no more than 63 characters long.\n* Bucket names must not contain uppercase characters or underscores.\n* Bucket names must start with a lowercase letter or number.\n* Bucket names must be a series of one or more labels. Adjacent labels are separated\n by a single period (.). Bucket names can contain lowercase letters, numbers, and\n hyphens. Each label must start and end with a lowercase letter or a number.\n* Bucket names must not be formatted as an IP address (for example, 192.168.5.4).\n* When you use virtual hosted–style buckets with Secure Sockets Layer (SSL), the SSL\n wildcard certificate only matches buckets that don't contain periods. To work around\n this, use HTTP or write your own certificate verification logic. We recommend that\n you do not use periods (\".\") in bucket names when using virtual hosted–style buckets.\n\"\"\"",
")",
"# Detect Django/Flask",
"try",
":",
"# pragma: no cover",
"import",
"django",
"has_django",
"=",
"True",
"except",
"ImportError",
"as",
"e",
":",
"has_django",
"=",
"False",
"try",
":",
"# pragma: no cover",
"import",
"flask",
"has_flask",
"=",
"True",
"except",
"ImportError",
"as",
"e",
":",
"has_flask",
"=",
"False",
"print",
"(",
"''",
")",
"# App-specific",
"if",
"has_django",
":",
"# pragma: no cover",
"click",
".",
"echo",
"(",
"\"It looks like this is a \"",
"+",
"click",
".",
"style",
"(",
"\"Django\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" application!\"",
")",
"click",
".",
"echo",
"(",
"\"What is the \"",
"+",
"click",
".",
"style",
"(",
"\"module path\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" to your projects's Django settings?\"",
")",
"django_settings",
"=",
"None",
"matches",
"=",
"detect_django_settings",
"(",
")",
"while",
"django_settings",
"in",
"[",
"None",
",",
"''",
"]",
":",
"if",
"matches",
":",
"click",
".",
"echo",
"(",
"\"We discovered: \"",
"+",
"click",
".",
"style",
"(",
"', '",
".",
"join",
"(",
"'{}'",
".",
"format",
"(",
"i",
")",
"for",
"v",
",",
"i",
"in",
"enumerate",
"(",
"matches",
")",
")",
",",
"bold",
"=",
"True",
")",
")",
"django_settings",
"=",
"input",
"(",
"\"Where are your project's settings? (default '%s'): \"",
"%",
"matches",
"[",
"0",
"]",
")",
"or",
"matches",
"[",
"0",
"]",
"else",
":",
"click",
".",
"echo",
"(",
"\"(This will likely be something like 'your_project.settings')\"",
")",
"django_settings",
"=",
"input",
"(",
"\"Where are your project's settings?: \"",
")",
"django_settings",
"=",
"django_settings",
".",
"replace",
"(",
"\"'\"",
",",
"\"\"",
")",
"django_settings",
"=",
"django_settings",
".",
"replace",
"(",
"'\"'",
",",
"\"\"",
")",
"else",
":",
"matches",
"=",
"None",
"if",
"has_flask",
":",
"click",
".",
"echo",
"(",
"\"It looks like this is a \"",
"+",
"click",
".",
"style",
"(",
"\"Flask\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" application.\"",
")",
"matches",
"=",
"detect_flask_apps",
"(",
")",
"click",
".",
"echo",
"(",
"\"What's the \"",
"+",
"click",
".",
"style",
"(",
"\"modular path\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" to your app's function?\"",
")",
"click",
".",
"echo",
"(",
"\"This will likely be something like 'your_module.app'.\"",
")",
"app_function",
"=",
"None",
"while",
"app_function",
"in",
"[",
"None",
",",
"''",
"]",
":",
"if",
"matches",
":",
"click",
".",
"echo",
"(",
"\"We discovered: \"",
"+",
"click",
".",
"style",
"(",
"', '",
".",
"join",
"(",
"'{}'",
".",
"format",
"(",
"i",
")",
"for",
"v",
",",
"i",
"in",
"enumerate",
"(",
"matches",
")",
")",
",",
"bold",
"=",
"True",
")",
")",
"app_function",
"=",
"input",
"(",
"\"Where is your app's function? (default '%s'): \"",
"%",
"matches",
"[",
"0",
"]",
")",
"or",
"matches",
"[",
"0",
"]",
"else",
":",
"app_function",
"=",
"input",
"(",
"\"Where is your app's function?: \"",
")",
"app_function",
"=",
"app_function",
".",
"replace",
"(",
"\"'\"",
",",
"\"\"",
")",
"app_function",
"=",
"app_function",
".",
"replace",
"(",
"'\"'",
",",
"\"\"",
")",
"# TODO: Create VPC?",
"# Memory size? Time limit?",
"# Domain? LE keys? Region?",
"# 'Advanced Settings' mode?",
"# Globalize",
"click",
".",
"echo",
"(",
"\"\\nYou can optionally deploy to \"",
"+",
"click",
".",
"style",
"(",
"\"all available regions\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" in order to provide fast global service.\"",
")",
"click",
".",
"echo",
"(",
"\"If you are using Zappa for the first time, you probably don't want to do this!\"",
")",
"global_deployment",
"=",
"False",
"while",
"True",
":",
"global_type",
"=",
"input",
"(",
"\"Would you like to deploy this application \"",
"+",
"click",
".",
"style",
"(",
"\"globally\"",
",",
"bold",
"=",
"True",
")",
"+",
"\"? (default 'n') [y/n/(p)rimary]: \"",
")",
"if",
"not",
"global_type",
":",
"break",
"if",
"global_type",
".",
"lower",
"(",
")",
"in",
"[",
"\"y\"",
",",
"\"yes\"",
",",
"\"p\"",
",",
"\"primary\"",
"]",
":",
"global_deployment",
"=",
"True",
"break",
"if",
"global_type",
".",
"lower",
"(",
")",
"in",
"[",
"\"n\"",
",",
"\"no\"",
"]",
":",
"global_deployment",
"=",
"False",
"break",
"# The given environment name",
"zappa_settings",
"=",
"{",
"env",
":",
"{",
"'profile_name'",
":",
"profile_name",
",",
"'s3_bucket'",
":",
"bucket",
",",
"'runtime'",
":",
"get_venv_from_python_version",
"(",
")",
",",
"'project_name'",
":",
"self",
".",
"get_project_name",
"(",
")",
"}",
"}",
"if",
"profile_region",
":",
"zappa_settings",
"[",
"env",
"]",
"[",
"'aws_region'",
"]",
"=",
"profile_region",
"if",
"has_django",
":",
"zappa_settings",
"[",
"env",
"]",
"[",
"'django_settings'",
"]",
"=",
"django_settings",
"else",
":",
"zappa_settings",
"[",
"env",
"]",
"[",
"'app_function'",
"]",
"=",
"app_function",
"# Global Region Deployment",
"if",
"global_deployment",
":",
"additional_regions",
"=",
"[",
"r",
"for",
"r",
"in",
"API_GATEWAY_REGIONS",
"if",
"r",
"!=",
"profile_region",
"]",
"# Create additional stages",
"if",
"global_type",
".",
"lower",
"(",
")",
"in",
"[",
"\"p\"",
",",
"\"primary\"",
"]",
":",
"additional_regions",
"=",
"[",
"r",
"for",
"r",
"in",
"additional_regions",
"if",
"'-1'",
"in",
"r",
"]",
"for",
"region",
"in",
"additional_regions",
":",
"env_name",
"=",
"env",
"+",
"'_'",
"+",
"region",
".",
"replace",
"(",
"'-'",
",",
"'_'",
")",
"g_env",
"=",
"{",
"env_name",
":",
"{",
"'extends'",
":",
"env",
",",
"'aws_region'",
":",
"region",
"}",
"}",
"zappa_settings",
".",
"update",
"(",
"g_env",
")",
"import",
"json",
"as",
"json",
"# hjson is fine for loading, not fine for writing.",
"zappa_settings_json",
"=",
"json",
".",
"dumps",
"(",
"zappa_settings",
",",
"sort_keys",
"=",
"True",
",",
"indent",
"=",
"4",
")",
"click",
".",
"echo",
"(",
"\"\\nOkay, here's your \"",
"+",
"click",
".",
"style",
"(",
"\"zappa_settings.json\"",
",",
"bold",
"=",
"True",
")",
"+",
"\":\\n\"",
")",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"zappa_settings_json",
",",
"fg",
"=",
"\"yellow\"",
",",
"bold",
"=",
"False",
")",
")",
"confirm",
"=",
"input",
"(",
"\"\\nDoes this look \"",
"+",
"click",
".",
"style",
"(",
"\"okay\"",
",",
"bold",
"=",
"True",
",",
"fg",
"=",
"\"green\"",
")",
"+",
"\"? (default 'y') [y/n]: \"",
")",
"or",
"'yes'",
"if",
"confirm",
"[",
"0",
"]",
"not",
"in",
"[",
"'y'",
",",
"'Y'",
",",
"'yes'",
",",
"'YES'",
"]",
":",
"click",
".",
"echo",
"(",
"\"\"",
"+",
"click",
".",
"style",
"(",
"\"Sorry\"",
",",
"bold",
"=",
"True",
",",
"fg",
"=",
"'red'",
")",
"+",
"\" to hear that! Please init again.\"",
")",
"return",
"# Write",
"with",
"open",
"(",
"\"zappa_settings.json\"",
",",
"\"w\"",
")",
"as",
"zappa_settings_file",
":",
"zappa_settings_file",
".",
"write",
"(",
"zappa_settings_json",
")",
"if",
"global_deployment",
":",
"click",
".",
"echo",
"(",
"\"\\n\"",
"+",
"click",
".",
"style",
"(",
"\"Done\"",
",",
"bold",
"=",
"True",
")",
"+",
"\"! You can also \"",
"+",
"click",
".",
"style",
"(",
"\"deploy all\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" by executing:\\n\"",
")",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"\\t$ zappa deploy --all\"",
",",
"bold",
"=",
"True",
")",
")",
"click",
".",
"echo",
"(",
"\"\\nAfter that, you can \"",
"+",
"click",
".",
"style",
"(",
"\"update\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" your application code with:\\n\"",
")",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"\\t$ zappa update --all\"",
",",
"bold",
"=",
"True",
")",
")",
"else",
":",
"click",
".",
"echo",
"(",
"\"\\n\"",
"+",
"click",
".",
"style",
"(",
"\"Done\"",
",",
"bold",
"=",
"True",
")",
"+",
"\"! Now you can \"",
"+",
"click",
".",
"style",
"(",
"\"deploy\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" your Zappa application by executing:\\n\"",
")",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"\\t$ zappa deploy %s\"",
"%",
"env",
",",
"bold",
"=",
"True",
")",
")",
"click",
".",
"echo",
"(",
"\"\\nAfter that, you can \"",
"+",
"click",
".",
"style",
"(",
"\"update\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" your application code with:\\n\"",
")",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"\\t$ zappa update %s\"",
"%",
"env",
",",
"bold",
"=",
"True",
")",
")",
"click",
".",
"echo",
"(",
"\"\\nTo learn more, check out our project page on \"",
"+",
"click",
".",
"style",
"(",
"\"GitHub\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" here: \"",
"+",
"click",
".",
"style",
"(",
"\"https://github.com/Miserlou/Zappa\"",
",",
"fg",
"=",
"\"cyan\"",
",",
"bold",
"=",
"True",
")",
")",
"click",
".",
"echo",
"(",
"\"and stop by our \"",
"+",
"click",
".",
"style",
"(",
"\"Slack\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" channel here: \"",
"+",
"click",
".",
"style",
"(",
"\"https://slack.zappa.io\"",
",",
"fg",
"=",
"\"cyan\"",
",",
"bold",
"=",
"True",
")",
")",
"click",
".",
"echo",
"(",
"\"\\nEnjoy!,\"",
")",
"click",
".",
"echo",
"(",
"\" ~ Team \"",
"+",
"click",
".",
"style",
"(",
"\"Zappa\"",
",",
"bold",
"=",
"True",
")",
"+",
"\"!\"",
")",
"return"
] | Initialize a new Zappa project by creating a new zappa_settings.json in a guided process.
This should probably be broken up into few separate componants once it's stable.
Testing these inputs requires monkeypatching with mock, which isn't pretty. | [
"Initialize",
"a",
"new",
"Zappa",
"project",
"by",
"creating",
"a",
"new",
"zappa_settings",
".",
"json",
"in",
"a",
"guided",
"process",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L1550-L1797 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.certify | def certify(self, no_confirm=True, manual=False):
"""
Register or update a domain certificate for this env.
"""
if not self.domain:
raise ClickException("Can't certify a domain without " + click.style("domain", fg="red", bold=True) + " configured!")
if not no_confirm: # pragma: no cover
confirm = input("Are you sure you want to certify? [y/n] ")
if confirm != 'y':
return
# Make sure this isn't already deployed.
deployed_versions = self.zappa.get_lambda_function_versions(self.lambda_name)
if len(deployed_versions) == 0:
raise ClickException("This application " + click.style("isn't deployed yet", fg="red") +
" - did you mean to call " + click.style("deploy", bold=True) + "?")
account_key_location = self.stage_config.get('lets_encrypt_key', None)
cert_location = self.stage_config.get('certificate', None)
cert_key_location = self.stage_config.get('certificate_key', None)
cert_chain_location = self.stage_config.get('certificate_chain', None)
cert_arn = self.stage_config.get('certificate_arn', None)
base_path = self.stage_config.get('base_path', None)
# These are sensitive
certificate_body = None
certificate_private_key = None
certificate_chain = None
# Prepare for custom Let's Encrypt
if not cert_location and not cert_arn:
if not account_key_location:
raise ClickException("Can't certify a domain without " + click.style("lets_encrypt_key", fg="red", bold=True) +
" or " + click.style("certificate", fg="red", bold=True)+
" or " + click.style("certificate_arn", fg="red", bold=True) + " configured!")
# Get install account_key to /tmp/account_key.pem
from .letsencrypt import gettempdir
if account_key_location.startswith('s3://'):
bucket, key_name = parse_s3_url(account_key_location)
self.zappa.s3_client.download_file(bucket, key_name, os.path.join(gettempdir(), 'account.key'))
else:
from shutil import copyfile
copyfile(account_key_location, os.path.join(gettempdir(), 'account.key'))
# Prepare for Custom SSL
elif not account_key_location and not cert_arn:
if not cert_location or not cert_key_location or not cert_chain_location:
raise ClickException("Can't certify a domain without " +
click.style("certificate, certificate_key and certificate_chain", fg="red", bold=True) + " configured!")
# Read the supplied certificates.
with open(cert_location) as f:
certificate_body = f.read()
with open(cert_key_location) as f:
certificate_private_key = f.read()
with open(cert_chain_location) as f:
certificate_chain = f.read()
click.echo("Certifying domain " + click.style(self.domain, fg="green", bold=True) + "..")
# Get cert and update domain.
# Let's Encrypt
if not cert_location and not cert_arn:
from .letsencrypt import get_cert_and_update_domain
cert_success = get_cert_and_update_domain(
self.zappa,
self.lambda_name,
self.api_stage,
self.domain,
manual
)
# Custom SSL / ACM
else:
route53 = self.stage_config.get('route53_enabled', True)
if not self.zappa.get_domain_name(self.domain, route53=route53):
dns_name = self.zappa.create_domain_name(
domain_name=self.domain,
certificate_name=self.domain + "-Zappa-Cert",
certificate_body=certificate_body,
certificate_private_key=certificate_private_key,
certificate_chain=certificate_chain,
certificate_arn=cert_arn,
lambda_name=self.lambda_name,
stage=self.api_stage,
base_path=base_path
)
if route53:
self.zappa.update_route53_records(self.domain, dns_name)
print("Created a new domain name with supplied certificate. Please note that it can take up to 40 minutes for this domain to be "
"created and propagated through AWS, but it requires no further work on your part.")
else:
self.zappa.update_domain_name(
domain_name=self.domain,
certificate_name=self.domain + "-Zappa-Cert",
certificate_body=certificate_body,
certificate_private_key=certificate_private_key,
certificate_chain=certificate_chain,
certificate_arn=cert_arn,
lambda_name=self.lambda_name,
stage=self.api_stage,
route53=route53,
base_path=base_path
)
cert_success = True
if cert_success:
click.echo("Certificate " + click.style("updated", fg="green", bold=True) + "!")
else:
click.echo(click.style("Failed", fg="red", bold=True) + " to generate or install certificate! :(")
click.echo("\n==============\n")
shamelessly_promote() | python | def certify(self, no_confirm=True, manual=False):
"""
Register or update a domain certificate for this env.
"""
if not self.domain:
raise ClickException("Can't certify a domain without " + click.style("domain", fg="red", bold=True) + " configured!")
if not no_confirm: # pragma: no cover
confirm = input("Are you sure you want to certify? [y/n] ")
if confirm != 'y':
return
# Make sure this isn't already deployed.
deployed_versions = self.zappa.get_lambda_function_versions(self.lambda_name)
if len(deployed_versions) == 0:
raise ClickException("This application " + click.style("isn't deployed yet", fg="red") +
" - did you mean to call " + click.style("deploy", bold=True) + "?")
account_key_location = self.stage_config.get('lets_encrypt_key', None)
cert_location = self.stage_config.get('certificate', None)
cert_key_location = self.stage_config.get('certificate_key', None)
cert_chain_location = self.stage_config.get('certificate_chain', None)
cert_arn = self.stage_config.get('certificate_arn', None)
base_path = self.stage_config.get('base_path', None)
# These are sensitive
certificate_body = None
certificate_private_key = None
certificate_chain = None
# Prepare for custom Let's Encrypt
if not cert_location and not cert_arn:
if not account_key_location:
raise ClickException("Can't certify a domain without " + click.style("lets_encrypt_key", fg="red", bold=True) +
" or " + click.style("certificate", fg="red", bold=True)+
" or " + click.style("certificate_arn", fg="red", bold=True) + " configured!")
# Get install account_key to /tmp/account_key.pem
from .letsencrypt import gettempdir
if account_key_location.startswith('s3://'):
bucket, key_name = parse_s3_url(account_key_location)
self.zappa.s3_client.download_file(bucket, key_name, os.path.join(gettempdir(), 'account.key'))
else:
from shutil import copyfile
copyfile(account_key_location, os.path.join(gettempdir(), 'account.key'))
# Prepare for Custom SSL
elif not account_key_location and not cert_arn:
if not cert_location or not cert_key_location or not cert_chain_location:
raise ClickException("Can't certify a domain without " +
click.style("certificate, certificate_key and certificate_chain", fg="red", bold=True) + " configured!")
# Read the supplied certificates.
with open(cert_location) as f:
certificate_body = f.read()
with open(cert_key_location) as f:
certificate_private_key = f.read()
with open(cert_chain_location) as f:
certificate_chain = f.read()
click.echo("Certifying domain " + click.style(self.domain, fg="green", bold=True) + "..")
# Get cert and update domain.
# Let's Encrypt
if not cert_location and not cert_arn:
from .letsencrypt import get_cert_and_update_domain
cert_success = get_cert_and_update_domain(
self.zappa,
self.lambda_name,
self.api_stage,
self.domain,
manual
)
# Custom SSL / ACM
else:
route53 = self.stage_config.get('route53_enabled', True)
if not self.zappa.get_domain_name(self.domain, route53=route53):
dns_name = self.zappa.create_domain_name(
domain_name=self.domain,
certificate_name=self.domain + "-Zappa-Cert",
certificate_body=certificate_body,
certificate_private_key=certificate_private_key,
certificate_chain=certificate_chain,
certificate_arn=cert_arn,
lambda_name=self.lambda_name,
stage=self.api_stage,
base_path=base_path
)
if route53:
self.zappa.update_route53_records(self.domain, dns_name)
print("Created a new domain name with supplied certificate. Please note that it can take up to 40 minutes for this domain to be "
"created and propagated through AWS, but it requires no further work on your part.")
else:
self.zappa.update_domain_name(
domain_name=self.domain,
certificate_name=self.domain + "-Zappa-Cert",
certificate_body=certificate_body,
certificate_private_key=certificate_private_key,
certificate_chain=certificate_chain,
certificate_arn=cert_arn,
lambda_name=self.lambda_name,
stage=self.api_stage,
route53=route53,
base_path=base_path
)
cert_success = True
if cert_success:
click.echo("Certificate " + click.style("updated", fg="green", bold=True) + "!")
else:
click.echo(click.style("Failed", fg="red", bold=True) + " to generate or install certificate! :(")
click.echo("\n==============\n")
shamelessly_promote() | [
"def",
"certify",
"(",
"self",
",",
"no_confirm",
"=",
"True",
",",
"manual",
"=",
"False",
")",
":",
"if",
"not",
"self",
".",
"domain",
":",
"raise",
"ClickException",
"(",
"\"Can't certify a domain without \"",
"+",
"click",
".",
"style",
"(",
"\"domain\"",
",",
"fg",
"=",
"\"red\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" configured!\"",
")",
"if",
"not",
"no_confirm",
":",
"# pragma: no cover",
"confirm",
"=",
"input",
"(",
"\"Are you sure you want to certify? [y/n] \"",
")",
"if",
"confirm",
"!=",
"'y'",
":",
"return",
"# Make sure this isn't already deployed.",
"deployed_versions",
"=",
"self",
".",
"zappa",
".",
"get_lambda_function_versions",
"(",
"self",
".",
"lambda_name",
")",
"if",
"len",
"(",
"deployed_versions",
")",
"==",
"0",
":",
"raise",
"ClickException",
"(",
"\"This application \"",
"+",
"click",
".",
"style",
"(",
"\"isn't deployed yet\"",
",",
"fg",
"=",
"\"red\"",
")",
"+",
"\" - did you mean to call \"",
"+",
"click",
".",
"style",
"(",
"\"deploy\"",
",",
"bold",
"=",
"True",
")",
"+",
"\"?\"",
")",
"account_key_location",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'lets_encrypt_key'",
",",
"None",
")",
"cert_location",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'certificate'",
",",
"None",
")",
"cert_key_location",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'certificate_key'",
",",
"None",
")",
"cert_chain_location",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'certificate_chain'",
",",
"None",
")",
"cert_arn",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'certificate_arn'",
",",
"None",
")",
"base_path",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'base_path'",
",",
"None",
")",
"# These are sensitive",
"certificate_body",
"=",
"None",
"certificate_private_key",
"=",
"None",
"certificate_chain",
"=",
"None",
"# Prepare for custom Let's Encrypt",
"if",
"not",
"cert_location",
"and",
"not",
"cert_arn",
":",
"if",
"not",
"account_key_location",
":",
"raise",
"ClickException",
"(",
"\"Can't certify a domain without \"",
"+",
"click",
".",
"style",
"(",
"\"lets_encrypt_key\"",
",",
"fg",
"=",
"\"red\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" or \"",
"+",
"click",
".",
"style",
"(",
"\"certificate\"",
",",
"fg",
"=",
"\"red\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" or \"",
"+",
"click",
".",
"style",
"(",
"\"certificate_arn\"",
",",
"fg",
"=",
"\"red\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" configured!\"",
")",
"# Get install account_key to /tmp/account_key.pem",
"from",
".",
"letsencrypt",
"import",
"gettempdir",
"if",
"account_key_location",
".",
"startswith",
"(",
"'s3://'",
")",
":",
"bucket",
",",
"key_name",
"=",
"parse_s3_url",
"(",
"account_key_location",
")",
"self",
".",
"zappa",
".",
"s3_client",
".",
"download_file",
"(",
"bucket",
",",
"key_name",
",",
"os",
".",
"path",
".",
"join",
"(",
"gettempdir",
"(",
")",
",",
"'account.key'",
")",
")",
"else",
":",
"from",
"shutil",
"import",
"copyfile",
"copyfile",
"(",
"account_key_location",
",",
"os",
".",
"path",
".",
"join",
"(",
"gettempdir",
"(",
")",
",",
"'account.key'",
")",
")",
"# Prepare for Custom SSL",
"elif",
"not",
"account_key_location",
"and",
"not",
"cert_arn",
":",
"if",
"not",
"cert_location",
"or",
"not",
"cert_key_location",
"or",
"not",
"cert_chain_location",
":",
"raise",
"ClickException",
"(",
"\"Can't certify a domain without \"",
"+",
"click",
".",
"style",
"(",
"\"certificate, certificate_key and certificate_chain\"",
",",
"fg",
"=",
"\"red\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" configured!\"",
")",
"# Read the supplied certificates.",
"with",
"open",
"(",
"cert_location",
")",
"as",
"f",
":",
"certificate_body",
"=",
"f",
".",
"read",
"(",
")",
"with",
"open",
"(",
"cert_key_location",
")",
"as",
"f",
":",
"certificate_private_key",
"=",
"f",
".",
"read",
"(",
")",
"with",
"open",
"(",
"cert_chain_location",
")",
"as",
"f",
":",
"certificate_chain",
"=",
"f",
".",
"read",
"(",
")",
"click",
".",
"echo",
"(",
"\"Certifying domain \"",
"+",
"click",
".",
"style",
"(",
"self",
".",
"domain",
",",
"fg",
"=",
"\"green\"",
",",
"bold",
"=",
"True",
")",
"+",
"\"..\"",
")",
"# Get cert and update domain.",
"# Let's Encrypt",
"if",
"not",
"cert_location",
"and",
"not",
"cert_arn",
":",
"from",
".",
"letsencrypt",
"import",
"get_cert_and_update_domain",
"cert_success",
"=",
"get_cert_and_update_domain",
"(",
"self",
".",
"zappa",
",",
"self",
".",
"lambda_name",
",",
"self",
".",
"api_stage",
",",
"self",
".",
"domain",
",",
"manual",
")",
"# Custom SSL / ACM",
"else",
":",
"route53",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'route53_enabled'",
",",
"True",
")",
"if",
"not",
"self",
".",
"zappa",
".",
"get_domain_name",
"(",
"self",
".",
"domain",
",",
"route53",
"=",
"route53",
")",
":",
"dns_name",
"=",
"self",
".",
"zappa",
".",
"create_domain_name",
"(",
"domain_name",
"=",
"self",
".",
"domain",
",",
"certificate_name",
"=",
"self",
".",
"domain",
"+",
"\"-Zappa-Cert\"",
",",
"certificate_body",
"=",
"certificate_body",
",",
"certificate_private_key",
"=",
"certificate_private_key",
",",
"certificate_chain",
"=",
"certificate_chain",
",",
"certificate_arn",
"=",
"cert_arn",
",",
"lambda_name",
"=",
"self",
".",
"lambda_name",
",",
"stage",
"=",
"self",
".",
"api_stage",
",",
"base_path",
"=",
"base_path",
")",
"if",
"route53",
":",
"self",
".",
"zappa",
".",
"update_route53_records",
"(",
"self",
".",
"domain",
",",
"dns_name",
")",
"print",
"(",
"\"Created a new domain name with supplied certificate. Please note that it can take up to 40 minutes for this domain to be \"",
"\"created and propagated through AWS, but it requires no further work on your part.\"",
")",
"else",
":",
"self",
".",
"zappa",
".",
"update_domain_name",
"(",
"domain_name",
"=",
"self",
".",
"domain",
",",
"certificate_name",
"=",
"self",
".",
"domain",
"+",
"\"-Zappa-Cert\"",
",",
"certificate_body",
"=",
"certificate_body",
",",
"certificate_private_key",
"=",
"certificate_private_key",
",",
"certificate_chain",
"=",
"certificate_chain",
",",
"certificate_arn",
"=",
"cert_arn",
",",
"lambda_name",
"=",
"self",
".",
"lambda_name",
",",
"stage",
"=",
"self",
".",
"api_stage",
",",
"route53",
"=",
"route53",
",",
"base_path",
"=",
"base_path",
")",
"cert_success",
"=",
"True",
"if",
"cert_success",
":",
"click",
".",
"echo",
"(",
"\"Certificate \"",
"+",
"click",
".",
"style",
"(",
"\"updated\"",
",",
"fg",
"=",
"\"green\"",
",",
"bold",
"=",
"True",
")",
"+",
"\"!\"",
")",
"else",
":",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"Failed\"",
",",
"fg",
"=",
"\"red\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" to generate or install certificate! :(\"",
")",
"click",
".",
"echo",
"(",
"\"\\n==============\\n\"",
")",
"shamelessly_promote",
"(",
")"
] | Register or update a domain certificate for this env. | [
"Register",
"or",
"update",
"a",
"domain",
"certificate",
"for",
"this",
"env",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L1799-L1919 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.shell | def shell(self):
"""
Spawn a debug shell.
"""
click.echo(click.style("NOTICE!", fg="yellow", bold=True) + " This is a " + click.style("local", fg="green", bold=True) + " shell, inside a " + click.style("Zappa", bold=True) + " object!")
self.zappa.shell()
return | python | def shell(self):
"""
Spawn a debug shell.
"""
click.echo(click.style("NOTICE!", fg="yellow", bold=True) + " This is a " + click.style("local", fg="green", bold=True) + " shell, inside a " + click.style("Zappa", bold=True) + " object!")
self.zappa.shell()
return | [
"def",
"shell",
"(",
"self",
")",
":",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"NOTICE!\"",
",",
"fg",
"=",
"\"yellow\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" This is a \"",
"+",
"click",
".",
"style",
"(",
"\"local\"",
",",
"fg",
"=",
"\"green\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" shell, inside a \"",
"+",
"click",
".",
"style",
"(",
"\"Zappa\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" object!\"",
")",
"self",
".",
"zappa",
".",
"shell",
"(",
")",
"return"
] | Spawn a debug shell. | [
"Spawn",
"a",
"debug",
"shell",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L1924-L1930 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.callback | def callback(self, position):
"""
Allows the execution of custom code between creation of the zip file and deployment to AWS.
:return: None
"""
callbacks = self.stage_config.get('callbacks', {})
callback = callbacks.get(position)
if callback:
(mod_path, cb_func_name) = callback.rsplit('.', 1)
try: # Prefer callback in working directory
if mod_path.count('.') >= 1: # Callback function is nested in a folder
(mod_folder_path, mod_name) = mod_path.rsplit('.', 1)
mod_folder_path_fragments = mod_folder_path.split('.')
working_dir = os.path.join(os.getcwd(), *mod_folder_path_fragments)
else:
mod_name = mod_path
working_dir = os.getcwd()
working_dir_importer = pkgutil.get_importer(working_dir)
module_ = working_dir_importer.find_module(mod_name).load_module(mod_name)
except (ImportError, AttributeError):
try: # Callback func might be in virtualenv
module_ = importlib.import_module(mod_path)
except ImportError: # pragma: no cover
raise ClickException(click.style("Failed ", fg="red") + 'to ' + click.style(
"import {position} callback ".format(position=position),
bold=True) + 'module: "{mod_path}"'.format(mod_path=click.style(mod_path, bold=True)))
if not hasattr(module_, cb_func_name): # pragma: no cover
raise ClickException(click.style("Failed ", fg="red") + 'to ' + click.style(
"find {position} callback ".format(position=position), bold=True) + 'function: "{cb_func_name}" '.format(
cb_func_name=click.style(cb_func_name, bold=True)) + 'in module "{mod_path}"'.format(mod_path=mod_path))
cb_func = getattr(module_, cb_func_name)
cb_func(self) | python | def callback(self, position):
"""
Allows the execution of custom code between creation of the zip file and deployment to AWS.
:return: None
"""
callbacks = self.stage_config.get('callbacks', {})
callback = callbacks.get(position)
if callback:
(mod_path, cb_func_name) = callback.rsplit('.', 1)
try: # Prefer callback in working directory
if mod_path.count('.') >= 1: # Callback function is nested in a folder
(mod_folder_path, mod_name) = mod_path.rsplit('.', 1)
mod_folder_path_fragments = mod_folder_path.split('.')
working_dir = os.path.join(os.getcwd(), *mod_folder_path_fragments)
else:
mod_name = mod_path
working_dir = os.getcwd()
working_dir_importer = pkgutil.get_importer(working_dir)
module_ = working_dir_importer.find_module(mod_name).load_module(mod_name)
except (ImportError, AttributeError):
try: # Callback func might be in virtualenv
module_ = importlib.import_module(mod_path)
except ImportError: # pragma: no cover
raise ClickException(click.style("Failed ", fg="red") + 'to ' + click.style(
"import {position} callback ".format(position=position),
bold=True) + 'module: "{mod_path}"'.format(mod_path=click.style(mod_path, bold=True)))
if not hasattr(module_, cb_func_name): # pragma: no cover
raise ClickException(click.style("Failed ", fg="red") + 'to ' + click.style(
"find {position} callback ".format(position=position), bold=True) + 'function: "{cb_func_name}" '.format(
cb_func_name=click.style(cb_func_name, bold=True)) + 'in module "{mod_path}"'.format(mod_path=mod_path))
cb_func = getattr(module_, cb_func_name)
cb_func(self) | [
"def",
"callback",
"(",
"self",
",",
"position",
")",
":",
"callbacks",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'callbacks'",
",",
"{",
"}",
")",
"callback",
"=",
"callbacks",
".",
"get",
"(",
"position",
")",
"if",
"callback",
":",
"(",
"mod_path",
",",
"cb_func_name",
")",
"=",
"callback",
".",
"rsplit",
"(",
"'.'",
",",
"1",
")",
"try",
":",
"# Prefer callback in working directory",
"if",
"mod_path",
".",
"count",
"(",
"'.'",
")",
">=",
"1",
":",
"# Callback function is nested in a folder",
"(",
"mod_folder_path",
",",
"mod_name",
")",
"=",
"mod_path",
".",
"rsplit",
"(",
"'.'",
",",
"1",
")",
"mod_folder_path_fragments",
"=",
"mod_folder_path",
".",
"split",
"(",
"'.'",
")",
"working_dir",
"=",
"os",
".",
"path",
".",
"join",
"(",
"os",
".",
"getcwd",
"(",
")",
",",
"*",
"mod_folder_path_fragments",
")",
"else",
":",
"mod_name",
"=",
"mod_path",
"working_dir",
"=",
"os",
".",
"getcwd",
"(",
")",
"working_dir_importer",
"=",
"pkgutil",
".",
"get_importer",
"(",
"working_dir",
")",
"module_",
"=",
"working_dir_importer",
".",
"find_module",
"(",
"mod_name",
")",
".",
"load_module",
"(",
"mod_name",
")",
"except",
"(",
"ImportError",
",",
"AttributeError",
")",
":",
"try",
":",
"# Callback func might be in virtualenv",
"module_",
"=",
"importlib",
".",
"import_module",
"(",
"mod_path",
")",
"except",
"ImportError",
":",
"# pragma: no cover",
"raise",
"ClickException",
"(",
"click",
".",
"style",
"(",
"\"Failed \"",
",",
"fg",
"=",
"\"red\"",
")",
"+",
"'to '",
"+",
"click",
".",
"style",
"(",
"\"import {position} callback \"",
".",
"format",
"(",
"position",
"=",
"position",
")",
",",
"bold",
"=",
"True",
")",
"+",
"'module: \"{mod_path}\"'",
".",
"format",
"(",
"mod_path",
"=",
"click",
".",
"style",
"(",
"mod_path",
",",
"bold",
"=",
"True",
")",
")",
")",
"if",
"not",
"hasattr",
"(",
"module_",
",",
"cb_func_name",
")",
":",
"# pragma: no cover",
"raise",
"ClickException",
"(",
"click",
".",
"style",
"(",
"\"Failed \"",
",",
"fg",
"=",
"\"red\"",
")",
"+",
"'to '",
"+",
"click",
".",
"style",
"(",
"\"find {position} callback \"",
".",
"format",
"(",
"position",
"=",
"position",
")",
",",
"bold",
"=",
"True",
")",
"+",
"'function: \"{cb_func_name}\" '",
".",
"format",
"(",
"cb_func_name",
"=",
"click",
".",
"style",
"(",
"cb_func_name",
",",
"bold",
"=",
"True",
")",
")",
"+",
"'in module \"{mod_path}\"'",
".",
"format",
"(",
"mod_path",
"=",
"mod_path",
")",
")",
"cb_func",
"=",
"getattr",
"(",
"module_",
",",
"cb_func_name",
")",
"cb_func",
"(",
"self",
")"
] | Allows the execution of custom code between creation of the zip file and deployment to AWS.
:return: None | [
"Allows",
"the",
"execution",
"of",
"custom",
"code",
"between",
"creation",
"of",
"the",
"zip",
"file",
"and",
"deployment",
"to",
"AWS",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L1936-L1977 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.check_for_update | def check_for_update(self):
"""
Print a warning if there's a new Zappa version available.
"""
try:
version = pkg_resources.require("zappa")[0].version
updateable = check_new_version_available(version)
if updateable:
click.echo(click.style("Important!", fg="yellow", bold=True) +
" A new version of " + click.style("Zappa", bold=True) + " is available!")
click.echo("Upgrade with: " + click.style("pip install zappa --upgrade", bold=True))
click.echo("Visit the project page on GitHub to see the latest changes: " +
click.style("https://github.com/Miserlou/Zappa", bold=True))
except Exception as e: # pragma: no cover
print(e)
return | python | def check_for_update(self):
"""
Print a warning if there's a new Zappa version available.
"""
try:
version = pkg_resources.require("zappa")[0].version
updateable = check_new_version_available(version)
if updateable:
click.echo(click.style("Important!", fg="yellow", bold=True) +
" A new version of " + click.style("Zappa", bold=True) + " is available!")
click.echo("Upgrade with: " + click.style("pip install zappa --upgrade", bold=True))
click.echo("Visit the project page on GitHub to see the latest changes: " +
click.style("https://github.com/Miserlou/Zappa", bold=True))
except Exception as e: # pragma: no cover
print(e)
return | [
"def",
"check_for_update",
"(",
"self",
")",
":",
"try",
":",
"version",
"=",
"pkg_resources",
".",
"require",
"(",
"\"zappa\"",
")",
"[",
"0",
"]",
".",
"version",
"updateable",
"=",
"check_new_version_available",
"(",
"version",
")",
"if",
"updateable",
":",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"Important!\"",
",",
"fg",
"=",
"\"yellow\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" A new version of \"",
"+",
"click",
".",
"style",
"(",
"\"Zappa\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" is available!\"",
")",
"click",
".",
"echo",
"(",
"\"Upgrade with: \"",
"+",
"click",
".",
"style",
"(",
"\"pip install zappa --upgrade\"",
",",
"bold",
"=",
"True",
")",
")",
"click",
".",
"echo",
"(",
"\"Visit the project page on GitHub to see the latest changes: \"",
"+",
"click",
".",
"style",
"(",
"\"https://github.com/Miserlou/Zappa\"",
",",
"bold",
"=",
"True",
")",
")",
"except",
"Exception",
"as",
"e",
":",
"# pragma: no cover",
"print",
"(",
"e",
")",
"return"
] | Print a warning if there's a new Zappa version available. | [
"Print",
"a",
"warning",
"if",
"there",
"s",
"a",
"new",
"Zappa",
"version",
"available",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L1979-L1994 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.load_settings | def load_settings(self, settings_file=None, session=None):
"""
Load the local zappa_settings file.
An existing boto session can be supplied, though this is likely for testing purposes.
Returns the loaded Zappa object.
"""
# Ensure we're passed a valid settings file.
if not settings_file:
settings_file = self.get_json_or_yaml_settings()
if not os.path.isfile(settings_file):
raise ClickException("Please configure your zappa_settings file.")
# Load up file
self.load_settings_file(settings_file)
# Make sure that the stages are valid names:
for stage_name in self.zappa_settings.keys():
try:
self.check_stage_name(stage_name)
except ValueError:
raise ValueError("API stage names must match a-zA-Z0-9_ ; '{0!s}' does not.".format(stage_name))
# Make sure that this stage is our settings
if self.api_stage not in self.zappa_settings.keys():
raise ClickException("Please define stage '{0!s}' in your Zappa settings.".format(self.api_stage))
# We need a working title for this project. Use one if supplied, else cwd dirname.
if 'project_name' in self.stage_config: # pragma: no cover
# If the name is invalid, this will throw an exception with message up stack
self.project_name = validate_name(self.stage_config['project_name'])
else:
self.project_name = self.get_project_name()
# The name of the actual AWS Lambda function, ex, 'helloworld-dev'
# Assume that we already have have validated the name beforehand.
# Related: https://github.com/Miserlou/Zappa/pull/664
# https://github.com/Miserlou/Zappa/issues/678
# And various others from Slack.
self.lambda_name = slugify.slugify(self.project_name + '-' + self.api_stage)
# Load stage-specific settings
self.s3_bucket_name = self.stage_config.get('s3_bucket', "zappa-" + ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(9)))
self.vpc_config = self.stage_config.get('vpc_config', {})
self.memory_size = self.stage_config.get('memory_size', 512)
self.app_function = self.stage_config.get('app_function', None)
self.exception_handler = self.stage_config.get('exception_handler', None)
self.aws_region = self.stage_config.get('aws_region', None)
self.debug = self.stage_config.get('debug', True)
self.prebuild_script = self.stage_config.get('prebuild_script', None)
self.profile_name = self.stage_config.get('profile_name', None)
self.log_level = self.stage_config.get('log_level', "DEBUG")
self.domain = self.stage_config.get('domain', None)
self.base_path = self.stage_config.get('base_path', None)
self.timeout_seconds = self.stage_config.get('timeout_seconds', 30)
dead_letter_arn = self.stage_config.get('dead_letter_arn', '')
self.dead_letter_config = {'TargetArn': dead_letter_arn} if dead_letter_arn else {}
self.cognito = self.stage_config.get('cognito', None)
self.num_retained_versions = self.stage_config.get('num_retained_versions',None)
# Check for valid values of num_retained_versions
if self.num_retained_versions is not None and type(self.num_retained_versions) is not int:
raise ClickException("Please supply either an integer or null for num_retained_versions in the zappa_settings.json. Found %s" % type(self.num_retained_versions))
elif type(self.num_retained_versions) is int and self.num_retained_versions<1:
raise ClickException("The value for num_retained_versions in the zappa_settings.json should be greater than 0.")
# Provide legacy support for `use_apigateway`, now `apigateway_enabled`.
# https://github.com/Miserlou/Zappa/issues/490
# https://github.com/Miserlou/Zappa/issues/493
self.use_apigateway = self.stage_config.get('use_apigateway', True)
if self.use_apigateway:
self.use_apigateway = self.stage_config.get('apigateway_enabled', True)
self.apigateway_description = self.stage_config.get('apigateway_description', None)
self.lambda_handler = self.stage_config.get('lambda_handler', 'handler.lambda_handler')
# DEPRECATED. https://github.com/Miserlou/Zappa/issues/456
self.remote_env_bucket = self.stage_config.get('remote_env_bucket', None)
self.remote_env_file = self.stage_config.get('remote_env_file', None)
self.remote_env = self.stage_config.get('remote_env', None)
self.settings_file = self.stage_config.get('settings_file', None)
self.django_settings = self.stage_config.get('django_settings', None)
self.manage_roles = self.stage_config.get('manage_roles', True)
self.binary_support = self.stage_config.get('binary_support', True)
self.api_key_required = self.stage_config.get('api_key_required', False)
self.api_key = self.stage_config.get('api_key')
self.endpoint_configuration = self.stage_config.get('endpoint_configuration', None)
self.iam_authorization = self.stage_config.get('iam_authorization', False)
self.cors = self.stage_config.get("cors", False)
self.lambda_description = self.stage_config.get('lambda_description', "Zappa Deployment")
self.environment_variables = self.stage_config.get('environment_variables', {})
self.aws_environment_variables = self.stage_config.get('aws_environment_variables', {})
self.check_environment(self.environment_variables)
self.authorizer = self.stage_config.get('authorizer', {})
self.runtime = self.stage_config.get('runtime', get_runtime_from_python_version())
self.aws_kms_key_arn = self.stage_config.get('aws_kms_key_arn', '')
self.context_header_mappings = self.stage_config.get('context_header_mappings', {})
self.xray_tracing = self.stage_config.get('xray_tracing', False)
self.desired_role_arn = self.stage_config.get('role_arn')
# Load ALB-related settings
self.use_alb = self.stage_config.get('alb_enabled', False)
self.alb_vpc_config = self.stage_config.get('alb_vpc_config', {})
# Additional tags
self.tags = self.stage_config.get('tags', {})
desired_role_name = self.lambda_name + "-ZappaLambdaExecutionRole"
self.zappa = Zappa( boto_session=session,
profile_name=self.profile_name,
aws_region=self.aws_region,
load_credentials=self.load_credentials,
desired_role_name=desired_role_name,
desired_role_arn=self.desired_role_arn,
runtime=self.runtime,
tags=self.tags,
endpoint_urls=self.stage_config.get('aws_endpoint_urls',{}),
xray_tracing=self.xray_tracing
)
for setting in CUSTOM_SETTINGS:
if setting in self.stage_config:
setting_val = self.stage_config[setting]
# Read the policy file contents.
if setting.endswith('policy'):
with open(setting_val, 'r') as f:
setting_val = f.read()
setattr(self.zappa, setting, setting_val)
if self.app_function:
self.collision_warning(self.app_function)
if self.app_function[-3:] == '.py':
click.echo(click.style("Warning!", fg="red", bold=True) +
" Your app_function is pointing to a " + click.style("file and not a function", bold=True) +
"! It should probably be something like 'my_file.app', not 'my_file.py'!")
return self.zappa | python | def load_settings(self, settings_file=None, session=None):
"""
Load the local zappa_settings file.
An existing boto session can be supplied, though this is likely for testing purposes.
Returns the loaded Zappa object.
"""
# Ensure we're passed a valid settings file.
if not settings_file:
settings_file = self.get_json_or_yaml_settings()
if not os.path.isfile(settings_file):
raise ClickException("Please configure your zappa_settings file.")
# Load up file
self.load_settings_file(settings_file)
# Make sure that the stages are valid names:
for stage_name in self.zappa_settings.keys():
try:
self.check_stage_name(stage_name)
except ValueError:
raise ValueError("API stage names must match a-zA-Z0-9_ ; '{0!s}' does not.".format(stage_name))
# Make sure that this stage is our settings
if self.api_stage not in self.zappa_settings.keys():
raise ClickException("Please define stage '{0!s}' in your Zappa settings.".format(self.api_stage))
# We need a working title for this project. Use one if supplied, else cwd dirname.
if 'project_name' in self.stage_config: # pragma: no cover
# If the name is invalid, this will throw an exception with message up stack
self.project_name = validate_name(self.stage_config['project_name'])
else:
self.project_name = self.get_project_name()
# The name of the actual AWS Lambda function, ex, 'helloworld-dev'
# Assume that we already have have validated the name beforehand.
# Related: https://github.com/Miserlou/Zappa/pull/664
# https://github.com/Miserlou/Zappa/issues/678
# And various others from Slack.
self.lambda_name = slugify.slugify(self.project_name + '-' + self.api_stage)
# Load stage-specific settings
self.s3_bucket_name = self.stage_config.get('s3_bucket', "zappa-" + ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(9)))
self.vpc_config = self.stage_config.get('vpc_config', {})
self.memory_size = self.stage_config.get('memory_size', 512)
self.app_function = self.stage_config.get('app_function', None)
self.exception_handler = self.stage_config.get('exception_handler', None)
self.aws_region = self.stage_config.get('aws_region', None)
self.debug = self.stage_config.get('debug', True)
self.prebuild_script = self.stage_config.get('prebuild_script', None)
self.profile_name = self.stage_config.get('profile_name', None)
self.log_level = self.stage_config.get('log_level', "DEBUG")
self.domain = self.stage_config.get('domain', None)
self.base_path = self.stage_config.get('base_path', None)
self.timeout_seconds = self.stage_config.get('timeout_seconds', 30)
dead_letter_arn = self.stage_config.get('dead_letter_arn', '')
self.dead_letter_config = {'TargetArn': dead_letter_arn} if dead_letter_arn else {}
self.cognito = self.stage_config.get('cognito', None)
self.num_retained_versions = self.stage_config.get('num_retained_versions',None)
# Check for valid values of num_retained_versions
if self.num_retained_versions is not None and type(self.num_retained_versions) is not int:
raise ClickException("Please supply either an integer or null for num_retained_versions in the zappa_settings.json. Found %s" % type(self.num_retained_versions))
elif type(self.num_retained_versions) is int and self.num_retained_versions<1:
raise ClickException("The value for num_retained_versions in the zappa_settings.json should be greater than 0.")
# Provide legacy support for `use_apigateway`, now `apigateway_enabled`.
# https://github.com/Miserlou/Zappa/issues/490
# https://github.com/Miserlou/Zappa/issues/493
self.use_apigateway = self.stage_config.get('use_apigateway', True)
if self.use_apigateway:
self.use_apigateway = self.stage_config.get('apigateway_enabled', True)
self.apigateway_description = self.stage_config.get('apigateway_description', None)
self.lambda_handler = self.stage_config.get('lambda_handler', 'handler.lambda_handler')
# DEPRECATED. https://github.com/Miserlou/Zappa/issues/456
self.remote_env_bucket = self.stage_config.get('remote_env_bucket', None)
self.remote_env_file = self.stage_config.get('remote_env_file', None)
self.remote_env = self.stage_config.get('remote_env', None)
self.settings_file = self.stage_config.get('settings_file', None)
self.django_settings = self.stage_config.get('django_settings', None)
self.manage_roles = self.stage_config.get('manage_roles', True)
self.binary_support = self.stage_config.get('binary_support', True)
self.api_key_required = self.stage_config.get('api_key_required', False)
self.api_key = self.stage_config.get('api_key')
self.endpoint_configuration = self.stage_config.get('endpoint_configuration', None)
self.iam_authorization = self.stage_config.get('iam_authorization', False)
self.cors = self.stage_config.get("cors", False)
self.lambda_description = self.stage_config.get('lambda_description', "Zappa Deployment")
self.environment_variables = self.stage_config.get('environment_variables', {})
self.aws_environment_variables = self.stage_config.get('aws_environment_variables', {})
self.check_environment(self.environment_variables)
self.authorizer = self.stage_config.get('authorizer', {})
self.runtime = self.stage_config.get('runtime', get_runtime_from_python_version())
self.aws_kms_key_arn = self.stage_config.get('aws_kms_key_arn', '')
self.context_header_mappings = self.stage_config.get('context_header_mappings', {})
self.xray_tracing = self.stage_config.get('xray_tracing', False)
self.desired_role_arn = self.stage_config.get('role_arn')
# Load ALB-related settings
self.use_alb = self.stage_config.get('alb_enabled', False)
self.alb_vpc_config = self.stage_config.get('alb_vpc_config', {})
# Additional tags
self.tags = self.stage_config.get('tags', {})
desired_role_name = self.lambda_name + "-ZappaLambdaExecutionRole"
self.zappa = Zappa( boto_session=session,
profile_name=self.profile_name,
aws_region=self.aws_region,
load_credentials=self.load_credentials,
desired_role_name=desired_role_name,
desired_role_arn=self.desired_role_arn,
runtime=self.runtime,
tags=self.tags,
endpoint_urls=self.stage_config.get('aws_endpoint_urls',{}),
xray_tracing=self.xray_tracing
)
for setting in CUSTOM_SETTINGS:
if setting in self.stage_config:
setting_val = self.stage_config[setting]
# Read the policy file contents.
if setting.endswith('policy'):
with open(setting_val, 'r') as f:
setting_val = f.read()
setattr(self.zappa, setting, setting_val)
if self.app_function:
self.collision_warning(self.app_function)
if self.app_function[-3:] == '.py':
click.echo(click.style("Warning!", fg="red", bold=True) +
" Your app_function is pointing to a " + click.style("file and not a function", bold=True) +
"! It should probably be something like 'my_file.app', not 'my_file.py'!")
return self.zappa | [
"def",
"load_settings",
"(",
"self",
",",
"settings_file",
"=",
"None",
",",
"session",
"=",
"None",
")",
":",
"# Ensure we're passed a valid settings file.",
"if",
"not",
"settings_file",
":",
"settings_file",
"=",
"self",
".",
"get_json_or_yaml_settings",
"(",
")",
"if",
"not",
"os",
".",
"path",
".",
"isfile",
"(",
"settings_file",
")",
":",
"raise",
"ClickException",
"(",
"\"Please configure your zappa_settings file.\"",
")",
"# Load up file",
"self",
".",
"load_settings_file",
"(",
"settings_file",
")",
"# Make sure that the stages are valid names:",
"for",
"stage_name",
"in",
"self",
".",
"zappa_settings",
".",
"keys",
"(",
")",
":",
"try",
":",
"self",
".",
"check_stage_name",
"(",
"stage_name",
")",
"except",
"ValueError",
":",
"raise",
"ValueError",
"(",
"\"API stage names must match a-zA-Z0-9_ ; '{0!s}' does not.\"",
".",
"format",
"(",
"stage_name",
")",
")",
"# Make sure that this stage is our settings",
"if",
"self",
".",
"api_stage",
"not",
"in",
"self",
".",
"zappa_settings",
".",
"keys",
"(",
")",
":",
"raise",
"ClickException",
"(",
"\"Please define stage '{0!s}' in your Zappa settings.\"",
".",
"format",
"(",
"self",
".",
"api_stage",
")",
")",
"# We need a working title for this project. Use one if supplied, else cwd dirname.",
"if",
"'project_name'",
"in",
"self",
".",
"stage_config",
":",
"# pragma: no cover",
"# If the name is invalid, this will throw an exception with message up stack",
"self",
".",
"project_name",
"=",
"validate_name",
"(",
"self",
".",
"stage_config",
"[",
"'project_name'",
"]",
")",
"else",
":",
"self",
".",
"project_name",
"=",
"self",
".",
"get_project_name",
"(",
")",
"# The name of the actual AWS Lambda function, ex, 'helloworld-dev'",
"# Assume that we already have have validated the name beforehand.",
"# Related: https://github.com/Miserlou/Zappa/pull/664",
"# https://github.com/Miserlou/Zappa/issues/678",
"# And various others from Slack.",
"self",
".",
"lambda_name",
"=",
"slugify",
".",
"slugify",
"(",
"self",
".",
"project_name",
"+",
"'-'",
"+",
"self",
".",
"api_stage",
")",
"# Load stage-specific settings",
"self",
".",
"s3_bucket_name",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'s3_bucket'",
",",
"\"zappa-\"",
"+",
"''",
".",
"join",
"(",
"random",
".",
"choice",
"(",
"string",
".",
"ascii_lowercase",
"+",
"string",
".",
"digits",
")",
"for",
"_",
"in",
"range",
"(",
"9",
")",
")",
")",
"self",
".",
"vpc_config",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'vpc_config'",
",",
"{",
"}",
")",
"self",
".",
"memory_size",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'memory_size'",
",",
"512",
")",
"self",
".",
"app_function",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'app_function'",
",",
"None",
")",
"self",
".",
"exception_handler",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'exception_handler'",
",",
"None",
")",
"self",
".",
"aws_region",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'aws_region'",
",",
"None",
")",
"self",
".",
"debug",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'debug'",
",",
"True",
")",
"self",
".",
"prebuild_script",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'prebuild_script'",
",",
"None",
")",
"self",
".",
"profile_name",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'profile_name'",
",",
"None",
")",
"self",
".",
"log_level",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'log_level'",
",",
"\"DEBUG\"",
")",
"self",
".",
"domain",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'domain'",
",",
"None",
")",
"self",
".",
"base_path",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'base_path'",
",",
"None",
")",
"self",
".",
"timeout_seconds",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'timeout_seconds'",
",",
"30",
")",
"dead_letter_arn",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'dead_letter_arn'",
",",
"''",
")",
"self",
".",
"dead_letter_config",
"=",
"{",
"'TargetArn'",
":",
"dead_letter_arn",
"}",
"if",
"dead_letter_arn",
"else",
"{",
"}",
"self",
".",
"cognito",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'cognito'",
",",
"None",
")",
"self",
".",
"num_retained_versions",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'num_retained_versions'",
",",
"None",
")",
"# Check for valid values of num_retained_versions",
"if",
"self",
".",
"num_retained_versions",
"is",
"not",
"None",
"and",
"type",
"(",
"self",
".",
"num_retained_versions",
")",
"is",
"not",
"int",
":",
"raise",
"ClickException",
"(",
"\"Please supply either an integer or null for num_retained_versions in the zappa_settings.json. Found %s\"",
"%",
"type",
"(",
"self",
".",
"num_retained_versions",
")",
")",
"elif",
"type",
"(",
"self",
".",
"num_retained_versions",
")",
"is",
"int",
"and",
"self",
".",
"num_retained_versions",
"<",
"1",
":",
"raise",
"ClickException",
"(",
"\"The value for num_retained_versions in the zappa_settings.json should be greater than 0.\"",
")",
"# Provide legacy support for `use_apigateway`, now `apigateway_enabled`.",
"# https://github.com/Miserlou/Zappa/issues/490",
"# https://github.com/Miserlou/Zappa/issues/493",
"self",
".",
"use_apigateway",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'use_apigateway'",
",",
"True",
")",
"if",
"self",
".",
"use_apigateway",
":",
"self",
".",
"use_apigateway",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'apigateway_enabled'",
",",
"True",
")",
"self",
".",
"apigateway_description",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'apigateway_description'",
",",
"None",
")",
"self",
".",
"lambda_handler",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'lambda_handler'",
",",
"'handler.lambda_handler'",
")",
"# DEPRECATED. https://github.com/Miserlou/Zappa/issues/456",
"self",
".",
"remote_env_bucket",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'remote_env_bucket'",
",",
"None",
")",
"self",
".",
"remote_env_file",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'remote_env_file'",
",",
"None",
")",
"self",
".",
"remote_env",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'remote_env'",
",",
"None",
")",
"self",
".",
"settings_file",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'settings_file'",
",",
"None",
")",
"self",
".",
"django_settings",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'django_settings'",
",",
"None",
")",
"self",
".",
"manage_roles",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'manage_roles'",
",",
"True",
")",
"self",
".",
"binary_support",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'binary_support'",
",",
"True",
")",
"self",
".",
"api_key_required",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'api_key_required'",
",",
"False",
")",
"self",
".",
"api_key",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'api_key'",
")",
"self",
".",
"endpoint_configuration",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'endpoint_configuration'",
",",
"None",
")",
"self",
".",
"iam_authorization",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'iam_authorization'",
",",
"False",
")",
"self",
".",
"cors",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"\"cors\"",
",",
"False",
")",
"self",
".",
"lambda_description",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'lambda_description'",
",",
"\"Zappa Deployment\"",
")",
"self",
".",
"environment_variables",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'environment_variables'",
",",
"{",
"}",
")",
"self",
".",
"aws_environment_variables",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'aws_environment_variables'",
",",
"{",
"}",
")",
"self",
".",
"check_environment",
"(",
"self",
".",
"environment_variables",
")",
"self",
".",
"authorizer",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'authorizer'",
",",
"{",
"}",
")",
"self",
".",
"runtime",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'runtime'",
",",
"get_runtime_from_python_version",
"(",
")",
")",
"self",
".",
"aws_kms_key_arn",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'aws_kms_key_arn'",
",",
"''",
")",
"self",
".",
"context_header_mappings",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'context_header_mappings'",
",",
"{",
"}",
")",
"self",
".",
"xray_tracing",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'xray_tracing'",
",",
"False",
")",
"self",
".",
"desired_role_arn",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'role_arn'",
")",
"# Load ALB-related settings",
"self",
".",
"use_alb",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'alb_enabled'",
",",
"False",
")",
"self",
".",
"alb_vpc_config",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'alb_vpc_config'",
",",
"{",
"}",
")",
"# Additional tags",
"self",
".",
"tags",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'tags'",
",",
"{",
"}",
")",
"desired_role_name",
"=",
"self",
".",
"lambda_name",
"+",
"\"-ZappaLambdaExecutionRole\"",
"self",
".",
"zappa",
"=",
"Zappa",
"(",
"boto_session",
"=",
"session",
",",
"profile_name",
"=",
"self",
".",
"profile_name",
",",
"aws_region",
"=",
"self",
".",
"aws_region",
",",
"load_credentials",
"=",
"self",
".",
"load_credentials",
",",
"desired_role_name",
"=",
"desired_role_name",
",",
"desired_role_arn",
"=",
"self",
".",
"desired_role_arn",
",",
"runtime",
"=",
"self",
".",
"runtime",
",",
"tags",
"=",
"self",
".",
"tags",
",",
"endpoint_urls",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'aws_endpoint_urls'",
",",
"{",
"}",
")",
",",
"xray_tracing",
"=",
"self",
".",
"xray_tracing",
")",
"for",
"setting",
"in",
"CUSTOM_SETTINGS",
":",
"if",
"setting",
"in",
"self",
".",
"stage_config",
":",
"setting_val",
"=",
"self",
".",
"stage_config",
"[",
"setting",
"]",
"# Read the policy file contents.",
"if",
"setting",
".",
"endswith",
"(",
"'policy'",
")",
":",
"with",
"open",
"(",
"setting_val",
",",
"'r'",
")",
"as",
"f",
":",
"setting_val",
"=",
"f",
".",
"read",
"(",
")",
"setattr",
"(",
"self",
".",
"zappa",
",",
"setting",
",",
"setting_val",
")",
"if",
"self",
".",
"app_function",
":",
"self",
".",
"collision_warning",
"(",
"self",
".",
"app_function",
")",
"if",
"self",
".",
"app_function",
"[",
"-",
"3",
":",
"]",
"==",
"'.py'",
":",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"Warning!\"",
",",
"fg",
"=",
"\"red\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" Your app_function is pointing to a \"",
"+",
"click",
".",
"style",
"(",
"\"file and not a function\"",
",",
"bold",
"=",
"True",
")",
"+",
"\"! It should probably be something like 'my_file.app', not 'my_file.py'!\"",
")",
"return",
"self",
".",
"zappa"
] | Load the local zappa_settings file.
An existing boto session can be supplied, though this is likely for testing purposes.
Returns the loaded Zappa object. | [
"Load",
"the",
"local",
"zappa_settings",
"file",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L1996-L2133 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.get_json_or_yaml_settings | def get_json_or_yaml_settings(self, settings_name="zappa_settings"):
"""
Return zappa_settings path as JSON or YAML (or TOML), as appropriate.
"""
zs_json = settings_name + ".json"
zs_yml = settings_name + ".yml"
zs_yaml = settings_name + ".yaml"
zs_toml = settings_name + ".toml"
# Must have at least one
if not os.path.isfile(zs_json) \
and not os.path.isfile(zs_yml) \
and not os.path.isfile(zs_yaml) \
and not os.path.isfile(zs_toml):
raise ClickException("Please configure a zappa_settings file or call `zappa init`.")
# Prefer JSON
if os.path.isfile(zs_json):
settings_file = zs_json
elif os.path.isfile(zs_toml):
settings_file = zs_toml
elif os.path.isfile(zs_yml):
settings_file = zs_yml
else:
settings_file = zs_yaml
return settings_file | python | def get_json_or_yaml_settings(self, settings_name="zappa_settings"):
"""
Return zappa_settings path as JSON or YAML (or TOML), as appropriate.
"""
zs_json = settings_name + ".json"
zs_yml = settings_name + ".yml"
zs_yaml = settings_name + ".yaml"
zs_toml = settings_name + ".toml"
# Must have at least one
if not os.path.isfile(zs_json) \
and not os.path.isfile(zs_yml) \
and not os.path.isfile(zs_yaml) \
and not os.path.isfile(zs_toml):
raise ClickException("Please configure a zappa_settings file or call `zappa init`.")
# Prefer JSON
if os.path.isfile(zs_json):
settings_file = zs_json
elif os.path.isfile(zs_toml):
settings_file = zs_toml
elif os.path.isfile(zs_yml):
settings_file = zs_yml
else:
settings_file = zs_yaml
return settings_file | [
"def",
"get_json_or_yaml_settings",
"(",
"self",
",",
"settings_name",
"=",
"\"zappa_settings\"",
")",
":",
"zs_json",
"=",
"settings_name",
"+",
"\".json\"",
"zs_yml",
"=",
"settings_name",
"+",
"\".yml\"",
"zs_yaml",
"=",
"settings_name",
"+",
"\".yaml\"",
"zs_toml",
"=",
"settings_name",
"+",
"\".toml\"",
"# Must have at least one",
"if",
"not",
"os",
".",
"path",
".",
"isfile",
"(",
"zs_json",
")",
"and",
"not",
"os",
".",
"path",
".",
"isfile",
"(",
"zs_yml",
")",
"and",
"not",
"os",
".",
"path",
".",
"isfile",
"(",
"zs_yaml",
")",
"and",
"not",
"os",
".",
"path",
".",
"isfile",
"(",
"zs_toml",
")",
":",
"raise",
"ClickException",
"(",
"\"Please configure a zappa_settings file or call `zappa init`.\"",
")",
"# Prefer JSON",
"if",
"os",
".",
"path",
".",
"isfile",
"(",
"zs_json",
")",
":",
"settings_file",
"=",
"zs_json",
"elif",
"os",
".",
"path",
".",
"isfile",
"(",
"zs_toml",
")",
":",
"settings_file",
"=",
"zs_toml",
"elif",
"os",
".",
"path",
".",
"isfile",
"(",
"zs_yml",
")",
":",
"settings_file",
"=",
"zs_yml",
"else",
":",
"settings_file",
"=",
"zs_yaml",
"return",
"settings_file"
] | Return zappa_settings path as JSON or YAML (or TOML), as appropriate. | [
"Return",
"zappa_settings",
"path",
"as",
"JSON",
"or",
"YAML",
"(",
"or",
"TOML",
")",
"as",
"appropriate",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L2135-L2161 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.load_settings_file | def load_settings_file(self, settings_file=None):
"""
Load our settings file.
"""
if not settings_file:
settings_file = self.get_json_or_yaml_settings()
if not os.path.isfile(settings_file):
raise ClickException("Please configure your zappa_settings file or call `zappa init`.")
path, ext = os.path.splitext(settings_file)
if ext == '.yml' or ext == '.yaml':
with open(settings_file) as yaml_file:
try:
self.zappa_settings = yaml.load(yaml_file)
except ValueError: # pragma: no cover
raise ValueError("Unable to load the Zappa settings YAML. It may be malformed.")
elif ext == '.toml':
with open(settings_file) as toml_file:
try:
self.zappa_settings = toml.load(toml_file)
except ValueError: # pragma: no cover
raise ValueError("Unable to load the Zappa settings TOML. It may be malformed.")
else:
with open(settings_file) as json_file:
try:
self.zappa_settings = json.load(json_file)
except ValueError: # pragma: no cover
raise ValueError("Unable to load the Zappa settings JSON. It may be malformed.") | python | def load_settings_file(self, settings_file=None):
"""
Load our settings file.
"""
if not settings_file:
settings_file = self.get_json_or_yaml_settings()
if not os.path.isfile(settings_file):
raise ClickException("Please configure your zappa_settings file or call `zappa init`.")
path, ext = os.path.splitext(settings_file)
if ext == '.yml' or ext == '.yaml':
with open(settings_file) as yaml_file:
try:
self.zappa_settings = yaml.load(yaml_file)
except ValueError: # pragma: no cover
raise ValueError("Unable to load the Zappa settings YAML. It may be malformed.")
elif ext == '.toml':
with open(settings_file) as toml_file:
try:
self.zappa_settings = toml.load(toml_file)
except ValueError: # pragma: no cover
raise ValueError("Unable to load the Zappa settings TOML. It may be malformed.")
else:
with open(settings_file) as json_file:
try:
self.zappa_settings = json.load(json_file)
except ValueError: # pragma: no cover
raise ValueError("Unable to load the Zappa settings JSON. It may be malformed.") | [
"def",
"load_settings_file",
"(",
"self",
",",
"settings_file",
"=",
"None",
")",
":",
"if",
"not",
"settings_file",
":",
"settings_file",
"=",
"self",
".",
"get_json_or_yaml_settings",
"(",
")",
"if",
"not",
"os",
".",
"path",
".",
"isfile",
"(",
"settings_file",
")",
":",
"raise",
"ClickException",
"(",
"\"Please configure your zappa_settings file or call `zappa init`.\"",
")",
"path",
",",
"ext",
"=",
"os",
".",
"path",
".",
"splitext",
"(",
"settings_file",
")",
"if",
"ext",
"==",
"'.yml'",
"or",
"ext",
"==",
"'.yaml'",
":",
"with",
"open",
"(",
"settings_file",
")",
"as",
"yaml_file",
":",
"try",
":",
"self",
".",
"zappa_settings",
"=",
"yaml",
".",
"load",
"(",
"yaml_file",
")",
"except",
"ValueError",
":",
"# pragma: no cover",
"raise",
"ValueError",
"(",
"\"Unable to load the Zappa settings YAML. It may be malformed.\"",
")",
"elif",
"ext",
"==",
"'.toml'",
":",
"with",
"open",
"(",
"settings_file",
")",
"as",
"toml_file",
":",
"try",
":",
"self",
".",
"zappa_settings",
"=",
"toml",
".",
"load",
"(",
"toml_file",
")",
"except",
"ValueError",
":",
"# pragma: no cover",
"raise",
"ValueError",
"(",
"\"Unable to load the Zappa settings TOML. It may be malformed.\"",
")",
"else",
":",
"with",
"open",
"(",
"settings_file",
")",
"as",
"json_file",
":",
"try",
":",
"self",
".",
"zappa_settings",
"=",
"json",
".",
"load",
"(",
"json_file",
")",
"except",
"ValueError",
":",
"# pragma: no cover",
"raise",
"ValueError",
"(",
"\"Unable to load the Zappa settings JSON. It may be malformed.\"",
")"
] | Load our settings file. | [
"Load",
"our",
"settings",
"file",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L2163-L2191 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.create_package | def create_package(self, output=None):
"""
Ensure that the package can be properly configured,
and then create it.
"""
# Create the Lambda zip package (includes project and virtualenvironment)
# Also define the path the handler file so it can be copied to the zip
# root for Lambda.
current_file = os.path.dirname(os.path.abspath(
inspect.getfile(inspect.currentframe())))
handler_file = os.sep.join(current_file.split(os.sep)[0:]) + os.sep + 'handler.py'
# Create the zip file(s)
if self.stage_config.get('slim_handler', False):
# Create two zips. One with the application and the other with just the handler.
# https://github.com/Miserlou/Zappa/issues/510
self.zip_path = self.zappa.create_lambda_zip(
prefix=self.lambda_name,
use_precompiled_packages=self.stage_config.get('use_precompiled_packages', True),
exclude=self.stage_config.get('exclude', []),
disable_progress=self.disable_progress,
archive_format='tarball'
)
# Make sure the normal venv is not included in the handler's zip
exclude = self.stage_config.get('exclude', [])
cur_venv = self.zappa.get_current_venv()
exclude.append(cur_venv.split('/')[-1])
self.handler_path = self.zappa.create_lambda_zip(
prefix='handler_{0!s}'.format(self.lambda_name),
venv=self.zappa.create_handler_venv(),
handler_file=handler_file,
slim_handler=True,
exclude=exclude,
output=output,
disable_progress=self.disable_progress
)
else:
# Custom excludes for different versions.
# Related: https://github.com/kennethreitz/requests/issues/3985
if sys.version_info[0] < 3:
# Exclude packages already builtin to the python lambda environment
# Related: https://github.com/Miserlou/Zappa/issues/556
exclude = self.stage_config.get(
'exclude', [
"boto3",
"dateutil",
"botocore",
"s3transfer",
"six.py",
"jmespath",
"concurrent"
])
else:
# This could be python3.6 optimized.
exclude = self.stage_config.get(
'exclude', [
"boto3",
"dateutil",
"botocore",
"s3transfer",
"concurrent"
])
# Create a single zip that has the handler and application
self.zip_path = self.zappa.create_lambda_zip(
prefix=self.lambda_name,
handler_file=handler_file,
use_precompiled_packages=self.stage_config.get('use_precompiled_packages', True),
exclude=exclude,
output=output,
disable_progress=self.disable_progress
)
# Warn if this is too large for Lambda.
file_stats = os.stat(self.zip_path)
if file_stats.st_size > 52428800: # pragma: no cover
print('\n\nWarning: Application zip package is likely to be too large for AWS Lambda. '
'Try setting "slim_handler" to true in your Zappa settings file.\n\n')
# Throw custom settings into the zip that handles requests
if self.stage_config.get('slim_handler', False):
handler_zip = self.handler_path
else:
handler_zip = self.zip_path
with zipfile.ZipFile(handler_zip, 'a') as lambda_zip:
settings_s = "# Generated by Zappa\n"
if self.app_function:
if '.' not in self.app_function: # pragma: no cover
raise ClickException("Your " + click.style("app_function", fg='red', bold=True) + " value is not a modular path." +
" It needs to be in the format `" + click.style("your_module.your_app_object", bold=True) + "`.")
app_module, app_function = self.app_function.rsplit('.', 1)
settings_s = settings_s + "APP_MODULE='{0!s}'\nAPP_FUNCTION='{1!s}'\n".format(app_module, app_function)
if self.exception_handler:
settings_s += "EXCEPTION_HANDLER='{0!s}'\n".format(self.exception_handler)
else:
settings_s += "EXCEPTION_HANDLER=None\n"
if self.debug:
settings_s = settings_s + "DEBUG=True\n"
else:
settings_s = settings_s + "DEBUG=False\n"
settings_s = settings_s + "LOG_LEVEL='{0!s}'\n".format((self.log_level))
if self.binary_support:
settings_s = settings_s + "BINARY_SUPPORT=True\n"
else:
settings_s = settings_s + "BINARY_SUPPORT=False\n"
head_map_dict = {}
head_map_dict.update(dict(self.context_header_mappings))
settings_s = settings_s + "CONTEXT_HEADER_MAPPINGS={0}\n".format(
head_map_dict
)
# If we're on a domain, we don't need to define the /<<env>> in
# the WSGI PATH
if self.domain:
settings_s = settings_s + "DOMAIN='{0!s}'\n".format((self.domain))
else:
settings_s = settings_s + "DOMAIN=None\n"
if self.base_path:
settings_s = settings_s + "BASE_PATH='{0!s}'\n".format((self.base_path))
else:
settings_s = settings_s + "BASE_PATH=None\n"
# Pass through remote config bucket and path
if self.remote_env:
settings_s = settings_s + "REMOTE_ENV='{0!s}'\n".format(
self.remote_env
)
# DEPRECATED. use remove_env instead
elif self.remote_env_bucket and self.remote_env_file:
settings_s = settings_s + "REMOTE_ENV='s3://{0!s}/{1!s}'\n".format(
self.remote_env_bucket, self.remote_env_file
)
# Local envs
env_dict = {}
if self.aws_region:
env_dict['AWS_REGION'] = self.aws_region
env_dict.update(dict(self.environment_variables))
# Environment variable keys must be ascii
# https://github.com/Miserlou/Zappa/issues/604
# https://github.com/Miserlou/Zappa/issues/998
try:
env_dict = dict((k.encode('ascii').decode('ascii'), v) for (k, v) in env_dict.items())
except Exception:
raise ValueError("Environment variable keys must be ascii.")
settings_s = settings_s + "ENVIRONMENT_VARIABLES={0}\n".format(
env_dict
)
# We can be environment-aware
settings_s = settings_s + "API_STAGE='{0!s}'\n".format((self.api_stage))
settings_s = settings_s + "PROJECT_NAME='{0!s}'\n".format((self.project_name))
if self.settings_file:
settings_s = settings_s + "SETTINGS_FILE='{0!s}'\n".format((self.settings_file))
else:
settings_s = settings_s + "SETTINGS_FILE=None\n"
if self.django_settings:
settings_s = settings_s + "DJANGO_SETTINGS='{0!s}'\n".format((self.django_settings))
else:
settings_s = settings_s + "DJANGO_SETTINGS=None\n"
# If slim handler, path to project zip
if self.stage_config.get('slim_handler', False):
settings_s += "ARCHIVE_PATH='s3://{0!s}/{1!s}_{2!s}_current_project.tar.gz'\n".format(
self.s3_bucket_name, self.api_stage, self.project_name)
# since includes are for slim handler add the setting here by joining arbitrary list from zappa_settings file
# and tell the handler we are the slim_handler
# https://github.com/Miserlou/Zappa/issues/776
settings_s += "SLIM_HANDLER=True\n"
include = self.stage_config.get('include', [])
if len(include) >= 1:
settings_s += "INCLUDE=" + str(include) + '\n'
# AWS Events function mapping
event_mapping = {}
events = self.stage_config.get('events', [])
for event in events:
arn = event.get('event_source', {}).get('arn')
function = event.get('function')
if arn and function:
event_mapping[arn] = function
settings_s = settings_s + "AWS_EVENT_MAPPING={0!s}\n".format(event_mapping)
# Map Lext bot events
bot_events = self.stage_config.get('bot_events', [])
bot_events_mapping = {}
for bot_event in bot_events:
event_source = bot_event.get('event_source', {})
intent = event_source.get('intent')
invocation_source = event_source.get('invocation_source')
function = bot_event.get('function')
if intent and invocation_source and function:
bot_events_mapping[str(intent) + ':' + str(invocation_source)] = function
settings_s = settings_s + "AWS_BOT_EVENT_MAPPING={0!s}\n".format(bot_events_mapping)
# Map cognito triggers
cognito_trigger_mapping = {}
cognito_config = self.stage_config.get('cognito', {})
triggers = cognito_config.get('triggers', [])
for trigger in triggers:
source = trigger.get('source')
function = trigger.get('function')
if source and function:
cognito_trigger_mapping[source] = function
settings_s = settings_s + "COGNITO_TRIGGER_MAPPING={0!s}\n".format(cognito_trigger_mapping)
# Authorizer config
authorizer_function = self.authorizer.get('function', None)
if authorizer_function:
settings_s += "AUTHORIZER_FUNCTION='{0!s}'\n".format(authorizer_function)
# Copy our Django app into root of our package.
# It doesn't work otherwise.
if self.django_settings:
base = __file__.rsplit(os.sep, 1)[0]
django_py = ''.join(os.path.join(base, 'ext', 'django_zappa.py'))
lambda_zip.write(django_py, 'django_zappa_app.py')
# async response
async_response_table = self.stage_config.get('async_response_table', '')
settings_s += "ASYNC_RESPONSE_TABLE='{0!s}'\n".format(async_response_table)
# Lambda requires a specific chmod
temp_settings = tempfile.NamedTemporaryFile(delete=False)
os.chmod(temp_settings.name, 0o644)
temp_settings.write(bytes(settings_s, "utf-8"))
temp_settings.close()
lambda_zip.write(temp_settings.name, 'zappa_settings.py')
os.unlink(temp_settings.name) | python | def create_package(self, output=None):
"""
Ensure that the package can be properly configured,
and then create it.
"""
# Create the Lambda zip package (includes project and virtualenvironment)
# Also define the path the handler file so it can be copied to the zip
# root for Lambda.
current_file = os.path.dirname(os.path.abspath(
inspect.getfile(inspect.currentframe())))
handler_file = os.sep.join(current_file.split(os.sep)[0:]) + os.sep + 'handler.py'
# Create the zip file(s)
if self.stage_config.get('slim_handler', False):
# Create two zips. One with the application and the other with just the handler.
# https://github.com/Miserlou/Zappa/issues/510
self.zip_path = self.zappa.create_lambda_zip(
prefix=self.lambda_name,
use_precompiled_packages=self.stage_config.get('use_precompiled_packages', True),
exclude=self.stage_config.get('exclude', []),
disable_progress=self.disable_progress,
archive_format='tarball'
)
# Make sure the normal venv is not included in the handler's zip
exclude = self.stage_config.get('exclude', [])
cur_venv = self.zappa.get_current_venv()
exclude.append(cur_venv.split('/')[-1])
self.handler_path = self.zappa.create_lambda_zip(
prefix='handler_{0!s}'.format(self.lambda_name),
venv=self.zappa.create_handler_venv(),
handler_file=handler_file,
slim_handler=True,
exclude=exclude,
output=output,
disable_progress=self.disable_progress
)
else:
# Custom excludes for different versions.
# Related: https://github.com/kennethreitz/requests/issues/3985
if sys.version_info[0] < 3:
# Exclude packages already builtin to the python lambda environment
# Related: https://github.com/Miserlou/Zappa/issues/556
exclude = self.stage_config.get(
'exclude', [
"boto3",
"dateutil",
"botocore",
"s3transfer",
"six.py",
"jmespath",
"concurrent"
])
else:
# This could be python3.6 optimized.
exclude = self.stage_config.get(
'exclude', [
"boto3",
"dateutil",
"botocore",
"s3transfer",
"concurrent"
])
# Create a single zip that has the handler and application
self.zip_path = self.zappa.create_lambda_zip(
prefix=self.lambda_name,
handler_file=handler_file,
use_precompiled_packages=self.stage_config.get('use_precompiled_packages', True),
exclude=exclude,
output=output,
disable_progress=self.disable_progress
)
# Warn if this is too large for Lambda.
file_stats = os.stat(self.zip_path)
if file_stats.st_size > 52428800: # pragma: no cover
print('\n\nWarning: Application zip package is likely to be too large for AWS Lambda. '
'Try setting "slim_handler" to true in your Zappa settings file.\n\n')
# Throw custom settings into the zip that handles requests
if self.stage_config.get('slim_handler', False):
handler_zip = self.handler_path
else:
handler_zip = self.zip_path
with zipfile.ZipFile(handler_zip, 'a') as lambda_zip:
settings_s = "# Generated by Zappa\n"
if self.app_function:
if '.' not in self.app_function: # pragma: no cover
raise ClickException("Your " + click.style("app_function", fg='red', bold=True) + " value is not a modular path." +
" It needs to be in the format `" + click.style("your_module.your_app_object", bold=True) + "`.")
app_module, app_function = self.app_function.rsplit('.', 1)
settings_s = settings_s + "APP_MODULE='{0!s}'\nAPP_FUNCTION='{1!s}'\n".format(app_module, app_function)
if self.exception_handler:
settings_s += "EXCEPTION_HANDLER='{0!s}'\n".format(self.exception_handler)
else:
settings_s += "EXCEPTION_HANDLER=None\n"
if self.debug:
settings_s = settings_s + "DEBUG=True\n"
else:
settings_s = settings_s + "DEBUG=False\n"
settings_s = settings_s + "LOG_LEVEL='{0!s}'\n".format((self.log_level))
if self.binary_support:
settings_s = settings_s + "BINARY_SUPPORT=True\n"
else:
settings_s = settings_s + "BINARY_SUPPORT=False\n"
head_map_dict = {}
head_map_dict.update(dict(self.context_header_mappings))
settings_s = settings_s + "CONTEXT_HEADER_MAPPINGS={0}\n".format(
head_map_dict
)
# If we're on a domain, we don't need to define the /<<env>> in
# the WSGI PATH
if self.domain:
settings_s = settings_s + "DOMAIN='{0!s}'\n".format((self.domain))
else:
settings_s = settings_s + "DOMAIN=None\n"
if self.base_path:
settings_s = settings_s + "BASE_PATH='{0!s}'\n".format((self.base_path))
else:
settings_s = settings_s + "BASE_PATH=None\n"
# Pass through remote config bucket and path
if self.remote_env:
settings_s = settings_s + "REMOTE_ENV='{0!s}'\n".format(
self.remote_env
)
# DEPRECATED. use remove_env instead
elif self.remote_env_bucket and self.remote_env_file:
settings_s = settings_s + "REMOTE_ENV='s3://{0!s}/{1!s}'\n".format(
self.remote_env_bucket, self.remote_env_file
)
# Local envs
env_dict = {}
if self.aws_region:
env_dict['AWS_REGION'] = self.aws_region
env_dict.update(dict(self.environment_variables))
# Environment variable keys must be ascii
# https://github.com/Miserlou/Zappa/issues/604
# https://github.com/Miserlou/Zappa/issues/998
try:
env_dict = dict((k.encode('ascii').decode('ascii'), v) for (k, v) in env_dict.items())
except Exception:
raise ValueError("Environment variable keys must be ascii.")
settings_s = settings_s + "ENVIRONMENT_VARIABLES={0}\n".format(
env_dict
)
# We can be environment-aware
settings_s = settings_s + "API_STAGE='{0!s}'\n".format((self.api_stage))
settings_s = settings_s + "PROJECT_NAME='{0!s}'\n".format((self.project_name))
if self.settings_file:
settings_s = settings_s + "SETTINGS_FILE='{0!s}'\n".format((self.settings_file))
else:
settings_s = settings_s + "SETTINGS_FILE=None\n"
if self.django_settings:
settings_s = settings_s + "DJANGO_SETTINGS='{0!s}'\n".format((self.django_settings))
else:
settings_s = settings_s + "DJANGO_SETTINGS=None\n"
# If slim handler, path to project zip
if self.stage_config.get('slim_handler', False):
settings_s += "ARCHIVE_PATH='s3://{0!s}/{1!s}_{2!s}_current_project.tar.gz'\n".format(
self.s3_bucket_name, self.api_stage, self.project_name)
# since includes are for slim handler add the setting here by joining arbitrary list from zappa_settings file
# and tell the handler we are the slim_handler
# https://github.com/Miserlou/Zappa/issues/776
settings_s += "SLIM_HANDLER=True\n"
include = self.stage_config.get('include', [])
if len(include) >= 1:
settings_s += "INCLUDE=" + str(include) + '\n'
# AWS Events function mapping
event_mapping = {}
events = self.stage_config.get('events', [])
for event in events:
arn = event.get('event_source', {}).get('arn')
function = event.get('function')
if arn and function:
event_mapping[arn] = function
settings_s = settings_s + "AWS_EVENT_MAPPING={0!s}\n".format(event_mapping)
# Map Lext bot events
bot_events = self.stage_config.get('bot_events', [])
bot_events_mapping = {}
for bot_event in bot_events:
event_source = bot_event.get('event_source', {})
intent = event_source.get('intent')
invocation_source = event_source.get('invocation_source')
function = bot_event.get('function')
if intent and invocation_source and function:
bot_events_mapping[str(intent) + ':' + str(invocation_source)] = function
settings_s = settings_s + "AWS_BOT_EVENT_MAPPING={0!s}\n".format(bot_events_mapping)
# Map cognito triggers
cognito_trigger_mapping = {}
cognito_config = self.stage_config.get('cognito', {})
triggers = cognito_config.get('triggers', [])
for trigger in triggers:
source = trigger.get('source')
function = trigger.get('function')
if source and function:
cognito_trigger_mapping[source] = function
settings_s = settings_s + "COGNITO_TRIGGER_MAPPING={0!s}\n".format(cognito_trigger_mapping)
# Authorizer config
authorizer_function = self.authorizer.get('function', None)
if authorizer_function:
settings_s += "AUTHORIZER_FUNCTION='{0!s}'\n".format(authorizer_function)
# Copy our Django app into root of our package.
# It doesn't work otherwise.
if self.django_settings:
base = __file__.rsplit(os.sep, 1)[0]
django_py = ''.join(os.path.join(base, 'ext', 'django_zappa.py'))
lambda_zip.write(django_py, 'django_zappa_app.py')
# async response
async_response_table = self.stage_config.get('async_response_table', '')
settings_s += "ASYNC_RESPONSE_TABLE='{0!s}'\n".format(async_response_table)
# Lambda requires a specific chmod
temp_settings = tempfile.NamedTemporaryFile(delete=False)
os.chmod(temp_settings.name, 0o644)
temp_settings.write(bytes(settings_s, "utf-8"))
temp_settings.close()
lambda_zip.write(temp_settings.name, 'zappa_settings.py')
os.unlink(temp_settings.name) | [
"def",
"create_package",
"(",
"self",
",",
"output",
"=",
"None",
")",
":",
"# Create the Lambda zip package (includes project and virtualenvironment)",
"# Also define the path the handler file so it can be copied to the zip",
"# root for Lambda.",
"current_file",
"=",
"os",
".",
"path",
".",
"dirname",
"(",
"os",
".",
"path",
".",
"abspath",
"(",
"inspect",
".",
"getfile",
"(",
"inspect",
".",
"currentframe",
"(",
")",
")",
")",
")",
"handler_file",
"=",
"os",
".",
"sep",
".",
"join",
"(",
"current_file",
".",
"split",
"(",
"os",
".",
"sep",
")",
"[",
"0",
":",
"]",
")",
"+",
"os",
".",
"sep",
"+",
"'handler.py'",
"# Create the zip file(s)",
"if",
"self",
".",
"stage_config",
".",
"get",
"(",
"'slim_handler'",
",",
"False",
")",
":",
"# Create two zips. One with the application and the other with just the handler.",
"# https://github.com/Miserlou/Zappa/issues/510",
"self",
".",
"zip_path",
"=",
"self",
".",
"zappa",
".",
"create_lambda_zip",
"(",
"prefix",
"=",
"self",
".",
"lambda_name",
",",
"use_precompiled_packages",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'use_precompiled_packages'",
",",
"True",
")",
",",
"exclude",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'exclude'",
",",
"[",
"]",
")",
",",
"disable_progress",
"=",
"self",
".",
"disable_progress",
",",
"archive_format",
"=",
"'tarball'",
")",
"# Make sure the normal venv is not included in the handler's zip",
"exclude",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'exclude'",
",",
"[",
"]",
")",
"cur_venv",
"=",
"self",
".",
"zappa",
".",
"get_current_venv",
"(",
")",
"exclude",
".",
"append",
"(",
"cur_venv",
".",
"split",
"(",
"'/'",
")",
"[",
"-",
"1",
"]",
")",
"self",
".",
"handler_path",
"=",
"self",
".",
"zappa",
".",
"create_lambda_zip",
"(",
"prefix",
"=",
"'handler_{0!s}'",
".",
"format",
"(",
"self",
".",
"lambda_name",
")",
",",
"venv",
"=",
"self",
".",
"zappa",
".",
"create_handler_venv",
"(",
")",
",",
"handler_file",
"=",
"handler_file",
",",
"slim_handler",
"=",
"True",
",",
"exclude",
"=",
"exclude",
",",
"output",
"=",
"output",
",",
"disable_progress",
"=",
"self",
".",
"disable_progress",
")",
"else",
":",
"# Custom excludes for different versions.",
"# Related: https://github.com/kennethreitz/requests/issues/3985",
"if",
"sys",
".",
"version_info",
"[",
"0",
"]",
"<",
"3",
":",
"# Exclude packages already builtin to the python lambda environment",
"# Related: https://github.com/Miserlou/Zappa/issues/556",
"exclude",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'exclude'",
",",
"[",
"\"boto3\"",
",",
"\"dateutil\"",
",",
"\"botocore\"",
",",
"\"s3transfer\"",
",",
"\"six.py\"",
",",
"\"jmespath\"",
",",
"\"concurrent\"",
"]",
")",
"else",
":",
"# This could be python3.6 optimized.",
"exclude",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'exclude'",
",",
"[",
"\"boto3\"",
",",
"\"dateutil\"",
",",
"\"botocore\"",
",",
"\"s3transfer\"",
",",
"\"concurrent\"",
"]",
")",
"# Create a single zip that has the handler and application",
"self",
".",
"zip_path",
"=",
"self",
".",
"zappa",
".",
"create_lambda_zip",
"(",
"prefix",
"=",
"self",
".",
"lambda_name",
",",
"handler_file",
"=",
"handler_file",
",",
"use_precompiled_packages",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'use_precompiled_packages'",
",",
"True",
")",
",",
"exclude",
"=",
"exclude",
",",
"output",
"=",
"output",
",",
"disable_progress",
"=",
"self",
".",
"disable_progress",
")",
"# Warn if this is too large for Lambda.",
"file_stats",
"=",
"os",
".",
"stat",
"(",
"self",
".",
"zip_path",
")",
"if",
"file_stats",
".",
"st_size",
">",
"52428800",
":",
"# pragma: no cover",
"print",
"(",
"'\\n\\nWarning: Application zip package is likely to be too large for AWS Lambda. '",
"'Try setting \"slim_handler\" to true in your Zappa settings file.\\n\\n'",
")",
"# Throw custom settings into the zip that handles requests",
"if",
"self",
".",
"stage_config",
".",
"get",
"(",
"'slim_handler'",
",",
"False",
")",
":",
"handler_zip",
"=",
"self",
".",
"handler_path",
"else",
":",
"handler_zip",
"=",
"self",
".",
"zip_path",
"with",
"zipfile",
".",
"ZipFile",
"(",
"handler_zip",
",",
"'a'",
")",
"as",
"lambda_zip",
":",
"settings_s",
"=",
"\"# Generated by Zappa\\n\"",
"if",
"self",
".",
"app_function",
":",
"if",
"'.'",
"not",
"in",
"self",
".",
"app_function",
":",
"# pragma: no cover",
"raise",
"ClickException",
"(",
"\"Your \"",
"+",
"click",
".",
"style",
"(",
"\"app_function\"",
",",
"fg",
"=",
"'red'",
",",
"bold",
"=",
"True",
")",
"+",
"\" value is not a modular path.\"",
"+",
"\" It needs to be in the format `\"",
"+",
"click",
".",
"style",
"(",
"\"your_module.your_app_object\"",
",",
"bold",
"=",
"True",
")",
"+",
"\"`.\"",
")",
"app_module",
",",
"app_function",
"=",
"self",
".",
"app_function",
".",
"rsplit",
"(",
"'.'",
",",
"1",
")",
"settings_s",
"=",
"settings_s",
"+",
"\"APP_MODULE='{0!s}'\\nAPP_FUNCTION='{1!s}'\\n\"",
".",
"format",
"(",
"app_module",
",",
"app_function",
")",
"if",
"self",
".",
"exception_handler",
":",
"settings_s",
"+=",
"\"EXCEPTION_HANDLER='{0!s}'\\n\"",
".",
"format",
"(",
"self",
".",
"exception_handler",
")",
"else",
":",
"settings_s",
"+=",
"\"EXCEPTION_HANDLER=None\\n\"",
"if",
"self",
".",
"debug",
":",
"settings_s",
"=",
"settings_s",
"+",
"\"DEBUG=True\\n\"",
"else",
":",
"settings_s",
"=",
"settings_s",
"+",
"\"DEBUG=False\\n\"",
"settings_s",
"=",
"settings_s",
"+",
"\"LOG_LEVEL='{0!s}'\\n\"",
".",
"format",
"(",
"(",
"self",
".",
"log_level",
")",
")",
"if",
"self",
".",
"binary_support",
":",
"settings_s",
"=",
"settings_s",
"+",
"\"BINARY_SUPPORT=True\\n\"",
"else",
":",
"settings_s",
"=",
"settings_s",
"+",
"\"BINARY_SUPPORT=False\\n\"",
"head_map_dict",
"=",
"{",
"}",
"head_map_dict",
".",
"update",
"(",
"dict",
"(",
"self",
".",
"context_header_mappings",
")",
")",
"settings_s",
"=",
"settings_s",
"+",
"\"CONTEXT_HEADER_MAPPINGS={0}\\n\"",
".",
"format",
"(",
"head_map_dict",
")",
"# If we're on a domain, we don't need to define the /<<env>> in",
"# the WSGI PATH",
"if",
"self",
".",
"domain",
":",
"settings_s",
"=",
"settings_s",
"+",
"\"DOMAIN='{0!s}'\\n\"",
".",
"format",
"(",
"(",
"self",
".",
"domain",
")",
")",
"else",
":",
"settings_s",
"=",
"settings_s",
"+",
"\"DOMAIN=None\\n\"",
"if",
"self",
".",
"base_path",
":",
"settings_s",
"=",
"settings_s",
"+",
"\"BASE_PATH='{0!s}'\\n\"",
".",
"format",
"(",
"(",
"self",
".",
"base_path",
")",
")",
"else",
":",
"settings_s",
"=",
"settings_s",
"+",
"\"BASE_PATH=None\\n\"",
"# Pass through remote config bucket and path",
"if",
"self",
".",
"remote_env",
":",
"settings_s",
"=",
"settings_s",
"+",
"\"REMOTE_ENV='{0!s}'\\n\"",
".",
"format",
"(",
"self",
".",
"remote_env",
")",
"# DEPRECATED. use remove_env instead",
"elif",
"self",
".",
"remote_env_bucket",
"and",
"self",
".",
"remote_env_file",
":",
"settings_s",
"=",
"settings_s",
"+",
"\"REMOTE_ENV='s3://{0!s}/{1!s}'\\n\"",
".",
"format",
"(",
"self",
".",
"remote_env_bucket",
",",
"self",
".",
"remote_env_file",
")",
"# Local envs",
"env_dict",
"=",
"{",
"}",
"if",
"self",
".",
"aws_region",
":",
"env_dict",
"[",
"'AWS_REGION'",
"]",
"=",
"self",
".",
"aws_region",
"env_dict",
".",
"update",
"(",
"dict",
"(",
"self",
".",
"environment_variables",
")",
")",
"# Environment variable keys must be ascii",
"# https://github.com/Miserlou/Zappa/issues/604",
"# https://github.com/Miserlou/Zappa/issues/998",
"try",
":",
"env_dict",
"=",
"dict",
"(",
"(",
"k",
".",
"encode",
"(",
"'ascii'",
")",
".",
"decode",
"(",
"'ascii'",
")",
",",
"v",
")",
"for",
"(",
"k",
",",
"v",
")",
"in",
"env_dict",
".",
"items",
"(",
")",
")",
"except",
"Exception",
":",
"raise",
"ValueError",
"(",
"\"Environment variable keys must be ascii.\"",
")",
"settings_s",
"=",
"settings_s",
"+",
"\"ENVIRONMENT_VARIABLES={0}\\n\"",
".",
"format",
"(",
"env_dict",
")",
"# We can be environment-aware",
"settings_s",
"=",
"settings_s",
"+",
"\"API_STAGE='{0!s}'\\n\"",
".",
"format",
"(",
"(",
"self",
".",
"api_stage",
")",
")",
"settings_s",
"=",
"settings_s",
"+",
"\"PROJECT_NAME='{0!s}'\\n\"",
".",
"format",
"(",
"(",
"self",
".",
"project_name",
")",
")",
"if",
"self",
".",
"settings_file",
":",
"settings_s",
"=",
"settings_s",
"+",
"\"SETTINGS_FILE='{0!s}'\\n\"",
".",
"format",
"(",
"(",
"self",
".",
"settings_file",
")",
")",
"else",
":",
"settings_s",
"=",
"settings_s",
"+",
"\"SETTINGS_FILE=None\\n\"",
"if",
"self",
".",
"django_settings",
":",
"settings_s",
"=",
"settings_s",
"+",
"\"DJANGO_SETTINGS='{0!s}'\\n\"",
".",
"format",
"(",
"(",
"self",
".",
"django_settings",
")",
")",
"else",
":",
"settings_s",
"=",
"settings_s",
"+",
"\"DJANGO_SETTINGS=None\\n\"",
"# If slim handler, path to project zip",
"if",
"self",
".",
"stage_config",
".",
"get",
"(",
"'slim_handler'",
",",
"False",
")",
":",
"settings_s",
"+=",
"\"ARCHIVE_PATH='s3://{0!s}/{1!s}_{2!s}_current_project.tar.gz'\\n\"",
".",
"format",
"(",
"self",
".",
"s3_bucket_name",
",",
"self",
".",
"api_stage",
",",
"self",
".",
"project_name",
")",
"# since includes are for slim handler add the setting here by joining arbitrary list from zappa_settings file",
"# and tell the handler we are the slim_handler",
"# https://github.com/Miserlou/Zappa/issues/776",
"settings_s",
"+=",
"\"SLIM_HANDLER=True\\n\"",
"include",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'include'",
",",
"[",
"]",
")",
"if",
"len",
"(",
"include",
")",
">=",
"1",
":",
"settings_s",
"+=",
"\"INCLUDE=\"",
"+",
"str",
"(",
"include",
")",
"+",
"'\\n'",
"# AWS Events function mapping",
"event_mapping",
"=",
"{",
"}",
"events",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'events'",
",",
"[",
"]",
")",
"for",
"event",
"in",
"events",
":",
"arn",
"=",
"event",
".",
"get",
"(",
"'event_source'",
",",
"{",
"}",
")",
".",
"get",
"(",
"'arn'",
")",
"function",
"=",
"event",
".",
"get",
"(",
"'function'",
")",
"if",
"arn",
"and",
"function",
":",
"event_mapping",
"[",
"arn",
"]",
"=",
"function",
"settings_s",
"=",
"settings_s",
"+",
"\"AWS_EVENT_MAPPING={0!s}\\n\"",
".",
"format",
"(",
"event_mapping",
")",
"# Map Lext bot events",
"bot_events",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'bot_events'",
",",
"[",
"]",
")",
"bot_events_mapping",
"=",
"{",
"}",
"for",
"bot_event",
"in",
"bot_events",
":",
"event_source",
"=",
"bot_event",
".",
"get",
"(",
"'event_source'",
",",
"{",
"}",
")",
"intent",
"=",
"event_source",
".",
"get",
"(",
"'intent'",
")",
"invocation_source",
"=",
"event_source",
".",
"get",
"(",
"'invocation_source'",
")",
"function",
"=",
"bot_event",
".",
"get",
"(",
"'function'",
")",
"if",
"intent",
"and",
"invocation_source",
"and",
"function",
":",
"bot_events_mapping",
"[",
"str",
"(",
"intent",
")",
"+",
"':'",
"+",
"str",
"(",
"invocation_source",
")",
"]",
"=",
"function",
"settings_s",
"=",
"settings_s",
"+",
"\"AWS_BOT_EVENT_MAPPING={0!s}\\n\"",
".",
"format",
"(",
"bot_events_mapping",
")",
"# Map cognito triggers",
"cognito_trigger_mapping",
"=",
"{",
"}",
"cognito_config",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'cognito'",
",",
"{",
"}",
")",
"triggers",
"=",
"cognito_config",
".",
"get",
"(",
"'triggers'",
",",
"[",
"]",
")",
"for",
"trigger",
"in",
"triggers",
":",
"source",
"=",
"trigger",
".",
"get",
"(",
"'source'",
")",
"function",
"=",
"trigger",
".",
"get",
"(",
"'function'",
")",
"if",
"source",
"and",
"function",
":",
"cognito_trigger_mapping",
"[",
"source",
"]",
"=",
"function",
"settings_s",
"=",
"settings_s",
"+",
"\"COGNITO_TRIGGER_MAPPING={0!s}\\n\"",
".",
"format",
"(",
"cognito_trigger_mapping",
")",
"# Authorizer config",
"authorizer_function",
"=",
"self",
".",
"authorizer",
".",
"get",
"(",
"'function'",
",",
"None",
")",
"if",
"authorizer_function",
":",
"settings_s",
"+=",
"\"AUTHORIZER_FUNCTION='{0!s}'\\n\"",
".",
"format",
"(",
"authorizer_function",
")",
"# Copy our Django app into root of our package.",
"# It doesn't work otherwise.",
"if",
"self",
".",
"django_settings",
":",
"base",
"=",
"__file__",
".",
"rsplit",
"(",
"os",
".",
"sep",
",",
"1",
")",
"[",
"0",
"]",
"django_py",
"=",
"''",
".",
"join",
"(",
"os",
".",
"path",
".",
"join",
"(",
"base",
",",
"'ext'",
",",
"'django_zappa.py'",
")",
")",
"lambda_zip",
".",
"write",
"(",
"django_py",
",",
"'django_zappa_app.py'",
")",
"# async response",
"async_response_table",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'async_response_table'",
",",
"''",
")",
"settings_s",
"+=",
"\"ASYNC_RESPONSE_TABLE='{0!s}'\\n\"",
".",
"format",
"(",
"async_response_table",
")",
"# Lambda requires a specific chmod",
"temp_settings",
"=",
"tempfile",
".",
"NamedTemporaryFile",
"(",
"delete",
"=",
"False",
")",
"os",
".",
"chmod",
"(",
"temp_settings",
".",
"name",
",",
"0o644",
")",
"temp_settings",
".",
"write",
"(",
"bytes",
"(",
"settings_s",
",",
"\"utf-8\"",
")",
")",
"temp_settings",
".",
"close",
"(",
")",
"lambda_zip",
".",
"write",
"(",
"temp_settings",
".",
"name",
",",
"'zappa_settings.py'",
")",
"os",
".",
"unlink",
"(",
"temp_settings",
".",
"name",
")"
] | Ensure that the package can be properly configured,
and then create it. | [
"Ensure",
"that",
"the",
"package",
"can",
"be",
"properly",
"configured",
"and",
"then",
"create",
"it",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L2193-L2441 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.remove_local_zip | def remove_local_zip(self):
"""
Remove our local zip file.
"""
if self.stage_config.get('delete_local_zip', True):
try:
if os.path.isfile(self.zip_path):
os.remove(self.zip_path)
if self.handler_path and os.path.isfile(self.handler_path):
os.remove(self.handler_path)
except Exception as e: # pragma: no cover
sys.exit(-1) | python | def remove_local_zip(self):
"""
Remove our local zip file.
"""
if self.stage_config.get('delete_local_zip', True):
try:
if os.path.isfile(self.zip_path):
os.remove(self.zip_path)
if self.handler_path and os.path.isfile(self.handler_path):
os.remove(self.handler_path)
except Exception as e: # pragma: no cover
sys.exit(-1) | [
"def",
"remove_local_zip",
"(",
"self",
")",
":",
"if",
"self",
".",
"stage_config",
".",
"get",
"(",
"'delete_local_zip'",
",",
"True",
")",
":",
"try",
":",
"if",
"os",
".",
"path",
".",
"isfile",
"(",
"self",
".",
"zip_path",
")",
":",
"os",
".",
"remove",
"(",
"self",
".",
"zip_path",
")",
"if",
"self",
".",
"handler_path",
"and",
"os",
".",
"path",
".",
"isfile",
"(",
"self",
".",
"handler_path",
")",
":",
"os",
".",
"remove",
"(",
"self",
".",
"handler_path",
")",
"except",
"Exception",
"as",
"e",
":",
"# pragma: no cover",
"sys",
".",
"exit",
"(",
"-",
"1",
")"
] | Remove our local zip file. | [
"Remove",
"our",
"local",
"zip",
"file",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L2443-L2455 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.remove_uploaded_zip | def remove_uploaded_zip(self):
"""
Remove the local and S3 zip file after uploading and updating.
"""
# Remove the uploaded zip from S3, because it is now registered..
if self.stage_config.get('delete_s3_zip', True):
self.zappa.remove_from_s3(self.zip_path, self.s3_bucket_name)
if self.stage_config.get('slim_handler', False):
# Need to keep the project zip as the slim handler uses it.
self.zappa.remove_from_s3(self.handler_path, self.s3_bucket_name) | python | def remove_uploaded_zip(self):
"""
Remove the local and S3 zip file after uploading and updating.
"""
# Remove the uploaded zip from S3, because it is now registered..
if self.stage_config.get('delete_s3_zip', True):
self.zappa.remove_from_s3(self.zip_path, self.s3_bucket_name)
if self.stage_config.get('slim_handler', False):
# Need to keep the project zip as the slim handler uses it.
self.zappa.remove_from_s3(self.handler_path, self.s3_bucket_name) | [
"def",
"remove_uploaded_zip",
"(",
"self",
")",
":",
"# Remove the uploaded zip from S3, because it is now registered..",
"if",
"self",
".",
"stage_config",
".",
"get",
"(",
"'delete_s3_zip'",
",",
"True",
")",
":",
"self",
".",
"zappa",
".",
"remove_from_s3",
"(",
"self",
".",
"zip_path",
",",
"self",
".",
"s3_bucket_name",
")",
"if",
"self",
".",
"stage_config",
".",
"get",
"(",
"'slim_handler'",
",",
"False",
")",
":",
"# Need to keep the project zip as the slim handler uses it.",
"self",
".",
"zappa",
".",
"remove_from_s3",
"(",
"self",
".",
"handler_path",
",",
"self",
".",
"s3_bucket_name",
")"
] | Remove the local and S3 zip file after uploading and updating. | [
"Remove",
"the",
"local",
"and",
"S3",
"zip",
"file",
"after",
"uploading",
"and",
"updating",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L2457-L2467 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.on_exit | def on_exit(self):
"""
Cleanup after the command finishes.
Always called: SystemExit, KeyboardInterrupt and any other Exception that occurs.
"""
if self.zip_path:
# Only try to remove uploaded zip if we're running a command that has loaded credentials
if self.load_credentials:
self.remove_uploaded_zip()
self.remove_local_zip() | python | def on_exit(self):
"""
Cleanup after the command finishes.
Always called: SystemExit, KeyboardInterrupt and any other Exception that occurs.
"""
if self.zip_path:
# Only try to remove uploaded zip if we're running a command that has loaded credentials
if self.load_credentials:
self.remove_uploaded_zip()
self.remove_local_zip() | [
"def",
"on_exit",
"(",
"self",
")",
":",
"if",
"self",
".",
"zip_path",
":",
"# Only try to remove uploaded zip if we're running a command that has loaded credentials",
"if",
"self",
".",
"load_credentials",
":",
"self",
".",
"remove_uploaded_zip",
"(",
")",
"self",
".",
"remove_local_zip",
"(",
")"
] | Cleanup after the command finishes.
Always called: SystemExit, KeyboardInterrupt and any other Exception that occurs. | [
"Cleanup",
"after",
"the",
"command",
"finishes",
".",
"Always",
"called",
":",
"SystemExit",
"KeyboardInterrupt",
"and",
"any",
"other",
"Exception",
"that",
"occurs",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L2469-L2479 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.print_logs | def print_logs(self, logs, colorize=True, http=False, non_http=False, force_colorize=None):
"""
Parse, filter and print logs to the console.
"""
for log in logs:
timestamp = log['timestamp']
message = log['message']
if "START RequestId" in message:
continue
if "REPORT RequestId" in message:
continue
if "END RequestId" in message:
continue
if not colorize and not force_colorize:
if http:
if self.is_http_log_entry(message.strip()):
print("[" + str(timestamp) + "] " + message.strip())
elif non_http:
if not self.is_http_log_entry(message.strip()):
print("[" + str(timestamp) + "] " + message.strip())
else:
print("[" + str(timestamp) + "] " + message.strip())
else:
if http:
if self.is_http_log_entry(message.strip()):
click.echo(click.style("[", fg='cyan') + click.style(str(timestamp), bold=True) + click.style("]", fg='cyan') + self.colorize_log_entry(message.strip()), color=force_colorize)
elif non_http:
if not self.is_http_log_entry(message.strip()):
click.echo(click.style("[", fg='cyan') + click.style(str(timestamp), bold=True) + click.style("]", fg='cyan') + self.colorize_log_entry(message.strip()), color=force_colorize)
else:
click.echo(click.style("[", fg='cyan') + click.style(str(timestamp), bold=True) + click.style("]", fg='cyan') + self.colorize_log_entry(message.strip()), color=force_colorize) | python | def print_logs(self, logs, colorize=True, http=False, non_http=False, force_colorize=None):
"""
Parse, filter and print logs to the console.
"""
for log in logs:
timestamp = log['timestamp']
message = log['message']
if "START RequestId" in message:
continue
if "REPORT RequestId" in message:
continue
if "END RequestId" in message:
continue
if not colorize and not force_colorize:
if http:
if self.is_http_log_entry(message.strip()):
print("[" + str(timestamp) + "] " + message.strip())
elif non_http:
if not self.is_http_log_entry(message.strip()):
print("[" + str(timestamp) + "] " + message.strip())
else:
print("[" + str(timestamp) + "] " + message.strip())
else:
if http:
if self.is_http_log_entry(message.strip()):
click.echo(click.style("[", fg='cyan') + click.style(str(timestamp), bold=True) + click.style("]", fg='cyan') + self.colorize_log_entry(message.strip()), color=force_colorize)
elif non_http:
if not self.is_http_log_entry(message.strip()):
click.echo(click.style("[", fg='cyan') + click.style(str(timestamp), bold=True) + click.style("]", fg='cyan') + self.colorize_log_entry(message.strip()), color=force_colorize)
else:
click.echo(click.style("[", fg='cyan') + click.style(str(timestamp), bold=True) + click.style("]", fg='cyan') + self.colorize_log_entry(message.strip()), color=force_colorize) | [
"def",
"print_logs",
"(",
"self",
",",
"logs",
",",
"colorize",
"=",
"True",
",",
"http",
"=",
"False",
",",
"non_http",
"=",
"False",
",",
"force_colorize",
"=",
"None",
")",
":",
"for",
"log",
"in",
"logs",
":",
"timestamp",
"=",
"log",
"[",
"'timestamp'",
"]",
"message",
"=",
"log",
"[",
"'message'",
"]",
"if",
"\"START RequestId\"",
"in",
"message",
":",
"continue",
"if",
"\"REPORT RequestId\"",
"in",
"message",
":",
"continue",
"if",
"\"END RequestId\"",
"in",
"message",
":",
"continue",
"if",
"not",
"colorize",
"and",
"not",
"force_colorize",
":",
"if",
"http",
":",
"if",
"self",
".",
"is_http_log_entry",
"(",
"message",
".",
"strip",
"(",
")",
")",
":",
"print",
"(",
"\"[\"",
"+",
"str",
"(",
"timestamp",
")",
"+",
"\"] \"",
"+",
"message",
".",
"strip",
"(",
")",
")",
"elif",
"non_http",
":",
"if",
"not",
"self",
".",
"is_http_log_entry",
"(",
"message",
".",
"strip",
"(",
")",
")",
":",
"print",
"(",
"\"[\"",
"+",
"str",
"(",
"timestamp",
")",
"+",
"\"] \"",
"+",
"message",
".",
"strip",
"(",
")",
")",
"else",
":",
"print",
"(",
"\"[\"",
"+",
"str",
"(",
"timestamp",
")",
"+",
"\"] \"",
"+",
"message",
".",
"strip",
"(",
")",
")",
"else",
":",
"if",
"http",
":",
"if",
"self",
".",
"is_http_log_entry",
"(",
"message",
".",
"strip",
"(",
")",
")",
":",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"[\"",
",",
"fg",
"=",
"'cyan'",
")",
"+",
"click",
".",
"style",
"(",
"str",
"(",
"timestamp",
")",
",",
"bold",
"=",
"True",
")",
"+",
"click",
".",
"style",
"(",
"\"]\"",
",",
"fg",
"=",
"'cyan'",
")",
"+",
"self",
".",
"colorize_log_entry",
"(",
"message",
".",
"strip",
"(",
")",
")",
",",
"color",
"=",
"force_colorize",
")",
"elif",
"non_http",
":",
"if",
"not",
"self",
".",
"is_http_log_entry",
"(",
"message",
".",
"strip",
"(",
")",
")",
":",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"[\"",
",",
"fg",
"=",
"'cyan'",
")",
"+",
"click",
".",
"style",
"(",
"str",
"(",
"timestamp",
")",
",",
"bold",
"=",
"True",
")",
"+",
"click",
".",
"style",
"(",
"\"]\"",
",",
"fg",
"=",
"'cyan'",
")",
"+",
"self",
".",
"colorize_log_entry",
"(",
"message",
".",
"strip",
"(",
")",
")",
",",
"color",
"=",
"force_colorize",
")",
"else",
":",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"[\"",
",",
"fg",
"=",
"'cyan'",
")",
"+",
"click",
".",
"style",
"(",
"str",
"(",
"timestamp",
")",
",",
"bold",
"=",
"True",
")",
"+",
"click",
".",
"style",
"(",
"\"]\"",
",",
"fg",
"=",
"'cyan'",
")",
"+",
"self",
".",
"colorize_log_entry",
"(",
"message",
".",
"strip",
"(",
")",
")",
",",
"color",
"=",
"force_colorize",
")"
] | Parse, filter and print logs to the console. | [
"Parse",
"filter",
"and",
"print",
"logs",
"to",
"the",
"console",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L2481-L2514 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.is_http_log_entry | def is_http_log_entry(self, string):
"""
Determines if a log entry is an HTTP-formatted log string or not.
"""
# Debug event filter
if 'Zappa Event' in string:
return False
# IP address filter
for token in string.replace('\t', ' ').split(' '):
try:
if (token.count('.') is 3 and token.replace('.', '').isnumeric()):
return True
except Exception: # pragma: no cover
pass
return False | python | def is_http_log_entry(self, string):
"""
Determines if a log entry is an HTTP-formatted log string or not.
"""
# Debug event filter
if 'Zappa Event' in string:
return False
# IP address filter
for token in string.replace('\t', ' ').split(' '):
try:
if (token.count('.') is 3 and token.replace('.', '').isnumeric()):
return True
except Exception: # pragma: no cover
pass
return False | [
"def",
"is_http_log_entry",
"(",
"self",
",",
"string",
")",
":",
"# Debug event filter",
"if",
"'Zappa Event'",
"in",
"string",
":",
"return",
"False",
"# IP address filter",
"for",
"token",
"in",
"string",
".",
"replace",
"(",
"'\\t'",
",",
"' '",
")",
".",
"split",
"(",
"' '",
")",
":",
"try",
":",
"if",
"(",
"token",
".",
"count",
"(",
"'.'",
")",
"is",
"3",
"and",
"token",
".",
"replace",
"(",
"'.'",
",",
"''",
")",
".",
"isnumeric",
"(",
")",
")",
":",
"return",
"True",
"except",
"Exception",
":",
"# pragma: no cover",
"pass",
"return",
"False"
] | Determines if a log entry is an HTTP-formatted log string or not. | [
"Determines",
"if",
"a",
"log",
"entry",
"is",
"an",
"HTTP",
"-",
"formatted",
"log",
"string",
"or",
"not",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L2516-L2532 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.colorize_log_entry | def colorize_log_entry(self, string):
"""
Apply various heuristics to return a colorized version of a string.
If these fail, simply return the string in plaintext.
"""
final_string = string
try:
# First, do stuff in square brackets
inside_squares = re.findall(r'\[([^]]*)\]', string)
for token in inside_squares:
if token in ['CRITICAL', 'ERROR', 'WARNING', 'DEBUG', 'INFO', 'NOTSET']:
final_string = final_string.replace('[' + token + ']', click.style("[", fg='cyan') + click.style(token, fg='cyan', bold=True) + click.style("]", fg='cyan'))
else:
final_string = final_string.replace('[' + token + ']', click.style("[", fg='cyan') + click.style(token, bold=True) + click.style("]", fg='cyan'))
# Then do quoted strings
quotes = re.findall(r'"[^"]*"', string)
for token in quotes:
final_string = final_string.replace(token, click.style(token, fg="yellow"))
# And UUIDs
for token in final_string.replace('\t', ' ').split(' '):
try:
if token.count('-') is 4 and token.replace('-', '').isalnum():
final_string = final_string.replace(token, click.style(token, fg="magenta"))
except Exception: # pragma: no cover
pass
# And IP addresses
try:
if token.count('.') is 3 and token.replace('.', '').isnumeric():
final_string = final_string.replace(token, click.style(token, fg="red"))
except Exception: # pragma: no cover
pass
# And status codes
try:
if token in ['200']:
final_string = final_string.replace(token, click.style(token, fg="green"))
if token in ['400', '401', '403', '404', '405', '500']:
final_string = final_string.replace(token, click.style(token, fg="red"))
except Exception: # pragma: no cover
pass
# And Zappa Events
try:
if "Zappa Event:" in final_string:
final_string = final_string.replace("Zappa Event:", click.style("Zappa Event:", bold=True, fg="green"))
except Exception: # pragma: no cover
pass
# And dates
for token in final_string.split('\t'):
try:
is_date = parser.parse(token)
final_string = final_string.replace(token, click.style(token, fg="green"))
except Exception: # pragma: no cover
pass
final_string = final_string.replace('\t', ' ').replace(' ', ' ')
if final_string[0] != ' ':
final_string = ' ' + final_string
return final_string
except Exception as e: # pragma: no cover
return string | python | def colorize_log_entry(self, string):
"""
Apply various heuristics to return a colorized version of a string.
If these fail, simply return the string in plaintext.
"""
final_string = string
try:
# First, do stuff in square brackets
inside_squares = re.findall(r'\[([^]]*)\]', string)
for token in inside_squares:
if token in ['CRITICAL', 'ERROR', 'WARNING', 'DEBUG', 'INFO', 'NOTSET']:
final_string = final_string.replace('[' + token + ']', click.style("[", fg='cyan') + click.style(token, fg='cyan', bold=True) + click.style("]", fg='cyan'))
else:
final_string = final_string.replace('[' + token + ']', click.style("[", fg='cyan') + click.style(token, bold=True) + click.style("]", fg='cyan'))
# Then do quoted strings
quotes = re.findall(r'"[^"]*"', string)
for token in quotes:
final_string = final_string.replace(token, click.style(token, fg="yellow"))
# And UUIDs
for token in final_string.replace('\t', ' ').split(' '):
try:
if token.count('-') is 4 and token.replace('-', '').isalnum():
final_string = final_string.replace(token, click.style(token, fg="magenta"))
except Exception: # pragma: no cover
pass
# And IP addresses
try:
if token.count('.') is 3 and token.replace('.', '').isnumeric():
final_string = final_string.replace(token, click.style(token, fg="red"))
except Exception: # pragma: no cover
pass
# And status codes
try:
if token in ['200']:
final_string = final_string.replace(token, click.style(token, fg="green"))
if token in ['400', '401', '403', '404', '405', '500']:
final_string = final_string.replace(token, click.style(token, fg="red"))
except Exception: # pragma: no cover
pass
# And Zappa Events
try:
if "Zappa Event:" in final_string:
final_string = final_string.replace("Zappa Event:", click.style("Zappa Event:", bold=True, fg="green"))
except Exception: # pragma: no cover
pass
# And dates
for token in final_string.split('\t'):
try:
is_date = parser.parse(token)
final_string = final_string.replace(token, click.style(token, fg="green"))
except Exception: # pragma: no cover
pass
final_string = final_string.replace('\t', ' ').replace(' ', ' ')
if final_string[0] != ' ':
final_string = ' ' + final_string
return final_string
except Exception as e: # pragma: no cover
return string | [
"def",
"colorize_log_entry",
"(",
"self",
",",
"string",
")",
":",
"final_string",
"=",
"string",
"try",
":",
"# First, do stuff in square brackets",
"inside_squares",
"=",
"re",
".",
"findall",
"(",
"r'\\[([^]]*)\\]'",
",",
"string",
")",
"for",
"token",
"in",
"inside_squares",
":",
"if",
"token",
"in",
"[",
"'CRITICAL'",
",",
"'ERROR'",
",",
"'WARNING'",
",",
"'DEBUG'",
",",
"'INFO'",
",",
"'NOTSET'",
"]",
":",
"final_string",
"=",
"final_string",
".",
"replace",
"(",
"'['",
"+",
"token",
"+",
"']'",
",",
"click",
".",
"style",
"(",
"\"[\"",
",",
"fg",
"=",
"'cyan'",
")",
"+",
"click",
".",
"style",
"(",
"token",
",",
"fg",
"=",
"'cyan'",
",",
"bold",
"=",
"True",
")",
"+",
"click",
".",
"style",
"(",
"\"]\"",
",",
"fg",
"=",
"'cyan'",
")",
")",
"else",
":",
"final_string",
"=",
"final_string",
".",
"replace",
"(",
"'['",
"+",
"token",
"+",
"']'",
",",
"click",
".",
"style",
"(",
"\"[\"",
",",
"fg",
"=",
"'cyan'",
")",
"+",
"click",
".",
"style",
"(",
"token",
",",
"bold",
"=",
"True",
")",
"+",
"click",
".",
"style",
"(",
"\"]\"",
",",
"fg",
"=",
"'cyan'",
")",
")",
"# Then do quoted strings",
"quotes",
"=",
"re",
".",
"findall",
"(",
"r'\"[^\"]*\"'",
",",
"string",
")",
"for",
"token",
"in",
"quotes",
":",
"final_string",
"=",
"final_string",
".",
"replace",
"(",
"token",
",",
"click",
".",
"style",
"(",
"token",
",",
"fg",
"=",
"\"yellow\"",
")",
")",
"# And UUIDs",
"for",
"token",
"in",
"final_string",
".",
"replace",
"(",
"'\\t'",
",",
"' '",
")",
".",
"split",
"(",
"' '",
")",
":",
"try",
":",
"if",
"token",
".",
"count",
"(",
"'-'",
")",
"is",
"4",
"and",
"token",
".",
"replace",
"(",
"'-'",
",",
"''",
")",
".",
"isalnum",
"(",
")",
":",
"final_string",
"=",
"final_string",
".",
"replace",
"(",
"token",
",",
"click",
".",
"style",
"(",
"token",
",",
"fg",
"=",
"\"magenta\"",
")",
")",
"except",
"Exception",
":",
"# pragma: no cover",
"pass",
"# And IP addresses",
"try",
":",
"if",
"token",
".",
"count",
"(",
"'.'",
")",
"is",
"3",
"and",
"token",
".",
"replace",
"(",
"'.'",
",",
"''",
")",
".",
"isnumeric",
"(",
")",
":",
"final_string",
"=",
"final_string",
".",
"replace",
"(",
"token",
",",
"click",
".",
"style",
"(",
"token",
",",
"fg",
"=",
"\"red\"",
")",
")",
"except",
"Exception",
":",
"# pragma: no cover",
"pass",
"# And status codes",
"try",
":",
"if",
"token",
"in",
"[",
"'200'",
"]",
":",
"final_string",
"=",
"final_string",
".",
"replace",
"(",
"token",
",",
"click",
".",
"style",
"(",
"token",
",",
"fg",
"=",
"\"green\"",
")",
")",
"if",
"token",
"in",
"[",
"'400'",
",",
"'401'",
",",
"'403'",
",",
"'404'",
",",
"'405'",
",",
"'500'",
"]",
":",
"final_string",
"=",
"final_string",
".",
"replace",
"(",
"token",
",",
"click",
".",
"style",
"(",
"token",
",",
"fg",
"=",
"\"red\"",
")",
")",
"except",
"Exception",
":",
"# pragma: no cover",
"pass",
"# And Zappa Events",
"try",
":",
"if",
"\"Zappa Event:\"",
"in",
"final_string",
":",
"final_string",
"=",
"final_string",
".",
"replace",
"(",
"\"Zappa Event:\"",
",",
"click",
".",
"style",
"(",
"\"Zappa Event:\"",
",",
"bold",
"=",
"True",
",",
"fg",
"=",
"\"green\"",
")",
")",
"except",
"Exception",
":",
"# pragma: no cover",
"pass",
"# And dates",
"for",
"token",
"in",
"final_string",
".",
"split",
"(",
"'\\t'",
")",
":",
"try",
":",
"is_date",
"=",
"parser",
".",
"parse",
"(",
"token",
")",
"final_string",
"=",
"final_string",
".",
"replace",
"(",
"token",
",",
"click",
".",
"style",
"(",
"token",
",",
"fg",
"=",
"\"green\"",
")",
")",
"except",
"Exception",
":",
"# pragma: no cover",
"pass",
"final_string",
"=",
"final_string",
".",
"replace",
"(",
"'\\t'",
",",
"' '",
")",
".",
"replace",
"(",
"' '",
",",
"' '",
")",
"if",
"final_string",
"[",
"0",
"]",
"!=",
"' '",
":",
"final_string",
"=",
"' '",
"+",
"final_string",
"return",
"final_string",
"except",
"Exception",
"as",
"e",
":",
"# pragma: no cover",
"return",
"string"
] | Apply various heuristics to return a colorized version of a string.
If these fail, simply return the string in plaintext. | [
"Apply",
"various",
"heuristics",
"to",
"return",
"a",
"colorized",
"version",
"of",
"a",
"string",
".",
"If",
"these",
"fail",
"simply",
"return",
"the",
"string",
"in",
"plaintext",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L2537-L2603 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.execute_prebuild_script | def execute_prebuild_script(self):
"""
Parse and execute the prebuild_script from the zappa_settings.
"""
(pb_mod_path, pb_func) = self.prebuild_script.rsplit('.', 1)
try: # Prefer prebuild script in working directory
if pb_mod_path.count('.') >= 1: # Prebuild script func is nested in a folder
(mod_folder_path, mod_name) = pb_mod_path.rsplit('.', 1)
mod_folder_path_fragments = mod_folder_path.split('.')
working_dir = os.path.join(os.getcwd(), *mod_folder_path_fragments)
else:
mod_name = pb_mod_path
working_dir = os.getcwd()
working_dir_importer = pkgutil.get_importer(working_dir)
module_ = working_dir_importer.find_module(mod_name).load_module(mod_name)
except (ImportError, AttributeError):
try: # Prebuild func might be in virtualenv
module_ = importlib.import_module(pb_mod_path)
except ImportError: # pragma: no cover
raise ClickException(click.style("Failed ", fg="red") + 'to ' + click.style(
"import prebuild script ", bold=True) + 'module: "{pb_mod_path}"'.format(
pb_mod_path=click.style(pb_mod_path, bold=True)))
if not hasattr(module_, pb_func): # pragma: no cover
raise ClickException(click.style("Failed ", fg="red") + 'to ' + click.style(
"find prebuild script ", bold=True) + 'function: "{pb_func}" '.format(
pb_func=click.style(pb_func, bold=True)) + 'in module "{pb_mod_path}"'.format(
pb_mod_path=pb_mod_path))
prebuild_function = getattr(module_, pb_func)
prebuild_function() | python | def execute_prebuild_script(self):
"""
Parse and execute the prebuild_script from the zappa_settings.
"""
(pb_mod_path, pb_func) = self.prebuild_script.rsplit('.', 1)
try: # Prefer prebuild script in working directory
if pb_mod_path.count('.') >= 1: # Prebuild script func is nested in a folder
(mod_folder_path, mod_name) = pb_mod_path.rsplit('.', 1)
mod_folder_path_fragments = mod_folder_path.split('.')
working_dir = os.path.join(os.getcwd(), *mod_folder_path_fragments)
else:
mod_name = pb_mod_path
working_dir = os.getcwd()
working_dir_importer = pkgutil.get_importer(working_dir)
module_ = working_dir_importer.find_module(mod_name).load_module(mod_name)
except (ImportError, AttributeError):
try: # Prebuild func might be in virtualenv
module_ = importlib.import_module(pb_mod_path)
except ImportError: # pragma: no cover
raise ClickException(click.style("Failed ", fg="red") + 'to ' + click.style(
"import prebuild script ", bold=True) + 'module: "{pb_mod_path}"'.format(
pb_mod_path=click.style(pb_mod_path, bold=True)))
if not hasattr(module_, pb_func): # pragma: no cover
raise ClickException(click.style("Failed ", fg="red") + 'to ' + click.style(
"find prebuild script ", bold=True) + 'function: "{pb_func}" '.format(
pb_func=click.style(pb_func, bold=True)) + 'in module "{pb_mod_path}"'.format(
pb_mod_path=pb_mod_path))
prebuild_function = getattr(module_, pb_func)
prebuild_function() | [
"def",
"execute_prebuild_script",
"(",
"self",
")",
":",
"(",
"pb_mod_path",
",",
"pb_func",
")",
"=",
"self",
".",
"prebuild_script",
".",
"rsplit",
"(",
"'.'",
",",
"1",
")",
"try",
":",
"# Prefer prebuild script in working directory",
"if",
"pb_mod_path",
".",
"count",
"(",
"'.'",
")",
">=",
"1",
":",
"# Prebuild script func is nested in a folder",
"(",
"mod_folder_path",
",",
"mod_name",
")",
"=",
"pb_mod_path",
".",
"rsplit",
"(",
"'.'",
",",
"1",
")",
"mod_folder_path_fragments",
"=",
"mod_folder_path",
".",
"split",
"(",
"'.'",
")",
"working_dir",
"=",
"os",
".",
"path",
".",
"join",
"(",
"os",
".",
"getcwd",
"(",
")",
",",
"*",
"mod_folder_path_fragments",
")",
"else",
":",
"mod_name",
"=",
"pb_mod_path",
"working_dir",
"=",
"os",
".",
"getcwd",
"(",
")",
"working_dir_importer",
"=",
"pkgutil",
".",
"get_importer",
"(",
"working_dir",
")",
"module_",
"=",
"working_dir_importer",
".",
"find_module",
"(",
"mod_name",
")",
".",
"load_module",
"(",
"mod_name",
")",
"except",
"(",
"ImportError",
",",
"AttributeError",
")",
":",
"try",
":",
"# Prebuild func might be in virtualenv",
"module_",
"=",
"importlib",
".",
"import_module",
"(",
"pb_mod_path",
")",
"except",
"ImportError",
":",
"# pragma: no cover",
"raise",
"ClickException",
"(",
"click",
".",
"style",
"(",
"\"Failed \"",
",",
"fg",
"=",
"\"red\"",
")",
"+",
"'to '",
"+",
"click",
".",
"style",
"(",
"\"import prebuild script \"",
",",
"bold",
"=",
"True",
")",
"+",
"'module: \"{pb_mod_path}\"'",
".",
"format",
"(",
"pb_mod_path",
"=",
"click",
".",
"style",
"(",
"pb_mod_path",
",",
"bold",
"=",
"True",
")",
")",
")",
"if",
"not",
"hasattr",
"(",
"module_",
",",
"pb_func",
")",
":",
"# pragma: no cover",
"raise",
"ClickException",
"(",
"click",
".",
"style",
"(",
"\"Failed \"",
",",
"fg",
"=",
"\"red\"",
")",
"+",
"'to '",
"+",
"click",
".",
"style",
"(",
"\"find prebuild script \"",
",",
"bold",
"=",
"True",
")",
"+",
"'function: \"{pb_func}\" '",
".",
"format",
"(",
"pb_func",
"=",
"click",
".",
"style",
"(",
"pb_func",
",",
"bold",
"=",
"True",
")",
")",
"+",
"'in module \"{pb_mod_path}\"'",
".",
"format",
"(",
"pb_mod_path",
"=",
"pb_mod_path",
")",
")",
"prebuild_function",
"=",
"getattr",
"(",
"module_",
",",
"pb_func",
")",
"prebuild_function",
"(",
")"
] | Parse and execute the prebuild_script from the zappa_settings. | [
"Parse",
"and",
"execute",
"the",
"prebuild_script",
"from",
"the",
"zappa_settings",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L2605-L2641 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.collision_warning | def collision_warning(self, item):
"""
Given a string, print a warning if this could
collide with a Zappa core package module.
Use for app functions and events.
"""
namespace_collisions = [
"zappa.", "wsgi.", "middleware.", "handler.", "util.", "letsencrypt.", "cli."
]
for namespace_collision in namespace_collisions:
if item.startswith(namespace_collision):
click.echo(click.style("Warning!", fg="red", bold=True) +
" You may have a namespace collision between " +
click.style(item, bold=True) +
" and " +
click.style(namespace_collision, bold=True) +
"! You may want to rename that file.") | python | def collision_warning(self, item):
"""
Given a string, print a warning if this could
collide with a Zappa core package module.
Use for app functions and events.
"""
namespace_collisions = [
"zappa.", "wsgi.", "middleware.", "handler.", "util.", "letsencrypt.", "cli."
]
for namespace_collision in namespace_collisions:
if item.startswith(namespace_collision):
click.echo(click.style("Warning!", fg="red", bold=True) +
" You may have a namespace collision between " +
click.style(item, bold=True) +
" and " +
click.style(namespace_collision, bold=True) +
"! You may want to rename that file.") | [
"def",
"collision_warning",
"(",
"self",
",",
"item",
")",
":",
"namespace_collisions",
"=",
"[",
"\"zappa.\"",
",",
"\"wsgi.\"",
",",
"\"middleware.\"",
",",
"\"handler.\"",
",",
"\"util.\"",
",",
"\"letsencrypt.\"",
",",
"\"cli.\"",
"]",
"for",
"namespace_collision",
"in",
"namespace_collisions",
":",
"if",
"item",
".",
"startswith",
"(",
"namespace_collision",
")",
":",
"click",
".",
"echo",
"(",
"click",
".",
"style",
"(",
"\"Warning!\"",
",",
"fg",
"=",
"\"red\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" You may have a namespace collision between \"",
"+",
"click",
".",
"style",
"(",
"item",
",",
"bold",
"=",
"True",
")",
"+",
"\" and \"",
"+",
"click",
".",
"style",
"(",
"namespace_collision",
",",
"bold",
"=",
"True",
")",
"+",
"\"! You may want to rename that file.\"",
")"
] | Given a string, print a warning if this could
collide with a Zappa core package module.
Use for app functions and events. | [
"Given",
"a",
"string",
"print",
"a",
"warning",
"if",
"this",
"could",
"collide",
"with",
"a",
"Zappa",
"core",
"package",
"module",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L2643-L2661 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.check_venv | def check_venv(self):
""" Ensure we're inside a virtualenv. """
if self.zappa:
venv = self.zappa.get_current_venv()
else:
# Just for `init`, when we don't have settings yet.
venv = Zappa.get_current_venv()
if not venv:
raise ClickException(
click.style("Zappa", bold=True) + " requires an " + click.style("active virtual environment", bold=True, fg="red") + "!\n" +
"Learn more about virtual environments here: " + click.style("http://docs.python-guide.org/en/latest/dev/virtualenvs/", bold=False, fg="cyan")) | python | def check_venv(self):
""" Ensure we're inside a virtualenv. """
if self.zappa:
venv = self.zappa.get_current_venv()
else:
# Just for `init`, when we don't have settings yet.
venv = Zappa.get_current_venv()
if not venv:
raise ClickException(
click.style("Zappa", bold=True) + " requires an " + click.style("active virtual environment", bold=True, fg="red") + "!\n" +
"Learn more about virtual environments here: " + click.style("http://docs.python-guide.org/en/latest/dev/virtualenvs/", bold=False, fg="cyan")) | [
"def",
"check_venv",
"(",
"self",
")",
":",
"if",
"self",
".",
"zappa",
":",
"venv",
"=",
"self",
".",
"zappa",
".",
"get_current_venv",
"(",
")",
"else",
":",
"# Just for `init`, when we don't have settings yet.",
"venv",
"=",
"Zappa",
".",
"get_current_venv",
"(",
")",
"if",
"not",
"venv",
":",
"raise",
"ClickException",
"(",
"click",
".",
"style",
"(",
"\"Zappa\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" requires an \"",
"+",
"click",
".",
"style",
"(",
"\"active virtual environment\"",
",",
"bold",
"=",
"True",
",",
"fg",
"=",
"\"red\"",
")",
"+",
"\"!\\n\"",
"+",
"\"Learn more about virtual environments here: \"",
"+",
"click",
".",
"style",
"(",
"\"http://docs.python-guide.org/en/latest/dev/virtualenvs/\"",
",",
"bold",
"=",
"False",
",",
"fg",
"=",
"\"cyan\"",
")",
")"
] | Ensure we're inside a virtualenv. | [
"Ensure",
"we",
"re",
"inside",
"a",
"virtualenv",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L2679-L2689 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.silence | def silence(self):
"""
Route all stdout to null.
"""
sys.stdout = open(os.devnull, 'w')
sys.stderr = open(os.devnull, 'w') | python | def silence(self):
"""
Route all stdout to null.
"""
sys.stdout = open(os.devnull, 'w')
sys.stderr = open(os.devnull, 'w') | [
"def",
"silence",
"(",
"self",
")",
":",
"sys",
".",
"stdout",
"=",
"open",
"(",
"os",
".",
"devnull",
",",
"'w'",
")",
"sys",
".",
"stderr",
"=",
"open",
"(",
"os",
".",
"devnull",
",",
"'w'",
")"
] | Route all stdout to null. | [
"Route",
"all",
"stdout",
"to",
"null",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L2691-L2697 | train |
Miserlou/Zappa | zappa/cli.py | ZappaCLI.touch_endpoint | def touch_endpoint(self, endpoint_url):
"""
Test the deployed endpoint with a GET request.
"""
# Private APIGW endpoints most likely can't be reached by a deployer
# unless they're connected to the VPC by VPN. Instead of trying
# connect to the service, print a warning and let the user know
# to check it manually.
# See: https://github.com/Miserlou/Zappa/pull/1719#issuecomment-471341565
if 'PRIVATE' in self.stage_config.get('endpoint_configuration', []):
print(
click.style("Warning!", fg="yellow", bold=True) +
" Since you're deploying a private API Gateway endpoint,"
" Zappa cannot determine if your function is returning "
" a correct status code. You should check your API's response"
" manually before considering this deployment complete."
)
return
touch_path = self.stage_config.get('touch_path', '/')
req = requests.get(endpoint_url + touch_path)
# Sometimes on really large packages, it can take 60-90 secs to be
# ready and requests will return 504 status_code until ready.
# So, if we get a 504 status code, rerun the request up to 4 times or
# until we don't get a 504 error
if req.status_code == 504:
i = 0
status_code = 504
while status_code == 504 and i <= 4:
req = requests.get(endpoint_url + touch_path)
status_code = req.status_code
i += 1
if req.status_code >= 500:
raise ClickException(click.style("Warning!", fg="red", bold=True) +
" Status check on the deployed lambda failed." +
" A GET request to '" + touch_path + "' yielded a " +
click.style(str(req.status_code), fg="red", bold=True) + " response code.") | python | def touch_endpoint(self, endpoint_url):
"""
Test the deployed endpoint with a GET request.
"""
# Private APIGW endpoints most likely can't be reached by a deployer
# unless they're connected to the VPC by VPN. Instead of trying
# connect to the service, print a warning and let the user know
# to check it manually.
# See: https://github.com/Miserlou/Zappa/pull/1719#issuecomment-471341565
if 'PRIVATE' in self.stage_config.get('endpoint_configuration', []):
print(
click.style("Warning!", fg="yellow", bold=True) +
" Since you're deploying a private API Gateway endpoint,"
" Zappa cannot determine if your function is returning "
" a correct status code. You should check your API's response"
" manually before considering this deployment complete."
)
return
touch_path = self.stage_config.get('touch_path', '/')
req = requests.get(endpoint_url + touch_path)
# Sometimes on really large packages, it can take 60-90 secs to be
# ready and requests will return 504 status_code until ready.
# So, if we get a 504 status code, rerun the request up to 4 times or
# until we don't get a 504 error
if req.status_code == 504:
i = 0
status_code = 504
while status_code == 504 and i <= 4:
req = requests.get(endpoint_url + touch_path)
status_code = req.status_code
i += 1
if req.status_code >= 500:
raise ClickException(click.style("Warning!", fg="red", bold=True) +
" Status check on the deployed lambda failed." +
" A GET request to '" + touch_path + "' yielded a " +
click.style(str(req.status_code), fg="red", bold=True) + " response code.") | [
"def",
"touch_endpoint",
"(",
"self",
",",
"endpoint_url",
")",
":",
"# Private APIGW endpoints most likely can't be reached by a deployer",
"# unless they're connected to the VPC by VPN. Instead of trying",
"# connect to the service, print a warning and let the user know",
"# to check it manually.",
"# See: https://github.com/Miserlou/Zappa/pull/1719#issuecomment-471341565",
"if",
"'PRIVATE'",
"in",
"self",
".",
"stage_config",
".",
"get",
"(",
"'endpoint_configuration'",
",",
"[",
"]",
")",
":",
"print",
"(",
"click",
".",
"style",
"(",
"\"Warning!\"",
",",
"fg",
"=",
"\"yellow\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" Since you're deploying a private API Gateway endpoint,\"",
"\" Zappa cannot determine if your function is returning \"",
"\" a correct status code. You should check your API's response\"",
"\" manually before considering this deployment complete.\"",
")",
"return",
"touch_path",
"=",
"self",
".",
"stage_config",
".",
"get",
"(",
"'touch_path'",
",",
"'/'",
")",
"req",
"=",
"requests",
".",
"get",
"(",
"endpoint_url",
"+",
"touch_path",
")",
"# Sometimes on really large packages, it can take 60-90 secs to be",
"# ready and requests will return 504 status_code until ready.",
"# So, if we get a 504 status code, rerun the request up to 4 times or",
"# until we don't get a 504 error",
"if",
"req",
".",
"status_code",
"==",
"504",
":",
"i",
"=",
"0",
"status_code",
"=",
"504",
"while",
"status_code",
"==",
"504",
"and",
"i",
"<=",
"4",
":",
"req",
"=",
"requests",
".",
"get",
"(",
"endpoint_url",
"+",
"touch_path",
")",
"status_code",
"=",
"req",
".",
"status_code",
"i",
"+=",
"1",
"if",
"req",
".",
"status_code",
">=",
"500",
":",
"raise",
"ClickException",
"(",
"click",
".",
"style",
"(",
"\"Warning!\"",
",",
"fg",
"=",
"\"red\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" Status check on the deployed lambda failed.\"",
"+",
"\" A GET request to '\"",
"+",
"touch_path",
"+",
"\"' yielded a \"",
"+",
"click",
".",
"style",
"(",
"str",
"(",
"req",
".",
"status_code",
")",
",",
"fg",
"=",
"\"red\"",
",",
"bold",
"=",
"True",
")",
"+",
"\" response code.\"",
")"
] | Test the deployed endpoint with a GET request. | [
"Test",
"the",
"deployed",
"endpoint",
"with",
"a",
"GET",
"request",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/cli.py#L2699-L2738 | train |
Miserlou/Zappa | zappa/middleware.py | all_casings | def all_casings(input_string):
"""
Permute all casings of a given string.
A pretty algorithm, via @Amber
http://stackoverflow.com/questions/6792803/finding-all-possible-case-permutations-in-python
"""
if not input_string:
yield ""
else:
first = input_string[:1]
if first.lower() == first.upper():
for sub_casing in all_casings(input_string[1:]):
yield first + sub_casing
else:
for sub_casing in all_casings(input_string[1:]):
yield first.lower() + sub_casing
yield first.upper() + sub_casing | python | def all_casings(input_string):
"""
Permute all casings of a given string.
A pretty algorithm, via @Amber
http://stackoverflow.com/questions/6792803/finding-all-possible-case-permutations-in-python
"""
if not input_string:
yield ""
else:
first = input_string[:1]
if first.lower() == first.upper():
for sub_casing in all_casings(input_string[1:]):
yield first + sub_casing
else:
for sub_casing in all_casings(input_string[1:]):
yield first.lower() + sub_casing
yield first.upper() + sub_casing | [
"def",
"all_casings",
"(",
"input_string",
")",
":",
"if",
"not",
"input_string",
":",
"yield",
"\"\"",
"else",
":",
"first",
"=",
"input_string",
"[",
":",
"1",
"]",
"if",
"first",
".",
"lower",
"(",
")",
"==",
"first",
".",
"upper",
"(",
")",
":",
"for",
"sub_casing",
"in",
"all_casings",
"(",
"input_string",
"[",
"1",
":",
"]",
")",
":",
"yield",
"first",
"+",
"sub_casing",
"else",
":",
"for",
"sub_casing",
"in",
"all_casings",
"(",
"input_string",
"[",
"1",
":",
"]",
")",
":",
"yield",
"first",
".",
"lower",
"(",
")",
"+",
"sub_casing",
"yield",
"first",
".",
"upper",
"(",
")",
"+",
"sub_casing"
] | Permute all casings of a given string.
A pretty algorithm, via @Amber
http://stackoverflow.com/questions/6792803/finding-all-possible-case-permutations-in-python | [
"Permute",
"all",
"casings",
"of",
"a",
"given",
"string",
"."
] | 3ccf7490a8d8b8fa74a61ee39bf44234f3567739 | https://github.com/Miserlou/Zappa/blob/3ccf7490a8d8b8fa74a61ee39bf44234f3567739/zappa/middleware.py#L4-L21 | train |
binux/pyspider | pyspider/database/sqlite/taskdb.py | TaskDB.status_count | def status_count(self, project):
'''
return a dict
'''
result = dict()
if project not in self.projects:
self._list_project()
if project not in self.projects:
return result
tablename = self._tablename(project)
for status, count in self._execute("SELECT `status`, count(1) FROM %s GROUP BY `status`" %
self.escape(tablename)):
result[status] = count
return result | python | def status_count(self, project):
'''
return a dict
'''
result = dict()
if project not in self.projects:
self._list_project()
if project not in self.projects:
return result
tablename = self._tablename(project)
for status, count in self._execute("SELECT `status`, count(1) FROM %s GROUP BY `status`" %
self.escape(tablename)):
result[status] = count
return result | [
"def",
"status_count",
"(",
"self",
",",
"project",
")",
":",
"result",
"=",
"dict",
"(",
")",
"if",
"project",
"not",
"in",
"self",
".",
"projects",
":",
"self",
".",
"_list_project",
"(",
")",
"if",
"project",
"not",
"in",
"self",
".",
"projects",
":",
"return",
"result",
"tablename",
"=",
"self",
".",
"_tablename",
"(",
"project",
")",
"for",
"status",
",",
"count",
"in",
"self",
".",
"_execute",
"(",
"\"SELECT `status`, count(1) FROM %s GROUP BY `status`\"",
"%",
"self",
".",
"escape",
"(",
"tablename",
")",
")",
":",
"result",
"[",
"status",
"]",
"=",
"count",
"return",
"result"
] | return a dict | [
"return",
"a",
"dict"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/database/sqlite/taskdb.py#L85-L98 | train |
binux/pyspider | pyspider/libs/response.py | get_encoding | def get_encoding(headers, content):
"""Get encoding from request headers or page head."""
encoding = None
content_type = headers.get('content-type')
if content_type:
_, params = cgi.parse_header(content_type)
if 'charset' in params:
encoding = params['charset'].strip("'\"")
if not encoding:
content = utils.pretty_unicode(content[:1000]) if six.PY3 else content
charset_re = re.compile(r'<meta.*?charset=["\']*(.+?)["\'>]',
flags=re.I)
pragma_re = re.compile(r'<meta.*?content=["\']*;?charset=(.+?)["\'>]',
flags=re.I)
xml_re = re.compile(r'^<\?xml.*?encoding=["\']*(.+?)["\'>]')
encoding = (charset_re.findall(content) +
pragma_re.findall(content) +
xml_re.findall(content))
encoding = encoding and encoding[0] or None
return encoding | python | def get_encoding(headers, content):
"""Get encoding from request headers or page head."""
encoding = None
content_type = headers.get('content-type')
if content_type:
_, params = cgi.parse_header(content_type)
if 'charset' in params:
encoding = params['charset'].strip("'\"")
if not encoding:
content = utils.pretty_unicode(content[:1000]) if six.PY3 else content
charset_re = re.compile(r'<meta.*?charset=["\']*(.+?)["\'>]',
flags=re.I)
pragma_re = re.compile(r'<meta.*?content=["\']*;?charset=(.+?)["\'>]',
flags=re.I)
xml_re = re.compile(r'^<\?xml.*?encoding=["\']*(.+?)["\'>]')
encoding = (charset_re.findall(content) +
pragma_re.findall(content) +
xml_re.findall(content))
encoding = encoding and encoding[0] or None
return encoding | [
"def",
"get_encoding",
"(",
"headers",
",",
"content",
")",
":",
"encoding",
"=",
"None",
"content_type",
"=",
"headers",
".",
"get",
"(",
"'content-type'",
")",
"if",
"content_type",
":",
"_",
",",
"params",
"=",
"cgi",
".",
"parse_header",
"(",
"content_type",
")",
"if",
"'charset'",
"in",
"params",
":",
"encoding",
"=",
"params",
"[",
"'charset'",
"]",
".",
"strip",
"(",
"\"'\\\"\"",
")",
"if",
"not",
"encoding",
":",
"content",
"=",
"utils",
".",
"pretty_unicode",
"(",
"content",
"[",
":",
"1000",
"]",
")",
"if",
"six",
".",
"PY3",
"else",
"content",
"charset_re",
"=",
"re",
".",
"compile",
"(",
"r'<meta.*?charset=[\"\\']*(.+?)[\"\\'>]'",
",",
"flags",
"=",
"re",
".",
"I",
")",
"pragma_re",
"=",
"re",
".",
"compile",
"(",
"r'<meta.*?content=[\"\\']*;?charset=(.+?)[\"\\'>]'",
",",
"flags",
"=",
"re",
".",
"I",
")",
"xml_re",
"=",
"re",
".",
"compile",
"(",
"r'^<\\?xml.*?encoding=[\"\\']*(.+?)[\"\\'>]'",
")",
"encoding",
"=",
"(",
"charset_re",
".",
"findall",
"(",
"content",
")",
"+",
"pragma_re",
".",
"findall",
"(",
"content",
")",
"+",
"xml_re",
".",
"findall",
"(",
"content",
")",
")",
"encoding",
"=",
"encoding",
"and",
"encoding",
"[",
"0",
"]",
"or",
"None",
"return",
"encoding"
] | Get encoding from request headers or page head. | [
"Get",
"encoding",
"from",
"request",
"headers",
"or",
"page",
"head",
"."
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/response.py#L211-L234 | train |
binux/pyspider | pyspider/libs/response.py | Response.encoding | def encoding(self):
"""
encoding of Response.content.
if Response.encoding is None, encoding will be guessed
by header or content or chardet if available.
"""
if hasattr(self, '_encoding'):
return self._encoding
# content is unicode
if isinstance(self.content, six.text_type):
return 'unicode'
# Try charset from content-type or content
encoding = get_encoding(self.headers, self.content)
# Fallback to auto-detected encoding.
if not encoding and chardet is not None:
encoding = chardet.detect(self.content[:600])['encoding']
if encoding and encoding.lower() == 'gb2312':
encoding = 'gb18030'
self._encoding = encoding or 'utf-8'
return self._encoding | python | def encoding(self):
"""
encoding of Response.content.
if Response.encoding is None, encoding will be guessed
by header or content or chardet if available.
"""
if hasattr(self, '_encoding'):
return self._encoding
# content is unicode
if isinstance(self.content, six.text_type):
return 'unicode'
# Try charset from content-type or content
encoding = get_encoding(self.headers, self.content)
# Fallback to auto-detected encoding.
if not encoding and chardet is not None:
encoding = chardet.detect(self.content[:600])['encoding']
if encoding and encoding.lower() == 'gb2312':
encoding = 'gb18030'
self._encoding = encoding or 'utf-8'
return self._encoding | [
"def",
"encoding",
"(",
"self",
")",
":",
"if",
"hasattr",
"(",
"self",
",",
"'_encoding'",
")",
":",
"return",
"self",
".",
"_encoding",
"# content is unicode",
"if",
"isinstance",
"(",
"self",
".",
"content",
",",
"six",
".",
"text_type",
")",
":",
"return",
"'unicode'",
"# Try charset from content-type or content",
"encoding",
"=",
"get_encoding",
"(",
"self",
".",
"headers",
",",
"self",
".",
"content",
")",
"# Fallback to auto-detected encoding.",
"if",
"not",
"encoding",
"and",
"chardet",
"is",
"not",
"None",
":",
"encoding",
"=",
"chardet",
".",
"detect",
"(",
"self",
".",
"content",
"[",
":",
"600",
"]",
")",
"[",
"'encoding'",
"]",
"if",
"encoding",
"and",
"encoding",
".",
"lower",
"(",
")",
"==",
"'gb2312'",
":",
"encoding",
"=",
"'gb18030'",
"self",
".",
"_encoding",
"=",
"encoding",
"or",
"'utf-8'",
"return",
"self",
".",
"_encoding"
] | encoding of Response.content.
if Response.encoding is None, encoding will be guessed
by header or content or chardet if available. | [
"encoding",
"of",
"Response",
".",
"content",
"."
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/response.py#L61-L86 | train |
binux/pyspider | pyspider/libs/response.py | Response.text | def text(self):
"""
Content of the response, in unicode.
if Response.encoding is None and chardet module is available, encoding
will be guessed.
"""
if hasattr(self, '_text') and self._text:
return self._text
if not self.content:
return u''
if isinstance(self.content, six.text_type):
return self.content
content = None
encoding = self.encoding
# Decode unicode from given encoding.
try:
content = self.content.decode(encoding, 'replace')
except LookupError:
# A LookupError is raised if the encoding was not found which could
# indicate a misspelling or similar mistake.
#
# So we try blindly encoding.
content = self.content.decode('utf-8', 'replace')
self._text = content
return content | python | def text(self):
"""
Content of the response, in unicode.
if Response.encoding is None and chardet module is available, encoding
will be guessed.
"""
if hasattr(self, '_text') and self._text:
return self._text
if not self.content:
return u''
if isinstance(self.content, six.text_type):
return self.content
content = None
encoding = self.encoding
# Decode unicode from given encoding.
try:
content = self.content.decode(encoding, 'replace')
except LookupError:
# A LookupError is raised if the encoding was not found which could
# indicate a misspelling or similar mistake.
#
# So we try blindly encoding.
content = self.content.decode('utf-8', 'replace')
self._text = content
return content | [
"def",
"text",
"(",
"self",
")",
":",
"if",
"hasattr",
"(",
"self",
",",
"'_text'",
")",
"and",
"self",
".",
"_text",
":",
"return",
"self",
".",
"_text",
"if",
"not",
"self",
".",
"content",
":",
"return",
"u''",
"if",
"isinstance",
"(",
"self",
".",
"content",
",",
"six",
".",
"text_type",
")",
":",
"return",
"self",
".",
"content",
"content",
"=",
"None",
"encoding",
"=",
"self",
".",
"encoding",
"# Decode unicode from given encoding.",
"try",
":",
"content",
"=",
"self",
".",
"content",
".",
"decode",
"(",
"encoding",
",",
"'replace'",
")",
"except",
"LookupError",
":",
"# A LookupError is raised if the encoding was not found which could",
"# indicate a misspelling or similar mistake.",
"#",
"# So we try blindly encoding.",
"content",
"=",
"self",
".",
"content",
".",
"decode",
"(",
"'utf-8'",
",",
"'replace'",
")",
"self",
".",
"_text",
"=",
"content",
"return",
"content"
] | Content of the response, in unicode.
if Response.encoding is None and chardet module is available, encoding
will be guessed. | [
"Content",
"of",
"the",
"response",
"in",
"unicode",
"."
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/response.py#L98-L126 | train |
binux/pyspider | pyspider/libs/response.py | Response.json | def json(self):
"""Returns the json-encoded content of the response, if any."""
if hasattr(self, '_json'):
return self._json
try:
self._json = json.loads(self.text or self.content)
except ValueError:
self._json = None
return self._json | python | def json(self):
"""Returns the json-encoded content of the response, if any."""
if hasattr(self, '_json'):
return self._json
try:
self._json = json.loads(self.text or self.content)
except ValueError:
self._json = None
return self._json | [
"def",
"json",
"(",
"self",
")",
":",
"if",
"hasattr",
"(",
"self",
",",
"'_json'",
")",
":",
"return",
"self",
".",
"_json",
"try",
":",
"self",
".",
"_json",
"=",
"json",
".",
"loads",
"(",
"self",
".",
"text",
"or",
"self",
".",
"content",
")",
"except",
"ValueError",
":",
"self",
".",
"_json",
"=",
"None",
"return",
"self",
".",
"_json"
] | Returns the json-encoded content of the response, if any. | [
"Returns",
"the",
"json",
"-",
"encoded",
"content",
"of",
"the",
"response",
"if",
"any",
"."
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/response.py#L129-L137 | train |
binux/pyspider | pyspider/libs/response.py | Response.doc | def doc(self):
"""Returns a PyQuery object of the response's content"""
if hasattr(self, '_doc'):
return self._doc
elements = self.etree
doc = self._doc = PyQuery(elements)
doc.make_links_absolute(utils.text(self.url))
return doc | python | def doc(self):
"""Returns a PyQuery object of the response's content"""
if hasattr(self, '_doc'):
return self._doc
elements = self.etree
doc = self._doc = PyQuery(elements)
doc.make_links_absolute(utils.text(self.url))
return doc | [
"def",
"doc",
"(",
"self",
")",
":",
"if",
"hasattr",
"(",
"self",
",",
"'_doc'",
")",
":",
"return",
"self",
".",
"_doc",
"elements",
"=",
"self",
".",
"etree",
"doc",
"=",
"self",
".",
"_doc",
"=",
"PyQuery",
"(",
"elements",
")",
"doc",
".",
"make_links_absolute",
"(",
"utils",
".",
"text",
"(",
"self",
".",
"url",
")",
")",
"return",
"doc"
] | Returns a PyQuery object of the response's content | [
"Returns",
"a",
"PyQuery",
"object",
"of",
"the",
"response",
"s",
"content"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/response.py#L140-L147 | train |
binux/pyspider | pyspider/libs/response.py | Response.etree | def etree(self):
"""Returns a lxml object of the response's content that can be selected by xpath"""
if not hasattr(self, '_elements'):
try:
parser = lxml.html.HTMLParser(encoding=self.encoding)
self._elements = lxml.html.fromstring(self.content, parser=parser)
except LookupError:
# lxml would raise LookupError when encoding not supported
# try fromstring without encoding instead.
# on windows, unicode is not availabe as encoding for lxml
self._elements = lxml.html.fromstring(self.content)
if isinstance(self._elements, lxml.etree._ElementTree):
self._elements = self._elements.getroot()
return self._elements | python | def etree(self):
"""Returns a lxml object of the response's content that can be selected by xpath"""
if not hasattr(self, '_elements'):
try:
parser = lxml.html.HTMLParser(encoding=self.encoding)
self._elements = lxml.html.fromstring(self.content, parser=parser)
except LookupError:
# lxml would raise LookupError when encoding not supported
# try fromstring without encoding instead.
# on windows, unicode is not availabe as encoding for lxml
self._elements = lxml.html.fromstring(self.content)
if isinstance(self._elements, lxml.etree._ElementTree):
self._elements = self._elements.getroot()
return self._elements | [
"def",
"etree",
"(",
"self",
")",
":",
"if",
"not",
"hasattr",
"(",
"self",
",",
"'_elements'",
")",
":",
"try",
":",
"parser",
"=",
"lxml",
".",
"html",
".",
"HTMLParser",
"(",
"encoding",
"=",
"self",
".",
"encoding",
")",
"self",
".",
"_elements",
"=",
"lxml",
".",
"html",
".",
"fromstring",
"(",
"self",
".",
"content",
",",
"parser",
"=",
"parser",
")",
"except",
"LookupError",
":",
"# lxml would raise LookupError when encoding not supported",
"# try fromstring without encoding instead.",
"# on windows, unicode is not availabe as encoding for lxml",
"self",
".",
"_elements",
"=",
"lxml",
".",
"html",
".",
"fromstring",
"(",
"self",
".",
"content",
")",
"if",
"isinstance",
"(",
"self",
".",
"_elements",
",",
"lxml",
".",
"etree",
".",
"_ElementTree",
")",
":",
"self",
".",
"_elements",
"=",
"self",
".",
"_elements",
".",
"getroot",
"(",
")",
"return",
"self",
".",
"_elements"
] | Returns a lxml object of the response's content that can be selected by xpath | [
"Returns",
"a",
"lxml",
"object",
"of",
"the",
"response",
"s",
"content",
"that",
"can",
"be",
"selected",
"by",
"xpath"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/response.py#L150-L163 | train |
binux/pyspider | pyspider/libs/response.py | Response.raise_for_status | def raise_for_status(self, allow_redirects=True):
"""Raises stored :class:`HTTPError` or :class:`URLError`, if one occurred."""
if self.status_code == 304:
return
elif self.error:
if self.traceback:
six.reraise(Exception, Exception(self.error), Traceback.from_string(self.traceback).as_traceback())
http_error = HTTPError(self.error)
elif (self.status_code >= 300) and (self.status_code < 400) and not allow_redirects:
http_error = HTTPError('%s Redirection' % (self.status_code))
elif (self.status_code >= 400) and (self.status_code < 500):
http_error = HTTPError('%s Client Error' % (self.status_code))
elif (self.status_code >= 500) and (self.status_code < 600):
http_error = HTTPError('%s Server Error' % (self.status_code))
else:
return
http_error.response = self
raise http_error | python | def raise_for_status(self, allow_redirects=True):
"""Raises stored :class:`HTTPError` or :class:`URLError`, if one occurred."""
if self.status_code == 304:
return
elif self.error:
if self.traceback:
six.reraise(Exception, Exception(self.error), Traceback.from_string(self.traceback).as_traceback())
http_error = HTTPError(self.error)
elif (self.status_code >= 300) and (self.status_code < 400) and not allow_redirects:
http_error = HTTPError('%s Redirection' % (self.status_code))
elif (self.status_code >= 400) and (self.status_code < 500):
http_error = HTTPError('%s Client Error' % (self.status_code))
elif (self.status_code >= 500) and (self.status_code < 600):
http_error = HTTPError('%s Server Error' % (self.status_code))
else:
return
http_error.response = self
raise http_error | [
"def",
"raise_for_status",
"(",
"self",
",",
"allow_redirects",
"=",
"True",
")",
":",
"if",
"self",
".",
"status_code",
"==",
"304",
":",
"return",
"elif",
"self",
".",
"error",
":",
"if",
"self",
".",
"traceback",
":",
"six",
".",
"reraise",
"(",
"Exception",
",",
"Exception",
"(",
"self",
".",
"error",
")",
",",
"Traceback",
".",
"from_string",
"(",
"self",
".",
"traceback",
")",
".",
"as_traceback",
"(",
")",
")",
"http_error",
"=",
"HTTPError",
"(",
"self",
".",
"error",
")",
"elif",
"(",
"self",
".",
"status_code",
">=",
"300",
")",
"and",
"(",
"self",
".",
"status_code",
"<",
"400",
")",
"and",
"not",
"allow_redirects",
":",
"http_error",
"=",
"HTTPError",
"(",
"'%s Redirection'",
"%",
"(",
"self",
".",
"status_code",
")",
")",
"elif",
"(",
"self",
".",
"status_code",
">=",
"400",
")",
"and",
"(",
"self",
".",
"status_code",
"<",
"500",
")",
":",
"http_error",
"=",
"HTTPError",
"(",
"'%s Client Error'",
"%",
"(",
"self",
".",
"status_code",
")",
")",
"elif",
"(",
"self",
".",
"status_code",
">=",
"500",
")",
"and",
"(",
"self",
".",
"status_code",
"<",
"600",
")",
":",
"http_error",
"=",
"HTTPError",
"(",
"'%s Server Error'",
"%",
"(",
"self",
".",
"status_code",
")",
")",
"else",
":",
"return",
"http_error",
".",
"response",
"=",
"self",
"raise",
"http_error"
] | Raises stored :class:`HTTPError` or :class:`URLError`, if one occurred. | [
"Raises",
"stored",
":",
"class",
":",
"HTTPError",
"or",
":",
"class",
":",
"URLError",
"if",
"one",
"occurred",
"."
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/response.py#L165-L184 | train |
binux/pyspider | pyspider/libs/base_handler.py | not_send_status | def not_send_status(func):
"""
Do not send process status package back to scheduler.
It's used by callbacks like on_message, on_result etc...
"""
@functools.wraps(func)
def wrapper(self, response, task):
self._extinfo['not_send_status'] = True
function = func.__get__(self, self.__class__)
return self._run_func(function, response, task)
return wrapper | python | def not_send_status(func):
"""
Do not send process status package back to scheduler.
It's used by callbacks like on_message, on_result etc...
"""
@functools.wraps(func)
def wrapper(self, response, task):
self._extinfo['not_send_status'] = True
function = func.__get__(self, self.__class__)
return self._run_func(function, response, task)
return wrapper | [
"def",
"not_send_status",
"(",
"func",
")",
":",
"@",
"functools",
".",
"wraps",
"(",
"func",
")",
"def",
"wrapper",
"(",
"self",
",",
"response",
",",
"task",
")",
":",
"self",
".",
"_extinfo",
"[",
"'not_send_status'",
"]",
"=",
"True",
"function",
"=",
"func",
".",
"__get__",
"(",
"self",
",",
"self",
".",
"__class__",
")",
"return",
"self",
".",
"_run_func",
"(",
"function",
",",
"response",
",",
"task",
")",
"return",
"wrapper"
] | Do not send process status package back to scheduler.
It's used by callbacks like on_message, on_result etc... | [
"Do",
"not",
"send",
"process",
"status",
"package",
"back",
"to",
"scheduler",
"."
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/base_handler.py#L35-L46 | train |
binux/pyspider | pyspider/libs/base_handler.py | config | def config(_config=None, **kwargs):
"""
A decorator for setting the default kwargs of `BaseHandler.crawl`.
Any self.crawl with this callback will use this config.
"""
if _config is None:
_config = {}
_config.update(kwargs)
def wrapper(func):
func._config = _config
return func
return wrapper | python | def config(_config=None, **kwargs):
"""
A decorator for setting the default kwargs of `BaseHandler.crawl`.
Any self.crawl with this callback will use this config.
"""
if _config is None:
_config = {}
_config.update(kwargs)
def wrapper(func):
func._config = _config
return func
return wrapper | [
"def",
"config",
"(",
"_config",
"=",
"None",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"_config",
"is",
"None",
":",
"_config",
"=",
"{",
"}",
"_config",
".",
"update",
"(",
"kwargs",
")",
"def",
"wrapper",
"(",
"func",
")",
":",
"func",
".",
"_config",
"=",
"_config",
"return",
"func",
"return",
"wrapper"
] | A decorator for setting the default kwargs of `BaseHandler.crawl`.
Any self.crawl with this callback will use this config. | [
"A",
"decorator",
"for",
"setting",
"the",
"default",
"kwargs",
"of",
"BaseHandler",
".",
"crawl",
".",
"Any",
"self",
".",
"crawl",
"with",
"this",
"callback",
"will",
"use",
"this",
"config",
"."
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/base_handler.py#L49-L61 | train |
binux/pyspider | pyspider/libs/base_handler.py | every | def every(minutes=NOTSET, seconds=NOTSET):
"""
method will been called every minutes or seconds
"""
def wrapper(func):
# mark the function with variable 'is_cronjob=True', the function would be
# collected into the list Handler._cron_jobs by meta class
func.is_cronjob = True
# collect interval and unify to seconds, it's used in meta class. See the
# comments in meta class.
func.tick = minutes * 60 + seconds
return func
if inspect.isfunction(minutes):
func = minutes
minutes = 1
seconds = 0
return wrapper(func)
if minutes is NOTSET:
if seconds is NOTSET:
minutes = 1
seconds = 0
else:
minutes = 0
if seconds is NOTSET:
seconds = 0
return wrapper | python | def every(minutes=NOTSET, seconds=NOTSET):
"""
method will been called every minutes or seconds
"""
def wrapper(func):
# mark the function with variable 'is_cronjob=True', the function would be
# collected into the list Handler._cron_jobs by meta class
func.is_cronjob = True
# collect interval and unify to seconds, it's used in meta class. See the
# comments in meta class.
func.tick = minutes * 60 + seconds
return func
if inspect.isfunction(minutes):
func = minutes
minutes = 1
seconds = 0
return wrapper(func)
if minutes is NOTSET:
if seconds is NOTSET:
minutes = 1
seconds = 0
else:
minutes = 0
if seconds is NOTSET:
seconds = 0
return wrapper | [
"def",
"every",
"(",
"minutes",
"=",
"NOTSET",
",",
"seconds",
"=",
"NOTSET",
")",
":",
"def",
"wrapper",
"(",
"func",
")",
":",
"# mark the function with variable 'is_cronjob=True', the function would be",
"# collected into the list Handler._cron_jobs by meta class",
"func",
".",
"is_cronjob",
"=",
"True",
"# collect interval and unify to seconds, it's used in meta class. See the",
"# comments in meta class.",
"func",
".",
"tick",
"=",
"minutes",
"*",
"60",
"+",
"seconds",
"return",
"func",
"if",
"inspect",
".",
"isfunction",
"(",
"minutes",
")",
":",
"func",
"=",
"minutes",
"minutes",
"=",
"1",
"seconds",
"=",
"0",
"return",
"wrapper",
"(",
"func",
")",
"if",
"minutes",
"is",
"NOTSET",
":",
"if",
"seconds",
"is",
"NOTSET",
":",
"minutes",
"=",
"1",
"seconds",
"=",
"0",
"else",
":",
"minutes",
"=",
"0",
"if",
"seconds",
"is",
"NOTSET",
":",
"seconds",
"=",
"0",
"return",
"wrapper"
] | method will been called every minutes or seconds | [
"method",
"will",
"been",
"called",
"every",
"minutes",
"or",
"seconds"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/base_handler.py#L68-L97 | train |
binux/pyspider | pyspider/message_queue/rabbitmq.py | catch_error | def catch_error(func):
"""Catch errors of rabbitmq then reconnect"""
import amqp
try:
import pika.exceptions
connect_exceptions = (
pika.exceptions.ConnectionClosed,
pika.exceptions.AMQPConnectionError,
)
except ImportError:
connect_exceptions = ()
connect_exceptions += (
select.error,
socket.error,
amqp.ConnectionError
)
def wrap(self, *args, **kwargs):
try:
return func(self, *args, **kwargs)
except connect_exceptions as e:
logging.error('RabbitMQ error: %r, reconnect.', e)
self.reconnect()
return func(self, *args, **kwargs)
return wrap | python | def catch_error(func):
"""Catch errors of rabbitmq then reconnect"""
import amqp
try:
import pika.exceptions
connect_exceptions = (
pika.exceptions.ConnectionClosed,
pika.exceptions.AMQPConnectionError,
)
except ImportError:
connect_exceptions = ()
connect_exceptions += (
select.error,
socket.error,
amqp.ConnectionError
)
def wrap(self, *args, **kwargs):
try:
return func(self, *args, **kwargs)
except connect_exceptions as e:
logging.error('RabbitMQ error: %r, reconnect.', e)
self.reconnect()
return func(self, *args, **kwargs)
return wrap | [
"def",
"catch_error",
"(",
"func",
")",
":",
"import",
"amqp",
"try",
":",
"import",
"pika",
".",
"exceptions",
"connect_exceptions",
"=",
"(",
"pika",
".",
"exceptions",
".",
"ConnectionClosed",
",",
"pika",
".",
"exceptions",
".",
"AMQPConnectionError",
",",
")",
"except",
"ImportError",
":",
"connect_exceptions",
"=",
"(",
")",
"connect_exceptions",
"+=",
"(",
"select",
".",
"error",
",",
"socket",
".",
"error",
",",
"amqp",
".",
"ConnectionError",
")",
"def",
"wrap",
"(",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"try",
":",
"return",
"func",
"(",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
"except",
"connect_exceptions",
"as",
"e",
":",
"logging",
".",
"error",
"(",
"'RabbitMQ error: %r, reconnect.'",
",",
"e",
")",
"self",
".",
"reconnect",
"(",
")",
"return",
"func",
"(",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
"return",
"wrap"
] | Catch errors of rabbitmq then reconnect | [
"Catch",
"errors",
"of",
"rabbitmq",
"then",
"reconnect"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/message_queue/rabbitmq.py#L24-L49 | train |
binux/pyspider | pyspider/message_queue/rabbitmq.py | PikaQueue.reconnect | def reconnect(self):
"""Reconnect to rabbitmq server"""
import pika
import pika.exceptions
self.connection = pika.BlockingConnection(pika.URLParameters(self.amqp_url))
self.channel = self.connection.channel()
try:
self.channel.queue_declare(self.name)
except pika.exceptions.ChannelClosed:
self.connection = pika.BlockingConnection(pika.URLParameters(self.amqp_url))
self.channel = self.connection.channel() | python | def reconnect(self):
"""Reconnect to rabbitmq server"""
import pika
import pika.exceptions
self.connection = pika.BlockingConnection(pika.URLParameters(self.amqp_url))
self.channel = self.connection.channel()
try:
self.channel.queue_declare(self.name)
except pika.exceptions.ChannelClosed:
self.connection = pika.BlockingConnection(pika.URLParameters(self.amqp_url))
self.channel = self.connection.channel() | [
"def",
"reconnect",
"(",
"self",
")",
":",
"import",
"pika",
"import",
"pika",
".",
"exceptions",
"self",
".",
"connection",
"=",
"pika",
".",
"BlockingConnection",
"(",
"pika",
".",
"URLParameters",
"(",
"self",
".",
"amqp_url",
")",
")",
"self",
".",
"channel",
"=",
"self",
".",
"connection",
".",
"channel",
"(",
")",
"try",
":",
"self",
".",
"channel",
".",
"queue_declare",
"(",
"self",
".",
"name",
")",
"except",
"pika",
".",
"exceptions",
".",
"ChannelClosed",
":",
"self",
".",
"connection",
"=",
"pika",
".",
"BlockingConnection",
"(",
"pika",
".",
"URLParameters",
"(",
"self",
".",
"amqp_url",
")",
")",
"self",
".",
"channel",
"=",
"self",
".",
"connection",
".",
"channel",
"(",
")"
] | Reconnect to rabbitmq server | [
"Reconnect",
"to",
"rabbitmq",
"server"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/message_queue/rabbitmq.py#L91-L102 | train |
binux/pyspider | pyspider/message_queue/rabbitmq.py | AmqpQueue.reconnect | def reconnect(self):
"""Reconnect to rabbitmq server"""
parsed = urlparse.urlparse(self.amqp_url)
port = parsed.port or 5672
self.connection = amqp.Connection(host="%s:%s" % (parsed.hostname, port),
userid=parsed.username or 'guest',
password=parsed.password or 'guest',
virtual_host=unquote(
parsed.path.lstrip('/') or '%2F'))
self.channel = self.connection.channel()
try:
self.channel.queue_declare(self.name)
except amqp.exceptions.PreconditionFailed:
pass | python | def reconnect(self):
"""Reconnect to rabbitmq server"""
parsed = urlparse.urlparse(self.amqp_url)
port = parsed.port or 5672
self.connection = amqp.Connection(host="%s:%s" % (parsed.hostname, port),
userid=parsed.username or 'guest',
password=parsed.password or 'guest',
virtual_host=unquote(
parsed.path.lstrip('/') or '%2F'))
self.channel = self.connection.channel()
try:
self.channel.queue_declare(self.name)
except amqp.exceptions.PreconditionFailed:
pass | [
"def",
"reconnect",
"(",
"self",
")",
":",
"parsed",
"=",
"urlparse",
".",
"urlparse",
"(",
"self",
".",
"amqp_url",
")",
"port",
"=",
"parsed",
".",
"port",
"or",
"5672",
"self",
".",
"connection",
"=",
"amqp",
".",
"Connection",
"(",
"host",
"=",
"\"%s:%s\"",
"%",
"(",
"parsed",
".",
"hostname",
",",
"port",
")",
",",
"userid",
"=",
"parsed",
".",
"username",
"or",
"'guest'",
",",
"password",
"=",
"parsed",
".",
"password",
"or",
"'guest'",
",",
"virtual_host",
"=",
"unquote",
"(",
"parsed",
".",
"path",
".",
"lstrip",
"(",
"'/'",
")",
"or",
"'%2F'",
")",
")",
"self",
".",
"channel",
"=",
"self",
".",
"connection",
".",
"channel",
"(",
")",
"try",
":",
"self",
".",
"channel",
".",
"queue_declare",
"(",
"self",
".",
"name",
")",
"except",
"amqp",
".",
"exceptions",
".",
"PreconditionFailed",
":",
"pass"
] | Reconnect to rabbitmq server | [
"Reconnect",
"to",
"rabbitmq",
"server"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/message_queue/rabbitmq.py#L224-L237 | train |
binux/pyspider | pyspider/scheduler/task_queue.py | TaskQueue.put | def put(self, taskid, priority=0, exetime=0):
"""
Put a task into task queue
when use heap sort, if we put tasks(with the same priority and exetime=0) into queue,
the queue is not a strict FIFO queue, but more like a FILO stack.
It is very possible that when there are continuous big flow, the speed of select is
slower than request, resulting in priority-queue accumulation in short time.
In this scenario, the tasks more earlier entering the priority-queue will not get
processed until the request flow becomes small.
Thus, we store a global atom self increasing value into task.sequence which represent
the task enqueue sequence. When the comparison of exetime and priority have no
difference, we compare task.sequence to ensure that the entire queue is ordered.
"""
now = time.time()
task = InQueueTask(taskid, priority, exetime)
self.mutex.acquire()
if taskid in self.priority_queue:
self.priority_queue.put(task)
elif taskid in self.time_queue:
self.time_queue.put(task)
elif taskid in self.processing and self.processing[taskid].taskid:
# force update a processing task is not allowed as there are so many
# problems may happen
pass
else:
if exetime and exetime > now:
self.time_queue.put(task)
else:
task.exetime = 0
self.priority_queue.put(task)
self.mutex.release() | python | def put(self, taskid, priority=0, exetime=0):
"""
Put a task into task queue
when use heap sort, if we put tasks(with the same priority and exetime=0) into queue,
the queue is not a strict FIFO queue, but more like a FILO stack.
It is very possible that when there are continuous big flow, the speed of select is
slower than request, resulting in priority-queue accumulation in short time.
In this scenario, the tasks more earlier entering the priority-queue will not get
processed until the request flow becomes small.
Thus, we store a global atom self increasing value into task.sequence which represent
the task enqueue sequence. When the comparison of exetime and priority have no
difference, we compare task.sequence to ensure that the entire queue is ordered.
"""
now = time.time()
task = InQueueTask(taskid, priority, exetime)
self.mutex.acquire()
if taskid in self.priority_queue:
self.priority_queue.put(task)
elif taskid in self.time_queue:
self.time_queue.put(task)
elif taskid in self.processing and self.processing[taskid].taskid:
# force update a processing task is not allowed as there are so many
# problems may happen
pass
else:
if exetime and exetime > now:
self.time_queue.put(task)
else:
task.exetime = 0
self.priority_queue.put(task)
self.mutex.release() | [
"def",
"put",
"(",
"self",
",",
"taskid",
",",
"priority",
"=",
"0",
",",
"exetime",
"=",
"0",
")",
":",
"now",
"=",
"time",
".",
"time",
"(",
")",
"task",
"=",
"InQueueTask",
"(",
"taskid",
",",
"priority",
",",
"exetime",
")",
"self",
".",
"mutex",
".",
"acquire",
"(",
")",
"if",
"taskid",
"in",
"self",
".",
"priority_queue",
":",
"self",
".",
"priority_queue",
".",
"put",
"(",
"task",
")",
"elif",
"taskid",
"in",
"self",
".",
"time_queue",
":",
"self",
".",
"time_queue",
".",
"put",
"(",
"task",
")",
"elif",
"taskid",
"in",
"self",
".",
"processing",
"and",
"self",
".",
"processing",
"[",
"taskid",
"]",
".",
"taskid",
":",
"# force update a processing task is not allowed as there are so many",
"# problems may happen",
"pass",
"else",
":",
"if",
"exetime",
"and",
"exetime",
">",
"now",
":",
"self",
".",
"time_queue",
".",
"put",
"(",
"task",
")",
"else",
":",
"task",
".",
"exetime",
"=",
"0",
"self",
".",
"priority_queue",
".",
"put",
"(",
"task",
")",
"self",
".",
"mutex",
".",
"release",
"(",
")"
] | Put a task into task queue
when use heap sort, if we put tasks(with the same priority and exetime=0) into queue,
the queue is not a strict FIFO queue, but more like a FILO stack.
It is very possible that when there are continuous big flow, the speed of select is
slower than request, resulting in priority-queue accumulation in short time.
In this scenario, the tasks more earlier entering the priority-queue will not get
processed until the request flow becomes small.
Thus, we store a global atom self increasing value into task.sequence which represent
the task enqueue sequence. When the comparison of exetime and priority have no
difference, we compare task.sequence to ensure that the entire queue is ordered. | [
"Put",
"a",
"task",
"into",
"task",
"queue",
"when",
"use",
"heap",
"sort",
"if",
"we",
"put",
"tasks",
"(",
"with",
"the",
"same",
"priority",
"and",
"exetime",
"=",
"0",
")",
"into",
"queue",
"the",
"queue",
"is",
"not",
"a",
"strict",
"FIFO",
"queue",
"but",
"more",
"like",
"a",
"FILO",
"stack",
".",
"It",
"is",
"very",
"possible",
"that",
"when",
"there",
"are",
"continuous",
"big",
"flow",
"the",
"speed",
"of",
"select",
"is",
"slower",
"than",
"request",
"resulting",
"in",
"priority",
"-",
"queue",
"accumulation",
"in",
"short",
"time",
".",
"In",
"this",
"scenario",
"the",
"tasks",
"more",
"earlier",
"entering",
"the",
"priority",
"-",
"queue",
"will",
"not",
"get",
"processed",
"until",
"the",
"request",
"flow",
"becomes",
"small",
".",
"Thus",
"we",
"store",
"a",
"global",
"atom",
"self",
"increasing",
"value",
"into",
"task",
".",
"sequence",
"which",
"represent",
"the",
"task",
"enqueue",
"sequence",
".",
"When",
"the",
"comparison",
"of",
"exetime",
"and",
"priority",
"have",
"no",
"difference",
"we",
"compare",
"task",
".",
"sequence",
"to",
"ensure",
"that",
"the",
"entire",
"queue",
"is",
"ordered",
"."
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/scheduler/task_queue.py#L190-L225 | train |
binux/pyspider | pyspider/scheduler/task_queue.py | TaskQueue.get | def get(self):
'''Get a task from queue when bucket available'''
if self.bucket.get() < 1:
return None
now = time.time()
self.mutex.acquire()
try:
task = self.priority_queue.get_nowait()
self.bucket.desc()
except Queue.Empty:
self.mutex.release()
return None
task.exetime = now + self.processing_timeout
self.processing.put(task)
self.mutex.release()
return task.taskid | python | def get(self):
'''Get a task from queue when bucket available'''
if self.bucket.get() < 1:
return None
now = time.time()
self.mutex.acquire()
try:
task = self.priority_queue.get_nowait()
self.bucket.desc()
except Queue.Empty:
self.mutex.release()
return None
task.exetime = now + self.processing_timeout
self.processing.put(task)
self.mutex.release()
return task.taskid | [
"def",
"get",
"(",
"self",
")",
":",
"if",
"self",
".",
"bucket",
".",
"get",
"(",
")",
"<",
"1",
":",
"return",
"None",
"now",
"=",
"time",
".",
"time",
"(",
")",
"self",
".",
"mutex",
".",
"acquire",
"(",
")",
"try",
":",
"task",
"=",
"self",
".",
"priority_queue",
".",
"get_nowait",
"(",
")",
"self",
".",
"bucket",
".",
"desc",
"(",
")",
"except",
"Queue",
".",
"Empty",
":",
"self",
".",
"mutex",
".",
"release",
"(",
")",
"return",
"None",
"task",
".",
"exetime",
"=",
"now",
"+",
"self",
".",
"processing_timeout",
"self",
".",
"processing",
".",
"put",
"(",
"task",
")",
"self",
".",
"mutex",
".",
"release",
"(",
")",
"return",
"task",
".",
"taskid"
] | Get a task from queue when bucket available | [
"Get",
"a",
"task",
"from",
"queue",
"when",
"bucket",
"available"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/scheduler/task_queue.py#L227-L242 | train |
binux/pyspider | pyspider/scheduler/task_queue.py | TaskQueue.done | def done(self, taskid):
'''Mark task done'''
if taskid in self.processing:
self.mutex.acquire()
if taskid in self.processing:
del self.processing[taskid]
self.mutex.release()
return True
return False | python | def done(self, taskid):
'''Mark task done'''
if taskid in self.processing:
self.mutex.acquire()
if taskid in self.processing:
del self.processing[taskid]
self.mutex.release()
return True
return False | [
"def",
"done",
"(",
"self",
",",
"taskid",
")",
":",
"if",
"taskid",
"in",
"self",
".",
"processing",
":",
"self",
".",
"mutex",
".",
"acquire",
"(",
")",
"if",
"taskid",
"in",
"self",
".",
"processing",
":",
"del",
"self",
".",
"processing",
"[",
"taskid",
"]",
"self",
".",
"mutex",
".",
"release",
"(",
")",
"return",
"True",
"return",
"False"
] | Mark task done | [
"Mark",
"task",
"done"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/scheduler/task_queue.py#L244-L252 | train |
binux/pyspider | pyspider/scheduler/task_queue.py | TaskQueue.is_processing | def is_processing(self, taskid):
'''
return True if taskid is in processing
'''
return taskid in self.processing and self.processing[taskid].taskid | python | def is_processing(self, taskid):
'''
return True if taskid is in processing
'''
return taskid in self.processing and self.processing[taskid].taskid | [
"def",
"is_processing",
"(",
"self",
",",
"taskid",
")",
":",
"return",
"taskid",
"in",
"self",
".",
"processing",
"and",
"self",
".",
"processing",
"[",
"taskid",
"]",
".",
"taskid"
] | return True if taskid is in processing | [
"return",
"True",
"if",
"taskid",
"is",
"in",
"processing"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/scheduler/task_queue.py#L272-L276 | train |
binux/pyspider | pyspider/processor/processor.py | ProcessorResult.logstr | def logstr(self):
"""handler the log records to formatted string"""
result = []
formater = LogFormatter(color=False)
for record in self.logs:
if isinstance(record, six.string_types):
result.append(pretty_unicode(record))
else:
if record.exc_info:
a, b, tb = record.exc_info
tb = hide_me(tb, globals())
record.exc_info = a, b, tb
result.append(pretty_unicode(formater.format(record)))
result.append(u'\n')
return u''.join(result) | python | def logstr(self):
"""handler the log records to formatted string"""
result = []
formater = LogFormatter(color=False)
for record in self.logs:
if isinstance(record, six.string_types):
result.append(pretty_unicode(record))
else:
if record.exc_info:
a, b, tb = record.exc_info
tb = hide_me(tb, globals())
record.exc_info = a, b, tb
result.append(pretty_unicode(formater.format(record)))
result.append(u'\n')
return u''.join(result) | [
"def",
"logstr",
"(",
"self",
")",
":",
"result",
"=",
"[",
"]",
"formater",
"=",
"LogFormatter",
"(",
"color",
"=",
"False",
")",
"for",
"record",
"in",
"self",
".",
"logs",
":",
"if",
"isinstance",
"(",
"record",
",",
"six",
".",
"string_types",
")",
":",
"result",
".",
"append",
"(",
"pretty_unicode",
"(",
"record",
")",
")",
"else",
":",
"if",
"record",
".",
"exc_info",
":",
"a",
",",
"b",
",",
"tb",
"=",
"record",
".",
"exc_info",
"tb",
"=",
"hide_me",
"(",
"tb",
",",
"globals",
"(",
")",
")",
"record",
".",
"exc_info",
"=",
"a",
",",
"b",
",",
"tb",
"result",
".",
"append",
"(",
"pretty_unicode",
"(",
"formater",
".",
"format",
"(",
"record",
")",
")",
")",
"result",
".",
"append",
"(",
"u'\\n'",
")",
"return",
"u''",
".",
"join",
"(",
"result",
")"
] | handler the log records to formatted string | [
"handler",
"the",
"log",
"records",
"to",
"formatted",
"string"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/processor/processor.py#L44-L59 | train |
binux/pyspider | pyspider/processor/processor.py | Processor.on_task | def on_task(self, task, response):
'''Deal one task'''
start_time = time.time()
response = rebuild_response(response)
try:
assert 'taskid' in task, 'need taskid in task'
project = task['project']
updatetime = task.get('project_updatetime', None)
md5sum = task.get('project_md5sum', None)
project_data = self.project_manager.get(project, updatetime, md5sum)
assert project_data, "no such project!"
if project_data.get('exception'):
ret = ProcessorResult(logs=(project_data.get('exception_log'), ),
exception=project_data['exception'])
else:
ret = project_data['instance'].run_task(
project_data['module'], task, response)
except Exception as e:
logstr = traceback.format_exc()
ret = ProcessorResult(logs=(logstr, ), exception=e)
process_time = time.time() - start_time
if not ret.extinfo.get('not_send_status', False):
if ret.exception:
track_headers = dict(response.headers)
else:
track_headers = {}
for name in ('etag', 'last-modified'):
if name not in response.headers:
continue
track_headers[name] = response.headers[name]
status_pack = {
'taskid': task['taskid'],
'project': task['project'],
'url': task.get('url'),
'track': {
'fetch': {
'ok': response.isok(),
'redirect_url': response.url if response.url != response.orig_url else None,
'time': response.time,
'error': response.error,
'status_code': response.status_code,
'encoding': getattr(response, '_encoding', None),
'headers': track_headers,
'content': response.text[:500] if ret.exception else None,
},
'process': {
'ok': not ret.exception,
'time': process_time,
'follows': len(ret.follows),
'result': (
None if ret.result is None
else utils.text(ret.result)[:self.RESULT_RESULT_LIMIT]
),
'logs': ret.logstr()[-self.RESULT_LOGS_LIMIT:],
'exception': ret.exception,
},
'save': ret.save,
},
}
if 'schedule' in task:
status_pack['schedule'] = task['schedule']
# FIXME: unicode_obj should used in scheduler before store to database
# it's used here for performance.
self.status_queue.put(utils.unicode_obj(status_pack))
# FIXME: unicode_obj should used in scheduler before store to database
# it's used here for performance.
if ret.follows:
for each in (ret.follows[x:x + 1000] for x in range(0, len(ret.follows), 1000)):
self.newtask_queue.put([utils.unicode_obj(newtask) for newtask in each])
for project, msg, url in ret.messages:
try:
self.on_task({
'taskid': utils.md5string(url),
'project': project,
'url': url,
'process': {
'callback': '_on_message',
}
}, {
'status_code': 200,
'url': url,
'save': (task['project'], msg),
})
except Exception as e:
logger.exception('Sending message error.')
continue
if ret.exception:
logger_func = logger.error
else:
logger_func = logger.info
logger_func('process %s:%s %s -> [%d] len:%d -> result:%.10r fol:%d msg:%d err:%r' % (
task['project'], task['taskid'],
task.get('url'), response.status_code, len(response.content),
ret.result, len(ret.follows), len(ret.messages), ret.exception))
return True | python | def on_task(self, task, response):
'''Deal one task'''
start_time = time.time()
response = rebuild_response(response)
try:
assert 'taskid' in task, 'need taskid in task'
project = task['project']
updatetime = task.get('project_updatetime', None)
md5sum = task.get('project_md5sum', None)
project_data = self.project_manager.get(project, updatetime, md5sum)
assert project_data, "no such project!"
if project_data.get('exception'):
ret = ProcessorResult(logs=(project_data.get('exception_log'), ),
exception=project_data['exception'])
else:
ret = project_data['instance'].run_task(
project_data['module'], task, response)
except Exception as e:
logstr = traceback.format_exc()
ret = ProcessorResult(logs=(logstr, ), exception=e)
process_time = time.time() - start_time
if not ret.extinfo.get('not_send_status', False):
if ret.exception:
track_headers = dict(response.headers)
else:
track_headers = {}
for name in ('etag', 'last-modified'):
if name not in response.headers:
continue
track_headers[name] = response.headers[name]
status_pack = {
'taskid': task['taskid'],
'project': task['project'],
'url': task.get('url'),
'track': {
'fetch': {
'ok': response.isok(),
'redirect_url': response.url if response.url != response.orig_url else None,
'time': response.time,
'error': response.error,
'status_code': response.status_code,
'encoding': getattr(response, '_encoding', None),
'headers': track_headers,
'content': response.text[:500] if ret.exception else None,
},
'process': {
'ok': not ret.exception,
'time': process_time,
'follows': len(ret.follows),
'result': (
None if ret.result is None
else utils.text(ret.result)[:self.RESULT_RESULT_LIMIT]
),
'logs': ret.logstr()[-self.RESULT_LOGS_LIMIT:],
'exception': ret.exception,
},
'save': ret.save,
},
}
if 'schedule' in task:
status_pack['schedule'] = task['schedule']
# FIXME: unicode_obj should used in scheduler before store to database
# it's used here for performance.
self.status_queue.put(utils.unicode_obj(status_pack))
# FIXME: unicode_obj should used in scheduler before store to database
# it's used here for performance.
if ret.follows:
for each in (ret.follows[x:x + 1000] for x in range(0, len(ret.follows), 1000)):
self.newtask_queue.put([utils.unicode_obj(newtask) for newtask in each])
for project, msg, url in ret.messages:
try:
self.on_task({
'taskid': utils.md5string(url),
'project': project,
'url': url,
'process': {
'callback': '_on_message',
}
}, {
'status_code': 200,
'url': url,
'save': (task['project'], msg),
})
except Exception as e:
logger.exception('Sending message error.')
continue
if ret.exception:
logger_func = logger.error
else:
logger_func = logger.info
logger_func('process %s:%s %s -> [%d] len:%d -> result:%.10r fol:%d msg:%d err:%r' % (
task['project'], task['taskid'],
task.get('url'), response.status_code, len(response.content),
ret.result, len(ret.follows), len(ret.messages), ret.exception))
return True | [
"def",
"on_task",
"(",
"self",
",",
"task",
",",
"response",
")",
":",
"start_time",
"=",
"time",
".",
"time",
"(",
")",
"response",
"=",
"rebuild_response",
"(",
"response",
")",
"try",
":",
"assert",
"'taskid'",
"in",
"task",
",",
"'need taskid in task'",
"project",
"=",
"task",
"[",
"'project'",
"]",
"updatetime",
"=",
"task",
".",
"get",
"(",
"'project_updatetime'",
",",
"None",
")",
"md5sum",
"=",
"task",
".",
"get",
"(",
"'project_md5sum'",
",",
"None",
")",
"project_data",
"=",
"self",
".",
"project_manager",
".",
"get",
"(",
"project",
",",
"updatetime",
",",
"md5sum",
")",
"assert",
"project_data",
",",
"\"no such project!\"",
"if",
"project_data",
".",
"get",
"(",
"'exception'",
")",
":",
"ret",
"=",
"ProcessorResult",
"(",
"logs",
"=",
"(",
"project_data",
".",
"get",
"(",
"'exception_log'",
")",
",",
")",
",",
"exception",
"=",
"project_data",
"[",
"'exception'",
"]",
")",
"else",
":",
"ret",
"=",
"project_data",
"[",
"'instance'",
"]",
".",
"run_task",
"(",
"project_data",
"[",
"'module'",
"]",
",",
"task",
",",
"response",
")",
"except",
"Exception",
"as",
"e",
":",
"logstr",
"=",
"traceback",
".",
"format_exc",
"(",
")",
"ret",
"=",
"ProcessorResult",
"(",
"logs",
"=",
"(",
"logstr",
",",
")",
",",
"exception",
"=",
"e",
")",
"process_time",
"=",
"time",
".",
"time",
"(",
")",
"-",
"start_time",
"if",
"not",
"ret",
".",
"extinfo",
".",
"get",
"(",
"'not_send_status'",
",",
"False",
")",
":",
"if",
"ret",
".",
"exception",
":",
"track_headers",
"=",
"dict",
"(",
"response",
".",
"headers",
")",
"else",
":",
"track_headers",
"=",
"{",
"}",
"for",
"name",
"in",
"(",
"'etag'",
",",
"'last-modified'",
")",
":",
"if",
"name",
"not",
"in",
"response",
".",
"headers",
":",
"continue",
"track_headers",
"[",
"name",
"]",
"=",
"response",
".",
"headers",
"[",
"name",
"]",
"status_pack",
"=",
"{",
"'taskid'",
":",
"task",
"[",
"'taskid'",
"]",
",",
"'project'",
":",
"task",
"[",
"'project'",
"]",
",",
"'url'",
":",
"task",
".",
"get",
"(",
"'url'",
")",
",",
"'track'",
":",
"{",
"'fetch'",
":",
"{",
"'ok'",
":",
"response",
".",
"isok",
"(",
")",
",",
"'redirect_url'",
":",
"response",
".",
"url",
"if",
"response",
".",
"url",
"!=",
"response",
".",
"orig_url",
"else",
"None",
",",
"'time'",
":",
"response",
".",
"time",
",",
"'error'",
":",
"response",
".",
"error",
",",
"'status_code'",
":",
"response",
".",
"status_code",
",",
"'encoding'",
":",
"getattr",
"(",
"response",
",",
"'_encoding'",
",",
"None",
")",
",",
"'headers'",
":",
"track_headers",
",",
"'content'",
":",
"response",
".",
"text",
"[",
":",
"500",
"]",
"if",
"ret",
".",
"exception",
"else",
"None",
",",
"}",
",",
"'process'",
":",
"{",
"'ok'",
":",
"not",
"ret",
".",
"exception",
",",
"'time'",
":",
"process_time",
",",
"'follows'",
":",
"len",
"(",
"ret",
".",
"follows",
")",
",",
"'result'",
":",
"(",
"None",
"if",
"ret",
".",
"result",
"is",
"None",
"else",
"utils",
".",
"text",
"(",
"ret",
".",
"result",
")",
"[",
":",
"self",
".",
"RESULT_RESULT_LIMIT",
"]",
")",
",",
"'logs'",
":",
"ret",
".",
"logstr",
"(",
")",
"[",
"-",
"self",
".",
"RESULT_LOGS_LIMIT",
":",
"]",
",",
"'exception'",
":",
"ret",
".",
"exception",
",",
"}",
",",
"'save'",
":",
"ret",
".",
"save",
",",
"}",
",",
"}",
"if",
"'schedule'",
"in",
"task",
":",
"status_pack",
"[",
"'schedule'",
"]",
"=",
"task",
"[",
"'schedule'",
"]",
"# FIXME: unicode_obj should used in scheduler before store to database",
"# it's used here for performance.",
"self",
".",
"status_queue",
".",
"put",
"(",
"utils",
".",
"unicode_obj",
"(",
"status_pack",
")",
")",
"# FIXME: unicode_obj should used in scheduler before store to database",
"# it's used here for performance.",
"if",
"ret",
".",
"follows",
":",
"for",
"each",
"in",
"(",
"ret",
".",
"follows",
"[",
"x",
":",
"x",
"+",
"1000",
"]",
"for",
"x",
"in",
"range",
"(",
"0",
",",
"len",
"(",
"ret",
".",
"follows",
")",
",",
"1000",
")",
")",
":",
"self",
".",
"newtask_queue",
".",
"put",
"(",
"[",
"utils",
".",
"unicode_obj",
"(",
"newtask",
")",
"for",
"newtask",
"in",
"each",
"]",
")",
"for",
"project",
",",
"msg",
",",
"url",
"in",
"ret",
".",
"messages",
":",
"try",
":",
"self",
".",
"on_task",
"(",
"{",
"'taskid'",
":",
"utils",
".",
"md5string",
"(",
"url",
")",
",",
"'project'",
":",
"project",
",",
"'url'",
":",
"url",
",",
"'process'",
":",
"{",
"'callback'",
":",
"'_on_message'",
",",
"}",
"}",
",",
"{",
"'status_code'",
":",
"200",
",",
"'url'",
":",
"url",
",",
"'save'",
":",
"(",
"task",
"[",
"'project'",
"]",
",",
"msg",
")",
",",
"}",
")",
"except",
"Exception",
"as",
"e",
":",
"logger",
".",
"exception",
"(",
"'Sending message error.'",
")",
"continue",
"if",
"ret",
".",
"exception",
":",
"logger_func",
"=",
"logger",
".",
"error",
"else",
":",
"logger_func",
"=",
"logger",
".",
"info",
"logger_func",
"(",
"'process %s:%s %s -> [%d] len:%d -> result:%.10r fol:%d msg:%d err:%r'",
"%",
"(",
"task",
"[",
"'project'",
"]",
",",
"task",
"[",
"'taskid'",
"]",
",",
"task",
".",
"get",
"(",
"'url'",
")",
",",
"response",
".",
"status_code",
",",
"len",
"(",
"response",
".",
"content",
")",
",",
"ret",
".",
"result",
",",
"len",
"(",
"ret",
".",
"follows",
")",
",",
"len",
"(",
"ret",
".",
"messages",
")",
",",
"ret",
".",
"exception",
")",
")",
"return",
"True"
] | Deal one task | [
"Deal",
"one",
"task"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/processor/processor.py#L102-L203 | train |
binux/pyspider | pyspider/processor/processor.py | Processor.run | def run(self):
'''Run loop'''
logger.info("processor starting...")
while not self._quit:
try:
task, response = self.inqueue.get(timeout=1)
self.on_task(task, response)
self._exceptions = 0
except Queue.Empty as e:
continue
except KeyboardInterrupt:
break
except Exception as e:
logger.exception(e)
self._exceptions += 1
if self._exceptions > self.EXCEPTION_LIMIT:
break
continue
logger.info("processor exiting...") | python | def run(self):
'''Run loop'''
logger.info("processor starting...")
while not self._quit:
try:
task, response = self.inqueue.get(timeout=1)
self.on_task(task, response)
self._exceptions = 0
except Queue.Empty as e:
continue
except KeyboardInterrupt:
break
except Exception as e:
logger.exception(e)
self._exceptions += 1
if self._exceptions > self.EXCEPTION_LIMIT:
break
continue
logger.info("processor exiting...") | [
"def",
"run",
"(",
"self",
")",
":",
"logger",
".",
"info",
"(",
"\"processor starting...\"",
")",
"while",
"not",
"self",
".",
"_quit",
":",
"try",
":",
"task",
",",
"response",
"=",
"self",
".",
"inqueue",
".",
"get",
"(",
"timeout",
"=",
"1",
")",
"self",
".",
"on_task",
"(",
"task",
",",
"response",
")",
"self",
".",
"_exceptions",
"=",
"0",
"except",
"Queue",
".",
"Empty",
"as",
"e",
":",
"continue",
"except",
"KeyboardInterrupt",
":",
"break",
"except",
"Exception",
"as",
"e",
":",
"logger",
".",
"exception",
"(",
"e",
")",
"self",
".",
"_exceptions",
"+=",
"1",
"if",
"self",
".",
"_exceptions",
">",
"self",
".",
"EXCEPTION_LIMIT",
":",
"break",
"continue",
"logger",
".",
"info",
"(",
"\"processor exiting...\"",
")"
] | Run loop | [
"Run",
"loop"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/processor/processor.py#L209-L229 | train |
binux/pyspider | pyspider/message_queue/__init__.py | connect_message_queue | def connect_message_queue(name, url=None, maxsize=0, lazy_limit=True):
"""
create connection to message queue
name:
name of message queue
rabbitmq:
amqp://username:password@host:5672/%2F
see https://www.rabbitmq.com/uri-spec.html
beanstalk:
beanstalk://host:11300/
redis:
redis://host:6379/db
redis://host1:port1,host2:port2,...,hostn:portn (for redis 3.x in cluster mode)
kombu:
kombu+transport://userid:password@hostname:port/virtual_host
see http://kombu.readthedocs.org/en/latest/userguide/connections.html#urls
builtin:
None
"""
if not url:
from pyspider.libs.multiprocessing_queue import Queue
return Queue(maxsize=maxsize)
parsed = urlparse.urlparse(url)
if parsed.scheme == 'amqp':
from .rabbitmq import Queue
return Queue(name, url, maxsize=maxsize, lazy_limit=lazy_limit)
elif parsed.scheme == 'beanstalk':
from .beanstalk import Queue
return Queue(name, host=parsed.netloc, maxsize=maxsize)
elif parsed.scheme == 'redis':
from .redis_queue import Queue
if ',' in parsed.netloc:
"""
redis in cluster mode (there is no concept of 'db' in cluster mode)
ex. redis://host1:port1,host2:port2,...,hostn:portn
"""
cluster_nodes = []
for netloc in parsed.netloc.split(','):
cluster_nodes.append({'host': netloc.split(':')[0], 'port': int(netloc.split(':')[1])})
return Queue(name=name, maxsize=maxsize, lazy_limit=lazy_limit, cluster_nodes=cluster_nodes)
else:
db = parsed.path.lstrip('/').split('/')
try:
db = int(db[0])
except:
logging.warning('redis DB must zero-based numeric index, using 0 instead')
db = 0
password = parsed.password or None
return Queue(name=name, host=parsed.hostname, port=parsed.port, db=db, maxsize=maxsize, password=password, lazy_limit=lazy_limit)
elif url.startswith('kombu+'):
url = url[len('kombu+'):]
from .kombu_queue import Queue
return Queue(name, url, maxsize=maxsize, lazy_limit=lazy_limit)
else:
raise Exception('unknown connection url: %s', url) | python | def connect_message_queue(name, url=None, maxsize=0, lazy_limit=True):
"""
create connection to message queue
name:
name of message queue
rabbitmq:
amqp://username:password@host:5672/%2F
see https://www.rabbitmq.com/uri-spec.html
beanstalk:
beanstalk://host:11300/
redis:
redis://host:6379/db
redis://host1:port1,host2:port2,...,hostn:portn (for redis 3.x in cluster mode)
kombu:
kombu+transport://userid:password@hostname:port/virtual_host
see http://kombu.readthedocs.org/en/latest/userguide/connections.html#urls
builtin:
None
"""
if not url:
from pyspider.libs.multiprocessing_queue import Queue
return Queue(maxsize=maxsize)
parsed = urlparse.urlparse(url)
if parsed.scheme == 'amqp':
from .rabbitmq import Queue
return Queue(name, url, maxsize=maxsize, lazy_limit=lazy_limit)
elif parsed.scheme == 'beanstalk':
from .beanstalk import Queue
return Queue(name, host=parsed.netloc, maxsize=maxsize)
elif parsed.scheme == 'redis':
from .redis_queue import Queue
if ',' in parsed.netloc:
"""
redis in cluster mode (there is no concept of 'db' in cluster mode)
ex. redis://host1:port1,host2:port2,...,hostn:portn
"""
cluster_nodes = []
for netloc in parsed.netloc.split(','):
cluster_nodes.append({'host': netloc.split(':')[0], 'port': int(netloc.split(':')[1])})
return Queue(name=name, maxsize=maxsize, lazy_limit=lazy_limit, cluster_nodes=cluster_nodes)
else:
db = parsed.path.lstrip('/').split('/')
try:
db = int(db[0])
except:
logging.warning('redis DB must zero-based numeric index, using 0 instead')
db = 0
password = parsed.password or None
return Queue(name=name, host=parsed.hostname, port=parsed.port, db=db, maxsize=maxsize, password=password, lazy_limit=lazy_limit)
elif url.startswith('kombu+'):
url = url[len('kombu+'):]
from .kombu_queue import Queue
return Queue(name, url, maxsize=maxsize, lazy_limit=lazy_limit)
else:
raise Exception('unknown connection url: %s', url) | [
"def",
"connect_message_queue",
"(",
"name",
",",
"url",
"=",
"None",
",",
"maxsize",
"=",
"0",
",",
"lazy_limit",
"=",
"True",
")",
":",
"if",
"not",
"url",
":",
"from",
"pyspider",
".",
"libs",
".",
"multiprocessing_queue",
"import",
"Queue",
"return",
"Queue",
"(",
"maxsize",
"=",
"maxsize",
")",
"parsed",
"=",
"urlparse",
".",
"urlparse",
"(",
"url",
")",
"if",
"parsed",
".",
"scheme",
"==",
"'amqp'",
":",
"from",
".",
"rabbitmq",
"import",
"Queue",
"return",
"Queue",
"(",
"name",
",",
"url",
",",
"maxsize",
"=",
"maxsize",
",",
"lazy_limit",
"=",
"lazy_limit",
")",
"elif",
"parsed",
".",
"scheme",
"==",
"'beanstalk'",
":",
"from",
".",
"beanstalk",
"import",
"Queue",
"return",
"Queue",
"(",
"name",
",",
"host",
"=",
"parsed",
".",
"netloc",
",",
"maxsize",
"=",
"maxsize",
")",
"elif",
"parsed",
".",
"scheme",
"==",
"'redis'",
":",
"from",
".",
"redis_queue",
"import",
"Queue",
"if",
"','",
"in",
"parsed",
".",
"netloc",
":",
"\"\"\"\n redis in cluster mode (there is no concept of 'db' in cluster mode)\n ex. redis://host1:port1,host2:port2,...,hostn:portn\n \"\"\"",
"cluster_nodes",
"=",
"[",
"]",
"for",
"netloc",
"in",
"parsed",
".",
"netloc",
".",
"split",
"(",
"','",
")",
":",
"cluster_nodes",
".",
"append",
"(",
"{",
"'host'",
":",
"netloc",
".",
"split",
"(",
"':'",
")",
"[",
"0",
"]",
",",
"'port'",
":",
"int",
"(",
"netloc",
".",
"split",
"(",
"':'",
")",
"[",
"1",
"]",
")",
"}",
")",
"return",
"Queue",
"(",
"name",
"=",
"name",
",",
"maxsize",
"=",
"maxsize",
",",
"lazy_limit",
"=",
"lazy_limit",
",",
"cluster_nodes",
"=",
"cluster_nodes",
")",
"else",
":",
"db",
"=",
"parsed",
".",
"path",
".",
"lstrip",
"(",
"'/'",
")",
".",
"split",
"(",
"'/'",
")",
"try",
":",
"db",
"=",
"int",
"(",
"db",
"[",
"0",
"]",
")",
"except",
":",
"logging",
".",
"warning",
"(",
"'redis DB must zero-based numeric index, using 0 instead'",
")",
"db",
"=",
"0",
"password",
"=",
"parsed",
".",
"password",
"or",
"None",
"return",
"Queue",
"(",
"name",
"=",
"name",
",",
"host",
"=",
"parsed",
".",
"hostname",
",",
"port",
"=",
"parsed",
".",
"port",
",",
"db",
"=",
"db",
",",
"maxsize",
"=",
"maxsize",
",",
"password",
"=",
"password",
",",
"lazy_limit",
"=",
"lazy_limit",
")",
"elif",
"url",
".",
"startswith",
"(",
"'kombu+'",
")",
":",
"url",
"=",
"url",
"[",
"len",
"(",
"'kombu+'",
")",
":",
"]",
"from",
".",
"kombu_queue",
"import",
"Queue",
"return",
"Queue",
"(",
"name",
",",
"url",
",",
"maxsize",
"=",
"maxsize",
",",
"lazy_limit",
"=",
"lazy_limit",
")",
"else",
":",
"raise",
"Exception",
"(",
"'unknown connection url: %s'",
",",
"url",
")"
] | create connection to message queue
name:
name of message queue
rabbitmq:
amqp://username:password@host:5672/%2F
see https://www.rabbitmq.com/uri-spec.html
beanstalk:
beanstalk://host:11300/
redis:
redis://host:6379/db
redis://host1:port1,host2:port2,...,hostn:portn (for redis 3.x in cluster mode)
kombu:
kombu+transport://userid:password@hostname:port/virtual_host
see http://kombu.readthedocs.org/en/latest/userguide/connections.html#urls
builtin:
None | [
"create",
"connection",
"to",
"message",
"queue"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/message_queue/__init__.py#L16-L78 | train |
binux/pyspider | pyspider/libs/utils.py | hide_me | def hide_me(tb, g=globals()):
"""Hide stack traceback of given stack"""
base_tb = tb
try:
while tb and tb.tb_frame.f_globals is not g:
tb = tb.tb_next
while tb and tb.tb_frame.f_globals is g:
tb = tb.tb_next
except Exception as e:
logging.exception(e)
tb = base_tb
if not tb:
tb = base_tb
return tb | python | def hide_me(tb, g=globals()):
"""Hide stack traceback of given stack"""
base_tb = tb
try:
while tb and tb.tb_frame.f_globals is not g:
tb = tb.tb_next
while tb and tb.tb_frame.f_globals is g:
tb = tb.tb_next
except Exception as e:
logging.exception(e)
tb = base_tb
if not tb:
tb = base_tb
return tb | [
"def",
"hide_me",
"(",
"tb",
",",
"g",
"=",
"globals",
"(",
")",
")",
":",
"base_tb",
"=",
"tb",
"try",
":",
"while",
"tb",
"and",
"tb",
".",
"tb_frame",
".",
"f_globals",
"is",
"not",
"g",
":",
"tb",
"=",
"tb",
".",
"tb_next",
"while",
"tb",
"and",
"tb",
".",
"tb_frame",
".",
"f_globals",
"is",
"g",
":",
"tb",
"=",
"tb",
".",
"tb_next",
"except",
"Exception",
"as",
"e",
":",
"logging",
".",
"exception",
"(",
"e",
")",
"tb",
"=",
"base_tb",
"if",
"not",
"tb",
":",
"tb",
"=",
"base_tb",
"return",
"tb"
] | Hide stack traceback of given stack | [
"Hide",
"stack",
"traceback",
"of",
"given",
"stack"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/utils.py#L38-L51 | train |
binux/pyspider | pyspider/libs/utils.py | run_in_subprocess | def run_in_subprocess(func, *args, **kwargs):
"""Run function in subprocess, return a Process object"""
from multiprocessing import Process
thread = Process(target=func, args=args, kwargs=kwargs)
thread.daemon = True
thread.start()
return thread | python | def run_in_subprocess(func, *args, **kwargs):
"""Run function in subprocess, return a Process object"""
from multiprocessing import Process
thread = Process(target=func, args=args, kwargs=kwargs)
thread.daemon = True
thread.start()
return thread | [
"def",
"run_in_subprocess",
"(",
"func",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"from",
"multiprocessing",
"import",
"Process",
"thread",
"=",
"Process",
"(",
"target",
"=",
"func",
",",
"args",
"=",
"args",
",",
"kwargs",
"=",
"kwargs",
")",
"thread",
".",
"daemon",
"=",
"True",
"thread",
".",
"start",
"(",
")",
"return",
"thread"
] | Run function in subprocess, return a Process object | [
"Run",
"function",
"in",
"subprocess",
"return",
"a",
"Process",
"object"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/utils.py#L63-L69 | train |
binux/pyspider | pyspider/libs/utils.py | format_date | def format_date(date, gmt_offset=0, relative=True, shorter=False, full_format=False):
"""Formats the given date (which should be GMT).
By default, we return a relative time (e.g., "2 minutes ago"). You
can return an absolute date string with ``relative=False``.
You can force a full format date ("July 10, 1980") with
``full_format=True``.
This method is primarily intended for dates in the past.
For dates in the future, we fall back to full format.
From tornado
"""
if not date:
return '-'
if isinstance(date, float) or isinstance(date, int):
date = datetime.datetime.utcfromtimestamp(date)
now = datetime.datetime.utcnow()
if date > now:
if relative and (date - now).seconds < 60:
# Due to click skew, things are some things slightly
# in the future. Round timestamps in the immediate
# future down to now in relative mode.
date = now
else:
# Otherwise, future dates always use the full format.
full_format = True
local_date = date - datetime.timedelta(minutes=gmt_offset)
local_now = now - datetime.timedelta(minutes=gmt_offset)
local_yesterday = local_now - datetime.timedelta(hours=24)
difference = now - date
seconds = difference.seconds
days = difference.days
format = None
if not full_format:
ret_, fff_format = fix_full_format(days, seconds, relative, shorter, local_date, local_yesterday)
format = fff_format
if ret_:
return format
else:
format = format
if format is None:
format = "%(month_name)s %(day)s, %(year)s" if shorter else \
"%(month_name)s %(day)s, %(year)s at %(time)s"
str_time = "%d:%02d" % (local_date.hour, local_date.minute)
return format % {
"month_name": local_date.strftime('%b'),
"weekday": local_date.strftime('%A'),
"day": str(local_date.day),
"year": str(local_date.year),
"month": local_date.month,
"time": str_time
} | python | def format_date(date, gmt_offset=0, relative=True, shorter=False, full_format=False):
"""Formats the given date (which should be GMT).
By default, we return a relative time (e.g., "2 minutes ago"). You
can return an absolute date string with ``relative=False``.
You can force a full format date ("July 10, 1980") with
``full_format=True``.
This method is primarily intended for dates in the past.
For dates in the future, we fall back to full format.
From tornado
"""
if not date:
return '-'
if isinstance(date, float) or isinstance(date, int):
date = datetime.datetime.utcfromtimestamp(date)
now = datetime.datetime.utcnow()
if date > now:
if relative and (date - now).seconds < 60:
# Due to click skew, things are some things slightly
# in the future. Round timestamps in the immediate
# future down to now in relative mode.
date = now
else:
# Otherwise, future dates always use the full format.
full_format = True
local_date = date - datetime.timedelta(minutes=gmt_offset)
local_now = now - datetime.timedelta(minutes=gmt_offset)
local_yesterday = local_now - datetime.timedelta(hours=24)
difference = now - date
seconds = difference.seconds
days = difference.days
format = None
if not full_format:
ret_, fff_format = fix_full_format(days, seconds, relative, shorter, local_date, local_yesterday)
format = fff_format
if ret_:
return format
else:
format = format
if format is None:
format = "%(month_name)s %(day)s, %(year)s" if shorter else \
"%(month_name)s %(day)s, %(year)s at %(time)s"
str_time = "%d:%02d" % (local_date.hour, local_date.minute)
return format % {
"month_name": local_date.strftime('%b'),
"weekday": local_date.strftime('%A'),
"day": str(local_date.day),
"year": str(local_date.year),
"month": local_date.month,
"time": str_time
} | [
"def",
"format_date",
"(",
"date",
",",
"gmt_offset",
"=",
"0",
",",
"relative",
"=",
"True",
",",
"shorter",
"=",
"False",
",",
"full_format",
"=",
"False",
")",
":",
"if",
"not",
"date",
":",
"return",
"'-'",
"if",
"isinstance",
"(",
"date",
",",
"float",
")",
"or",
"isinstance",
"(",
"date",
",",
"int",
")",
":",
"date",
"=",
"datetime",
".",
"datetime",
".",
"utcfromtimestamp",
"(",
"date",
")",
"now",
"=",
"datetime",
".",
"datetime",
".",
"utcnow",
"(",
")",
"if",
"date",
">",
"now",
":",
"if",
"relative",
"and",
"(",
"date",
"-",
"now",
")",
".",
"seconds",
"<",
"60",
":",
"# Due to click skew, things are some things slightly",
"# in the future. Round timestamps in the immediate",
"# future down to now in relative mode.",
"date",
"=",
"now",
"else",
":",
"# Otherwise, future dates always use the full format.",
"full_format",
"=",
"True",
"local_date",
"=",
"date",
"-",
"datetime",
".",
"timedelta",
"(",
"minutes",
"=",
"gmt_offset",
")",
"local_now",
"=",
"now",
"-",
"datetime",
".",
"timedelta",
"(",
"minutes",
"=",
"gmt_offset",
")",
"local_yesterday",
"=",
"local_now",
"-",
"datetime",
".",
"timedelta",
"(",
"hours",
"=",
"24",
")",
"difference",
"=",
"now",
"-",
"date",
"seconds",
"=",
"difference",
".",
"seconds",
"days",
"=",
"difference",
".",
"days",
"format",
"=",
"None",
"if",
"not",
"full_format",
":",
"ret_",
",",
"fff_format",
"=",
"fix_full_format",
"(",
"days",
",",
"seconds",
",",
"relative",
",",
"shorter",
",",
"local_date",
",",
"local_yesterday",
")",
"format",
"=",
"fff_format",
"if",
"ret_",
":",
"return",
"format",
"else",
":",
"format",
"=",
"format",
"if",
"format",
"is",
"None",
":",
"format",
"=",
"\"%(month_name)s %(day)s, %(year)s\"",
"if",
"shorter",
"else",
"\"%(month_name)s %(day)s, %(year)s at %(time)s\"",
"str_time",
"=",
"\"%d:%02d\"",
"%",
"(",
"local_date",
".",
"hour",
",",
"local_date",
".",
"minute",
")",
"return",
"format",
"%",
"{",
"\"month_name\"",
":",
"local_date",
".",
"strftime",
"(",
"'%b'",
")",
",",
"\"weekday\"",
":",
"local_date",
".",
"strftime",
"(",
"'%A'",
")",
",",
"\"day\"",
":",
"str",
"(",
"local_date",
".",
"day",
")",
",",
"\"year\"",
":",
"str",
"(",
"local_date",
".",
"year",
")",
",",
"\"month\"",
":",
"local_date",
".",
"month",
",",
"\"time\"",
":",
"str_time",
"}"
] | Formats the given date (which should be GMT).
By default, we return a relative time (e.g., "2 minutes ago"). You
can return an absolute date string with ``relative=False``.
You can force a full format date ("July 10, 1980") with
``full_format=True``.
This method is primarily intended for dates in the past.
For dates in the future, we fall back to full format.
From tornado | [
"Formats",
"the",
"given",
"date",
"(",
"which",
"should",
"be",
"GMT",
")",
"."
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/utils.py#L72-L130 | train |
binux/pyspider | pyspider/libs/utils.py | utf8 | def utf8(string):
"""
Make sure string is utf8 encoded bytes.
If parameter is a object, object.__str__ will been called before encode as bytes
"""
if isinstance(string, six.text_type):
return string.encode('utf8')
elif isinstance(string, six.binary_type):
return string
else:
return six.text_type(string).encode('utf8') | python | def utf8(string):
"""
Make sure string is utf8 encoded bytes.
If parameter is a object, object.__str__ will been called before encode as bytes
"""
if isinstance(string, six.text_type):
return string.encode('utf8')
elif isinstance(string, six.binary_type):
return string
else:
return six.text_type(string).encode('utf8') | [
"def",
"utf8",
"(",
"string",
")",
":",
"if",
"isinstance",
"(",
"string",
",",
"six",
".",
"text_type",
")",
":",
"return",
"string",
".",
"encode",
"(",
"'utf8'",
")",
"elif",
"isinstance",
"(",
"string",
",",
"six",
".",
"binary_type",
")",
":",
"return",
"string",
"else",
":",
"return",
"six",
".",
"text_type",
"(",
"string",
")",
".",
"encode",
"(",
"'utf8'",
")"
] | Make sure string is utf8 encoded bytes.
If parameter is a object, object.__str__ will been called before encode as bytes | [
"Make",
"sure",
"string",
"is",
"utf8",
"encoded",
"bytes",
"."
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/utils.py#L213-L224 | train |
binux/pyspider | pyspider/libs/utils.py | text | def text(string, encoding='utf8'):
"""
Make sure string is unicode type, decode with given encoding if it's not.
If parameter is a object, object.__str__ will been called
"""
if isinstance(string, six.text_type):
return string
elif isinstance(string, six.binary_type):
return string.decode(encoding)
else:
return six.text_type(string) | python | def text(string, encoding='utf8'):
"""
Make sure string is unicode type, decode with given encoding if it's not.
If parameter is a object, object.__str__ will been called
"""
if isinstance(string, six.text_type):
return string
elif isinstance(string, six.binary_type):
return string.decode(encoding)
else:
return six.text_type(string) | [
"def",
"text",
"(",
"string",
",",
"encoding",
"=",
"'utf8'",
")",
":",
"if",
"isinstance",
"(",
"string",
",",
"six",
".",
"text_type",
")",
":",
"return",
"string",
"elif",
"isinstance",
"(",
"string",
",",
"six",
".",
"binary_type",
")",
":",
"return",
"string",
".",
"decode",
"(",
"encoding",
")",
"else",
":",
"return",
"six",
".",
"text_type",
"(",
"string",
")"
] | Make sure string is unicode type, decode with given encoding if it's not.
If parameter is a object, object.__str__ will been called | [
"Make",
"sure",
"string",
"is",
"unicode",
"type",
"decode",
"with",
"given",
"encoding",
"if",
"it",
"s",
"not",
"."
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/utils.py#L227-L238 | train |
binux/pyspider | pyspider/libs/utils.py | pretty_unicode | def pretty_unicode(string):
"""
Make sure string is unicode, try to decode with utf8, or unicode escaped string if failed.
"""
if isinstance(string, six.text_type):
return string
try:
return string.decode("utf8")
except UnicodeDecodeError:
return string.decode('Latin-1').encode('unicode_escape').decode("utf8") | python | def pretty_unicode(string):
"""
Make sure string is unicode, try to decode with utf8, or unicode escaped string if failed.
"""
if isinstance(string, six.text_type):
return string
try:
return string.decode("utf8")
except UnicodeDecodeError:
return string.decode('Latin-1').encode('unicode_escape').decode("utf8") | [
"def",
"pretty_unicode",
"(",
"string",
")",
":",
"if",
"isinstance",
"(",
"string",
",",
"six",
".",
"text_type",
")",
":",
"return",
"string",
"try",
":",
"return",
"string",
".",
"decode",
"(",
"\"utf8\"",
")",
"except",
"UnicodeDecodeError",
":",
"return",
"string",
".",
"decode",
"(",
"'Latin-1'",
")",
".",
"encode",
"(",
"'unicode_escape'",
")",
".",
"decode",
"(",
"\"utf8\"",
")"
] | Make sure string is unicode, try to decode with utf8, or unicode escaped string if failed. | [
"Make",
"sure",
"string",
"is",
"unicode",
"try",
"to",
"decode",
"with",
"utf8",
"or",
"unicode",
"escaped",
"string",
"if",
"failed",
"."
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/utils.py#L241-L250 | train |
binux/pyspider | pyspider/libs/utils.py | unicode_string | def unicode_string(string):
"""
Make sure string is unicode, try to default with utf8, or base64 if failed.
can been decode by `decode_unicode_string`
"""
if isinstance(string, six.text_type):
return string
try:
return string.decode("utf8")
except UnicodeDecodeError:
return '[BASE64-DATA]' + base64.b64encode(string) + '[/BASE64-DATA]' | python | def unicode_string(string):
"""
Make sure string is unicode, try to default with utf8, or base64 if failed.
can been decode by `decode_unicode_string`
"""
if isinstance(string, six.text_type):
return string
try:
return string.decode("utf8")
except UnicodeDecodeError:
return '[BASE64-DATA]' + base64.b64encode(string) + '[/BASE64-DATA]' | [
"def",
"unicode_string",
"(",
"string",
")",
":",
"if",
"isinstance",
"(",
"string",
",",
"six",
".",
"text_type",
")",
":",
"return",
"string",
"try",
":",
"return",
"string",
".",
"decode",
"(",
"\"utf8\"",
")",
"except",
"UnicodeDecodeError",
":",
"return",
"'[BASE64-DATA]'",
"+",
"base64",
".",
"b64encode",
"(",
"string",
")",
"+",
"'[/BASE64-DATA]'"
] | Make sure string is unicode, try to default with utf8, or base64 if failed.
can been decode by `decode_unicode_string` | [
"Make",
"sure",
"string",
"is",
"unicode",
"try",
"to",
"default",
"with",
"utf8",
"or",
"base64",
"if",
"failed",
"."
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/utils.py#L253-L264 | train |
binux/pyspider | pyspider/libs/utils.py | unicode_dict | def unicode_dict(_dict):
"""
Make sure keys and values of dict is unicode.
"""
r = {}
for k, v in iteritems(_dict):
r[unicode_obj(k)] = unicode_obj(v)
return r | python | def unicode_dict(_dict):
"""
Make sure keys and values of dict is unicode.
"""
r = {}
for k, v in iteritems(_dict):
r[unicode_obj(k)] = unicode_obj(v)
return r | [
"def",
"unicode_dict",
"(",
"_dict",
")",
":",
"r",
"=",
"{",
"}",
"for",
"k",
",",
"v",
"in",
"iteritems",
"(",
"_dict",
")",
":",
"r",
"[",
"unicode_obj",
"(",
"k",
")",
"]",
"=",
"unicode_obj",
"(",
"v",
")",
"return",
"r"
] | Make sure keys and values of dict is unicode. | [
"Make",
"sure",
"keys",
"and",
"values",
"of",
"dict",
"is",
"unicode",
"."
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/utils.py#L267-L274 | train |
binux/pyspider | pyspider/libs/utils.py | unicode_obj | def unicode_obj(obj):
"""
Make sure keys and values of dict/list/tuple is unicode. bytes will encode in base64.
Can been decode by `decode_unicode_obj`
"""
if isinstance(obj, dict):
return unicode_dict(obj)
elif isinstance(obj, (list, tuple)):
return unicode_list(obj)
elif isinstance(obj, six.string_types):
return unicode_string(obj)
elif isinstance(obj, (int, float)):
return obj
elif obj is None:
return obj
else:
try:
return text(obj)
except:
return text(repr(obj)) | python | def unicode_obj(obj):
"""
Make sure keys and values of dict/list/tuple is unicode. bytes will encode in base64.
Can been decode by `decode_unicode_obj`
"""
if isinstance(obj, dict):
return unicode_dict(obj)
elif isinstance(obj, (list, tuple)):
return unicode_list(obj)
elif isinstance(obj, six.string_types):
return unicode_string(obj)
elif isinstance(obj, (int, float)):
return obj
elif obj is None:
return obj
else:
try:
return text(obj)
except:
return text(repr(obj)) | [
"def",
"unicode_obj",
"(",
"obj",
")",
":",
"if",
"isinstance",
"(",
"obj",
",",
"dict",
")",
":",
"return",
"unicode_dict",
"(",
"obj",
")",
"elif",
"isinstance",
"(",
"obj",
",",
"(",
"list",
",",
"tuple",
")",
")",
":",
"return",
"unicode_list",
"(",
"obj",
")",
"elif",
"isinstance",
"(",
"obj",
",",
"six",
".",
"string_types",
")",
":",
"return",
"unicode_string",
"(",
"obj",
")",
"elif",
"isinstance",
"(",
"obj",
",",
"(",
"int",
",",
"float",
")",
")",
":",
"return",
"obj",
"elif",
"obj",
"is",
"None",
":",
"return",
"obj",
"else",
":",
"try",
":",
"return",
"text",
"(",
"obj",
")",
"except",
":",
"return",
"text",
"(",
"repr",
"(",
"obj",
")",
")"
] | Make sure keys and values of dict/list/tuple is unicode. bytes will encode in base64.
Can been decode by `decode_unicode_obj` | [
"Make",
"sure",
"keys",
"and",
"values",
"of",
"dict",
"/",
"list",
"/",
"tuple",
"is",
"unicode",
".",
"bytes",
"will",
"encode",
"in",
"base64",
"."
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/utils.py#L284-L304 | train |
binux/pyspider | pyspider/libs/utils.py | decode_unicode_string | def decode_unicode_string(string):
"""
Decode string encoded by `unicode_string`
"""
if string.startswith('[BASE64-DATA]') and string.endswith('[/BASE64-DATA]'):
return base64.b64decode(string[len('[BASE64-DATA]'):-len('[/BASE64-DATA]')])
return string | python | def decode_unicode_string(string):
"""
Decode string encoded by `unicode_string`
"""
if string.startswith('[BASE64-DATA]') and string.endswith('[/BASE64-DATA]'):
return base64.b64decode(string[len('[BASE64-DATA]'):-len('[/BASE64-DATA]')])
return string | [
"def",
"decode_unicode_string",
"(",
"string",
")",
":",
"if",
"string",
".",
"startswith",
"(",
"'[BASE64-DATA]'",
")",
"and",
"string",
".",
"endswith",
"(",
"'[/BASE64-DATA]'",
")",
":",
"return",
"base64",
".",
"b64decode",
"(",
"string",
"[",
"len",
"(",
"'[BASE64-DATA]'",
")",
":",
"-",
"len",
"(",
"'[/BASE64-DATA]'",
")",
"]",
")",
"return",
"string"
] | Decode string encoded by `unicode_string` | [
"Decode",
"string",
"encoded",
"by",
"unicode_string"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/utils.py#L307-L313 | train |
binux/pyspider | pyspider/libs/utils.py | decode_unicode_obj | def decode_unicode_obj(obj):
"""
Decode unicoded dict/list/tuple encoded by `unicode_obj`
"""
if isinstance(obj, dict):
r = {}
for k, v in iteritems(obj):
r[decode_unicode_string(k)] = decode_unicode_obj(v)
return r
elif isinstance(obj, six.string_types):
return decode_unicode_string(obj)
elif isinstance(obj, (list, tuple)):
return [decode_unicode_obj(x) for x in obj]
else:
return obj | python | def decode_unicode_obj(obj):
"""
Decode unicoded dict/list/tuple encoded by `unicode_obj`
"""
if isinstance(obj, dict):
r = {}
for k, v in iteritems(obj):
r[decode_unicode_string(k)] = decode_unicode_obj(v)
return r
elif isinstance(obj, six.string_types):
return decode_unicode_string(obj)
elif isinstance(obj, (list, tuple)):
return [decode_unicode_obj(x) for x in obj]
else:
return obj | [
"def",
"decode_unicode_obj",
"(",
"obj",
")",
":",
"if",
"isinstance",
"(",
"obj",
",",
"dict",
")",
":",
"r",
"=",
"{",
"}",
"for",
"k",
",",
"v",
"in",
"iteritems",
"(",
"obj",
")",
":",
"r",
"[",
"decode_unicode_string",
"(",
"k",
")",
"]",
"=",
"decode_unicode_obj",
"(",
"v",
")",
"return",
"r",
"elif",
"isinstance",
"(",
"obj",
",",
"six",
".",
"string_types",
")",
":",
"return",
"decode_unicode_string",
"(",
"obj",
")",
"elif",
"isinstance",
"(",
"obj",
",",
"(",
"list",
",",
"tuple",
")",
")",
":",
"return",
"[",
"decode_unicode_obj",
"(",
"x",
")",
"for",
"x",
"in",
"obj",
"]",
"else",
":",
"return",
"obj"
] | Decode unicoded dict/list/tuple encoded by `unicode_obj` | [
"Decode",
"unicoded",
"dict",
"/",
"list",
"/",
"tuple",
"encoded",
"by",
"unicode_obj"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/utils.py#L316-L330 | train |
binux/pyspider | pyspider/libs/utils.py | load_object | def load_object(name):
"""Load object from module"""
if "." not in name:
raise Exception('load object need module.object')
module_name, object_name = name.rsplit('.', 1)
if six.PY2:
module = __import__(module_name, globals(), locals(), [utf8(object_name)], -1)
else:
module = __import__(module_name, globals(), locals(), [object_name])
return getattr(module, object_name) | python | def load_object(name):
"""Load object from module"""
if "." not in name:
raise Exception('load object need module.object')
module_name, object_name = name.rsplit('.', 1)
if six.PY2:
module = __import__(module_name, globals(), locals(), [utf8(object_name)], -1)
else:
module = __import__(module_name, globals(), locals(), [object_name])
return getattr(module, object_name) | [
"def",
"load_object",
"(",
"name",
")",
":",
"if",
"\".\"",
"not",
"in",
"name",
":",
"raise",
"Exception",
"(",
"'load object need module.object'",
")",
"module_name",
",",
"object_name",
"=",
"name",
".",
"rsplit",
"(",
"'.'",
",",
"1",
")",
"if",
"six",
".",
"PY2",
":",
"module",
"=",
"__import__",
"(",
"module_name",
",",
"globals",
"(",
")",
",",
"locals",
"(",
")",
",",
"[",
"utf8",
"(",
"object_name",
")",
"]",
",",
"-",
"1",
")",
"else",
":",
"module",
"=",
"__import__",
"(",
"module_name",
",",
"globals",
"(",
")",
",",
"locals",
"(",
")",
",",
"[",
"object_name",
"]",
")",
"return",
"getattr",
"(",
"module",
",",
"object_name",
")"
] | Load object from module | [
"Load",
"object",
"from",
"module"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/utils.py#L359-L370 | train |
binux/pyspider | pyspider/libs/utils.py | get_python_console | def get_python_console(namespace=None):
"""
Return a interactive python console instance with caller's stack
"""
if namespace is None:
import inspect
frame = inspect.currentframe()
caller = frame.f_back
if not caller:
logging.error("can't find caller who start this console.")
caller = frame
namespace = dict(caller.f_globals)
namespace.update(caller.f_locals)
try:
from IPython.terminal.interactiveshell import TerminalInteractiveShell
shell = TerminalInteractiveShell(user_ns=namespace)
except ImportError:
try:
import readline
import rlcompleter
readline.set_completer(rlcompleter.Completer(namespace).complete)
readline.parse_and_bind("tab: complete")
except ImportError:
pass
import code
shell = code.InteractiveConsole(namespace)
shell._quit = False
def exit():
shell._quit = True
def readfunc(prompt=""):
if shell._quit:
raise EOFError
return six.moves.input(prompt)
# inject exit method
shell.ask_exit = exit
shell.raw_input = readfunc
return shell | python | def get_python_console(namespace=None):
"""
Return a interactive python console instance with caller's stack
"""
if namespace is None:
import inspect
frame = inspect.currentframe()
caller = frame.f_back
if not caller:
logging.error("can't find caller who start this console.")
caller = frame
namespace = dict(caller.f_globals)
namespace.update(caller.f_locals)
try:
from IPython.terminal.interactiveshell import TerminalInteractiveShell
shell = TerminalInteractiveShell(user_ns=namespace)
except ImportError:
try:
import readline
import rlcompleter
readline.set_completer(rlcompleter.Completer(namespace).complete)
readline.parse_and_bind("tab: complete")
except ImportError:
pass
import code
shell = code.InteractiveConsole(namespace)
shell._quit = False
def exit():
shell._quit = True
def readfunc(prompt=""):
if shell._quit:
raise EOFError
return six.moves.input(prompt)
# inject exit method
shell.ask_exit = exit
shell.raw_input = readfunc
return shell | [
"def",
"get_python_console",
"(",
"namespace",
"=",
"None",
")",
":",
"if",
"namespace",
"is",
"None",
":",
"import",
"inspect",
"frame",
"=",
"inspect",
".",
"currentframe",
"(",
")",
"caller",
"=",
"frame",
".",
"f_back",
"if",
"not",
"caller",
":",
"logging",
".",
"error",
"(",
"\"can't find caller who start this console.\"",
")",
"caller",
"=",
"frame",
"namespace",
"=",
"dict",
"(",
"caller",
".",
"f_globals",
")",
"namespace",
".",
"update",
"(",
"caller",
".",
"f_locals",
")",
"try",
":",
"from",
"IPython",
".",
"terminal",
".",
"interactiveshell",
"import",
"TerminalInteractiveShell",
"shell",
"=",
"TerminalInteractiveShell",
"(",
"user_ns",
"=",
"namespace",
")",
"except",
"ImportError",
":",
"try",
":",
"import",
"readline",
"import",
"rlcompleter",
"readline",
".",
"set_completer",
"(",
"rlcompleter",
".",
"Completer",
"(",
"namespace",
")",
".",
"complete",
")",
"readline",
".",
"parse_and_bind",
"(",
"\"tab: complete\"",
")",
"except",
"ImportError",
":",
"pass",
"import",
"code",
"shell",
"=",
"code",
".",
"InteractiveConsole",
"(",
"namespace",
")",
"shell",
".",
"_quit",
"=",
"False",
"def",
"exit",
"(",
")",
":",
"shell",
".",
"_quit",
"=",
"True",
"def",
"readfunc",
"(",
"prompt",
"=",
"\"\"",
")",
":",
"if",
"shell",
".",
"_quit",
":",
"raise",
"EOFError",
"return",
"six",
".",
"moves",
".",
"input",
"(",
"prompt",
")",
"# inject exit method",
"shell",
".",
"ask_exit",
"=",
"exit",
"shell",
".",
"raw_input",
"=",
"readfunc",
"return",
"shell"
] | Return a interactive python console instance with caller's stack | [
"Return",
"a",
"interactive",
"python",
"console",
"instance",
"with",
"caller",
"s",
"stack"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/utils.py#L373-L415 | train |
binux/pyspider | pyspider/libs/utils.py | python_console | def python_console(namespace=None):
"""Start a interactive python console with caller's stack"""
if namespace is None:
import inspect
frame = inspect.currentframe()
caller = frame.f_back
if not caller:
logging.error("can't find caller who start this console.")
caller = frame
namespace = dict(caller.f_globals)
namespace.update(caller.f_locals)
return get_python_console(namespace=namespace).interact() | python | def python_console(namespace=None):
"""Start a interactive python console with caller's stack"""
if namespace is None:
import inspect
frame = inspect.currentframe()
caller = frame.f_back
if not caller:
logging.error("can't find caller who start this console.")
caller = frame
namespace = dict(caller.f_globals)
namespace.update(caller.f_locals)
return get_python_console(namespace=namespace).interact() | [
"def",
"python_console",
"(",
"namespace",
"=",
"None",
")",
":",
"if",
"namespace",
"is",
"None",
":",
"import",
"inspect",
"frame",
"=",
"inspect",
".",
"currentframe",
"(",
")",
"caller",
"=",
"frame",
".",
"f_back",
"if",
"not",
"caller",
":",
"logging",
".",
"error",
"(",
"\"can't find caller who start this console.\"",
")",
"caller",
"=",
"frame",
"namespace",
"=",
"dict",
"(",
"caller",
".",
"f_globals",
")",
"namespace",
".",
"update",
"(",
"caller",
".",
"f_locals",
")",
"return",
"get_python_console",
"(",
"namespace",
"=",
"namespace",
")",
".",
"interact",
"(",
")"
] | Start a interactive python console with caller's stack | [
"Start",
"a",
"interactive",
"python",
"console",
"with",
"caller",
"s",
"stack"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/utils.py#L418-L431 | train |
binux/pyspider | pyspider/libs/wsgi_xmlrpc.py | WSGIXMLRPCApplication.handler | def handler(self, environ, start_response):
"""XMLRPC service for windmill browser core to communicate with"""
if environ['REQUEST_METHOD'] == 'POST':
return self.handle_POST(environ, start_response)
else:
start_response("400 Bad request", [('Content-Type', 'text/plain')])
return [''] | python | def handler(self, environ, start_response):
"""XMLRPC service for windmill browser core to communicate with"""
if environ['REQUEST_METHOD'] == 'POST':
return self.handle_POST(environ, start_response)
else:
start_response("400 Bad request", [('Content-Type', 'text/plain')])
return [''] | [
"def",
"handler",
"(",
"self",
",",
"environ",
",",
"start_response",
")",
":",
"if",
"environ",
"[",
"'REQUEST_METHOD'",
"]",
"==",
"'POST'",
":",
"return",
"self",
".",
"handle_POST",
"(",
"environ",
",",
"start_response",
")",
"else",
":",
"start_response",
"(",
"\"400 Bad request\"",
",",
"[",
"(",
"'Content-Type'",
",",
"'text/plain'",
")",
"]",
")",
"return",
"[",
"''",
"]"
] | XMLRPC service for windmill browser core to communicate with | [
"XMLRPC",
"service",
"for",
"windmill",
"browser",
"core",
"to",
"communicate",
"with"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/wsgi_xmlrpc.py#L48-L55 | train |
binux/pyspider | pyspider/libs/wsgi_xmlrpc.py | WSGIXMLRPCApplication.handle_POST | def handle_POST(self, environ, start_response):
"""Handles the HTTP POST request.
Attempts to interpret all HTTP POST requests as XML-RPC calls,
which are forwarded to the server's _dispatch method for handling.
Most code taken from SimpleXMLRPCServer with modifications for wsgi and my custom dispatcher.
"""
try:
# Get arguments by reading body of request.
# We read this in chunks to avoid straining
# socket.read(); around the 10 or 15Mb mark, some platforms
# begin to have problems (bug #792570).
length = int(environ['CONTENT_LENGTH'])
data = environ['wsgi.input'].read(length)
# In previous versions of SimpleXMLRPCServer, _dispatch
# could be overridden in this class, instead of in
# SimpleXMLRPCDispatcher. To maintain backwards compatibility,
# check to see if a subclass implements _dispatch and
# using that method if present.
response = self.dispatcher._marshaled_dispatch(
data, getattr(self.dispatcher, '_dispatch', None)
)
response += b'\n'
except Exception as e: # This should only happen if the module is buggy
# internal error, report as HTTP server error
logger.exception(e)
start_response("500 Server error", [('Content-Type', 'text/plain')])
return []
else:
# got a valid XML RPC response
start_response("200 OK", [('Content-Type', 'text/xml'), ('Content-Length', str(len(response)),)])
return [response] | python | def handle_POST(self, environ, start_response):
"""Handles the HTTP POST request.
Attempts to interpret all HTTP POST requests as XML-RPC calls,
which are forwarded to the server's _dispatch method for handling.
Most code taken from SimpleXMLRPCServer with modifications for wsgi and my custom dispatcher.
"""
try:
# Get arguments by reading body of request.
# We read this in chunks to avoid straining
# socket.read(); around the 10 or 15Mb mark, some platforms
# begin to have problems (bug #792570).
length = int(environ['CONTENT_LENGTH'])
data = environ['wsgi.input'].read(length)
# In previous versions of SimpleXMLRPCServer, _dispatch
# could be overridden in this class, instead of in
# SimpleXMLRPCDispatcher. To maintain backwards compatibility,
# check to see if a subclass implements _dispatch and
# using that method if present.
response = self.dispatcher._marshaled_dispatch(
data, getattr(self.dispatcher, '_dispatch', None)
)
response += b'\n'
except Exception as e: # This should only happen if the module is buggy
# internal error, report as HTTP server error
logger.exception(e)
start_response("500 Server error", [('Content-Type', 'text/plain')])
return []
else:
# got a valid XML RPC response
start_response("200 OK", [('Content-Type', 'text/xml'), ('Content-Length', str(len(response)),)])
return [response] | [
"def",
"handle_POST",
"(",
"self",
",",
"environ",
",",
"start_response",
")",
":",
"try",
":",
"# Get arguments by reading body of request.",
"# We read this in chunks to avoid straining",
"# socket.read(); around the 10 or 15Mb mark, some platforms",
"# begin to have problems (bug #792570).",
"length",
"=",
"int",
"(",
"environ",
"[",
"'CONTENT_LENGTH'",
"]",
")",
"data",
"=",
"environ",
"[",
"'wsgi.input'",
"]",
".",
"read",
"(",
"length",
")",
"# In previous versions of SimpleXMLRPCServer, _dispatch",
"# could be overridden in this class, instead of in",
"# SimpleXMLRPCDispatcher. To maintain backwards compatibility,",
"# check to see if a subclass implements _dispatch and",
"# using that method if present.",
"response",
"=",
"self",
".",
"dispatcher",
".",
"_marshaled_dispatch",
"(",
"data",
",",
"getattr",
"(",
"self",
".",
"dispatcher",
",",
"'_dispatch'",
",",
"None",
")",
")",
"response",
"+=",
"b'\\n'",
"except",
"Exception",
"as",
"e",
":",
"# This should only happen if the module is buggy",
"# internal error, report as HTTP server error",
"logger",
".",
"exception",
"(",
"e",
")",
"start_response",
"(",
"\"500 Server error\"",
",",
"[",
"(",
"'Content-Type'",
",",
"'text/plain'",
")",
"]",
")",
"return",
"[",
"]",
"else",
":",
"# got a valid XML RPC response",
"start_response",
"(",
"\"200 OK\"",
",",
"[",
"(",
"'Content-Type'",
",",
"'text/xml'",
")",
",",
"(",
"'Content-Length'",
",",
"str",
"(",
"len",
"(",
"response",
")",
")",
",",
")",
"]",
")",
"return",
"[",
"response",
"]"
] | Handles the HTTP POST request.
Attempts to interpret all HTTP POST requests as XML-RPC calls,
which are forwarded to the server's _dispatch method for handling.
Most code taken from SimpleXMLRPCServer with modifications for wsgi and my custom dispatcher. | [
"Handles",
"the",
"HTTP",
"POST",
"request",
"."
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/libs/wsgi_xmlrpc.py#L57-L92 | train |
binux/pyspider | pyspider/database/__init__.py | connect_database | def connect_database(url):
"""
create database object by url
mysql:
mysql+type://user:passwd@host:port/database
sqlite:
# relative path
sqlite+type:///path/to/database.db
# absolute path
sqlite+type:////path/to/database.db
# memory database
sqlite+type://
mongodb:
mongodb+type://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
more: http://docs.mongodb.org/manual/reference/connection-string/
sqlalchemy:
sqlalchemy+postgresql+type://user:passwd@host:port/database
sqlalchemy+mysql+mysqlconnector+type://user:passwd@host:port/database
more: http://docs.sqlalchemy.org/en/rel_0_9/core/engines.html
redis:
redis+taskdb://host:port/db
elasticsearch:
elasticsearch+type://host:port/?index=pyspider
local:
local+projectdb://filepath,filepath
type:
taskdb
projectdb
resultdb
"""
db = _connect_database(url)
db.copy = lambda: _connect_database(url)
return db | python | def connect_database(url):
"""
create database object by url
mysql:
mysql+type://user:passwd@host:port/database
sqlite:
# relative path
sqlite+type:///path/to/database.db
# absolute path
sqlite+type:////path/to/database.db
# memory database
sqlite+type://
mongodb:
mongodb+type://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
more: http://docs.mongodb.org/manual/reference/connection-string/
sqlalchemy:
sqlalchemy+postgresql+type://user:passwd@host:port/database
sqlalchemy+mysql+mysqlconnector+type://user:passwd@host:port/database
more: http://docs.sqlalchemy.org/en/rel_0_9/core/engines.html
redis:
redis+taskdb://host:port/db
elasticsearch:
elasticsearch+type://host:port/?index=pyspider
local:
local+projectdb://filepath,filepath
type:
taskdb
projectdb
resultdb
"""
db = _connect_database(url)
db.copy = lambda: _connect_database(url)
return db | [
"def",
"connect_database",
"(",
"url",
")",
":",
"db",
"=",
"_connect_database",
"(",
"url",
")",
"db",
".",
"copy",
"=",
"lambda",
":",
"_connect_database",
"(",
"url",
")",
"return",
"db"
] | create database object by url
mysql:
mysql+type://user:passwd@host:port/database
sqlite:
# relative path
sqlite+type:///path/to/database.db
# absolute path
sqlite+type:////path/to/database.db
# memory database
sqlite+type://
mongodb:
mongodb+type://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
more: http://docs.mongodb.org/manual/reference/connection-string/
sqlalchemy:
sqlalchemy+postgresql+type://user:passwd@host:port/database
sqlalchemy+mysql+mysqlconnector+type://user:passwd@host:port/database
more: http://docs.sqlalchemy.org/en/rel_0_9/core/engines.html
redis:
redis+taskdb://host:port/db
elasticsearch:
elasticsearch+type://host:port/?index=pyspider
local:
local+projectdb://filepath,filepath
type:
taskdb
projectdb
resultdb | [
"create",
"database",
"object",
"by",
"url"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/database/__init__.py#L11-L46 | train |
binux/pyspider | pyspider/scheduler/scheduler.py | Scheduler._update_projects | def _update_projects(self):
'''Check project update'''
now = time.time()
if (
not self._force_update_project
and self._last_update_project + self.UPDATE_PROJECT_INTERVAL > now
):
return
for project in self.projectdb.check_update(self._last_update_project):
self._update_project(project)
logger.debug("project: %s updated.", project['name'])
self._force_update_project = False
self._last_update_project = now | python | def _update_projects(self):
'''Check project update'''
now = time.time()
if (
not self._force_update_project
and self._last_update_project + self.UPDATE_PROJECT_INTERVAL > now
):
return
for project in self.projectdb.check_update(self._last_update_project):
self._update_project(project)
logger.debug("project: %s updated.", project['name'])
self._force_update_project = False
self._last_update_project = now | [
"def",
"_update_projects",
"(",
"self",
")",
":",
"now",
"=",
"time",
".",
"time",
"(",
")",
"if",
"(",
"not",
"self",
".",
"_force_update_project",
"and",
"self",
".",
"_last_update_project",
"+",
"self",
".",
"UPDATE_PROJECT_INTERVAL",
">",
"now",
")",
":",
"return",
"for",
"project",
"in",
"self",
".",
"projectdb",
".",
"check_update",
"(",
"self",
".",
"_last_update_project",
")",
":",
"self",
".",
"_update_project",
"(",
"project",
")",
"logger",
".",
"debug",
"(",
"\"project: %s updated.\"",
",",
"project",
"[",
"'name'",
"]",
")",
"self",
".",
"_force_update_project",
"=",
"False",
"self",
".",
"_last_update_project",
"=",
"now"
] | Check project update | [
"Check",
"project",
"update"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/scheduler/scheduler.py#L206-L218 | train |
binux/pyspider | pyspider/scheduler/scheduler.py | Scheduler._update_project | def _update_project(self, project):
'''update one project'''
if project['name'] not in self.projects:
self.projects[project['name']] = Project(self, project)
else:
self.projects[project['name']].update(project)
project = self.projects[project['name']]
if project._send_on_get_info:
# update project runtime info from processor by sending a _on_get_info
# request, result is in status_page.track.save
project._send_on_get_info = False
self.on_select_task({
'taskid': '_on_get_info',
'project': project.name,
'url': 'data:,_on_get_info',
'status': self.taskdb.SUCCESS,
'fetch': {
'save': self.get_info_attributes,
},
'process': {
'callback': '_on_get_info',
},
})
# load task queue when project is running and delete task_queue when project is stoped
if project.active:
if not project.task_loaded:
self._load_tasks(project)
project.task_loaded = True
else:
if project.task_loaded:
project.task_queue = TaskQueue()
project.task_loaded = False
if project not in self._cnt['all']:
self._update_project_cnt(project.name) | python | def _update_project(self, project):
'''update one project'''
if project['name'] not in self.projects:
self.projects[project['name']] = Project(self, project)
else:
self.projects[project['name']].update(project)
project = self.projects[project['name']]
if project._send_on_get_info:
# update project runtime info from processor by sending a _on_get_info
# request, result is in status_page.track.save
project._send_on_get_info = False
self.on_select_task({
'taskid': '_on_get_info',
'project': project.name,
'url': 'data:,_on_get_info',
'status': self.taskdb.SUCCESS,
'fetch': {
'save': self.get_info_attributes,
},
'process': {
'callback': '_on_get_info',
},
})
# load task queue when project is running and delete task_queue when project is stoped
if project.active:
if not project.task_loaded:
self._load_tasks(project)
project.task_loaded = True
else:
if project.task_loaded:
project.task_queue = TaskQueue()
project.task_loaded = False
if project not in self._cnt['all']:
self._update_project_cnt(project.name) | [
"def",
"_update_project",
"(",
"self",
",",
"project",
")",
":",
"if",
"project",
"[",
"'name'",
"]",
"not",
"in",
"self",
".",
"projects",
":",
"self",
".",
"projects",
"[",
"project",
"[",
"'name'",
"]",
"]",
"=",
"Project",
"(",
"self",
",",
"project",
")",
"else",
":",
"self",
".",
"projects",
"[",
"project",
"[",
"'name'",
"]",
"]",
".",
"update",
"(",
"project",
")",
"project",
"=",
"self",
".",
"projects",
"[",
"project",
"[",
"'name'",
"]",
"]",
"if",
"project",
".",
"_send_on_get_info",
":",
"# update project runtime info from processor by sending a _on_get_info",
"# request, result is in status_page.track.save",
"project",
".",
"_send_on_get_info",
"=",
"False",
"self",
".",
"on_select_task",
"(",
"{",
"'taskid'",
":",
"'_on_get_info'",
",",
"'project'",
":",
"project",
".",
"name",
",",
"'url'",
":",
"'data:,_on_get_info'",
",",
"'status'",
":",
"self",
".",
"taskdb",
".",
"SUCCESS",
",",
"'fetch'",
":",
"{",
"'save'",
":",
"self",
".",
"get_info_attributes",
",",
"}",
",",
"'process'",
":",
"{",
"'callback'",
":",
"'_on_get_info'",
",",
"}",
",",
"}",
")",
"# load task queue when project is running and delete task_queue when project is stoped",
"if",
"project",
".",
"active",
":",
"if",
"not",
"project",
".",
"task_loaded",
":",
"self",
".",
"_load_tasks",
"(",
"project",
")",
"project",
".",
"task_loaded",
"=",
"True",
"else",
":",
"if",
"project",
".",
"task_loaded",
":",
"project",
".",
"task_queue",
"=",
"TaskQueue",
"(",
")",
"project",
".",
"task_loaded",
"=",
"False",
"if",
"project",
"not",
"in",
"self",
".",
"_cnt",
"[",
"'all'",
"]",
":",
"self",
".",
"_update_project_cnt",
"(",
"project",
".",
"name",
")"
] | update one project | [
"update",
"one",
"project"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/scheduler/scheduler.py#L222-L259 | train |
binux/pyspider | pyspider/scheduler/scheduler.py | Scheduler._load_tasks | def _load_tasks(self, project):
'''load tasks from database'''
task_queue = project.task_queue
for task in self.taskdb.load_tasks(
self.taskdb.ACTIVE, project.name, self.scheduler_task_fields
):
taskid = task['taskid']
_schedule = task.get('schedule', self.default_schedule)
priority = _schedule.get('priority', self.default_schedule['priority'])
exetime = _schedule.get('exetime', self.default_schedule['exetime'])
task_queue.put(taskid, priority, exetime)
project.task_loaded = True
logger.debug('project: %s loaded %d tasks.', project.name, len(task_queue))
if project not in self._cnt['all']:
self._update_project_cnt(project.name)
self._cnt['all'].value((project.name, 'pending'), len(project.task_queue)) | python | def _load_tasks(self, project):
'''load tasks from database'''
task_queue = project.task_queue
for task in self.taskdb.load_tasks(
self.taskdb.ACTIVE, project.name, self.scheduler_task_fields
):
taskid = task['taskid']
_schedule = task.get('schedule', self.default_schedule)
priority = _schedule.get('priority', self.default_schedule['priority'])
exetime = _schedule.get('exetime', self.default_schedule['exetime'])
task_queue.put(taskid, priority, exetime)
project.task_loaded = True
logger.debug('project: %s loaded %d tasks.', project.name, len(task_queue))
if project not in self._cnt['all']:
self._update_project_cnt(project.name)
self._cnt['all'].value((project.name, 'pending'), len(project.task_queue)) | [
"def",
"_load_tasks",
"(",
"self",
",",
"project",
")",
":",
"task_queue",
"=",
"project",
".",
"task_queue",
"for",
"task",
"in",
"self",
".",
"taskdb",
".",
"load_tasks",
"(",
"self",
".",
"taskdb",
".",
"ACTIVE",
",",
"project",
".",
"name",
",",
"self",
".",
"scheduler_task_fields",
")",
":",
"taskid",
"=",
"task",
"[",
"'taskid'",
"]",
"_schedule",
"=",
"task",
".",
"get",
"(",
"'schedule'",
",",
"self",
".",
"default_schedule",
")",
"priority",
"=",
"_schedule",
".",
"get",
"(",
"'priority'",
",",
"self",
".",
"default_schedule",
"[",
"'priority'",
"]",
")",
"exetime",
"=",
"_schedule",
".",
"get",
"(",
"'exetime'",
",",
"self",
".",
"default_schedule",
"[",
"'exetime'",
"]",
")",
"task_queue",
".",
"put",
"(",
"taskid",
",",
"priority",
",",
"exetime",
")",
"project",
".",
"task_loaded",
"=",
"True",
"logger",
".",
"debug",
"(",
"'project: %s loaded %d tasks.'",
",",
"project",
".",
"name",
",",
"len",
"(",
"task_queue",
")",
")",
"if",
"project",
"not",
"in",
"self",
".",
"_cnt",
"[",
"'all'",
"]",
":",
"self",
".",
"_update_project_cnt",
"(",
"project",
".",
"name",
")",
"self",
".",
"_cnt",
"[",
"'all'",
"]",
".",
"value",
"(",
"(",
"project",
".",
"name",
",",
"'pending'",
")",
",",
"len",
"(",
"project",
".",
"task_queue",
")",
")"
] | load tasks from database | [
"load",
"tasks",
"from",
"database"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/scheduler/scheduler.py#L263-L280 | train |
binux/pyspider | pyspider/scheduler/scheduler.py | Scheduler.task_verify | def task_verify(self, task):
'''
return False if any of 'taskid', 'project', 'url' is not in task dict
or project in not in task_queue
'''
for each in ('taskid', 'project', 'url', ):
if each not in task or not task[each]:
logger.error('%s not in task: %.200r', each, task)
return False
if task['project'] not in self.projects:
logger.error('unknown project: %s', task['project'])
return False
project = self.projects[task['project']]
if not project.active:
logger.error('project %s not started, please set status to RUNNING or DEBUG',
task['project'])
return False
return True | python | def task_verify(self, task):
'''
return False if any of 'taskid', 'project', 'url' is not in task dict
or project in not in task_queue
'''
for each in ('taskid', 'project', 'url', ):
if each not in task or not task[each]:
logger.error('%s not in task: %.200r', each, task)
return False
if task['project'] not in self.projects:
logger.error('unknown project: %s', task['project'])
return False
project = self.projects[task['project']]
if not project.active:
logger.error('project %s not started, please set status to RUNNING or DEBUG',
task['project'])
return False
return True | [
"def",
"task_verify",
"(",
"self",
",",
"task",
")",
":",
"for",
"each",
"in",
"(",
"'taskid'",
",",
"'project'",
",",
"'url'",
",",
")",
":",
"if",
"each",
"not",
"in",
"task",
"or",
"not",
"task",
"[",
"each",
"]",
":",
"logger",
".",
"error",
"(",
"'%s not in task: %.200r'",
",",
"each",
",",
"task",
")",
"return",
"False",
"if",
"task",
"[",
"'project'",
"]",
"not",
"in",
"self",
".",
"projects",
":",
"logger",
".",
"error",
"(",
"'unknown project: %s'",
",",
"task",
"[",
"'project'",
"]",
")",
"return",
"False",
"project",
"=",
"self",
".",
"projects",
"[",
"task",
"[",
"'project'",
"]",
"]",
"if",
"not",
"project",
".",
"active",
":",
"logger",
".",
"error",
"(",
"'project %s not started, please set status to RUNNING or DEBUG'",
",",
"task",
"[",
"'project'",
"]",
")",
"return",
"False",
"return",
"True"
] | return False if any of 'taskid', 'project', 'url' is not in task dict
or project in not in task_queue | [
"return",
"False",
"if",
"any",
"of",
"taskid",
"project",
"url",
"is",
"not",
"in",
"task",
"dict",
"or",
"project",
"in",
"not",
"in",
"task_queue"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/scheduler/scheduler.py#L297-L315 | train |
binux/pyspider | pyspider/scheduler/scheduler.py | Scheduler.put_task | def put_task(self, task):
'''put task to task queue'''
_schedule = task.get('schedule', self.default_schedule)
self.projects[task['project']].task_queue.put(
task['taskid'],
priority=_schedule.get('priority', self.default_schedule['priority']),
exetime=_schedule.get('exetime', self.default_schedule['exetime'])
) | python | def put_task(self, task):
'''put task to task queue'''
_schedule = task.get('schedule', self.default_schedule)
self.projects[task['project']].task_queue.put(
task['taskid'],
priority=_schedule.get('priority', self.default_schedule['priority']),
exetime=_schedule.get('exetime', self.default_schedule['exetime'])
) | [
"def",
"put_task",
"(",
"self",
",",
"task",
")",
":",
"_schedule",
"=",
"task",
".",
"get",
"(",
"'schedule'",
",",
"self",
".",
"default_schedule",
")",
"self",
".",
"projects",
"[",
"task",
"[",
"'project'",
"]",
"]",
".",
"task_queue",
".",
"put",
"(",
"task",
"[",
"'taskid'",
"]",
",",
"priority",
"=",
"_schedule",
".",
"get",
"(",
"'priority'",
",",
"self",
".",
"default_schedule",
"[",
"'priority'",
"]",
")",
",",
"exetime",
"=",
"_schedule",
".",
"get",
"(",
"'exetime'",
",",
"self",
".",
"default_schedule",
"[",
"'exetime'",
"]",
")",
")"
] | put task to task queue | [
"put",
"task",
"to",
"task",
"queue"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/scheduler/scheduler.py#L325-L332 | train |
binux/pyspider | pyspider/scheduler/scheduler.py | Scheduler.send_task | def send_task(self, task, force=True):
'''
dispatch task to fetcher
out queue may have size limit to prevent block, a send_buffer is used
'''
try:
self.out_queue.put_nowait(task)
except Queue.Full:
if force:
self._send_buffer.appendleft(task)
else:
raise | python | def send_task(self, task, force=True):
'''
dispatch task to fetcher
out queue may have size limit to prevent block, a send_buffer is used
'''
try:
self.out_queue.put_nowait(task)
except Queue.Full:
if force:
self._send_buffer.appendleft(task)
else:
raise | [
"def",
"send_task",
"(",
"self",
",",
"task",
",",
"force",
"=",
"True",
")",
":",
"try",
":",
"self",
".",
"out_queue",
".",
"put_nowait",
"(",
"task",
")",
"except",
"Queue",
".",
"Full",
":",
"if",
"force",
":",
"self",
".",
"_send_buffer",
".",
"appendleft",
"(",
"task",
")",
"else",
":",
"raise"
] | dispatch task to fetcher
out queue may have size limit to prevent block, a send_buffer is used | [
"dispatch",
"task",
"to",
"fetcher"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/scheduler/scheduler.py#L334-L346 | train |
binux/pyspider | pyspider/scheduler/scheduler.py | Scheduler._check_task_done | def _check_task_done(self):
'''Check status queue'''
cnt = 0
try:
while True:
task = self.status_queue.get_nowait()
# check _on_get_info result here
if task.get('taskid') == '_on_get_info' and 'project' in task and 'track' in task:
if task['project'] not in self.projects:
continue
project = self.projects[task['project']]
project.on_get_info(task['track'].get('save') or {})
logger.info(
'%s on_get_info %r', task['project'], task['track'].get('save', {})
)
continue
elif not self.task_verify(task):
continue
self.on_task_status(task)
cnt += 1
except Queue.Empty:
pass
return cnt | python | def _check_task_done(self):
'''Check status queue'''
cnt = 0
try:
while True:
task = self.status_queue.get_nowait()
# check _on_get_info result here
if task.get('taskid') == '_on_get_info' and 'project' in task and 'track' in task:
if task['project'] not in self.projects:
continue
project = self.projects[task['project']]
project.on_get_info(task['track'].get('save') or {})
logger.info(
'%s on_get_info %r', task['project'], task['track'].get('save', {})
)
continue
elif not self.task_verify(task):
continue
self.on_task_status(task)
cnt += 1
except Queue.Empty:
pass
return cnt | [
"def",
"_check_task_done",
"(",
"self",
")",
":",
"cnt",
"=",
"0",
"try",
":",
"while",
"True",
":",
"task",
"=",
"self",
".",
"status_queue",
".",
"get_nowait",
"(",
")",
"# check _on_get_info result here",
"if",
"task",
".",
"get",
"(",
"'taskid'",
")",
"==",
"'_on_get_info'",
"and",
"'project'",
"in",
"task",
"and",
"'track'",
"in",
"task",
":",
"if",
"task",
"[",
"'project'",
"]",
"not",
"in",
"self",
".",
"projects",
":",
"continue",
"project",
"=",
"self",
".",
"projects",
"[",
"task",
"[",
"'project'",
"]",
"]",
"project",
".",
"on_get_info",
"(",
"task",
"[",
"'track'",
"]",
".",
"get",
"(",
"'save'",
")",
"or",
"{",
"}",
")",
"logger",
".",
"info",
"(",
"'%s on_get_info %r'",
",",
"task",
"[",
"'project'",
"]",
",",
"task",
"[",
"'track'",
"]",
".",
"get",
"(",
"'save'",
",",
"{",
"}",
")",
")",
"continue",
"elif",
"not",
"self",
".",
"task_verify",
"(",
"task",
")",
":",
"continue",
"self",
".",
"on_task_status",
"(",
"task",
")",
"cnt",
"+=",
"1",
"except",
"Queue",
".",
"Empty",
":",
"pass",
"return",
"cnt"
] | Check status queue | [
"Check",
"status",
"queue"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/scheduler/scheduler.py#L348-L370 | train |
binux/pyspider | pyspider/scheduler/scheduler.py | Scheduler._check_request | def _check_request(self):
'''Check new task queue'''
# check _postpone_request first
todo = []
for task in self._postpone_request:
if task['project'] not in self.projects:
continue
if self.projects[task['project']].task_queue.is_processing(task['taskid']):
todo.append(task)
else:
self.on_request(task)
self._postpone_request = todo
tasks = {}
while len(tasks) < self.LOOP_LIMIT:
try:
task = self.newtask_queue.get_nowait()
except Queue.Empty:
break
if isinstance(task, list):
_tasks = task
else:
_tasks = (task, )
for task in _tasks:
if not self.task_verify(task):
continue
if task['taskid'] in self.projects[task['project']].task_queue:
if not task.get('schedule', {}).get('force_update', False):
logger.debug('ignore newtask %(project)s:%(taskid)s %(url)s', task)
continue
if task['taskid'] in tasks:
if not task.get('schedule', {}).get('force_update', False):
continue
tasks[task['taskid']] = task
for task in itervalues(tasks):
self.on_request(task)
return len(tasks) | python | def _check_request(self):
'''Check new task queue'''
# check _postpone_request first
todo = []
for task in self._postpone_request:
if task['project'] not in self.projects:
continue
if self.projects[task['project']].task_queue.is_processing(task['taskid']):
todo.append(task)
else:
self.on_request(task)
self._postpone_request = todo
tasks = {}
while len(tasks) < self.LOOP_LIMIT:
try:
task = self.newtask_queue.get_nowait()
except Queue.Empty:
break
if isinstance(task, list):
_tasks = task
else:
_tasks = (task, )
for task in _tasks:
if not self.task_verify(task):
continue
if task['taskid'] in self.projects[task['project']].task_queue:
if not task.get('schedule', {}).get('force_update', False):
logger.debug('ignore newtask %(project)s:%(taskid)s %(url)s', task)
continue
if task['taskid'] in tasks:
if not task.get('schedule', {}).get('force_update', False):
continue
tasks[task['taskid']] = task
for task in itervalues(tasks):
self.on_request(task)
return len(tasks) | [
"def",
"_check_request",
"(",
"self",
")",
":",
"# check _postpone_request first",
"todo",
"=",
"[",
"]",
"for",
"task",
"in",
"self",
".",
"_postpone_request",
":",
"if",
"task",
"[",
"'project'",
"]",
"not",
"in",
"self",
".",
"projects",
":",
"continue",
"if",
"self",
".",
"projects",
"[",
"task",
"[",
"'project'",
"]",
"]",
".",
"task_queue",
".",
"is_processing",
"(",
"task",
"[",
"'taskid'",
"]",
")",
":",
"todo",
".",
"append",
"(",
"task",
")",
"else",
":",
"self",
".",
"on_request",
"(",
"task",
")",
"self",
".",
"_postpone_request",
"=",
"todo",
"tasks",
"=",
"{",
"}",
"while",
"len",
"(",
"tasks",
")",
"<",
"self",
".",
"LOOP_LIMIT",
":",
"try",
":",
"task",
"=",
"self",
".",
"newtask_queue",
".",
"get_nowait",
"(",
")",
"except",
"Queue",
".",
"Empty",
":",
"break",
"if",
"isinstance",
"(",
"task",
",",
"list",
")",
":",
"_tasks",
"=",
"task",
"else",
":",
"_tasks",
"=",
"(",
"task",
",",
")",
"for",
"task",
"in",
"_tasks",
":",
"if",
"not",
"self",
".",
"task_verify",
"(",
"task",
")",
":",
"continue",
"if",
"task",
"[",
"'taskid'",
"]",
"in",
"self",
".",
"projects",
"[",
"task",
"[",
"'project'",
"]",
"]",
".",
"task_queue",
":",
"if",
"not",
"task",
".",
"get",
"(",
"'schedule'",
",",
"{",
"}",
")",
".",
"get",
"(",
"'force_update'",
",",
"False",
")",
":",
"logger",
".",
"debug",
"(",
"'ignore newtask %(project)s:%(taskid)s %(url)s'",
",",
"task",
")",
"continue",
"if",
"task",
"[",
"'taskid'",
"]",
"in",
"tasks",
":",
"if",
"not",
"task",
".",
"get",
"(",
"'schedule'",
",",
"{",
"}",
")",
".",
"get",
"(",
"'force_update'",
",",
"False",
")",
":",
"continue",
"tasks",
"[",
"task",
"[",
"'taskid'",
"]",
"]",
"=",
"task",
"for",
"task",
"in",
"itervalues",
"(",
"tasks",
")",
":",
"self",
".",
"on_request",
"(",
"task",
")",
"return",
"len",
"(",
"tasks",
")"
] | Check new task queue | [
"Check",
"new",
"task",
"queue"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/scheduler/scheduler.py#L374-L417 | train |
binux/pyspider | pyspider/scheduler/scheduler.py | Scheduler._check_cronjob | def _check_cronjob(self):
"""Check projects cronjob tick, return True when a new tick is sended"""
now = time.time()
self._last_tick = int(self._last_tick)
if now - self._last_tick < 1:
return False
self._last_tick += 1
for project in itervalues(self.projects):
if not project.active:
continue
if project.waiting_get_info:
continue
if int(project.min_tick) == 0:
continue
if self._last_tick % int(project.min_tick) != 0:
continue
self.on_select_task({
'taskid': '_on_cronjob',
'project': project.name,
'url': 'data:,_on_cronjob',
'status': self.taskdb.SUCCESS,
'fetch': {
'save': {
'tick': self._last_tick,
},
},
'process': {
'callback': '_on_cronjob',
},
})
return True | python | def _check_cronjob(self):
"""Check projects cronjob tick, return True when a new tick is sended"""
now = time.time()
self._last_tick = int(self._last_tick)
if now - self._last_tick < 1:
return False
self._last_tick += 1
for project in itervalues(self.projects):
if not project.active:
continue
if project.waiting_get_info:
continue
if int(project.min_tick) == 0:
continue
if self._last_tick % int(project.min_tick) != 0:
continue
self.on_select_task({
'taskid': '_on_cronjob',
'project': project.name,
'url': 'data:,_on_cronjob',
'status': self.taskdb.SUCCESS,
'fetch': {
'save': {
'tick': self._last_tick,
},
},
'process': {
'callback': '_on_cronjob',
},
})
return True | [
"def",
"_check_cronjob",
"(",
"self",
")",
":",
"now",
"=",
"time",
".",
"time",
"(",
")",
"self",
".",
"_last_tick",
"=",
"int",
"(",
"self",
".",
"_last_tick",
")",
"if",
"now",
"-",
"self",
".",
"_last_tick",
"<",
"1",
":",
"return",
"False",
"self",
".",
"_last_tick",
"+=",
"1",
"for",
"project",
"in",
"itervalues",
"(",
"self",
".",
"projects",
")",
":",
"if",
"not",
"project",
".",
"active",
":",
"continue",
"if",
"project",
".",
"waiting_get_info",
":",
"continue",
"if",
"int",
"(",
"project",
".",
"min_tick",
")",
"==",
"0",
":",
"continue",
"if",
"self",
".",
"_last_tick",
"%",
"int",
"(",
"project",
".",
"min_tick",
")",
"!=",
"0",
":",
"continue",
"self",
".",
"on_select_task",
"(",
"{",
"'taskid'",
":",
"'_on_cronjob'",
",",
"'project'",
":",
"project",
".",
"name",
",",
"'url'",
":",
"'data:,_on_cronjob'",
",",
"'status'",
":",
"self",
".",
"taskdb",
".",
"SUCCESS",
",",
"'fetch'",
":",
"{",
"'save'",
":",
"{",
"'tick'",
":",
"self",
".",
"_last_tick",
",",
"}",
",",
"}",
",",
"'process'",
":",
"{",
"'callback'",
":",
"'_on_cronjob'",
",",
"}",
",",
"}",
")",
"return",
"True"
] | Check projects cronjob tick, return True when a new tick is sended | [
"Check",
"projects",
"cronjob",
"tick",
"return",
"True",
"when",
"a",
"new",
"tick",
"is",
"sended"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/scheduler/scheduler.py#L419-L449 | train |
binux/pyspider | pyspider/scheduler/scheduler.py | Scheduler._check_select | def _check_select(self):
'''Select task to fetch & process'''
while self._send_buffer:
_task = self._send_buffer.pop()
try:
# use force=False here to prevent automatic send_buffer append and get exception
self.send_task(_task, False)
except Queue.Full:
self._send_buffer.append(_task)
break
if self.out_queue.full():
return {}
taskids = []
cnt = 0
cnt_dict = dict()
limit = self.LOOP_LIMIT
# dynamic assign select limit for each project, use qsize as weight
project_weights, total_weight = dict(), 0
for project in itervalues(self.projects): # type:Project
if not project.active:
continue
# only check project pause when select new tasks, cronjob and new request still working
if project.paused:
continue
if project.waiting_get_info:
continue
# task queue
task_queue = project.task_queue # type:TaskQueue
pro_weight = task_queue.size()
total_weight += pro_weight
project_weights[project.name] = pro_weight
pass
min_project_limit = int(limit / 10.) # ensure minimum select limit for each project
max_project_limit = int(limit / 3.0) # ensure maximum select limit for each project
for pro_name, pro_weight in iteritems(project_weights):
if cnt >= limit:
break
project = self.projects[pro_name] # type:Project
# task queue
task_queue = project.task_queue
task_queue.check_update()
project_cnt = 0
# calculate select limit for project
if total_weight < 1 or pro_weight < 1:
project_limit = min_project_limit
else:
project_limit = int((1.0 * pro_weight / total_weight) * limit)
if project_limit < min_project_limit:
project_limit = min_project_limit
elif project_limit > max_project_limit:
project_limit = max_project_limit
# check send_buffer here. when not empty, out_queue may blocked. Not sending tasks
while cnt < limit and project_cnt < project_limit:
taskid = task_queue.get()
if not taskid:
break
taskids.append((project.name, taskid))
if taskid != 'on_finished':
project_cnt += 1
cnt += 1
cnt_dict[project.name] = project_cnt
if project_cnt:
project._selected_tasks = True
project._send_finished_event_wait = 0
# check and send finished event to project
if not project_cnt and len(task_queue) == 0 and project._selected_tasks:
# wait for self.FAIL_PAUSE_NUM steps to make sure all tasks in queue have been processed
if project._send_finished_event_wait < self.FAIL_PAUSE_NUM:
project._send_finished_event_wait += 1
else:
project._selected_tasks = False
project._send_finished_event_wait = 0
self._postpone_request.append({
'project': project.name,
'taskid': 'on_finished',
'url': 'data:,on_finished',
'process': {
'callback': 'on_finished',
},
"schedule": {
"age": 0,
"priority": 9,
"force_update": True,
},
})
for project, taskid in taskids:
self._load_put_task(project, taskid)
return cnt_dict | python | def _check_select(self):
'''Select task to fetch & process'''
while self._send_buffer:
_task = self._send_buffer.pop()
try:
# use force=False here to prevent automatic send_buffer append and get exception
self.send_task(_task, False)
except Queue.Full:
self._send_buffer.append(_task)
break
if self.out_queue.full():
return {}
taskids = []
cnt = 0
cnt_dict = dict()
limit = self.LOOP_LIMIT
# dynamic assign select limit for each project, use qsize as weight
project_weights, total_weight = dict(), 0
for project in itervalues(self.projects): # type:Project
if not project.active:
continue
# only check project pause when select new tasks, cronjob and new request still working
if project.paused:
continue
if project.waiting_get_info:
continue
# task queue
task_queue = project.task_queue # type:TaskQueue
pro_weight = task_queue.size()
total_weight += pro_weight
project_weights[project.name] = pro_weight
pass
min_project_limit = int(limit / 10.) # ensure minimum select limit for each project
max_project_limit = int(limit / 3.0) # ensure maximum select limit for each project
for pro_name, pro_weight in iteritems(project_weights):
if cnt >= limit:
break
project = self.projects[pro_name] # type:Project
# task queue
task_queue = project.task_queue
task_queue.check_update()
project_cnt = 0
# calculate select limit for project
if total_weight < 1 or pro_weight < 1:
project_limit = min_project_limit
else:
project_limit = int((1.0 * pro_weight / total_weight) * limit)
if project_limit < min_project_limit:
project_limit = min_project_limit
elif project_limit > max_project_limit:
project_limit = max_project_limit
# check send_buffer here. when not empty, out_queue may blocked. Not sending tasks
while cnt < limit and project_cnt < project_limit:
taskid = task_queue.get()
if not taskid:
break
taskids.append((project.name, taskid))
if taskid != 'on_finished':
project_cnt += 1
cnt += 1
cnt_dict[project.name] = project_cnt
if project_cnt:
project._selected_tasks = True
project._send_finished_event_wait = 0
# check and send finished event to project
if not project_cnt and len(task_queue) == 0 and project._selected_tasks:
# wait for self.FAIL_PAUSE_NUM steps to make sure all tasks in queue have been processed
if project._send_finished_event_wait < self.FAIL_PAUSE_NUM:
project._send_finished_event_wait += 1
else:
project._selected_tasks = False
project._send_finished_event_wait = 0
self._postpone_request.append({
'project': project.name,
'taskid': 'on_finished',
'url': 'data:,on_finished',
'process': {
'callback': 'on_finished',
},
"schedule": {
"age": 0,
"priority": 9,
"force_update": True,
},
})
for project, taskid in taskids:
self._load_put_task(project, taskid)
return cnt_dict | [
"def",
"_check_select",
"(",
"self",
")",
":",
"while",
"self",
".",
"_send_buffer",
":",
"_task",
"=",
"self",
".",
"_send_buffer",
".",
"pop",
"(",
")",
"try",
":",
"# use force=False here to prevent automatic send_buffer append and get exception",
"self",
".",
"send_task",
"(",
"_task",
",",
"False",
")",
"except",
"Queue",
".",
"Full",
":",
"self",
".",
"_send_buffer",
".",
"append",
"(",
"_task",
")",
"break",
"if",
"self",
".",
"out_queue",
".",
"full",
"(",
")",
":",
"return",
"{",
"}",
"taskids",
"=",
"[",
"]",
"cnt",
"=",
"0",
"cnt_dict",
"=",
"dict",
"(",
")",
"limit",
"=",
"self",
".",
"LOOP_LIMIT",
"# dynamic assign select limit for each project, use qsize as weight",
"project_weights",
",",
"total_weight",
"=",
"dict",
"(",
")",
",",
"0",
"for",
"project",
"in",
"itervalues",
"(",
"self",
".",
"projects",
")",
":",
"# type:Project",
"if",
"not",
"project",
".",
"active",
":",
"continue",
"# only check project pause when select new tasks, cronjob and new request still working",
"if",
"project",
".",
"paused",
":",
"continue",
"if",
"project",
".",
"waiting_get_info",
":",
"continue",
"# task queue",
"task_queue",
"=",
"project",
".",
"task_queue",
"# type:TaskQueue",
"pro_weight",
"=",
"task_queue",
".",
"size",
"(",
")",
"total_weight",
"+=",
"pro_weight",
"project_weights",
"[",
"project",
".",
"name",
"]",
"=",
"pro_weight",
"pass",
"min_project_limit",
"=",
"int",
"(",
"limit",
"/",
"10.",
")",
"# ensure minimum select limit for each project",
"max_project_limit",
"=",
"int",
"(",
"limit",
"/",
"3.0",
")",
"# ensure maximum select limit for each project",
"for",
"pro_name",
",",
"pro_weight",
"in",
"iteritems",
"(",
"project_weights",
")",
":",
"if",
"cnt",
">=",
"limit",
":",
"break",
"project",
"=",
"self",
".",
"projects",
"[",
"pro_name",
"]",
"# type:Project",
"# task queue",
"task_queue",
"=",
"project",
".",
"task_queue",
"task_queue",
".",
"check_update",
"(",
")",
"project_cnt",
"=",
"0",
"# calculate select limit for project",
"if",
"total_weight",
"<",
"1",
"or",
"pro_weight",
"<",
"1",
":",
"project_limit",
"=",
"min_project_limit",
"else",
":",
"project_limit",
"=",
"int",
"(",
"(",
"1.0",
"*",
"pro_weight",
"/",
"total_weight",
")",
"*",
"limit",
")",
"if",
"project_limit",
"<",
"min_project_limit",
":",
"project_limit",
"=",
"min_project_limit",
"elif",
"project_limit",
">",
"max_project_limit",
":",
"project_limit",
"=",
"max_project_limit",
"# check send_buffer here. when not empty, out_queue may blocked. Not sending tasks",
"while",
"cnt",
"<",
"limit",
"and",
"project_cnt",
"<",
"project_limit",
":",
"taskid",
"=",
"task_queue",
".",
"get",
"(",
")",
"if",
"not",
"taskid",
":",
"break",
"taskids",
".",
"append",
"(",
"(",
"project",
".",
"name",
",",
"taskid",
")",
")",
"if",
"taskid",
"!=",
"'on_finished'",
":",
"project_cnt",
"+=",
"1",
"cnt",
"+=",
"1",
"cnt_dict",
"[",
"project",
".",
"name",
"]",
"=",
"project_cnt",
"if",
"project_cnt",
":",
"project",
".",
"_selected_tasks",
"=",
"True",
"project",
".",
"_send_finished_event_wait",
"=",
"0",
"# check and send finished event to project",
"if",
"not",
"project_cnt",
"and",
"len",
"(",
"task_queue",
")",
"==",
"0",
"and",
"project",
".",
"_selected_tasks",
":",
"# wait for self.FAIL_PAUSE_NUM steps to make sure all tasks in queue have been processed",
"if",
"project",
".",
"_send_finished_event_wait",
"<",
"self",
".",
"FAIL_PAUSE_NUM",
":",
"project",
".",
"_send_finished_event_wait",
"+=",
"1",
"else",
":",
"project",
".",
"_selected_tasks",
"=",
"False",
"project",
".",
"_send_finished_event_wait",
"=",
"0",
"self",
".",
"_postpone_request",
".",
"append",
"(",
"{",
"'project'",
":",
"project",
".",
"name",
",",
"'taskid'",
":",
"'on_finished'",
",",
"'url'",
":",
"'data:,on_finished'",
",",
"'process'",
":",
"{",
"'callback'",
":",
"'on_finished'",
",",
"}",
",",
"\"schedule\"",
":",
"{",
"\"age\"",
":",
"0",
",",
"\"priority\"",
":",
"9",
",",
"\"force_update\"",
":",
"True",
",",
"}",
",",
"}",
")",
"for",
"project",
",",
"taskid",
"in",
"taskids",
":",
"self",
".",
"_load_put_task",
"(",
"project",
",",
"taskid",
")",
"return",
"cnt_dict"
] | Select task to fetch & process | [
"Select",
"task",
"to",
"fetch",
"&",
"process"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/scheduler/scheduler.py#L463-L566 | train |
binux/pyspider | pyspider/scheduler/scheduler.py | Scheduler._dump_cnt | def _dump_cnt(self):
'''Dump counters to file'''
self._cnt['1h'].dump(os.path.join(self.data_path, 'scheduler.1h'))
self._cnt['1d'].dump(os.path.join(self.data_path, 'scheduler.1d'))
self._cnt['all'].dump(os.path.join(self.data_path, 'scheduler.all')) | python | def _dump_cnt(self):
'''Dump counters to file'''
self._cnt['1h'].dump(os.path.join(self.data_path, 'scheduler.1h'))
self._cnt['1d'].dump(os.path.join(self.data_path, 'scheduler.1d'))
self._cnt['all'].dump(os.path.join(self.data_path, 'scheduler.all')) | [
"def",
"_dump_cnt",
"(",
"self",
")",
":",
"self",
".",
"_cnt",
"[",
"'1h'",
"]",
".",
"dump",
"(",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"data_path",
",",
"'scheduler.1h'",
")",
")",
"self",
".",
"_cnt",
"[",
"'1d'",
"]",
".",
"dump",
"(",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"data_path",
",",
"'scheduler.1d'",
")",
")",
"self",
".",
"_cnt",
"[",
"'all'",
"]",
".",
"dump",
"(",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"data_path",
",",
"'scheduler.all'",
")",
")"
] | Dump counters to file | [
"Dump",
"counters",
"to",
"file"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/scheduler/scheduler.py#L616-L620 | train |
binux/pyspider | pyspider/scheduler/scheduler.py | Scheduler._try_dump_cnt | def _try_dump_cnt(self):
'''Dump counters every 60 seconds'''
now = time.time()
if now - self._last_dump_cnt > 60:
self._last_dump_cnt = now
self._dump_cnt()
self._print_counter_log() | python | def _try_dump_cnt(self):
'''Dump counters every 60 seconds'''
now = time.time()
if now - self._last_dump_cnt > 60:
self._last_dump_cnt = now
self._dump_cnt()
self._print_counter_log() | [
"def",
"_try_dump_cnt",
"(",
"self",
")",
":",
"now",
"=",
"time",
".",
"time",
"(",
")",
"if",
"now",
"-",
"self",
".",
"_last_dump_cnt",
">",
"60",
":",
"self",
".",
"_last_dump_cnt",
"=",
"now",
"self",
".",
"_dump_cnt",
"(",
")",
"self",
".",
"_print_counter_log",
"(",
")"
] | Dump counters every 60 seconds | [
"Dump",
"counters",
"every",
"60",
"seconds"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/scheduler/scheduler.py#L622-L628 | train |
binux/pyspider | pyspider/scheduler/scheduler.py | Scheduler._check_delete | def _check_delete(self):
'''Check project delete'''
now = time.time()
for project in list(itervalues(self.projects)):
if project.db_status != 'STOP':
continue
if now - project.updatetime < self.DELETE_TIME:
continue
if 'delete' not in self.projectdb.split_group(project.group):
continue
logger.warning("deleting project: %s!", project.name)
del self.projects[project.name]
self.taskdb.drop(project.name)
self.projectdb.drop(project.name)
if self.resultdb:
self.resultdb.drop(project.name)
for each in self._cnt.values():
del each[project.name] | python | def _check_delete(self):
'''Check project delete'''
now = time.time()
for project in list(itervalues(self.projects)):
if project.db_status != 'STOP':
continue
if now - project.updatetime < self.DELETE_TIME:
continue
if 'delete' not in self.projectdb.split_group(project.group):
continue
logger.warning("deleting project: %s!", project.name)
del self.projects[project.name]
self.taskdb.drop(project.name)
self.projectdb.drop(project.name)
if self.resultdb:
self.resultdb.drop(project.name)
for each in self._cnt.values():
del each[project.name] | [
"def",
"_check_delete",
"(",
"self",
")",
":",
"now",
"=",
"time",
".",
"time",
"(",
")",
"for",
"project",
"in",
"list",
"(",
"itervalues",
"(",
"self",
".",
"projects",
")",
")",
":",
"if",
"project",
".",
"db_status",
"!=",
"'STOP'",
":",
"continue",
"if",
"now",
"-",
"project",
".",
"updatetime",
"<",
"self",
".",
"DELETE_TIME",
":",
"continue",
"if",
"'delete'",
"not",
"in",
"self",
".",
"projectdb",
".",
"split_group",
"(",
"project",
".",
"group",
")",
":",
"continue",
"logger",
".",
"warning",
"(",
"\"deleting project: %s!\"",
",",
"project",
".",
"name",
")",
"del",
"self",
".",
"projects",
"[",
"project",
".",
"name",
"]",
"self",
".",
"taskdb",
".",
"drop",
"(",
"project",
".",
"name",
")",
"self",
".",
"projectdb",
".",
"drop",
"(",
"project",
".",
"name",
")",
"if",
"self",
".",
"resultdb",
":",
"self",
".",
"resultdb",
".",
"drop",
"(",
"project",
".",
"name",
")",
"for",
"each",
"in",
"self",
".",
"_cnt",
".",
"values",
"(",
")",
":",
"del",
"each",
"[",
"project",
".",
"name",
"]"
] | Check project delete | [
"Check",
"project",
"delete"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/scheduler/scheduler.py#L630-L648 | train |
binux/pyspider | pyspider/scheduler/scheduler.py | Scheduler.quit | def quit(self):
'''Set quit signal'''
self._quit = True
# stop xmlrpc server
if hasattr(self, 'xmlrpc_server'):
self.xmlrpc_ioloop.add_callback(self.xmlrpc_server.stop)
self.xmlrpc_ioloop.add_callback(self.xmlrpc_ioloop.stop) | python | def quit(self):
'''Set quit signal'''
self._quit = True
# stop xmlrpc server
if hasattr(self, 'xmlrpc_server'):
self.xmlrpc_ioloop.add_callback(self.xmlrpc_server.stop)
self.xmlrpc_ioloop.add_callback(self.xmlrpc_ioloop.stop) | [
"def",
"quit",
"(",
"self",
")",
":",
"self",
".",
"_quit",
"=",
"True",
"# stop xmlrpc server",
"if",
"hasattr",
"(",
"self",
",",
"'xmlrpc_server'",
")",
":",
"self",
".",
"xmlrpc_ioloop",
".",
"add_callback",
"(",
"self",
".",
"xmlrpc_server",
".",
"stop",
")",
"self",
".",
"xmlrpc_ioloop",
".",
"add_callback",
"(",
"self",
".",
"xmlrpc_ioloop",
".",
"stop",
")"
] | Set quit signal | [
"Set",
"quit",
"signal"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/scheduler/scheduler.py#L653-L659 | train |
binux/pyspider | pyspider/scheduler/scheduler.py | Scheduler.run_once | def run_once(self):
'''comsume queues and feed tasks to fetcher, once'''
self._update_projects()
self._check_task_done()
self._check_request()
while self._check_cronjob():
pass
self._check_select()
self._check_delete()
self._try_dump_cnt() | python | def run_once(self):
'''comsume queues and feed tasks to fetcher, once'''
self._update_projects()
self._check_task_done()
self._check_request()
while self._check_cronjob():
pass
self._check_select()
self._check_delete()
self._try_dump_cnt() | [
"def",
"run_once",
"(",
"self",
")",
":",
"self",
".",
"_update_projects",
"(",
")",
"self",
".",
"_check_task_done",
"(",
")",
"self",
".",
"_check_request",
"(",
")",
"while",
"self",
".",
"_check_cronjob",
"(",
")",
":",
"pass",
"self",
".",
"_check_select",
"(",
")",
"self",
".",
"_check_delete",
"(",
")",
"self",
".",
"_try_dump_cnt",
"(",
")"
] | comsume queues and feed tasks to fetcher, once | [
"comsume",
"queues",
"and",
"feed",
"tasks",
"to",
"fetcher",
"once"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/scheduler/scheduler.py#L661-L671 | train |
binux/pyspider | pyspider/scheduler/scheduler.py | Scheduler.run | def run(self):
'''Start scheduler loop'''
logger.info("scheduler starting...")
while not self._quit:
try:
time.sleep(self.LOOP_INTERVAL)
self.run_once()
self._exceptions = 0
except KeyboardInterrupt:
break
except Exception as e:
logger.exception(e)
self._exceptions += 1
if self._exceptions > self.EXCEPTION_LIMIT:
break
continue
logger.info("scheduler exiting...")
self._dump_cnt() | python | def run(self):
'''Start scheduler loop'''
logger.info("scheduler starting...")
while not self._quit:
try:
time.sleep(self.LOOP_INTERVAL)
self.run_once()
self._exceptions = 0
except KeyboardInterrupt:
break
except Exception as e:
logger.exception(e)
self._exceptions += 1
if self._exceptions > self.EXCEPTION_LIMIT:
break
continue
logger.info("scheduler exiting...")
self._dump_cnt() | [
"def",
"run",
"(",
"self",
")",
":",
"logger",
".",
"info",
"(",
"\"scheduler starting...\"",
")",
"while",
"not",
"self",
".",
"_quit",
":",
"try",
":",
"time",
".",
"sleep",
"(",
"self",
".",
"LOOP_INTERVAL",
")",
"self",
".",
"run_once",
"(",
")",
"self",
".",
"_exceptions",
"=",
"0",
"except",
"KeyboardInterrupt",
":",
"break",
"except",
"Exception",
"as",
"e",
":",
"logger",
".",
"exception",
"(",
"e",
")",
"self",
".",
"_exceptions",
"+=",
"1",
"if",
"self",
".",
"_exceptions",
">",
"self",
".",
"EXCEPTION_LIMIT",
":",
"break",
"continue",
"logger",
".",
"info",
"(",
"\"scheduler exiting...\"",
")",
"self",
".",
"_dump_cnt",
"(",
")"
] | Start scheduler loop | [
"Start",
"scheduler",
"loop"
] | 3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9 | https://github.com/binux/pyspider/blob/3fccfabe2b057b7a56d4a4c79dc0dd6cd2239fe9/pyspider/scheduler/scheduler.py#L673-L692 | train |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.