Skip to content

Commit

Permalink
build: limit 150 nodes and 4 extensions for demo edition
Browse files Browse the repository at this point in the history
  • Loading branch information
eri24816 committed Jun 28, 2024
1 parent 2f526c3 commit 3386ed6
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 9 deletions.
56 changes: 53 additions & 3 deletions backend/src/grapycal/core/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,28 +259,78 @@ def _initialize_workspace(self) -> None:
self._workspace_object = self._objectsync.create_object(
WorkspaceObject, parent_id="root"
)
# ===CHECK_LICENSE=== #
try:
self._extention_manager.import_extension("grapycal_builtin")
except ModuleNotFoundError:
pass

def _save_workspace(self, path: str, send_message=True) -> None:
workspace_serialized = self._workspace_object.serialize()
with self._objectsync.record(): # lock the state of the workspace
node_count = len(main_store.main_editor.top_down_search(type=Node))
edge_count = len(main_store.main_editor.top_down_search(type=Edge))

# % enable_for_demo
# if node_count > 150:
# logger.warning(
# f"Sorry, cannot save workspace with more than 150 nodes in demo edition. {node_count} nodes found. Please remove some nodes and try again."
# )
# if send_message:
# self._send_message_to_all(
# f"Sorry, cannot save workspace with more than 150 nodes in demo edition. {node_count} nodes found. Please remove some nodes and try again."
# )
# return
# else:
# workspace_serialized = self._workspace_object.serialize()
# % end_enable_for_demo

# % disable_for_demo
workspace_serialized = self._workspace_object.serialize()
# % end_disable_for_demo

metadata = {
"version": grapycal.__version__,
"extensions": self._extention_manager.get_extensions_info(),
}

# % enable_for_demo
# e = self._extention_manager.get_extention_names()
# a = 64
# b = 58
# if (len(e) * len(e) * len(e) > a) or not str(((b * b + a) ^ 1545) / 53)[
# 5:12
# ] == "8679245":
# logger.warning(
# f"Sorry, cannot save workspace with more than 4 extensions in demo edition. {len(e)} extensions found. Please unimport some extensions and try again."
# )
# if send_message:
# self._send_message_to_all(
# f"Sorry, cannot save workspace with more than 4 extensions in demo edition. {len(e)} extensions found. Please unimport some extensions and try again."
# )
# return
# else:
# if len([64,64,1545,2,a,b]*2) != a-52:
# return
# data = {
# "extensions": self._extention_manager.get_extention_names()[0:4],
# "client_id_count": self._objectsync.get_client_id_count(),
# "id_count": self._objectsync.get_id_count(),
# "grapycal_id_count": self.grapycal_id_count,
# "workspace_serialized": workspace_serialized.to_dict(),
# }
# % end_enable_for_demo

# % disable_for_demo
data = {
"extensions": self._extention_manager.get_extention_names(),
"client_id_count": self._objectsync.get_client_id_count(),
"id_count": self._objectsync.get_id_count(),
"grapycal_id_count": self.grapycal_id_count,
"workspace_serialized": workspace_serialized.to_dict(),
}
# % end_disable_for_demo

file_size = write_workspace(path, metadata, data, compress=True)
node_count = len(main_store.main_editor.top_down_search(type=Node))
edge_count = len(main_store.main_editor.top_down_search(type=Edge))
logger.info(
f"Workspace saved to {path}. {node_count} nodes, {edge_count} edges, {file_size // 1024} KB."
)
Expand Down
32 changes: 27 additions & 5 deletions packaging/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def __init__(
def run(self, src: Path, dst: Path):
x = From(src / self.package_src_dir.parent) * Select(self.package_src_dir.name)
if self.edition in ["demo", "full"]:
x *= AddLicenseCheckCode()
x *= Preprocessor()
x * Pyarmor(self.pyarmor_config) * To(dst / self.package_src_dir.parent)

From(src) * Select("pyproject.toml") * To(dst)
Expand Down Expand Up @@ -380,6 +380,13 @@ def insert_code_into_lines(lines, idx, code, indent):
return lines


def find_next_line_with(lines, start, text):
for i in range(start, len(lines)):
if text in lines[i].replace(" ", "").replace("\t", ""):
return i
raise Exception(f"Text {text} not found in lines")


SIGNATURE_E = int(os.environ["SIGNATURE_E"])
SIGNATURE_N = int(os.environ["SIGNATURE_N"])

Expand Down Expand Up @@ -444,13 +451,12 @@ def get_ip_addresses(family):
"""


class AddLicenseCheckCode(Step):
class Preprocessor(Step):
"""
Replaces the # ===CHECK_LICENSE=== # markers in the source code with the actual license check code
And other preprocessing tasks
"""

# TODO check mac address and time

def run(self, src: Path, dst: Path):
# copy src to dst to avoid modifying the original files
From(src) * To(dst)
Expand All @@ -463,11 +469,27 @@ def run(self, src: Path, dst: Path):
# find the line with the license check code
i = 0
while i < len(lines):
if "# ===CHECK_LICENSE=== #" in lines[i]:
line_no_space = lines[i].replace(" ", "").replace("\t", "")
if "#===CHECK_LICENSE===#" in line_no_space:
indent = len(lines[i]) - len(lines[i].lstrip())
lines[i] = ""
# insert the license check code
lines = insert_code_into_lines(lines, i, check_license_code, indent)
elif "#%enable_for_demo" in line_no_space:
j = find_next_line_with(lines, i, "#%end_enable_for_demo")
lines[i] = ""
lines[j] = ""
for k in range(i + 1, j):
assert "# " in lines[k], (
f"Line {k} does not start with # ." + lines[k]
)
lines[k] = lines[k].replace("# ", "", 1)
elif "#%disable_for_demo" in line_no_space:
j = find_next_line_with(lines, i, "#%end_disable_for_demo")
lines[i] = ""
lines[j] = ""
for k in range(i, j):
lines[k] = ""
i += 1

# write the modified lines back to the file
Expand Down
2 changes: 1 addition & 1 deletion submodules/topicsync

0 comments on commit 3386ed6

Please sign in to comment.