forked from oosmos/oosmos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoosmos.py
executable file
·128 lines (98 loc) · 4.02 KB
/
oosmos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#
# oosmos.py - A set of common python functions.
#
import sys
import shutil
import subprocess
import os
import glob
from collections.abc import Callable
def Path(P: str) -> str:
return os.path.normpath(P)
def Join(Array: list[str]) -> str:
return ' '.join(Array)
def RemoveDir(dir: str) -> None:
if os.path.exists(dir):
shutil.rmtree(dir)
def WalkDir(Dir: str, pFunc: Callable[[str, str], str], UserArg: str) -> None:
for RootDir, DirList, FileList in os.walk(Dir):
for File in FileList:
FullFilePath = os.path.join(RootDir, File)
pFunc(os.path.normpath(FullFilePath), UserArg)
for Dir in DirList:
WalkDir(Dir, pFunc, UserArg)
def Clean(Dir: str, ExtensionsArg: str) -> None:
Extensions = ExtensionsArg.split()
for SubDir, Dirs, Files in os.walk(Dir):
if 'dist' in Dirs:
shutil.rmtree(SubDir+'/dist')
if 'build' in Dirs:
shutil.rmtree(SubDir+'/build')
if SubDir == 'dist' or SubDir == 'build':
print('remove '+SubDir)
continue
for File in Files:
for Extension in Extensions:
bExtension = Extension.encode('ascii')
bFile = File.encode('ascii')
if bFile.endswith((b'.'+bExtension,)):
os.remove(SubDir+'/'+File)
class cWindows:
@staticmethod
def Clean() -> None:
WildRemove('*.exp')
WildRemove('*.lib')
WildRemove('*.exe')
WildRemove('*.pdb')
WildRemove('*.obj')
WildRemove('*.pch')
WildRemove('*.ilk')
WildRemove('*.suo')
WildRemove('*.tds')
WildRemove('*.bak')
RemoveDir('x64')
@staticmethod
def Compile(oosmos_dir: str, FileArray: list[str], Options: str = '') -> None:
print('Compiling...')
Files = ' '.join(FileArray)
Line = r'cl -I. -I%s\Source -I%s\Classes -I%s\Classes\Tests -nologo -Zi -W4 -wd4204 -wd4065 -wd4100 -wd4127 -D_CRT_SECURE_NO_WARNINGS '%(oosmos_dir,oosmos_dir,oosmos_dir)+Files+' -Doosmos_DEBUG '+Options
#Line = r'CLang-cl -I. -I%s\Source -I%s\Classes -I%s\Classes\Tests -nologo -Zi -W4 -wd4204 -wd4065 -wd4100 -wd4127 -D_CRT_SECURE_NO_WARNINGS '%(oosmos_dir,oosmos_dir,oosmos_dir)+Files+' -Doosmos_DEBUG '+Options
#Line = r'CLang-tidy ' + Files + ' -checks=*,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,-readability-magic-numbers,-cppcoreguidelines-avoid-magic-numbers,-google-readability-avoid-underscore-in-googletest-name -- -I. -I%s\Source -I%s\Classes -I%s\Classes\Tests -c -D_CRT_SECURE_NO_WARNINGS '%(oosmos_dir,oosmos_dir,oosmos_dir)+' -Doosmos_DEBUG '+Options
# To enable asan, add /fsanitize=address /MT clang_rt.asan-i386.lib immediately after cl. Requires VS 2017.
print(Line)
try :
p = subprocess.Popen(Line, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd = os.getcwd())
except:
print("\n*** Unable to compile. Is Visual Studio installed?")
sys.exit(16)
if p.stdout is not None:
for Line in p.stdout:
Line = Line.rstrip()
if Line.startswith((b'reg', b'Generating Code...', b'Compiling...')):
continue
if Line.endswith((b'.c', b'.cpp')):
continue
print(Line.decode('utf-8'))
class cLinux:
@staticmethod
def Compile(oosmos_dir: str, Target: str, FileArray: list[str], Options: str = '') -> None:
Files = ' '.join(FileArray)
print(f'Compiling {Target}...')
classes_dir = os.path.normpath(oosmos_dir+'/Classes')
Line = "gcc -I%s/Source -I%s -I%s/Tests -I. -std=c99 -Wall -Wno-overflow -Wno-unused-parameter -pedantic -Werror -Wshadow -flto -o %s -D_POSIX_C_SOURCE=199309 -D__linux__ -Doosmos_DEBUG -Doosmos_ORTHO %s " % (oosmos_dir, classes_dir, classes_dir, Target, Files) + Options
os.system(Line)
def WildRemove(FilenamePattern: str) -> None:
FileList = glob.glob(FilenamePattern)
for FileName in FileList:
os.remove(FileName)
def MakeReadWrite(Filename: str) -> None:
os.chmod(Filename, 0o777)
def MakeReadOnly(Filename: str) -> None:
os.chmod(Filename, 0o444)
def CopyFileReadOnly(FromFile: str, ToFile: str) -> None:
if os.path.exists(ToFile):
MakeReadWrite(ToFile)
shutil.copyfile(FromFile, ToFile)
MakeReadOnly(ToFile)
if __name__ == '__main__':
print("'oosmos.py' is a module of reusable scripting elements and is not a standalone script.")