Browse Source

First script commit

Matteo Zeccoli Marazzini 5 years ago
parent
commit
340d159d11
3 changed files with 114 additions and 61 deletions
  1. 3 60
      .gitignore
  2. 2 1
      README.md
  3. 109 0
      lcmlog-server

+ 3 - 60
.gitignore

@@ -1,60 +1,3 @@
-# ---> Python
-# Byte-compiled / optimized / DLL files
-__pycache__/
-*.py[cod]
-*$py.class
-
-# C extensions
-*.so
-
-# Distribution / packaging
-.Python
-env/
-build/
-develop-eggs/
-dist/
-downloads/
-eggs/
-.eggs/
-lib/
-lib64/
-parts/
-sdist/
-var/
-*.egg-info/
-.installed.cfg
-*.egg
-
-# PyInstaller
-#  Usually these files are written by a python script from a template
-#  before PyInstaller builds the exe, so as to inject date/other infos into it.
-*.manifest
-*.spec
-
-# Installer logs
-pip-log.txt
-pip-delete-this-directory.txt
-
-# Unit test / coverage reports
-htmlcov/
-.tox/
-.coverage
-.coverage.*
-.cache
-nosetests.xml
-coverage.xml
-*,cover
-
-# Translations
-*.mo
-*.pot
-
-# Django stuff:
-*.log
-
-# Sphinx documentation
-docs/_build/
-
-# PyBuilder
-target/
-
+data/*/*
+auth/*/*
+logs/*

+ 2 - 1
README.md

@@ -1,2 +1,3 @@
-# lcmlog-server
+# lcmlog
 
+Tool for reading and writing the lcm staff logs

+ 109 - 0
lcmlog-server

@@ -0,0 +1,109 @@
+#!/usr/bin/env python3
+
+DIR="/usr/local/src/lcm-unimi/lcmlog"
+
+import os
+import sys
+import pwd
+import logging
+import logging.handlers
+
+def main():
+
+	# Preparing the logger
+	logger = logging.getLogger(__name__)
+	file_formatter = logging.Formatter("%(asctime)s | %(levelname)8s | %(message)s")
+	logger.setLevel(logging.INFO)
+
+	# Logger handle to log all info
+	file_handler = logging.handlers.TimedRotatingFileHandler(filename = DIR + "/logs/logfile", when = "W6", backupCount = 10)
+	file_handler.setFormatter(file_formatter)
+
+	logger.addHandler(file_handler)
+
+	user_id = os.geteuid()
+	user_name = pwd.getpwuid(user_id).pw_name
+
+	logger.info("Started by user " + user_name + " (id " + str(user_id) + ")")
+
+	try:
+		method = input() # Can be GET or POST
+		logger.info("Method: " + method)
+		kind = input()   # Can be 150 or Admin
+		logger.info("Kind: " + kind)
+		date = input()
+		logger.info("Date: " + date)
+
+		if kind != "150" and kind != "Admin":
+			raise KindError
+		if method == "POST":
+			auth(user_id, "POST", kind)
+			tags = input()
+			logger.info("Tags: " + tags)
+			log = sys.stdin.read() # Read the log content
+			post(user_name, kind, date, tags, log)
+		elif method == "GET":
+			auth(user_id, "GET", kind)
+			get(kind, date)
+		else:
+			raise MethodError
+
+	except EOFError as error:
+		logger.critical("1 Not enough input lines")
+		sys.exit(1)
+	except FileNotFoundError as error:
+		logger.critical("2 File not found")
+		sys.exit(2)
+	except FileExistsError as error:
+		logger.critical("3 File already exists")
+		sys.exit(3)
+	except OSError as error:
+		logger.critical("4 File error")
+		sys.exit(4)
+	except MethodError as error:
+		logger.critical("5 Undefined method")
+		sys.exit(5)
+	except KindError as error:
+		logger.critical("6 Undefined log kind")
+		sys.exit(6)
+	except AuthError as error:
+		logger.critical("7 Authentication error")
+		sys.exit(7)
+	except Exception as error:
+		logger.critical("8 Generic error: " + str(error))
+		sys.exit(8)
+
+	finally:
+		logger.info("End\n")
+
+# Print specified log on stdout
+def get(kind, date):
+	with open(DIR + "/data/" + kind + "/" + date, "r") as f:
+		sys.stdout.write("User: " + f.readline())
+		sys.stdout.write("Tags: " + f.readline())
+		sys.stdout.write("\n" + f.read())
+
+# Write log
+def post(user_name, kind, date, tags, log):
+	with open(DIR + "/data/" + kind + "/" + date, "x") as f:
+		f.write(user_name + "\n" + tags + "\n" + log)
+
+# Checks if the user has the permissions to use the requested method
+def auth(user_id, method, kind):
+	with open(DIR + "/auth/" + kind + "/" + method) as f:
+		for line in f:
+			if int(line) == user_id:
+				return
+	raise AuthError()
+
+# Error definitions
+class AuthError(Exception):
+	pass
+class KindError(Exception):
+	pass
+class MethodError(Exception):
+	pass
+
+# Starting point
+if __name__ == "__main__":
+	main()