]> git.mxchange.org Git - fba.git/blobdiff - fba/boot.py
WIP:
[fba.git] / fba / boot.py
index 605d2ea8522c5ac432f96de7bc997f97c43722fa..91c2880d59f595f96c9696c13fdee36cc391ee01 100644 (file)
 # 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/>.
 
+import argparse
 import os
 import sys
 import tempfile
 import zc.lockfile
+
+from fba import commands
 from fba import fba
 
 # Lock file
 lockfile = tempfile.gettempdir() + '/fba.lock'
 LOCK = None
+_PARSER = None
 
 def acquire_lock():
     global LOCK
     try:
-        print(f"DEBUG: Acquiring lock: '{lockfile}'")
+        # DEBUG: print(f"DEBUG: Acquiring lock: '{lockfile}'")
         LOCK = zc.lockfile.LockFile(lockfile)
-        print("DEBUG: Lock obtained.")
+        # DEBUG: print("DEBUG: Lock obtained.")
 
     except zc.lockfile.LockError:
         print(f"ERROR: Cannot aquire lock: '{lockfile}'")
         sys.exit(100)
 
+def init_parser():
+    # DEBUG: # DEBUG: print("DEBUG: init_parser(): CALLED!")
+    global _PARSER
+
+    # DEBUG: # DEBUG: print("DEBUG: Initializing parser ...")
+    _PARSER = argparse.ArgumentParser(
+        description="Fetches block reasons from the fediverse",
+        epilog="Please note that some commands have optional arguments, you may want to try fba.py <command> --help to find them out.",
+    )
+    subparser_command = _PARSER.add_subparsers(
+        dest="command",
+        title="Commands to execute",
+        required=True,
+        help="Command to perform",
+    )
+
+    ### Check instance ###
+    parser = subparser_command.add_parser(
+        "check_instance",
+        help="Checks given instance if it exists and returns proper exit code"
+    )
+    parser.add_argument("--domain", required=True, help="Instance name (aka. domain) to check")
+    parser.set_defaults(command=commands.check_instance)
+
+    ### Fetch from bka.li ###
+    parser = subparser_command.add_parser(
+        "fetch_bkali",
+        help="Fetches domain names from bka.li API",
+    )
+    parser.set_defaults(command=commands.fetch_bkali)
+
+    ### Fetch blocks from registered instances or given ###
+    parser = subparser_command.add_parser(
+        "fetch_blocks",
+        help="Fetches blocks from registered instances (run command fetch_instances first!).",
+    )
+    parser.add_argument("--domain", help="Instance name (aka. domain) to fetch blocks from")
+    parser.set_defaults(command=commands.fetch_blocks)
+
+    ### Fetch blocks from chaos.social ###
+    parser = subparser_command.add_parser(
+        "fetch_cs",
+        help="Fetches blocks from chaos.social's meta sub domain.",
+    )
+    parser.set_defaults(command=commands.fetch_cs)
+
+    ### Fetch blocks from a FBA-specific RSS feed  ###
+    parser = subparser_command.add_parser(
+        "fetch_fba_rss",
+        help="Fetches domains from a FBA-specific RSS feed.",
+    )
+    parser.add_argument("--feed", required=True, help="RSS feed to fetch domains from (e.g. https://fba.ryoma.agency/rss?domain=foo.bar).")
+    parser.set_defaults(command=commands.fetch_fba_rss)
+
+    ### Fetch blocks from FBA's bot account ###
+    parser = subparser_command.add_parser(
+        "fetch_fbabot_atom",
+        help="Fetches ATOM feed with domains from FBA's bot account. You may wish to re-run this command several times (at least 3 with big instances) to have a decent amount of valid instances.",
+    )
+    parser.set_defaults(command=commands.fetch_fbabot_atom)
+
+    ### Fetch blocks from federater ###
+    parser = subparser_command.add_parser(
+        "fetch_federater",
+        help="Fetches CSV file (block recommendations) for more possible instances to disover",
+    )
+    parser.set_defaults(command=commands.fetch_federater)
+
+    ### Fetch instances from given initial instance ###
+    parser = subparser_command.add_parser(
+        "fetch_instances",
+        help="Fetches instances (aka. \"domains\") from an initial instance.",
+    )
+    parser.add_argument("--domain", required=True, help="Instance name (aka. domain) to fetch further instances from. Start with a large instance, e.g. mastodon.social .")
+    parser.add_argument("--single", action="store_true", help="Only fetch given instance.")
+    parser.set_defaults(command=commands.fetch_instances)
+
+    # DEBUG: # DEBUG: print("DEBUG: init_parser(): EXIT!")
+
+def run_command():
+    # DEBUG: # DEBUG: print("DEBUG: run_command(): CALLED!")
+    args = _PARSER.parse_args()
+    # DEBUG: # DEBUG: print(f"DEBUG: args[{type(args)}]={args}")
+    status = args.command(args)
+    # DEBUG: # DEBUG: print("DEBUG: status={status} - EXIT!")
+    return status if isinstance(status, int) else 0
+
 def shutdown():
-    print("DEBUG: Closing database connection ...")
+    # DEBUG: print("DEBUG: Closing database connection ...")
     fba.connection.close()
-    print("DEBUG: Releasing lock ...")
-    LOCK.close()
-    print(f"DEBUG: Deleting lockfile='{lockfile}' ...")
-    os.remove(lockfile)
-    print("DEBUG: Shutdown completed.")
+
+    if LOCK is not None:
+        # DEBUG: print("DEBUG: Releasing lock ...")
+        LOCK.close()
+        # DEBUG: print(f"DEBUG: Deleting lockfile='{lockfile}' ...")
+        os.remove(lockfile)
+
+    # DEBUG: print("DEBUG: Shutdown completed.")