forked from serantes/nepoogle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
executable file
·135 lines (101 loc) · 4.09 KB
/
build.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#***************************************************************************
#* nepoogle - build script. *
#* *
#* Copyright (C) 2011-13 Ignacio Serantes <[email protected]> *
#* *
#* This program is free software; you can redistribute it and/or modify *
#* it under the terms of the GNU General Public License as published by *
#* the Free Software Foundation; either version 2 of the License, or *
#* (at your option) any later version. *
#* *
#* This program is distributed in the hope that it will be useful, *
#* but WITHOUT ANY WARRANTY; without even the implied warranty of *
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
#* GNU General Public License for more details. *
#* *
#* You should have received a copy of the GNU General Public License *
#* along with this program; if not, write to the *
#* Free Software Foundation, Inc., *
#* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
#***************************************************************************
import os, stat, sys
def addFile(outFile, fileName):
if not os.path.exists(fileName):
raise Exception("ERROR: \"%s\" file don\'t exists." % fileName)
f = open(fileName, 'r')
copyLine = False
beginTag = "#BEGIN %s" % fileName
endTag = "#END %s" % fileName
try:
lines = f.readlines()
for line in lines:
if line.strip() == beginTag:
copyLine = True
elif line.strip() == endTag:
copyLine = False
elif copyLine:
outFile.write(line)
finally:
f.close()
def build(inFileName = '', outFileName = ''):
inFile = open(inFileName, 'r')
outFile = open(outFileName, 'w')
currExp = ""
try:
lines = inFile.readlines()
for line in lines:
if (line.strip() == "#IF BUILD"):
currExp = "IF"
continue
elif (line.strip() == "#ELSE"):
currExp = "ELSE"
continue
elif (line.strip() == "#ENDIF"):
currExp = ""
continue
if (currExp == "ELSE"):
pass
else:
if (line.strip()[:9] == "#INCLUDE "):
addFileName = line.strip()[9:].strip()
addFile(outFile, addFileName)
print(" +++ \"%s\" file added." % addFileName)
else:
outFile.write(line)
except:
raise
finally:
inFile.close()
outFile.close()
try:
fd = os.open(outFileName, os.O_RDONLY )
try:
# Change permissions.
os.fchmod(fd, stat.S_IREAD + stat.S_IWRITE + stat.S_IEXEC + stat.S_IRGRP + stat.S_IXGRP + stat.S_IROTH + stat.S_IXOTH)
finally:
os.close(fd)
except:
raise
inFileName = outFileName = None
try:
inFileName = sys.argv[1]
outFileName = sys.argv[2]
except:
pass
print "Script builder v0.2\n"
if (outFileName == None):
print("usage: build.py nepoogle nepoogle.py")
else:
if not os.path.exists(inFileName):
print("ERROR: \"%s\" file don\'t exists." % (inFileName))
else:
if (inFileName == outFileName):
print("ERROR: input and output file can't be the same.")
elif os.path.exists(outFileName):
print("ERROR: \"%s\" file exists, delete before proceeding." % (outFileName))
else:
build(inFileName, outFileName)
print(" >>> \"%s\" file has been built." % outFileName)
print("")