easily manage build commands
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
2.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #!/usr/bin/env python3
  2. """dirbuild
  3. Usage:
  4. dirbuild [-v]
  5. dirbuild [-v] set <command>
  6. dirbuild [-v] get
  7. dirbuild [-v] build
  8. dirbuild [-v] dump
  9. Options:
  10. -h --help Show this screen.
  11. -v --verbose Show verbose information.
  12. --version Show version.
  13. """
  14. import logging
  15. import os
  16. import sqlite3
  17. from sqlite3.dbapi2 import Cursor
  18. from docopt import docopt
  19. from xdg import BaseDirectory
  20. logger: logging.Logger
  21. def init_logger():
  22. """Initializes the application logger.
  23. :returns: logger
  24. """
  25. global logger
  26. logger = logging.getLogger()
  27. handler = logging.StreamHandler()
  28. formatter = logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s %(message)s")
  29. handler.setFormatter(formatter)
  30. logger.addHandler(handler)
  31. logger.setLevel(logging.ERROR)
  32. def init_db() -> sqlite3.Connection:
  33. path = BaseDirectory.save_data_path("dirbuild")
  34. logging.debug("Using path for data: %s", path)
  35. db_path = f"{path}/commands.db"
  36. logging.debug("Using database %s", db_path)
  37. conn = sqlite3.connect(db_path)
  38. conn.cursor().execute(
  39. """CREATE TABLE IF NOT EXISTS commands (id INTEGER PRIMARY KEY AUTOINCREMENT, dir TEXT, cmd TEXT)"""
  40. )
  41. return conn
  42. def set_command(cursor: Cursor, cwd: str, command: str):
  43. """Sets a command for the current directory
  44. :db: sqlite Cursor
  45. :cwd: String
  46. :returns: None
  47. """
  48. logger.debug("Setting command '%s' for directory %s", command, cwd)
  49. cursor.execute(
  50. "INSERT INTO commands (dir, cmd) VALUES (?, ?)",
  51. (
  52. cwd,
  53. command,
  54. ),
  55. )
  56. def get_command(cursor: Cursor, cwd: str) -> str:
  57. """Get the command for the current directory"""
  58. cmd = cursor.execute(
  59. "SELECT cmd FROM commands WHERE dir = ? ORDER BY id DESC LIMIT 1", (cwd,)
  60. ).fetchone()
  61. return cmd[0] if cmd is not None else ""
  62. def dump_db(cursor: Cursor):
  63. """Dump all entries of the database"""
  64. for row in cursor.execute("SELECT id, dir, cmd FROM commands"):
  65. print(row)
  66. def main():
  67. """ Main function """
  68. init_logger()
  69. arguments = docopt(__doc__, version="dirbuild v0.1.0")
  70. if arguments["--verbose"]:
  71. logger.setLevel(logging.DEBUG)
  72. logger.debug("arguments: %s", arguments)
  73. db = init_db()
  74. if arguments["set"]:
  75. set_command(db.cursor(), os.getcwd(), arguments["<command>"])
  76. print("Saved command successfully")
  77. elif arguments["get"]:
  78. print(get_command(db.cursor(), os.getcwd()))
  79. elif arguments["dump"]:
  80. dump_db(db.cursor())
  81. else:
  82. cmd = get_command(db.cursor(), str(os.getcwd()))
  83. logger.debug("Executing '%s'", cmd)
  84. os.system(cmd)
  85. db.commit()
  86. db.close()
  87. if __name__ == "__main__":
  88. main()