diff --git a/.scripts/UpdateMoose.py b/.scripts/UpdateMoose.py index 8d609cf..9e9f173 100644 --- a/.scripts/UpdateMoose.py +++ b/.scripts/UpdateMoose.py @@ -14,6 +14,8 @@ from zipfile import ZipFile, ZIP_DEFLATED from shutil import rmtree, copy import argparse import filecmp +import urllib.request +import os def findMoose(path: Path): # Loop over all lua files (recursively) @@ -24,12 +26,13 @@ def findMoose(path: Path): def copyScripts(path: Path, topath): # Loop over all lua files (recursively) + print('Copy all lua files to parent folder') for f in path.rglob("*.lua"): if not (f.name.lower().startswith("moose")): print(f"Found script: {f}") copy(f, topath) -def update(f: Path, MooseLua: Path, Temp: Path): +def update(f: Path, MooseLuaDev: Path, MooseLuaMaster: Path, Temp: Path, UpdateMoose: bool): """ Update the Moose.lua file in given file. """ @@ -39,6 +42,7 @@ def update(f: Path, MooseLua: Path, Temp: Path): # Extract all the contents of zip file in different directory with ZipFile(f, mode='r') as miz: miz.extractall(Temp) + print('MIZ extracted to {}'.format(Temp)) # Folder where script is located ScriptDir=Temp/"l10n/DEFAULT/" @@ -48,43 +52,39 @@ def update(f: Path, MooseLua: Path, Temp: Path): print(f"WARNING: {ScriptDir.name} does not exit!") return - # Find old Moose file in Scrit directory. - MooseOld=findMoose(ScriptDir) - if not MooseOld.is_file(): - print("WARNING: Could not find any file that starts with Moose!") - return - - # Copy all script files (all files that do NOT start with moose and end with lua) copyScripts(ScriptDir, f.parent) - # Script file. - #ScriptFile=ScriptDir/Path(f.stem + ".lua") + if UpdateMoose: + # Find old Moose file in Script directory. + MooseOld=findMoose(ScriptDir) + if not MooseOld.is_file(): + print("WARNING: Could not find any file that starts with Moose!") + return - #Copy script file to directory. - #if ScriptFile.is_file(): - # print(f"Copying script file {ScriptFile} to {f.parent}") - # copy(ScriptFile, f.parent) - #else: - # print(f"Warning: expected script file {ScriptFile} does NOT exist in miz file!") + if 'master' in os.path.basename(f): + print('This mission needs Moose_.lua from master branch') + MooseLua = MooseLuaMaster + else: + MooseLua = MooseLuaDev - # Check if Moose.lua file is already. - if filecmp.cmp(MooseLua, MooseOld): - print(f"INFO: {MooseOld.name} file is up-to-date ==> Nothing to do!") - else: + # Check if Moose.lua file is up to date. + if filecmp.cmp(MooseLua, MooseOld): + print(f"INFO: {MooseOld.name} file is up-to-date ==> Nothing to do!") + else: + # Info. + print(f"INFO: Updating {MooseOld.name} with current version") - # Info. - print(f"INFO: Updating {MooseOld.name} with current version") + # Copy Moose.lua to temp dir. + copy(MooseLua, MooseOld) - # Copy Moose.lua to temp dir. - copy(MooseLua, MooseOld) - - # Create new miz file - with ZipFile(f, mode='w', compression=ZIP_DEFLATED, allowZip64=False, compresslevel=9) as archive: - for file_path in Temp.rglob("*"): - archive.write(file_path, arcname=file_path.relative_to(Temp)) + # Create new miz file + with ZipFile(f, mode='w', compression=ZIP_DEFLATED, allowZip64=False, compresslevel=9) as archive: + for file_path in Temp.rglob("*"): + archive.write(file_path, arcname=file_path.relative_to(Temp)) # Remove temp dir. + print( 'Removing temp folder' ) try: rmtree(Temp) except: @@ -109,7 +109,7 @@ if __name__ == '__main__': parser.add_argument('--MoosePath', metavar='moose', type=str, help='path to Moose.lua file', default="./") # Add argument for Moose path. - parser.add_argument('--MissionPath', metavar='missions', type=str, help='path to missions', default="./") + parser.add_argument('--MissionPath', metavar='missions', type=str, help='path to missions', default="../") # parser.add_argument('--UpdateMoose', action='store_true') @@ -120,25 +120,39 @@ if __name__ == '__main__': # Path to Moose.lua Moose=Path(args.MoosePath) + print("MoosePath=" + args.MoosePath) + print("MissionPath"+ args.MissionPath) + if args.UpdateMoose: + print("UpdateMoose is given. Moose_.lua files will be updated.") + + branch='develop' + website = 'https://raw.githubusercontent.com/FlightControl-Master/MOOSE_INCLUDE' + url = f'{website}/{branch}/Moose_Include_Static/Moose_.lua' + # Moose.lua file MooseLua=Moose/"Moose_.lua" - if args.UpdateMoose: - print("Will update all Moose_.lua files") - - print(args.MoosePath) - print(args.MissionPath) + if not MooseLua.exists(): + urllib.request.urlretrieve( url, MooseLua) # Check that Moose.lua exists if MooseLua.exists(): print("Moose_.lua exists") with open(MooseLua) as myfile: head = [next(myfile) for x in range(1)] - print(head) + print( '\nHeader:\n{}\n'.format(head) ) else: print(f"{MooseLua.name} does not exist") quit() + # Download Moose from master branch + branch='master' + url = f'{website}/{branch}/Moose_Include_Static/Moose_.lua' + # Moose.lua file + MooseLuaMaster=Moose/"Moose_master.lua" + if not MooseLuaMaster.exists(): + urllib.request.urlretrieve( url, MooseLuaMaster) + # Path to search for mission (miz) files Missions=Path(args.MissionPath) @@ -150,6 +164,6 @@ if __name__ == '__main__': rmtree(Temp) # Loop over all miz files (recursively) - print("\nMiz files:\n----------") + print("\nSearch and process MIZ files:\n----------") for f in Missions.rglob("*.miz"): - update(f, MooseLua, Temp) + update(f, MooseLua, MooseLuaMaster, Temp, args.UpdateMoose) diff --git a/.scripts/download.py b/.scripts/download.py deleted file mode 100644 index 5f9f360..0000000 --- a/.scripts/download.py +++ /dev/null @@ -1,4 +0,0 @@ -import urllib.request - -urllib.request.urlretrieve('https://raw.githubusercontent.com/FlightControl-Master/MOOSE_INCLUDE/develop/Moose_Include_Static/Moose_.lua', '../.build/Moose_develop.lua') -urllib.request.urlretrieve('https://raw.githubusercontent.com/FlightControl-Master/MOOSE_INCLUDE/master/Moose_Include_Static/Moose_.lua', '../.build/Moose_master.lua') diff --git a/.scripts/run.sh b/.scripts/run.sh index cc2da48..134bbd7 100644 --- a/.scripts/run.sh +++ b/.scripts/run.sh @@ -1,3 +1,3 @@ #!/bin/bash cd "$(dirname "$0")" -python ./UpdateMoose.py --MoosePath ../../MOOSE_INCLUDE/Moose_Include_Static/ --MissionPath ../ --UpdateMoose +python ./UpdateMoose.py --UpdateMoose