11
11
import re
12
12
import shutil
13
13
import datetime
14
+
14
15
if sys .version_info [0 ] < 3 :
15
- sys .stdout .write (" Sorry, requires Python 3.x or better\n " )
16
+ sys .stdout .write (' Sorry, requires Python 3.x or better\n ' )
16
17
sys .exit (1 )
17
18
18
19
SCRIPTPATH = os .path .dirname (os .path .abspath (sys .argv [0 ]))
19
20
PROJECTPATH = os .path .dirname (SCRIPTPATH )
20
- print (f" SCRIPTPATH={ SCRIPTPATH } PROJECTPATH={ PROJECTPATH } " )
21
+ print (f' SCRIPTPATH={ SCRIPTPATH } PROJECTPATH={ PROJECTPATH } ' )
21
22
22
- if " AMALGAMATE_SOURCE_PATH" not in os .environ :
23
- AMALGAMATE_SOURCE_PATH = os .path .join (PROJECTPATH , " src" )
23
+ if ' AMALGAMATE_SOURCE_PATH' not in os .environ :
24
+ AMALGAMATE_SOURCE_PATH = os .path .join (PROJECTPATH , ' src' )
24
25
else :
25
- AMALGAMATE_SOURCE_PATH = os .environ [" AMALGAMATE_SOURCE_PATH" ]
26
- if " AMALGAMATE_INCLUDE_PATH" not in os .environ :
27
- AMALGAMATE_INCLUDE_PATH = os .path .join (PROJECTPATH , " include" )
26
+ AMALGAMATE_SOURCE_PATH = os .environ [' AMALGAMATE_SOURCE_PATH' ]
27
+ if ' AMALGAMATE_INCLUDE_PATH' not in os .environ :
28
+ AMALGAMATE_INCLUDE_PATH = os .path .join (PROJECTPATH , ' include' )
28
29
else :
29
- AMALGAMATE_INCLUDE_PATH = os .environ [" AMALGAMATE_INCLUDE_PATH" ]
30
- if " AMALGAMATE_OUTPUT_PATH" not in os .environ :
30
+ AMALGAMATE_INCLUDE_PATH = os .environ [' AMALGAMATE_INCLUDE_PATH' ]
31
+ if ' AMALGAMATE_OUTPUT_PATH' not in os .environ :
31
32
AMALGAMATE_OUTPUT_PATH = os .path .join (SCRIPTPATH )
32
33
else :
33
- AMALGAMATE_OUTPUT_PATH = os .environ [" AMALGAMATE_OUTPUT_PATH" ]
34
+ AMALGAMATE_OUTPUT_PATH = os .environ [' AMALGAMATE_OUTPUT_PATH' ]
34
35
35
36
# this list excludes the "src/generic headers"
36
- ALLCFILES = [" ada.cpp" ]
37
+ ALLCFILES = [' ada.cpp' ]
37
38
38
39
# order matters
39
- ALLCHEADERS = [" ada.h" ]
40
+ ALLCHEADERS = [' ada.h' ]
40
41
41
42
found_includes = []
42
43
43
- current_implementation = ''
44
+ current_implementation = ''
44
45
45
- def doinclude (fid : str , file : str , line : str , origin : str ) -> None :
46
46
47
+ def doinclude (fid : str , file : str , line : str , origin : str ) -> None :
47
48
p = os .path .join (AMALGAMATE_INCLUDE_PATH , file )
48
49
pi = os .path .join (AMALGAMATE_SOURCE_PATH , file )
49
50
@@ -61,22 +62,23 @@ def doinclude(fid: str, file: str, line: str, origin: str) -> None:
61
62
pass
62
63
else :
63
64
# If we don't recognize it, just emit the #include
64
- print (" unrecognized:" , file , " from " , line , " in " , origin )
65
+ print (' unrecognized:' , file , ' from ' , line , ' in ' , origin )
65
66
print (line , file = fid )
66
67
68
+
67
69
def dofile (fid : str , prepath : str , filename : str ) -> None :
68
70
file = os .path .join (prepath , filename )
69
71
RELFILE = os .path .relpath (file , PROJECTPATH )
70
72
# Last lines are always ignored. Files should end by an empty lines.
71
- print (f" /* begin file { RELFILE } */" , file = fid )
73
+ print (f' /* begin file { RELFILE } */' , file = fid )
72
74
includepattern = re .compile ('\\ s*#\\ s*include "(.*)"' )
73
75
with open (file , 'r' ) as fid2 :
74
76
for line in fid2 :
75
77
line = line .rstrip ('\n ' )
76
78
s = includepattern .search (line )
77
79
if s :
78
80
includedfile = s .group (1 )
79
- if includedfile == " ada.h" and filename == " ada.cpp" :
81
+ if includedfile == ' ada.h' and filename == ' ada.cpp' :
80
82
print (line , file = fid )
81
83
continue
82
84
@@ -86,7 +88,7 @@ def dofile(fid: str, prepath: str, filename: str) -> None:
86
88
doinclude (fid , includedfile , line , filename )
87
89
else :
88
90
print (line , file = fid )
89
- print (f" /* end file { RELFILE } */" , file = fid )
91
+ print (f' /* end file { RELFILE } */' , file = fid )
90
92
91
93
92
94
# Get the generation date from git, so the output is reproducible.
@@ -95,51 +97,54 @@ def dofile(fid: str, prepath: str, filename: str) -> None:
95
97
# Forcing it to be UTC is difficult, because it needs to be portable
96
98
# between gnu date and busybox date.
97
99
try :
98
- timestamp = subprocess .run (['git' , 'show' , '-s' , '--format=%ci' , 'HEAD' ],
99
- stdout = subprocess .PIPE ).stdout .decode ('utf-8' ).strip ()
100
+ timestamp = (
101
+ subprocess .run (['git' , 'show' , '-s' , '--format=%ci' , 'HEAD' ], stdout = subprocess .PIPE )
102
+ .stdout .decode ('utf-8' )
103
+ .strip ()
104
+ )
100
105
except Exception :
101
- print (" git not found, timestamp based on current time" )
106
+ print (' git not found, timestamp based on current time' )
102
107
timestamp = str (datetime .datetime .now ())
103
- print (f" timestamp is { timestamp } " )
108
+ print (f' timestamp is { timestamp } ' )
104
109
105
110
os .makedirs (AMALGAMATE_OUTPUT_PATH , exist_ok = True )
106
- AMAL_H = os .path .join (AMALGAMATE_OUTPUT_PATH , " ada.h" )
107
- AMAL_C = os .path .join (AMALGAMATE_OUTPUT_PATH , " ada.cpp" )
108
- DEMOCPP = os .path .join (AMALGAMATE_OUTPUT_PATH , " cpp" )
109
- README = os .path .join (AMALGAMATE_OUTPUT_PATH , " README.md" )
111
+ AMAL_H = os .path .join (AMALGAMATE_OUTPUT_PATH , ' ada.h' )
112
+ AMAL_C = os .path .join (AMALGAMATE_OUTPUT_PATH , ' ada.cpp' )
113
+ DEMOCPP = os .path .join (AMALGAMATE_OUTPUT_PATH , ' cpp' )
114
+ README = os .path .join (AMALGAMATE_OUTPUT_PATH , ' README.md' )
110
115
111
- print (f" Creating { AMAL_H } " )
116
+ print (f' Creating { AMAL_H } ' )
112
117
amal_h = open (AMAL_H , 'w' )
113
- print (f" /* auto-generated on { timestamp } . Do not edit! */" , file = amal_h )
118
+ print (f' /* auto-generated on { timestamp } . Do not edit! */' , file = amal_h )
114
119
for h in ALLCHEADERS :
115
- doinclude (amal_h , h , f" ERROR { h } not found" , h )
120
+ doinclude (amal_h , h , f' ERROR { h } not found' , h )
116
121
117
122
amal_h .close ()
118
123
print ()
119
124
print ()
120
- print (f" Creating { AMAL_C } " )
125
+ print (f' Creating { AMAL_C } ' )
121
126
amal_c = open (AMAL_C , 'w' )
122
- print (f" /* auto-generated on { timestamp } . Do not edit! */" , file = amal_c )
127
+ print (f' /* auto-generated on { timestamp } . Do not edit! */' , file = amal_c )
123
128
for c in ALLCFILES :
124
- doinclude (amal_c , c , f" ERROR { c } not found" , c )
129
+ doinclude (amal_c , c , f' ERROR { c } not found' , c )
125
130
126
131
amal_c .close ()
127
132
128
133
# copy the README and DEMOCPP
129
134
if SCRIPTPATH != AMALGAMATE_OUTPUT_PATH :
130
- shutil .copy2 (os .path .join (SCRIPTPATH ," demo.cpp" ), AMALGAMATE_OUTPUT_PATH )
131
- shutil .copy2 (os .path .join (SCRIPTPATH ," demo.c" ), AMALGAMATE_OUTPUT_PATH )
132
- shutil .copy2 (os .path .join (SCRIPTPATH ," README.md" ), AMALGAMATE_OUTPUT_PATH )
135
+ shutil .copy2 (os .path .join (SCRIPTPATH , ' demo.cpp' ), AMALGAMATE_OUTPUT_PATH )
136
+ shutil .copy2 (os .path .join (SCRIPTPATH , ' demo.c' ), AMALGAMATE_OUTPUT_PATH )
137
+ shutil .copy2 (os .path .join (SCRIPTPATH , ' README.md' ), AMALGAMATE_OUTPUT_PATH )
133
138
134
- shutil .copy2 (os .path .join (AMALGAMATE_INCLUDE_PATH ," ada_c.h" ), AMALGAMATE_OUTPUT_PATH )
139
+ shutil .copy2 (os .path .join (AMALGAMATE_INCLUDE_PATH , ' ada_c.h' ), AMALGAMATE_OUTPUT_PATH )
135
140
136
- zf = zipfile .ZipFile (os .path .join (AMALGAMATE_OUTPUT_PATH ,'singleheader.zip' ), 'w' , zipfile .ZIP_DEFLATED )
137
- zf .write (os .path .join (AMALGAMATE_OUTPUT_PATH ," ada.cpp" ), " ada.cpp" )
138
- zf .write (os .path .join (AMALGAMATE_OUTPUT_PATH ," ada.h" ), " ada.h" )
139
- zf .write (os .path .join (AMALGAMATE_INCLUDE_PATH ," ada_c.h" ), " ada_c.h" )
141
+ zf = zipfile .ZipFile (os .path .join (AMALGAMATE_OUTPUT_PATH , 'singleheader.zip' ), 'w' , zipfile .ZIP_DEFLATED )
142
+ zf .write (os .path .join (AMALGAMATE_OUTPUT_PATH , ' ada.cpp' ), ' ada.cpp' )
143
+ zf .write (os .path .join (AMALGAMATE_OUTPUT_PATH , ' ada.h' ), ' ada.h' )
144
+ zf .write (os .path .join (AMALGAMATE_INCLUDE_PATH , ' ada_c.h' ), ' ada_c.h' )
140
145
141
146
142
- print (" Done with all files generation." )
147
+ print (' Done with all files generation.' )
143
148
144
- print (f" Files have been written to directory: { AMALGAMATE_OUTPUT_PATH } /" )
145
- print (" Done with all files generation." )
149
+ print (f' Files have been written to directory: { AMALGAMATE_OUTPUT_PATH } /' )
150
+ print (' Done with all files generation.' )
0 commit comments