# 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 lock_process():
+def acquire_lock():
global LOCK
try:
print(f"DEBUG: Acquiring lock: '{lockfile}'")
print(f"ERROR: Cannot aquire lock: '{lockfile}'")
sys.exit(100)
+def init_parser():
+ # DEBUG: print("DEBUG: init_parser(): CALLED!")
+ global _PARSER
+
+ # 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: print("DEBUG: init_parser(): EXIT!")
+
+def run_command():
+ # DEBUG: print("DEBUG: run_command(): CALLED!")
+ args = _PARSER.parse_args()
+ # DEBUG: print(f"DEBUG: args[{type(args)}]={args}")
+ status = args.command(args)
+ # DEBUG: print("DEBUG: status={status} - EXIT!")
+ return status if isinstance(status, int) else 0
+
def shutdown():
print("DEBUG: Closing database connection ...")
fba.connection.close()
- print("DEBUG: Releasing lock ...")
- LOCK.close()
- print(f"DEBUG: Deleting lockfile='{lockfile}' ...")
- os.remove(lockfile)
+
+ if LOCK is not None:
+ print("DEBUG: Releasing lock ...")
+ LOCK.close()
+ print(f"DEBUG: Deleting lockfile='{lockfile}' ...")
+ os.remove(lockfile)
+
print("DEBUG: Shutdown completed.")