Skip to content

Commit

Permalink
fixed: add payme to the INSTALLED_APPS
Browse files Browse the repository at this point in the history
  • Loading branch information
ChogirmaliYigit committed Jan 21, 2024
1 parent 629b8c2 commit 860e333
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions add_app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ast
import os
import sys

Expand All @@ -9,14 +10,41 @@ def add_app_to_installed_apps(app_name, project_path):
with open(settings_file_path, 'r') as settings_file:
settings_content = settings_file.read()

# Parse the AST of the settings file
tree = ast.parse(settings_content)

# Check if the app is already in INSTALLED_APPS
if f"'{app_name}'," in settings_content:
app_already_added = any(
isinstance(node, ast.Str) and node.s == app_name
for node in ast.walk(tree)
if isinstance(node, ast.List) and node.elts
)

if app_already_added:
return

# Find the INSTALLED_APPS assignment node
installed_apps_node = None
for node in ast.walk(tree):
if (
isinstance(node, ast.Assign)
and len(node.targets) == 1
and isinstance(node.targets[0], ast.Name)
and node.targets[0].id == 'INSTALLED_APPS'
):
installed_apps_node = node
break

if installed_apps_node is None:
return

# Add the app to INSTALLED_APPS
with open(settings_file_path, 'a') as settings_file:
settings_file.write(f"\n '{app_name}', # Added by GitHub Actions\n")
except Exception:
installed_apps_node.value.elts.append(ast.Str(s=app_name))

# Write the modified AST back to the settings file
with open(settings_file_path, 'w') as settings_file:
settings_file.write(ast.unparse(tree))
except Exception as e:
pass


Expand Down

0 comments on commit 860e333

Please sign in to comment.