This repository was archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
225 lines (185 loc) · 6.22 KB
/
main.cpp
File metadata and controls
225 lines (185 loc) · 6.22 KB
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
221
222
223
224
225
#include <cstdio>
#include <getopt.h>
#include <dirent.h>
#include "pycompile.hpp"
#include "defines.h"
#include "parsed_arguments.h"
static constexpr const struct option long_opts[] = {
{ "input", required_argument, nullptr, 'i' },
{ "output", required_argument, nullptr, 'o' },
{ "verbose", no_argument, &verbose_flag, 'v' },
{ "version", no_argument, nullptr, 'V' },
{ "help", no_argument, nullptr, 'h' },
{ nullptr, no_argument, nullptr, 0 }
};
static constexpr const char* short_opts = "i:o:vhV";
std::deque<std::basic_string<char>> tmpins, tmpouts;
void version();
void help(int exit_code = 0);
void parse_args(int argc, char** argv);
void print_compile_table();
void compile_all_files();
int main(int argc, char** argv)
{
parse_args(argc, argv);
print_compile_table();
compile_all_files();
return 0;
}
auto system(const std::string& command)
{
return ::system(command.c_str());
}
void expand_directory_into_list(const std::string& input_prefix, const std::string& output_prefix, const std::string& directory = "")
{
DIR* dir;
struct dirent* entry;
if (!(dir = opendir((input_prefix + directory).c_str())))
return;
while ((entry = readdir(dir)))
{
auto path = directory + "/" + entry->d_name;
if (entry->d_type == DT_DIR)
{
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
::system("mkdir -p" + std::string(verbose_flag ? "v" : "") + " \"" + output_prefix + path + "\"");
expand_directory_into_list(input_prefix, output_prefix, path);
}
else
{
inputs.push_back(input_prefix + path);
outputs.push_back(output_prefix + path + ".py");
}
}
closedir(dir);
}
void compile_all_files()
{
tmpins = inputs;
tmpouts = outputs;
inputs = outputs = { };
struct stat sti, sto;
for (auto i = tmpins.begin(), o = tmpouts.begin(); i != tmpins.end() && o != tmpouts.end(); ++i, ++o)
{
sti = { };
sto = { };
int resi = ::stat(i->c_str(), &sti);
int reso = ::stat(o->c_str(), &sto);
if (resi < 0)
{
std::cerr << "Can't find filesystem entry \"" << *i << "\"\n";
::exit(-4);
}
if (S_ISDIR(sti.st_mode))
{
if (reso >= 0 && !S_ISDIR(sto.st_mode))
{
std::cerr << "\"" << *i << "\" is a directory but \"" << *o << "\" in not (and can't be).";
::exit(-3);
}
else if (reso < 0) ::system("mkdir -p" + std::string(verbose_flag ? "v" : "") + " \"" + *o + "\"");
expand_directory_into_list(*i, *o);
}
else if (S_ISREG(sti.st_mode))
{
if (S_ISREG(sto.st_mode) || reso < 0)
{
inputs.push_back(*i);
outputs.push_back(*o);
}
else
{
std::cerr << "\"" << *i << "\" is a regular file but \"" << *o << "\" in not (and can't be).";
::exit(-6);
}
}
else
{
std::cerr << "\"" << *i << "\" is not a file or directory\n";
::exit(-5);
}
}
for (auto i = inputs.begin(), o = outputs.begin(); i != inputs.end() && o != outputs.end(); ++i, ++o)
{
py::pycompile compile(*i, *o, std::cerr);
compile.compile();
}
}
void print_compile_table()
{
if (inputs.size() != outputs.size())
{
std::cerr << "An error occurred during arguments' parse: inputs.size() != outputs.size(): "
<< inputs.size() << " != " << outputs.size() << "\n"
"You must specify the same amount of inputs and outputs (check help)";
::exit(-2);
}
std::cout << "Compile table:\n";
for (auto i = inputs.begin(), o = outputs.begin(); i != inputs.end() && o != outputs.end(); ++i, ++o)
{
std::cout << "\t" << *i << " -> " << *o << "\n";
}
}
void version()
{
::printf(C_YELLOW "Version of " C_BOLD_MAGENTA APPLICATION_NAME C_RESET ": " C_BOLD_BLUE APPLICATION_VERSION "\n\n");
}
void help(int exit_code)
{
version();
::printf(C_RESET "HELP\n\nUsage: " C_BOLD_MAGENTA APPLICATION_NAME C_RESET " [ARGS]...\n");
::printf(C_RESET "Arguments:\n");
::printf(C_RESET " " C_GREEN "-i" C_RESET " | " C_BOLD_BLUE "--input " C_BOLD_YELLOW " <path> " C_BOLD_WHITE "input path\n");
::printf(C_RESET " " C_GREEN "-o" C_RESET " | " C_BOLD_BLUE "--output " C_BOLD_YELLOW " <path> " C_BOLD_WHITE "output path\n");
::printf(C_RESET " " C_GREEN "-v" C_RESET " | " C_BOLD_BLUE "--verbose" C_BOLD_YELLOW " " C_BOLD_WHITE "verbose output\n");
::printf(
C_RESET " " C_GREEN "-V" C_RESET " | " C_BOLD_BLUE "--version" C_BOLD_YELLOW " " C_BOLD_WHITE "print application version\n"
);
::printf(C_RESET " " C_GREEN "-h" C_RESET " | " C_BOLD_BLUE "--help " C_BOLD_YELLOW " " C_BOLD_WHITE "get help\n");
::printf(C_RESET "\n");
::printf(C_RESET C_BOLD_WHITE "*You can use multiple -i and -o parameters which will compile multiple files.\n");
::printf(C_RESET C_BOLD_WHITE " But the amount of -i and -o arguments must be the same.\n");
::printf(C_RESET C_BOLD_WHITE " Also if you are using directory as input you must specify a directory as an\n");
::printf(C_RESET C_BOLD_WHITE " output (or an non-existing path, which means that directory will be created).\n");
::printf(C_RESET C_BOLD_WHITE " Input and output arguments are adding to the separate lists.\n");
::printf(C_RESET C_BOLD_WHITE " It means that you can write something like this:\n");
::printf(
C_RESET " " C_BOLD_MAGENTA APPLICATION_NAME C_BOLD_BLUE " -i" C_BOLD_WHITE " file1.mpy"
C_BOLD_BLUE " -i" C_BOLD_WHITE " file2.mpy" C_BOLD_BLUE " -o" C_BOLD_WHITE " file1.py"
C_BOLD_BLUE " -i" C_BOLD_WHITE " dir3-in/" C_BOLD_BLUE " -o" C_BOLD_WHITE " file2.py"
C_BOLD_BLUE " -o" C_BOLD_WHITE " dir3-out/\n"
);
::printf(C_RESET C_BOLD_WHITE " Compiler will interpret it as:\n");
::printf(C_RESET C_BOLD_WHITE " file1.mpy -> file1.py\n");
::printf(C_RESET C_BOLD_WHITE " file2.mpy -> file2.py\n");
::printf(C_RESET C_BOLD_WHITE " dir3-in/ -> dir3-out/\n");
::printf(C_RESET);
::exit(exit_code);
}
void parse_args(int argc, char** argv)
{
if (argc < 2) help();
int option_index = 0, c;
while ((c = getopt_long(argc, argv, short_opts, long_opts, &option_index)) >= 0)
{
switch (c)
{
case 'i':
inputs.push_back(::optarg);
break;
case 'o':
outputs.push_back(::optarg);
break;
case 'v':
break;
case 'h':
help();
case 'V':
version();
::exit(0);
default:
help(-1);
}
}
}