Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Python scripting #192

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 0 additions & 51 deletions config.cmd

This file was deleted.

75 changes: 75 additions & 0 deletions config.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
Write-Output "Configuring started..."
$root_dir = Split-Path $MyInvocation.MyCommand.Path

# This makes M$ Invoke-WebRequest and Expand-Archive work 4 times faster o.O
$global:ProgressPreference = "SilentlyContinue"

$packages_dir = "$env:APPDATA\ko4life"
if (-not (Get-Command git -ErrorAction Ignore)) {
if (-not (Test-Path "$packages_dir\git")) {
Write-Output "INFO: Git not found. Installing portable version at: $packages_dir"
$tmp_dir = (New-TemporaryFile | ForEach-Object{ Remove-Item $_; mkdir $_ }).FullName
Invoke-WebRequest `
"https://github.com/git-for-windows/git/releases/download/v2.39.1.windows.1/MinGit-2.39.1-busybox-32-bit.zip" `
-OutFile "$tmp_dir\PortableGit.zip"
Remove-Item "$packages_dir\git" -Recurse -Force -ErrorAction Ignore
Expand-Archive "$tmp_dir\PortableGit.zip" -DestinationPath "$packages_dir\git" -Force
$env:Path = "$packages_dir\git\cmd;$env:Path"
} else {
$env:Path = "$packages_dir\git\cmd;$env:Path"
}
if (-not (Get-Command git -ErrorAction Ignore)) {
Write-Output "ERROR: Failed to install portable git..."
pause
exit(1)
}
}

if (-not (Get-Command python, pip, virtualenv -ErrorAction Ignore)) {
if (-not (Test-Path "$packages_dir\python")) {
Write-Output "INFO: Python not found. Installng portable version at: $packages_dir"
$tmp_dir = (New-TemporaryFile | ForEach-Object{ Remove-Item $_; mkdir $_ }).FullName
Invoke-WebRequest `
"https://www.python.org/ftp/python/3.11.1/python-3.11.1-embed-win32.zip" `
-OutFile "$tmp_dir\PortablePython.zip"
Invoke-WebRequest `
"https://bootstrap.pypa.io/get-pip.py" `
-OutFile "$tmp_dir\get-pip.py"

Remove-Item "$packages_dir\python" -Recurse -Force -ErrorAction Ignore
Expand-Archive "$tmp_dir\PortablePython.zip" -DestinationPath "$packages_dir\python" -Force
Remove-Item "$packages_dir\python\python3*._pth" -Force -ErrorAction Ignore
$env:Path = "$packages_dir\python;$packages_dir\python\Scripts;$env:Path"
python "$tmp_dir\get-pip.py"
pip install virtualenv
} else {
$env:Path = "$packages_dir\python;$packages_dir\python\Scripts;$env:Path"
}
if (-not (Get-Command python, pip, virtualenv -ErrorAction Ignore)) {
Write-Output "ERROR: Failed to install portable python..."
pause
exit(1)
}
}

Remove-Item "$root_dir\script\venv" -Recurse -Force -ErrorAction Ignore
virtualenv "$root_dir\script\venv"
& "$root_dir\script\venv\Scripts\activate.ps1"
pip install -r "$root_dir\script\requirements.txt"
pip install "$root_dir\script"

# TODO: Modular this in python code
# As well as the export.ps1 and import.ps1 scripts for db, so that it can be managed in one place
Remove-Item "$root_dir\src\assets" -Recurse -Force -ErrorAction Ignore
Remove-Item "$root_dir\src\vendor" -Recurse -Force -ErrorAction Ignore
Remove-Item "$root_dir\src\db" -Recurse -Force -ErrorAction Ignore

git clone --depth=1 https://github.com/ko4life-net/ko-assets "$root_dir\src\assets"
git clone --depth=1 https://github.com/ko4life-net/ko-vendor "$root_dir\src\vendor"
git clone https://github.com/ko4life-net/ko-db "$root_dir\src\db"

Write-Output "`n`nConfiguring successfully done.`n"
Write-Output "You may want to to start the src\All.sln Visual Studio solution to start developing."
Write-Output "`n`nThank you and happy coding!`n"

pause
47 changes: 47 additions & 0 deletions odbcad.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
$server = "localhost"
$database = "kodb"

$drivers = Get-OdbcDriver | Where-Object { $_.Name -like "*SQL Server*" -and $_.Platform -eq "32-bit" }
if (!$drivers) {
Write-Output "Are you sure SQL Server is installed? I couldn't find any drivers."
exit(1)
}

$selected_driver = $null
if ($drivers.Count -eq 1) {
$selected_driver = $drivers[0]
} else {
while (!$selected_driver) {
Write-Output "Select the desired SQL Driver:"
for ($i = 0; $i -lt $drivers.Count; $i++) {
Write-Output "$($i+1). $($drivers[$i].Name)"
}

$user_input = -1
$input_valid = [int]::TryParse((Read-Host "Enter the number of the driver you want to select"), [ref]$user_input)
if (-not $input_valid -or $user_input -lt 1 -or $user_input -gt $drivers.Count) {
Write-Output "Invalid selection.`n"
} else {
$selected_driver = $drivers[$user_input - 1]
Write-Output "Selected SQL Driver: $($selected_driver.Name)`n"
break
}
}
}

