summary refs log tree commit diff stats
path: root/ganarchy/cli
diff options
context:
space:
mode:
authorSoniEx2 <endermoneymod@gmail.com>2020-07-19 20:38:05 -0300
committerSoniEx2 <endermoneymod@gmail.com>2020-07-19 20:38:05 -0300
commitaf3fb6c6eb233af00229a908bab92afbf38bad0b (patch)
tree6aaaa1726c585d25fa84d1a09cb4b1950278097f /ganarchy/cli
parentf216c88680a880db75ab24d9ce8fa35f327ac697 (diff)
Move a lot of things around (broken)
Diffstat (limited to 'ganarchy/cli')
-rw-r--r--ganarchy/cli/__init__.py6
-rw-r--r--ganarchy/cli/db.py71
-rw-r--r--ganarchy/cli/run_targets.py111
3 files changed, 188 insertions, 0 deletions
diff --git a/ganarchy/cli/__init__.py b/ganarchy/cli/__init__.py
index 9effabb..727c48d 100644
--- a/ganarchy/cli/__init__.py
+++ b/ganarchy/cli/__init__.py
@@ -14,6 +14,12 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
+"""The GAnarchy CLI.
+
+This module just defines the main command group. Submodules define
+actual commands.
+"""
+
 import click
 
 @click.group()
diff --git a/ganarchy/cli/db.py b/ganarchy/cli/db.py
new file mode 100644
index 0000000..e754c34
--- /dev/null
+++ b/ganarchy/cli/db.py
@@ -0,0 +1,71 @@
+# This file is part of GAnarchy - decentralized project hub
+# Copyright (C) 2020  Soni L.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+"""Database-related CLI commands.
+
+"""
+
+import os
+
+import click
+
+import ganarchy.cli
+import ganarchy.data
+import ganarchy.db
+import ganarchy.dirs
+
+@ganarchy.cli.main.command()
+def initdb():
+    """Initializes the ganarchy database."""
+    # TODO: makedirs in a separate command?
+    os.makedirs(ganarchy.dirs.DATA_HOME, exist_ok=True)
+    db = ganarchy.db.connect_database(ganarchy.data.ConfigManager.new_default())
+    db.initialize()
+    db.close()
+
+@ganarchy.cli.main.group()
+def migrations():
+    """Modifies the DB to work with a newer/older version.
+
+    WARNING: THIS COMMAND CAN BE EXTREMELY DESTRUCTIVE!"""
+
+@migrations.command()
+@click.argument('migration')
+def apply(migration):
+    """Applies the migration with the given name."""
+    db = ganarchy.db.connect_database(ganarchy.data.ConfigManager.new_default())
+    click.echo(ganarchy.db.MIGRATIONS[migration][0])
+    db.apply_migration(migration)
+    db.close()
+
+@click.argument('migration')
+@migrations.command()
+def revert(migration):
+    """Reverts the migration with the given name."""
+    db = ganarchy.db.connect_database(ganarchy.data.ConfigManager.new_default())
+    click.echo(ganarchy.db.MIGRATIONS[migration][1])
+    db.revert_migration(migration)
+    db.close()
+
+@click.argument('migration', required=False)
+@migrations.command()
+def info(migration):
+    """Shows information about the migration with the given name."""
+    if not migration:
+        # TODO could be improved
+        click.echo(ganarchy.db.MIGRATIONS.keys())
+    else:
+        click.echo(ganarchy.db.MIGRATIONS[migration][2])
diff --git a/ganarchy/cli/run_targets.py b/ganarchy/cli/run_targets.py
new file mode 100644
index 0000000..5bf48d9
--- /dev/null
+++ b/ganarchy/cli/run_targets.py
@@ -0,0 +1,111 @@
+# This file is part of GAnarchy - decentralized project hub
+# Copyright (C) 2020  Soni L.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+"""This module contains the CLI Run Targets.
+"""
+
+import click
+
+from ganarchy import cli
+from ganarchy.templating import environment
+
+#@cli.main.command()
+#@click.argument('out', required=True)
+#def run(out):
+#    """Runs ganarchy standalone.
+#
+#    This will run ganarchy so it regularly updates the output directory given by OUT.
+#    Additionally, it'll also search for the following hooks in its config dirs:
+#
+#        - post_object_update_hook - executed after an object is updated.
+#
+#        - post_update_cycle_hook - executed after all objects in an update cycle are updated."""
+#    pass
+
+@ganarchy.command()
+@click.option('--update/--no-update', default=True)
+@click.argument('project', required=False)
+def cron_target(update, project):
+    """Runs ganarchy as a cron target.
+
+    "Deprecated". Useful if you want full control over how GAnarchy
+    generates the pages.
+    """
+    #conf = None
+    ## reverse order is intentional
+    #for d in reversed(config_dirs):
+    #    try:
+    #        conf = Config(open(d + "/config.toml", 'r', encoding='utf-8', newline=''), conf)
+    #    except (OSError, UnicodeDecodeError, qtoml.decoder.TOMLDecodeError):
+    #        pass
+    #with open(config_home + "/config.toml", 'r', encoding='utf-8', newline='') as f:
+    #    conf = Config(f, conf)
+    env = get_env()
+    if project == "config":
+        # render the config
+        # doesn't have access to a GAnarchy object. this is deliberate.
+        template = env.get_template('index.toml')
+        click.echo(template.render(config = conf))
+        return
+    if project == "project-list":
+        # could be done with a template but eh w/e, this is probably better
+        for project in conf.projects.keys():
+            click.echo(project)
+        return
+    # make sure the cache dir exists
+    os.makedirs(cache_home, exist_ok=True)
+    # make sure it is a git repo
+    subprocess.call(["git", "-C", cache_home, "init", "-q"])
+    conn = sqlite3.connect(data_home + "/ganarchy.db")
+    instance = GAnarchy(conn, conf, list_projects=project in ["index", "config"])
+    if project == "index":
+        # render the index
+        template = env.get_template('index.html')
+        click.echo(template.render(ganarchy = instance))
+        return
+    if not instance.base_url or not project:
+        click.echo("No base URL or project commit specified", err=True)
+        return
+    entries = []
+    generate_html = []
+    c = conn.cursor()
+    p = Project(conn, project, list_repos=True)
+    results = p.update(update)
+    for (repo, count) in results:
+        if count is not None:
+            entries.append((repo.url, count, repo.hash, repo.branch, project))
+            generate_html.append((repo.url, repo.message, count, repo.branch))
+    # sort stuff twice because reasons
+    entries.sort(key=lambda x: x[1], reverse=True)
+    generate_html.sort(key=lambda x: x[2], reverse=True)
+    if update:
+        c.executemany('''INSERT INTO "repo_history" ("url", "count", "head_commit", "branch", "project") VALUES (?, ?, ?, ?, ?)''', entries)
+        conn.commit()
+    html_entries = []
+    for (url, msg, count, branch) in generate_html:
+        history = c.execute('''SELECT "count" FROM "repo_history" WHERE "url" = ? AND "branch" IS ? AND "project" IS ? ORDER BY "entry" ASC''', (url, branch, project)).fetchall()
+        # TODO process history into SVG
+        html_entries.append((url, msg, "", branch))
+    template = env.get_template('project.html')
+    click.echo(template.render(project_title  = p.title,
+                               project_desc   = p.description,
+                               project_body   = p.commit_body,
+                               project_commit = p.commit,
+                               repos          = html_entries,
+                               base_url       = instance.base_url,
+                               # I don't think this thing supports deprecating the above?
+                               project        = p,
+                               ganarchy       = instance))