This repository has been archived by the owner on May 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommands.py
220 lines (172 loc) · 6.33 KB
/
commands.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import argparse
from .system import *
from .arguments import parser
COMMAND_FILENAME = '.cyther'
class SimpleCommand:
def __init__(self):
self.runtime_names = []
self.include_names = []
self.cython_name = None
self.python_name = None
self.c_name = None
self.o_name = None
self.dll_name = None
def getCythonFileName(self):
return self.cython_name
def setCythonFileName(self, obj):
self.cython_name = obj
def getPythonFileName(self):
return self.python_name
def setPythonFileName(self, obj):
self.python_name = obj
def getCName(self):
return self.c_name
def setCName(self, obj):
self.c_name = obj
def getOName(self):
return self.o_name
def setOName(self, obj):
self.o_name = obj
def getDLLName(self):
return self.dll_name
def setDLLName(self, obj):
self.dll_name = obj
def getRuntimeNames(self):
return self.runtime_names
def setRuntimeNames(self, obj):
self.runtime_names = obj
def getIncludeNames(self):
return self.include_names
def setIncludeNames(self, obj):
self.include_names = obj
def furtherArgsProcessing(args):
"""
Converts args, and deals with incongruities that argparse couldn't handle
"""
if isinstance(args, str):
unprocessed = args.strip().split(' ')
if unprocessed[0] == 'cyther':
del unprocessed[0]
args = parser.parse_args(unprocessed).__dict__
elif isinstance(args, argparse.Namespace):
args = args.__dict__
elif isinstance(args, dict):
pass
else:
raise CytherError(
"Args must be a instance of str or argparse.Namespace, not '{}'".format(
str(type(args))))
if args['watch']:
args['timestamp'] = True
args['watch_stats'] = {'counter': 0, 'errors': 0, 'compiles': 0,
'polls': 0}
args['print_args'] = True
return args
def processFiles(args):
"""
Generates and error checks each file's information before the compilation actually starts
"""
to_process = []
for filename in args['filenames']:
file = dict()
if args['include']:
file['include'] = INCLUDE_STRING + ''.join(
['-I' + item for item in args['include']])
else:
file['include'] = INCLUDE_STRING
file['file_path'] = getPath(filename)
file['file_base_name'] = \
os.path.splitext(os.path.basename(file['file_path']))[0]
file['no_extension'], file['extension'] = os.path.splitext(
file['file_path'])
if file['extension'] not in CYTHONIZABLE_FILE_EXTS:
raise CytherError(
"The file '{}' is not a designated cython file".format(
file['file_path']))
base_path = os.path.dirname(file['file_path'])
local_build = args['local']
if not local_build:
cache_name = os.path.join(base_path, '__cythercache__')
os.makedirs(cache_name, exist_ok=True)
file['c_name'] = os.path.join(cache_name,
file['file_base_name']) + '.c'
else:
file['c_name'] = file['no_extension'] + '.c'
file['object_file_name'] = os.path.splitext(file['c_name'])[0] + '.o'
output_name = args['output_name']
if args['watch']:
file['output_name'] = file['no_extension']+DEFAULT_OUTPUT_EXTENSION
elif output_name:
if os.path.exists(output_name) and os.path.isfile(output_name):
file['output_name'] = output_name
else:
dirname = os.path.dirname(output_name)
if not dirname:
dirname = os.getcwd()
if os.path.exists(dirname):
file['output_name'] = output_name
else:
raise CytherError('The directory specified to write'
'the output file in does not exist')
else:
file['output_name'] = file['no_extension']+DEFAULT_OUTPUT_EXTENSION
file['stamp_if_error'] = 0
to_process.append(file)
return to_process
"""
Each file gets read and information gets determined
Depencencies are determined
Dependencies are matched and a heirarchy is created
In order from lowest to highest, those commands get created
Put them into cytherize
"""
class Commands:
"""
Class to hold the data and methods for processing a manager of instructions
into commands to execute in order to do what should be accomplished
"""
def __init__(self):
self.__unprocessed = []
def toFile(self, filename=None):
if not filename:
filename = COMMAND_FILENAME
commands = self.generateCommands()
string = str()
for command in commands:
string += (' '.join(command) + '\n')
# TODO What is the best file permission?
with open(filename, 'w+') as file:
chars = file.write(string)
return chars
@staticmethod
def fromFile(filename=None):
if not filename:
filename = COMMAND_FILENAME
with open(filename) as file:
lines = file.readlines()
output = []
for line in lines:
output.append(line.split())
return output
def generateCommands(self):
"""
Generate a list of lists of commands from the internal manager object
"""
# 1) Sort the commands
# 2) Return the commands in the form of a list
# 3) This does what makeCommands does right now
pass
def makeCommands(file):
"""
Given a high level preset, it will construct the basic args to pass over.
'ninja', 'beast', 'minimal', 'swift'
"""
commands = [['cython', '-a', '-p', '-o',
file['c_name'], file['file_path']],
['gcc', '-DNDEBUG', '-g', '-fwrapv', '-O3', '-Wall', '-Wextra',
'-pthread', '-fPIC', '-c', file['include'], '-o',
file['object_file_name'], file['c_name']],
['gcc', '-g', '-Wall', '-Wextra', '-pthread', '-shared',
RUNTIME_STRING, '-o', file['output_name'],
file['object_file_name'], L_OPTION]]
return commands