Remove-OdbcDsn -Name $database -DsnType "User" -ErrorAction Ignore
Add-OdbcDsn -Name $database -DriverName $selected_driver.Name -DsnType "User" -SetPropertyValue @("Server=$server", "Database=$database", "AutoTranslate=No", "Trusted_Connection=Yes")

# Test connection
$con = New-Object System.Data.Odbc.OdbcConnection
$con.ConnectionString = "DSN=$database"
$con.Open()
$is_successful = $con.State -eq "Open"
$con.Close()
if ($is_successful) {
Write-Output "Successfully created odbcad connection driver and tested connection!"
} else {
Write-Output "Failed to test connection. Check that you first imported the database."
Write-Output "If that didn't work, depending on how you installed MSSQL (Default or Named Instance), you may need to change the server above from localhost to yours."
exit(1)
}
5 changes: 5 additions & 0 deletions script/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
venv/
build/
ko.egg-info/
__pycache__/
*.pyc
5 changes: 5 additions & 0 deletions script/.pep8
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[pycodestyle]
indent-size = 2
recursive = true
aggressive = 0
max_line_length = 140
35 changes: 35 additions & 0 deletions script/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Knight Online Scripting

Scripting project for Knight Online for the purpose of writing simple and portable scripts to help managing this project better.

Note that you do not have to install stuff by yourself (unless you prefer), because the config.ps1 in the root dir of the project will setup a portable python installation for you.

Some useful command below:
```sh
# assuming you're inside the script dir, even though not needed
cd script

# Install requirements
virtualenv venv
& venv/Scripts/activate.ps1
pip install -r requirements.txt
pip install .

# Test that all works
ko --help

# or
python ko/main.py --help

# add `--editable` if you want to run the shorter `ko` command whilst editing the scripts:
pip install --editable .

# Run some useful command
ko --help
ko build --config debug --project game --project server
ko db --action import
ko format

# format your code before submitting a PR
autopep8 -i . --exclude venv
```
3 changes: 3 additions & 0 deletions script/ko/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

def util_entry(**args):
print(f"Using clang-format to format code in the following directory: {args['dir']}")
4 changes: 4 additions & 0 deletions script/ko/build/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .project import build_project

def build_entry(**args):
build_project(**args)
4 changes: 4 additions & 0 deletions script/ko/build/project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

def build_project(**args):
print(f"Building {args['config']} projects (cores: {args['cores']}): {list(args['project'])}")
print(args)
3 changes: 3 additions & 0 deletions script/ko/db/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

def db_entry(**args):
print(f"Database action: {args['action']}")
Empty file added script/ko/db/export.py
Empty file.
Empty file added script/ko/db/import.py
Empty file.
Empty file added script/ko/db/odbcad.py
Empty file.
41 changes: 41 additions & 0 deletions script/ko/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os
import click

from ko.build import build_entry
from ko.db import db_entry
from ko.util import util_entry


class CaseInsensitiveChoice(click.Choice):
def convert(self, value, param, ctx):
return super().convert(value.lower(), param, ctx)


@click.group()
def cli():
pass


@cli.command()
@click.option('--config', '-c', type=CaseInsensitiveChoice(['release', 'debug']), default='release', help='Build configuration mode')
@click.option('--project', '-p', type=CaseInsensitiveChoice(['all', 'game', 'server', 'tool']),
default=['all'], multiple=True, help='Multiple projects to build')
@click.option('--cores', '-j', type=click.INT, default=os.cpu_count(), help='CPU cores count for building')
def build(**args):
build_entry(**args)


@cli.command()
@click.option('--action', '-a', type=CaseInsensitiveChoice(['import', 'export']), required=True, help='Import or export database')
def db(**args):
db_entry(**args)


@cli.command()
@click.option('--dir', '-d', type=click.STRING, help='Path to a directory to apply code formatting')
def format(**args):
util_entry(**args)


if __name__ == '__main__':
cli()
3 changes: 3 additions & 0 deletions script/ko/util/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

def util_entry(**args):
print(f"Using clang-format to format code in the following directory: {args['dir']}")
Empty file added script/ko/util/format.py
Empty file.
5 changes: 5 additions & 0 deletions script/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
autopep8
click
mssql-scripter
pipreqs
pyodbc
14 changes: 14 additions & 0 deletions script/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from setuptools import setup, find_packages

setup(
name='ko',
version='1.0.0',
packages=find_packages(),
include_package_data=True,
install_requires=['click'],
entry_points={
'console_scripts': [
'ko = ko.main:cli',
],
},
)