mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-08-28 08:06:24 -07:00
Make changes to build scripts as suggested by deepsource
This commit is contained in:
parent
58279f535a
commit
351737117d
2 changed files with 158 additions and 123 deletions
210
build.py
210
build.py
|
@ -9,8 +9,6 @@ from os import path
|
|||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tarfile
|
||||
from zipfile import ZipFile
|
||||
|
||||
gc_dependencies = (
|
||||
"github.com/disintegration/imaging",
|
||||
|
@ -39,7 +37,6 @@ release_files = (
|
|||
"html/notbanned.png",
|
||||
"html/permabanned.jpg",
|
||||
"sample-configs",
|
||||
# "sql",
|
||||
"templates",
|
||||
"initdb_master.sql",
|
||||
"initdb_mysql.sql",
|
||||
|
@ -48,24 +45,32 @@ release_files = (
|
|||
"README.md",
|
||||
)
|
||||
|
||||
def fs_action(action, sourcefile, destfile = ""):
|
||||
gcos = ""
|
||||
gcos_name = "" # used for release, since macOS GOOS is "darwin"
|
||||
exe = ""
|
||||
gochan_bin = ""
|
||||
gochan_exe = ""
|
||||
version = ""
|
||||
|
||||
|
||||
def fs_action(action_str, sourcefile, destfile=""):
|
||||
isfile = path.isfile(sourcefile) or path.islink(sourcefile)
|
||||
isdir = path.isdir(sourcefile)
|
||||
if action == "copy":
|
||||
if action_str == "copy":
|
||||
fs_action("delete", destfile)
|
||||
if isfile:
|
||||
shutil.copy(sourcefile, destfile)
|
||||
elif isdir:
|
||||
shutil.copytree(sourcefile, destfile)
|
||||
elif action == "move":
|
||||
elif action_str == "move":
|
||||
fs_action("delete", destfile)
|
||||
shutil.move(sourcefile, destfile)
|
||||
elif action == "mkdir":
|
||||
elif action_str == "mkdir":
|
||||
if isfile:
|
||||
fs_action("delete", sourcefile)
|
||||
elif not isdir:
|
||||
elif isdir is False:
|
||||
os.makedirs(sourcefile)
|
||||
elif action == "delete":
|
||||
elif action_str == "delete":
|
||||
if isfile:
|
||||
os.remove(sourcefile)
|
||||
elif isdir:
|
||||
|
@ -73,14 +78,17 @@ def fs_action(action, sourcefile, destfile = ""):
|
|||
else:
|
||||
raise Exception("Invalid action, must be 'copy', 'move', 'mkdir', or 'delete'")
|
||||
|
||||
def run_cmd(cmd, print_output = True, realtime = False, print_command = False):
|
||||
|
||||
def run_cmd(cmd, print_output=True, realtime=False, print_command=False):
|
||||
if print_command:
|
||||
print(cmd)
|
||||
|
||||
proc = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr=subprocess.STDOUT, shell = True)
|
||||
proc = subprocess.Popen(cmd,
|
||||
stdout = subprocess.PIPE,
|
||||
stderr = subprocess.STDOUT,
|
||||
shell = True)
|
||||
output = ""
|
||||
status = 0
|
||||
if realtime: # print the command's output in real time, ignores print_output
|
||||
if realtime: # print the command's output in real time, ignores print_output
|
||||
while True:
|
||||
realtime_output = proc.stdout.readline().decode("utf-8")
|
||||
if realtime_output == "" and status is not None:
|
||||
|
@ -89,7 +97,7 @@ def run_cmd(cmd, print_output = True, realtime = False, print_command = False):
|
|||
print(realtime_output.strip())
|
||||
output += realtime_output
|
||||
status = proc.poll()
|
||||
else: # wait until the command is finished to print the output
|
||||
else: # wait until the command is finished to print the output
|
||||
output = proc.communicate()[0]
|
||||
if output is not None:
|
||||
output = output.decode("utf-8").strip()
|
||||
|
@ -103,7 +111,7 @@ def run_cmd(cmd, print_output = True, realtime = False, print_command = False):
|
|||
return (output, status)
|
||||
|
||||
|
||||
def set_vars(goos = ""):
|
||||
def set_vars(goos=""):
|
||||
""" Sets version and GOOS-related variables to be used globally"""
|
||||
global gcos
|
||||
global gcos_name # used for release, since macOS GOOS is "darwin"
|
||||
|
@ -119,7 +127,7 @@ def set_vars(goos = ""):
|
|||
exe, exe_status = run_cmd("go env GOEXE", print_output = False)
|
||||
if gcos_status + exe_status != 0:
|
||||
print("Invalid GOOS value, check your GOOS environment variable")
|
||||
exit(1)
|
||||
sys.exit(1)
|
||||
gcos_name = gcos
|
||||
if gcos_name == "darwin":
|
||||
gcos_name = "macos"
|
||||
|
@ -127,11 +135,11 @@ def set_vars(goos = ""):
|
|||
gochan_bin = "gochan"
|
||||
gochan_exe = "gochan" + exe
|
||||
|
||||
version_file = open("version", "r")
|
||||
version = version_file.read().strip()
|
||||
version_file.close()
|
||||
with open("version", "r") as version_file:
|
||||
version = version_file.read().strip()
|
||||
|
||||
def build(debugging = False):
|
||||
|
||||
def build(debugging=False):
|
||||
"""Build the gochan executable for the current GOOS"""
|
||||
pwd = os.getcwd()
|
||||
trimpath = "-trimpath=" + pwd
|
||||
|
@ -140,7 +148,7 @@ def build(debugging = False):
|
|||
build_cmd = "go build -v -asmflags=" + trimpath
|
||||
|
||||
if debugging:
|
||||
print("Building for", gcos,"with debugging symbols")
|
||||
print("Building for", gcos, "with debugging symbols")
|
||||
gcflags = gcflags.format(" -l -N")
|
||||
ldflags = ldflags.format("")
|
||||
else:
|
||||
|
@ -149,11 +157,13 @@ def build(debugging = False):
|
|||
print("Building for", gcos)
|
||||
build_cmd += gcflags + ldflags
|
||||
|
||||
status = run_cmd(build_cmd + " -o " + gochan_exe + " ./cmd/gochan", realtime = True, print_command = True)[1]
|
||||
status = run_cmd(build_cmd + " -o " + gochan_exe + " ./cmd/gochan",
|
||||
realtime = True, print_command = True)[1]
|
||||
if status != 0:
|
||||
print("Failed building gochan, see command output for details")
|
||||
exit(1)
|
||||
print("Built gochan successfully\n")
|
||||
sys.exit(1)
|
||||
print("Built gochan successfully")
|
||||
|
||||
|
||||
def clean():
|
||||
print("Cleaning up")
|
||||
|
@ -161,11 +171,13 @@ def clean():
|
|||
for del_file in del_files:
|
||||
fs_action("delete", del_file)
|
||||
|
||||
|
||||
def dependencies():
|
||||
for dep in gc_dependencies:
|
||||
run_cmd("go get -v " + dep, realtime = True, print_command = True)
|
||||
|
||||
def docker(option = "guestdb", attached = False):
|
||||
|
||||
def docker(option="guestdb", attached=False):
|
||||
cmd = "docker-compose -f {} up --build"
|
||||
if option == "guestdb":
|
||||
cmd = cmd.format("docker/docker-compose-mariadb.yaml")
|
||||
|
@ -173,23 +185,24 @@ def docker(option = "guestdb", attached = False):
|
|||
cmd = cmd.format("docker/docker-compose.yml.default")
|
||||
elif option == "macos":
|
||||
cmd = cmd.format("docker/docker-compose-syncForMac.yaml")
|
||||
if not attached:
|
||||
if attached is False:
|
||||
cmd += " --detach"
|
||||
status = run_cmd(cmd, print_output = True, realtime = True, print_command = True)[1]
|
||||
if status != 0:
|
||||
print("Failed starting a docker container, exited with status code", status)
|
||||
exit(1)
|
||||
print("Failed starting a docker container, exited with status code", status)
|
||||
sys.exit(1)
|
||||
|
||||
def install(prefix = "/usr", document_root = "/srv/gochan", js_only = False, css_only = False, templates_only = False):
|
||||
if gcos == "windows" or gcos == "darwin":
|
||||
|
||||
def install(prefix="/usr", document_root="/srv/gochan", js_only=False, css_only=False, templates_only=False):
|
||||
if gcos in ('windows', 'darwin'):
|
||||
print("Installation is not currently supported for Windows and macOS, use the respective directory created by running `python build.py release`")
|
||||
exit(1)
|
||||
sys.exit(1)
|
||||
|
||||
done = False
|
||||
if js_only:
|
||||
print("Installing gochan JavaScript files")
|
||||
js_install_dir = path.join(document_root, "js")
|
||||
if not path.exists(js_install_dir):
|
||||
if path.exists(js_install_dir) is False:
|
||||
fs_action("mkdir", js_install_dir)
|
||||
fs_action("copy", "html/js/gochan.js", path.join(js_install_dir, "gochan.js"))
|
||||
fs_action("copy", "html/js/maps", path.join(js_install_dir, "maps"))
|
||||
|
@ -202,7 +215,7 @@ def install(prefix = "/usr", document_root = "/srv/gochan", js_only = False, css
|
|||
if templates_only:
|
||||
print("Installing template files")
|
||||
templates_install_dir = path.join(prefix, "share/gochan/templates")
|
||||
if not path.exists(templates_install_dir):
|
||||
if path.exists(templates_install_dir) is False:
|
||||
fs_action("mkdir", templates_install_dir)
|
||||
template_files = os.listdir("templates")
|
||||
for template in template_files:
|
||||
|
@ -214,7 +227,7 @@ def install(prefix = "/usr", document_root = "/srv/gochan", js_only = False, css
|
|||
done = True
|
||||
if done:
|
||||
return
|
||||
|
||||
|
||||
fs_action("mkdir", "/etc/gochan")
|
||||
fs_action("mkdir", path.join(prefix, "share/gochan"))
|
||||
fs_action("mkdir", document_root)
|
||||
|
@ -223,34 +236,33 @@ def install(prefix = "/usr", document_root = "/srv/gochan", js_only = False, css
|
|||
out_path = path.join(prefix, "share", "gochan", file)
|
||||
if file.startswith("html/"):
|
||||
out_path = path.join(document_root, file.replace("html/", ""))
|
||||
|
||||
|
||||
print("Installing", file, "to", out_path)
|
||||
fs_action("copy", file, out_path)
|
||||
|
||||
if path.exists(gochan_exe) == False:
|
||||
if path.exists(gochan_exe) is False:
|
||||
build()
|
||||
print("Installing",gochan_exe,"to",path.join(prefix, "bin", gochan_exe))
|
||||
print("Installing", gochan_exe, "to", path.join(prefix, "bin", gochan_exe))
|
||||
fs_action("copy", gochan_exe, path.join(prefix, "bin", gochan_exe))
|
||||
print("Note: gochan-migration has been put on indefinite suspention. See README.md")
|
||||
|
||||
print(
|
||||
"gochan successfully installed. If you haven't already, you should copy\n",
|
||||
"sample-configs/gochan.example.json to /etc/gochan/gochan.json and modify that as needed.\n",
|
||||
"You may also need to go to https://yourgochansite/manage?action=rebuildall to rebuild the javascript config"
|
||||
)
|
||||
"gochan was successfully installed. If you haven't already, you should copy\n",
|
||||
"sample-configs/gochan.example.json to /etc/gochan/gochan.json (modify as needed)\n",
|
||||
"You may also need to go to https://yourgochansite/manage?action=rebuildall to rebuild the javascript config")
|
||||
if gcos == "linux":
|
||||
print("If your Linux distribution has systemd, you will also need to run the following commands:\n",
|
||||
print(
|
||||
"If your Linux distribution has systemd, you will also need to run the following commands:\n",
|
||||
"cp sample-configs/gochan-[mysql|postgresql].service /lib/systemd/system/gochan.service\n",
|
||||
"systemctl daemon-reload\n",
|
||||
"systemctl enable gochan.service\n",
|
||||
"systemctl start gochan.service",
|
||||
)
|
||||
"systemctl start gochan.service")
|
||||
|
||||
|
||||
def js(nominify = False, watch = False):
|
||||
print("Transpiling JS")
|
||||
npm_cmd = "npm --prefix frontend/ run build"
|
||||
if nominify == False:
|
||||
if nominify is False:
|
||||
npm_cmd += "-minify"
|
||||
if watch:
|
||||
npm_cmd += "-watch"
|
||||
|
@ -258,20 +270,22 @@ def js(nominify = False, watch = False):
|
|||
status = run_cmd(npm_cmd, True, True, True)[1]
|
||||
if status != 0:
|
||||
print("JS transpiling failed with status", status)
|
||||
exit(status)
|
||||
sys.exit(status)
|
||||
|
||||
|
||||
def release(goos):
|
||||
set_vars(goos)
|
||||
build(False)
|
||||
release_name = gochan_bin + "-v" + version + "_" + gcos_name
|
||||
release_dir = path.join("releases", release_name)
|
||||
print("Creating release for", gcos_name)
|
||||
print("Creating release for", gcos_name, "\n")
|
||||
fs_action("mkdir", path.join(release_dir, "html"))
|
||||
for file in release_files:
|
||||
fs_action("copy", file, path.join(release_dir, file))
|
||||
fs_action("copy", gochan_exe, path.join(release_dir, gochan_exe))
|
||||
format = "zip" if goos == "windows" or goos == "darwin" else "gztar"
|
||||
shutil.make_archive(release_dir, format, root_dir="releases", base_dir=release_name)
|
||||
archive_type = "zip" if goos in ('windows', 'darwin') else "gztar"
|
||||
shutil.make_archive(release_dir, archive_type, root_dir="releases", base_dir=release_name)
|
||||
|
||||
|
||||
def sass(minify = False):
|
||||
sass_cmd = "sass "
|
||||
|
@ -281,49 +295,57 @@ def sass(minify = False):
|
|||
status = run_cmd(sass_cmd, realtime = True, print_command = True)[1]
|
||||
if status != 0:
|
||||
print("Failed running sass with status", status)
|
||||
exit(status)
|
||||
sys.exit(status)
|
||||
|
||||
|
||||
def test():
|
||||
pkgs = os.listdir("pkg")
|
||||
for pkg in pkgs:
|
||||
run_cmd("go test " + path.join("./pkg", pkg), realtime = True, print_command = True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
action = "build"
|
||||
try:
|
||||
sys.platform
|
||||
action = sys.argv.pop(1)
|
||||
except Exception: # no argument was passed
|
||||
except IndexError: # no argument was passed
|
||||
pass
|
||||
if(action.startswith("-") == False):
|
||||
if action.startswith("-") is False:
|
||||
sys.argv.insert(1, action)
|
||||
if(action != "dependencies"):
|
||||
if action != "dependencies":
|
||||
set_vars()
|
||||
|
||||
valid_actions = ["build", "clean", "dependencies", "docker", "install", "js", "release", "sass", "test"]
|
||||
valid_actions = [
|
||||
"build", "clean", "dependencies", "docker", "install", "js", "release", "sass", "test"
|
||||
]
|
||||
parser = argparse.ArgumentParser(description = "gochan build script")
|
||||
parser.add_argument("action", nargs = 1, default = "build", choices = valid_actions)
|
||||
if action == "--help" or action == "-h":
|
||||
if action in ('--help', '-h'):
|
||||
parser.print_help()
|
||||
exit(2)
|
||||
sys.exit(2)
|
||||
|
||||
if action == "build":
|
||||
parser.add_argument("--debug", help = "build gochan and gochan-frontend with debugging symbols", action = "store_true")
|
||||
parser.add_argument(
|
||||
"--debug",
|
||||
help="build gochan and gochan-frontend with debugging symbols",
|
||||
action="store_true")
|
||||
args = parser.parse_args()
|
||||
build(args.debug)
|
||||
elif action == "clean":
|
||||
clean()
|
||||
exit(0)
|
||||
sys.exit(0)
|
||||
elif action == "dependencies":
|
||||
dependencies()
|
||||
elif action == "docker":
|
||||
parser.add_argument("--option",
|
||||
default = "guestdb", choices = ["guestdb", "hostdb", "macos"],
|
||||
help = "create a Docker container, see docker/README.md for more info"
|
||||
)
|
||||
parser.add_argument("--attached",
|
||||
action = "store_true",
|
||||
help = "keep the command line attached to the container (by default it runs detached)"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--option",
|
||||
default="guestdb",
|
||||
choices=["guestdb", "hostdb", "macos"],
|
||||
help="create a Docker container, see docker/README.md for more info")
|
||||
parser.add_argument(
|
||||
"--attached",
|
||||
action="store_true",
|
||||
help="keep the command line attached to the container while it runs")
|
||||
args = parser.parse_args()
|
||||
try:
|
||||
docker(args.option, args.attached)
|
||||
|
@ -331,34 +353,42 @@ if __name__ == "__main__":
|
|||
print("Received keyboard interrupt, exiting")
|
||||
elif action == "install":
|
||||
parser.add_argument("--js",
|
||||
action = "store_true",
|
||||
help = "only install JavaScript (useful for frontend development)",
|
||||
)
|
||||
parser.add_argument("--css",
|
||||
action = "store_true",
|
||||
help = "only install CSS"
|
||||
)
|
||||
parser.add_argument("--templates",
|
||||
action = "store_true",
|
||||
help = "install the template files"
|
||||
)
|
||||
parser.add_argument("--prefix",
|
||||
default = "/usr",
|
||||
help = "install gochan to this directory and its subdirectories",
|
||||
)
|
||||
parser.add_argument("--documentroot",
|
||||
default = "/srv/gochan",
|
||||
help = "install files in ./html/ to this directory to be requested by a browser"
|
||||
)
|
||||
action="store_true",
|
||||
help="only install JavaScript (useful for frontend development)")
|
||||
parser.add_argument(
|
||||
"--css",
|
||||
action="store_true",
|
||||
help="only install CSS")
|
||||
parser.add_argument(
|
||||
"--templates",
|
||||
action="store_true",
|
||||
help="install the template files")
|
||||
parser.add_argument(
|
||||
"--prefix",
|
||||
default="/usr",
|
||||
help="install gochan to this directory and its subdirectories")
|
||||
parser.add_argument(
|
||||
"--documentroot",
|
||||
default="/srv/gochan",
|
||||
help="install files in ./html/ to this directory to be requested by a browser")
|
||||
args = parser.parse_args()
|
||||
install(args.prefix, args.documentroot, args.js, args.css, args.templates)
|
||||
elif action == "js":
|
||||
parser.add_argument("--nominify", action = "store_true", help = "Don't minify gochan.js")
|
||||
parser.add_argument("--watch", action = "store_true", help = "automatically rebuild when you change a file (keeps running)")
|
||||
parser.add_argument(
|
||||
"--nominify",
|
||||
action="store_true",
|
||||
help="Don't minify gochan.js")
|
||||
parser.add_argument(
|
||||
"--watch",
|
||||
action="store_true",
|
||||
help="automatically rebuild when you change a file (keeps running)")
|
||||
args = parser.parse_args()
|
||||
js(args.nominify, args.watch)
|
||||
elif action == "release":
|
||||
parser.add_argument("--all", help = "build releases for Windows, macOS, and Linux", action = "store_true")
|
||||
parser.add_argument(
|
||||
"--all",
|
||||
help="build releases for Windows, macOS, and Linux",
|
||||
action="store_true")
|
||||
args = parser.parse_args()
|
||||
fs_action("mkdir", "releases")
|
||||
if args.all:
|
||||
|
@ -368,7 +398,7 @@ if __name__ == "__main__":
|
|||
else:
|
||||
release(gcos)
|
||||
elif action == "sass":
|
||||
parser.add_argument("--minify", action = "store_true")
|
||||
parser.add_argument("--minify", action="store_true")
|
||||
args = parser.parse_args()
|
||||
sass(args.minify)
|
||||
elif action == "test":
|
||||
|
|
|
@ -1,41 +1,48 @@
|
|||
from os import path
|
||||
#
|
||||
# Use a macro like this {exact macro name}
|
||||
#
|
||||
|
||||
|
||||
class macro():
|
||||
""" Use a macro like this {exact macro name} """
|
||||
def __init__(self, macroname, postgres, mysql):
|
||||
self.macroname = macroname
|
||||
self.postgres = postgres
|
||||
self.mysql = mysql
|
||||
|
||||
|
||||
|
||||
# macros
|
||||
macros = [
|
||||
macro("serial pk", "BIGSERIAL PRIMARY KEY", "BIGINT NOT NULL AUTO_INCREMENT UNIQUE PRIMARY KEY"),
|
||||
macro("serial pk","BIGSERIAL PRIMARY KEY",
|
||||
"BIGINT NOT NULL AUTO_INCREMENT UNIQUE PRIMARY KEY"),
|
||||
macro("fk to serial", "BIGINT", "BIGINT"),
|
||||
macro("drop fk", "DROP CONSTRAINT", "DROP FOREIGN KEY")
|
||||
]
|
||||
]
|
||||
|
||||
|
||||
def compileOutIfs(text, flag):
|
||||
lines = text.splitlines()
|
||||
newText = ""
|
||||
compile = True
|
||||
doCompile = True
|
||||
for i in lines:
|
||||
if "#IF" in i:
|
||||
if flag in i:
|
||||
compile = True
|
||||
else:
|
||||
compile = False
|
||||
doCompile = True if flag in i else False
|
||||
elif "#ENDIF" in i:
|
||||
compile = True
|
||||
elif compile:
|
||||
doCompile = True
|
||||
elif doCompile:
|
||||
newText += i + "\n"
|
||||
return newText
|
||||
|
||||
|
||||
def hasError(text):
|
||||
if '{' in text or '}' in text:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def dofile(filestart):
|
||||
print("building " + filestart + " sql file")
|
||||
masterfileIn = open(filestart + "master.sql", 'r')
|
||||
masterfile = masterfileIn.read()
|
||||
masterfileIn.close()
|
||||
masterfile = ""
|
||||
with open(filestart + "master.sql", 'r') as masterfileIn:
|
||||
masterfile = masterfileIn.read()
|
||||
|
||||
postgresProcessed = compileOutIfs(masterfile, "POSTGRES")
|
||||
mysqlProcessed = compileOutIfs(masterfile, "MYSQL")
|
||||
|
@ -44,25 +51,23 @@ def dofile(filestart):
|
|||
macroCode = "{" + item.macroname + "}"
|
||||
postgresProcessed = postgresProcessed.replace(macroCode, item.postgres)
|
||||
mysqlProcessed = mysqlProcessed.replace(macroCode, item.mysql)
|
||||
|
||||
def hasError(text):
|
||||
if '{' in text or '}' in text:
|
||||
return True
|
||||
|
||||
|
||||
error = hasError(postgresProcessed)
|
||||
error = error or hasError(mysqlProcessed)
|
||||
|
||||
i = open(filestart + "postgres.sql", 'w')
|
||||
i.write(postgresProcessed)
|
||||
i.close()
|
||||
with open(filestart + "postgres.sql", 'w') as i:
|
||||
i.write(postgresProcessed)
|
||||
|
||||
with open(filestart + "mysql.sql", 'w') as i:
|
||||
i.write(mysqlProcessed)
|
||||
|
||||
i = open(filestart + "mysql.sql", 'w')
|
||||
i.write(mysqlProcessed)
|
||||
i.close()
|
||||
|
||||
if error:
|
||||
input("Error processing macros, files still contain curly braces (might be in comments?), press any key to continue")
|
||||
|
||||
dofile(path.join("..", "initdb_"))
|
||||
dofile(path.join("..", "sql", "preapril2020migration", "initdb_"))
|
||||
dofile(path.join("..", "sql", "preapril2020migration", "oldDBMigration_"))
|
||||
input(
|
||||
"Error processing macros, files still contain curly braces (might be in comments?)\n",
|
||||
"press any key to continue")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
dofile(path.join("..", "initdb_"))
|
||||
dofile(path.join("..", "sql", "preapril2020migration", "initdb_"))
|
||||
dofile(path.join("..", "sql", "preapril2020migration", "oldDBMigration_"))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue