Commit 3d66360c authored by Nong Hoang Tu's avatar Nong Hoang Tu
Browse files

new modules for logger and new logic for init config

parent 29f05090
Pipeline #2173 canceled with stages
......@@ -2,6 +2,8 @@
"root_path": "/home/dmknght/ParrotProjects/",
"log_path": "/home/dmknght/Templates/logs/",
"git_url": "git@git.parrot.sh:packages/tools/",
"gbp_path": "/usr/bin/gbp",
"uscan_path": "/usr/bin/uscan",
"dev_name": "Nong Hoang Tu",
"dev_email": "dmknght@parrotsec.org"
}
\ No newline at end of file
......@@ -8,9 +8,12 @@ type
Err* = enum
updated,
outdated,
checkVersionErr,
checkVersionErr
LogHandler* = object
print*: ConsoleLogger
save*: FileLogger
DebPackage* = object
handle*: FileLogger
log*: LogHandler
name*: string # name of project
path*: string # full project path
ProgConf* = object
......@@ -21,12 +24,15 @@ type
devMail*: string
# TODO variables for config parser, logger
var mainLog = newConsoleLogger(fmtStr="$levelname [$date $time] ")
proc checkPrograms(): bool =
if not fileExists("/usr/bin/gbp"):
return false # TODO alert here
if not fileExists("/usr/bin/uscan"):
return false # TODO alert here
proc checkPrograms(gbpPath, uscanPath: string): bool =
if not fileExists(gbpPath):
mainLog.log(lvlError, gbpPath & " not found. Try install git-buildpackage")
return false
if not fileExists(uscanPath):
mainLog.log(lvlError, uscanPath & " not found. Try install uscan")
return false
return true
......@@ -42,36 +48,52 @@ proc initConfig*(config: var ProgConf): bool =
Read the config from update file and generate log
]#
const path = "config.json" # TODO edit here
if not checkPrograms():
# TODO alert here
return false
try:
let
conf = parseFile(path)
rootFolder = conf["root_path"].getStr()
logFolder = conf["log_path"].getStr()
gitUrl = conf["git_url"].getStr()
gbpPath = conf["gbp_path"].getStr()
uscanPath = conf["uscan_path"].getStr()
# Check if gbp and uscan is available
if not checkPrograms(gbpPath, uscanPath):
return false
# Check if root folder of projects is available
if isEmptyOrWhitespace(rootFolder):
config.rootPath = dirName(os.getCurrentDir()) # TODO get folder of program instead
else:
config.rootPath = dirName(rootFolder)
# Create new log folder as day
# TODO check or makeDir here
let logDate = now().format("dd-MM-yyyy")
if isEmptyOrWhitespace(logFolder):
config.logPath = dirName(os.getCurrentDir().dirName() & logDate)
else:
config.logPath = dirName(logFolder.dirName() & logDate)
# Check for valid URL
if gitUrl.startSwith("http"):
# TODO alert url format here
mainLog.log(lvlError, "HTTP protocol is not supported. Please use SSH URL!")
return false
elif isEmptyOrWhitespace(gitUrl):
mainLog.log(lvlError, "URL of git repository must be not empty.")
else:
config.gitUrl = gitURL
# Check name and email of maintainer
if isEmptyOrWhitespace(conf["dev_name"].getStr()) or
isEmptyOrWhitespace(conf["dev_mail"].getStr()):
return false # TODO alert here
mainLog.log(lvlError, "Name and email of maintainer must be not empty!")
return false
else:
config.devName = conf["dev_name"].getStr()
config.devMail = conf["dev_mail"].getStr()
mainLog.log(lvlInfo, "Load configuration completed.")
return true
except:
# TODO show error here
mainLog.log(lvlError, "Unexpected error while reading configurations.")
return false
import configs
import .. / interfaces / logutils
proc updateProjectAll(config: ProgConf) =
let resultLog = initResultLogs(config.logPath)
discard
proc updateProjectSingle(config: ProgConf) =
let resultLog = initResultLogs(config.logPath)
discard
......
#[
]#
import .. / cores / configs
import times
import logging
# TODO rewrite log functions for better code structure
proc getLogTime(): string =
let thisTime = now()
return thisTime.format("hh:mm:ss dd-MM-yyyy")
proc makeLogMsg(name, msg: string): string =
return "[" & getLogTime() & "] [" & name & "] " & msg
method logInfo*(obj: DebPackage, event: string) {.base.} =
obj.handle.log(lvlInfo, makeLogMsg(obj.name, event))
echo " [+] ", event
proc initLogs(fullPath: string): LogHandler =
var
resultLogFile = newFileLogger(
fullPath,
fmtStr = "$levelname [$date $time] "
)
resultLogConsole = newConsoleLogger(
fmtStr = "$levelname [$date $time] "
)
var log = LogHandler(
print: resultLogConsole,
save: resultLogFile
)
return log
method logError*(obj: DebPackage, event: string) {.base.} =
obj.handle.log(lvlError, makeLogMsg(obj.name, event))
echo " [x] ", event
proc initResultLogs*(path: string): LogHandler =
let fullPath = path & "result.log"
return initLogs(fullPath)
method logNoti*(obj: DebPackage, event: string) {.base.} =
obj.handle.log(lvlInfo, makeLogMsg(obj.name, event))
let logTime = getLogTime()
echo "[i] [", logtime, "] [", obj.name, "] [", event, "]"
proc initFullLogs*(path: string): LogHandler =
let fullPath = path & "full.log"
return initLogs(fullPath)
proc retError*(obj: FileLogger, prjName: string) =
# Log for the result
let logTime = getLogTime()
echo "[i] [", logtime, "] [", prjName, "] [Update failed]"
obj.log(lvlError, makeLogMsg(prjName, "Update error"))
proc logInfo*(handler: LogHandler, msg: string) =
handler.print.log(lvlInfo, msg)
handler.save.log(lvlInfo, msg)
proc retLatest*(obj: FileLogger, prjName: string) =
# Log for the result
let logTime = getLogTime()
echo "[i] [", logtime, "] [", prjName, "] [Project is up-to-dated]"
obj.log(lvlInfo, makeLogMsg(prjName, "Update up-to-dated"))
proc logErr*(handler: LogHandler, msg: string) =
handler.print.log(lvlError, msg)
handler.save.log(lvlError, msg)
proc retCompleted*(obj: FileLogger, prjName: string) =
# Log for the result
let logTime = getLogTime()
echo "[i] [", logtime, "] [", prjName, "] [Update completed]"
obj.log(lvlInfo, makeLogMsg(prjName, "Update completed"))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment