Added debug, reduced imports
parent
bead2da226
commit
e105a06534
|
@ -10,16 +10,17 @@ __version__ = "1.0"
|
|||
__copyright__ = "Copyright © 2022 Nicholas Hope. See LICENSE for details."
|
||||
|
||||
# Standard libraries
|
||||
import sys
|
||||
import os
|
||||
from sys import stderr
|
||||
from os import mkdir
|
||||
from os.path import isdir, expanduser, normpath
|
||||
import argparse
|
||||
import warnings
|
||||
from warnings import warn
|
||||
from tempfile import NamedTemporaryFile
|
||||
import yaml
|
||||
from platform import system as GetOSName, release as GetOSRelease
|
||||
|
||||
try:
|
||||
import winreg
|
||||
from winreg import HKEY_LOCAL_MACHINE, ConnectRegistry, OpenKey, EnumValue
|
||||
except ModuleNotFoundError:
|
||||
# Not running on windows
|
||||
pass
|
||||
|
@ -40,7 +41,7 @@ def parseFile(inputFile) -> (dict, dict):
|
|||
config, aliases = verify.verifyConfig(config)
|
||||
if "errors" in config:
|
||||
for e in config["errors"]:
|
||||
print(e, file=sys.stderr)
|
||||
print(e, file=stderr)
|
||||
return None, None
|
||||
else:
|
||||
return config, aliases
|
||||
|
@ -50,10 +51,10 @@ def parseConfig(config, defaults):
|
|||
global args
|
||||
global targetDir
|
||||
|
||||
if os.path.isdir(targetDir) == False:
|
||||
os.mkdir(targetDir)
|
||||
if isdir(targetDir) == False:
|
||||
mkdir(targetDir)
|
||||
if args.debug:
|
||||
print( f"DEBUG: Created directory {targetDir}", file=sys.stderr)
|
||||
print( f"DEBUG: Created directory {targetDir}", file=stderr)
|
||||
|
||||
tempsAndReals = {}
|
||||
|
||||
|
@ -91,21 +92,21 @@ def parseCLI():
|
|||
def getTargetDir(systemName):
|
||||
if systemName == "Darwin":
|
||||
if float( '.'.join( GetOSRelease().split('.')[0:2] ) ) >= 10.15:
|
||||
warnings.warn(
|
||||
warn(
|
||||
"As of macOS Catalina (v10.15), 32-bit applications "
|
||||
"like TF2 do not run. tfscript will run, but you can't run TF2 "
|
||||
"on this system",
|
||||
category=RuntimeWarning )
|
||||
return os.path.expanduser("~/Library/Application Support/Steam/")
|
||||
return expanduser("~/Library/Application Support/Steam")
|
||||
|
||||
elif systemName == "Windows":
|
||||
# oh god why do we have to use the registry
|
||||
accessReg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
|
||||
accessKey = winreg.OpenKey(accessReg, "SOFTWARE\\WOW6432Node\\Valve\\Steam")
|
||||
accessReg = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
|
||||
accessKey = OpenKey(accessReg, "SOFTWARE\\WOW6432Node\\Valve\\Steam")
|
||||
keyNum = 0
|
||||
while True:
|
||||
try:
|
||||
accessSubkeyName, data, _ = winreg.EnumValue(accessKey, keyNum)
|
||||
accessSubkeyName, data, _ = EnumValue(accessKey, keyNum)
|
||||
if accessSubkeyName == "InstallPath":
|
||||
return data
|
||||
except EnvironmentError:
|
||||
|
@ -114,10 +115,10 @@ def getTargetDir(systemName):
|
|||
return None
|
||||
|
||||
elif systemName == "Linux":
|
||||
return os.path.expanduser("~/.local/Steam")
|
||||
return expanduser("~/.local/Steam")
|
||||
|
||||
elif systemName == "Java":
|
||||
warnings.warn("Java-based OSes are not supported yet by tfscript.", category=RuntimeWarning)
|
||||
warn("Java-based OSes are not supported yet by tfscript.", category=RuntimeWarning)
|
||||
|
||||
return None
|
||||
|
||||
|
@ -135,13 +136,11 @@ def main() -> int:
|
|||
targetDir = getTargetDir(systemName)
|
||||
if targetDir is not None:
|
||||
# Supported OS: add steamapps path
|
||||
if targetDir[-1] != '/':
|
||||
targetDir += '/'
|
||||
targetDir += "steamapps/common/Team Fortress 2/tf/cfg"
|
||||
targetDir += normpath("/steamapps/common/Team Fortress 2/tf/cfg/")
|
||||
elif args.force:
|
||||
# Unsupported OS but -f specified
|
||||
if args.debug:
|
||||
print("DEBUG: forced to continue, output set to current directory", file=sys.stderr)
|
||||
print("DEBUG: forced to continue, output set to current directory", file=stderr)
|
||||
targetDir = '.'
|
||||
else:
|
||||
# Unsupported OS and not forced to continue
|
||||
|
@ -153,7 +152,7 @@ def main() -> int:
|
|||
|
||||
fileNames = parseConfig(config, defaults)
|
||||
fileList = writing.replaceFiles(targetDir, fileNames, args)
|
||||
writing.appendToActuals(targetDir, fileList)
|
||||
writing.appendToActuals(targetDir, fileList, args)
|
||||
|
||||
return 0
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import os
|
||||
import sys
|
||||
from sys import stderr
|
||||
from os.path import exists
|
||||
from tempfile import NamedTemporaryFile
|
||||
from shutil import move as moveRobust
|
||||
|
@ -21,7 +20,7 @@ def writeOutput(data, className, args) -> dict:
|
|||
maxFileSize = 2 ** 20 # 1MiB maximum cfg file size
|
||||
filesNeeded = 1 + int( len(data)/maxFileSize )
|
||||
if args.debug:
|
||||
print( f'DEBUG: need {filesNeeded} files for {className}', file=sys.stderr)
|
||||
print( f'DEBUG: need {filesNeeded} files for {className}', file=stderr)
|
||||
|
||||
FilNedLen = len(str(filesNeeded))
|
||||
# extra 4 bytes is leeway
|
||||
|
@ -42,7 +41,7 @@ def writeOutput(data, className, args) -> dict:
|
|||
outfile.write( ('exec %s_script_%0*d' % (className, FilNedLen, fileNum+1)).encode('utf8') )
|
||||
bytesWritten += reservedSpace
|
||||
if args.debug:
|
||||
print( f'DEBUG: Wrote {bytesWritten} bytes to {className} ({fileNum}/{filesNeeded})', file=sys.stderr)
|
||||
print( f'DEBUG: Wrote {bytesWritten} bytes to {className} ({fileNum}/{filesNeeded})', file=stderr)
|
||||
|
||||
outfile.close()
|
||||
outfile = NamedTemporaryFile(prefix=className, delete=False)
|
||||
|
@ -56,7 +55,7 @@ def writeOutput(data, className, args) -> dict:
|
|||
|
||||
outfile.close() # the most-recent tempfile will not have been closed
|
||||
if args.debug:
|
||||
print( f'DEBUG: Wrote {bytesWritten} bytes to {className} ({fileNum}/{filesNeeded})', file=sys.stderr)
|
||||
print( f'DEBUG: Wrote {bytesWritten} bytes to {className} ({fileNum}/{filesNeeded})', file=stderr)
|
||||
|
||||
return namesDict
|
||||
|
||||
|
@ -64,30 +63,31 @@ def replaceFiles(targetDir, fileNames, args):
|
|||
for tmpName, realName in fileNames.items():
|
||||
if args.dry_run:
|
||||
if args.debug:
|
||||
print( f'DEBUG: {tmpName} would be {targetDir}/{realName}.cfg', file=sys.stderr)
|
||||
print( f'DEBUG: {tmpName} would be {targetDir}{realName}.cfg', file=stderr)
|
||||
else:
|
||||
# using shutil.move() because it can move files across disk drives on windows
|
||||
moveRobust( tmpName, f'{targetDir}/{realName}' )
|
||||
moveRobust( tmpName, f'{targetDir}{realName}' )
|
||||
if args.debug:
|
||||
print( f'DEBUG: Created {targetDir}/{realName}', file=sys.stderr)
|
||||
print( f'DEBUG: Created {targetDir}{realName}', file=stderr)
|
||||
|
||||
return list(fileNames.values())
|
||||
|
||||
def appendToActuals(targetDir, fileList):
|
||||
def appendToActuals(targetDir, fileList, args):
|
||||
fileList = onlyFirsts(fileList)
|
||||
for currFile in fileList:
|
||||
execStr = f'exec {currFile.split(".")[0]}'
|
||||
realFilePath = targetDir + '/' + getRealName(currFile)
|
||||
realFilePath = targetDir + getRealName(currFile)
|
||||
|
||||
realExists = exists(realFilePath)
|
||||
|
||||
# creates if it doesn't exist, so must come after the exists() call
|
||||
cfgFile = open(realFilePath, 'a+')
|
||||
if not realExists:
|
||||
if args.debug:
|
||||
print( f"DEBUG: Created file {targetDir}{realFilePath}" )
|
||||
cfgFile.write(execStr)
|
||||
|
||||
if (realExists == False or not strInFile(execStr, cfgFile)):
|
||||
# since the file was created if it didn't exist,
|
||||
# and it was opened in append mode, this will
|
||||
# always place it at the end
|
||||
elif not strInFile(execStr, cfgFile):
|
||||
cfgFile.write('\n' + execStr)
|
||||
|
||||
cfgFile.close()
|
||||
|
|
Loading…
Reference in New Issue