Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# (using __temp prefix makes it show up in the middle, because it's a file)
archive_name = f'{project_path.name}.otreezip'
settings_file = project_path / 'settings.py'
if not settings_file.exists():
msg = (
"Cannot find oTree settings. "
"You must run this command from the folder that contains your "
"settings.py file."
)
logger.error(msg)
sys.exit(1)
try:
check_requirements_files(project_path)
except RequirementsError as exc:
logger.error(str(exc))
sys.exit(1)
# once Heroku uses py 3.7 by default, we can remove this runtime stuff.
runtime_txt = project_path / 'runtime.txt'
runtime_existed = runtime_txt.exists()
if not runtime_existed:
# don't use sys.version_info because it might be newer than what
# heroku supports
runtime_txt.write_text(f'python-3.7.3')
try:
with tarfile.open(archive_name, 'w:gz') as tar:
# if i omit arcname, it nests the project 2 levels deep.
# if i say arcname=proj, it puts the whole project in a folder.
# if i say arcname='', it has 0 levels of nesting.
tar.add(project_path, arcname='', filter=filter_func)
reqs_base_exists = reqs_base_path.exists()
if not reqs_path.exists():
raise RequirementsError("You need a requirements.txt in your project folder")
with reqs_path.open() as f:
all_req_lines = get_non_comment_lines(f)
reqs_base_should_exist = False
for ln in all_req_lines:
if 'requirements_base.txt' in ln:
reqs_base_should_exist = True
if reqs_base_exists != reqs_base_should_exist:
if reqs_base_should_exist:
raise RequirementsError(
'Your requirements.txt calls requirements_base.txt, '
'but requirements_base.txt was not found.'
)
else:
raise RequirementsError(
'Your requirements_base.txt '
'is being ignored. '
'Add the following line to requirements.txt:\n'
'-r requirements_base.txt'
)
if reqs_base_exists:
with reqs_base_path.open() as f:
all_req_lines.extend(get_non_comment_lines(f))
psycopg2_found = False
def check_requirements_files(project_path: Path):
reqs_server_path = project_path / 'requirements_server.txt'
if reqs_server_path.exists():
# checking legacy requirements structure is too complicated,
# skip it.
return
reqs_path = project_path / 'requirements.txt'
reqs_base_path = project_path / 'requirements_base.txt'
reqs_base_exists = reqs_base_path.exists()
if not reqs_path.exists():
raise RequirementsError("You need a requirements.txt in your project folder")
with reqs_path.open() as f:
all_req_lines = get_non_comment_lines(f)
reqs_base_should_exist = False
for ln in all_req_lines:
if 'requirements_base.txt' in ln:
reqs_base_should_exist = True
if reqs_base_exists != reqs_base_should_exist:
if reqs_base_should_exist:
raise RequirementsError(
'Your requirements.txt calls requirements_base.txt, '
'but requirements_base.txt was not found.'
)
else:
'is being ignored. '
'Add the following line to requirements.txt:\n'
'-r requirements_base.txt'
)
if reqs_base_exists:
with reqs_base_path.open() as f:
all_req_lines.extend(get_non_comment_lines(f))
psycopg2_found = False
for ln in all_req_lines:
if 'psycopg2' in ln:
psycopg2_found = True
if not psycopg2_found:
raise RequirementsError(
'Your requirements.txt must have a line that says "psycopg2", '
'which is necessary for Postgres. '
)
# check duplicates
already_seen = set()
for ln in all_req_lines:
m = re.match('(^[\w-]+).*?', ln)
if m:
package = m.group(1)
if package in already_seen:
if reqs_base_exists:
raise RequirementsError(
f'"{package}" is listed more than once '
'in your requirements_base.txt and/or requirements.txt. '
if not psycopg2_found:
raise RequirementsError(
'Your requirements.txt must have a line that says "psycopg2", '
'which is necessary for Postgres. '
)
# check duplicates
already_seen = set()
for ln in all_req_lines:
m = re.match('(^[\w-]+).*?', ln)
if m:
package = m.group(1)
if package in already_seen:
if reqs_base_exists:
raise RequirementsError(
f'"{package}" is listed more than once '
'in your requirements_base.txt and/or requirements.txt. '
)
else:
raise RequirementsError(
f'"{package}" is listed more than once '
'in your requirements.txt. '
)
already_seen.add(package)
# check duplicates
already_seen = set()
for ln in all_req_lines:
m = re.match('(^[\w-]+).*?', ln)
if m:
package = m.group(1)
if package in already_seen:
if reqs_base_exists:
raise RequirementsError(
f'"{package}" is listed more than once '
'in your requirements_base.txt and/or requirements.txt. '
)
else:
raise RequirementsError(
f'"{package}" is listed more than once '
'in your requirements.txt. '
)
already_seen.add(package)