import logging import os import json import strutils import times type Err* = enum updated, outdated, updateSuccess, checkVersionErr, failed LogHandler* = object print*: ConsoleLogger save*: FileLogger DebPackage* = object log*: LogHandler name*: string # name of project path*: string # full project path ProgConf* = object rootPath*: string logPath*: string uscanPath*: string gbpPath*: string gitUrl*: string devName*: string devMail*: string # TODO variables for config parser, logger var mainLog = newConsoleLogger(fmtStr="$levelname [$date $time] ") 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 proc dirName*(name: string): string = #[ Add slash for folder name ]# result = if not name.endsWith("/"): name & "/" else: name proc initConfig*(config: var ProgConf): bool = #[ Read the config from update file and generate log ]# const path = "config.json" # TODO edit here 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 value of gbp path if isEmptyOrWhitespace(gbpPath): mainLog.log(lvlInfo, "gbp path is empty! Use default value.") config.gbpPath = "/usr/bin/gbp" else: config.gbpPath = gbpPath # Check value of uscan path if isEmptyOrWhitespace(uscanPath): mainLog.log(lvlInfo, "uscan path is empty! Use default value.") config.uscanPath = "/usr/bin/gbp" else: config.uscanPath = uscanPath # Check if gbp and uscan is available if not checkPrograms(config.gbpPath, uscanPath): return false # Check if root folder of projects is available if isEmptyOrWhitespace(rootFolder): config.rootPath = dirName(parentDir(paramStr(0))) else: config.rootPath = dirName(rootFolder) # Create new log folder as day let logDate = now().format("dd-MM-yyyy") if isEmptyOrWhitespace(logFolder): config.logPath = dirName(parentDir(paramStr(0)).dirName() & logDate) else: config.logPath = dirName(logFolder.dirName() & logDate) if not dirExists(config.logPath): createDir(config.logPath) # Check for valid URL if gitUrl.startSwith("http"): 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()): 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: mainLog.log(lvlError, "Unexpected error while reading configurations.") return false