-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.py
844 lines (631 loc) · 20.6 KB
/
generator.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
import os, sys, re, shutil
import random
Letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
#---------------------------------------------------------
def encode(n):
s=''
while True:
k = n % 26
s = Letters[k] + s
n = int(n/26) - 1
if n < 0:
break
return (s)
#---------------------------------------------------------
def write_text (file_name, text):
try:
f = open(file_name, 'w')
f.write (text)
finally:
f.close ()
def create_dirs (path, end = ''):
head, tail = os.path.split (path)
#print '> (path=%s end=%s) => (head=%s tail=%s)' % (path, end, head, tail)
if head != '':
create_dirs (head, os.path.join (tail, end))
try:
#print '> mkdir %s' % (path)
os.mkdir (path)
except:
pass
CMTProjects = {}
CMTPackages = {}
Script = ''
class CMTPackage:
#---------------------------------------------------------
def __init__ (self, project, prefix, package):
#print 'creating the package %s %s' % (package, prefix)
self.project = project
self.prefix = prefix
self.name = package
if self.prefix == '':
self.full_name = self.name
else:
self.full_name = prefix + '/' + self.name
self.path = os.path.join (project, 'src', self.full_name)
self.uses = {}
#---------------------------------------------------------
def cleanup (self):
#print '> rmdir (%s)' % self.path
if os.path.exists(self.path):
shutil.rmtree(self.path)
#---------------------------------------------------------
def set_structure (self):
#print '> mkdir %s' % (self.path)
#os.mkdir (self.path)
create_dirs (self.path)
# Create the package structure:
# <project>/<prefix>/<package>
# /includes/<package>/
# /src/
#print '> mkdir %(path)s/includes' % {'path':self.path}
os.mkdir (os.path.join (self.path, "includes"))
#print '> mkdir %(path)s/includes/%(package)s' % {'path':self.path, 'package':self.name}
os.mkdir (os.path.join (self.path, "includes", self.name))
#print '> mkdir %(path)s/src' % {'path':self.path}
os.mkdir (os.path.join (self.path, 'src'))
#---------------------------------------------------------
def set_headers (self):
#print '> create %s/%s/Lib%s.hxx uses=%s' % (self.path, self.name, self.name, self.uses)
text = '''
#ifndef __Lib%(p)s_hxx__
#define __Lib%(p)s_hxx__
// --------------------------------------
''' % {"p":self.name}
if len(self.uses) > 0:
for u in self.uses:
text += '#include <%s/Lib%s.hxx>\n' % (u, u)
text += '''
#ifdef _MSC_VER
#define DllExport __declspec( dllexport )
#else
#define DllExport
#endif
class DllExport C%(p)s
{
public:
C%(p)s ();
~C%(p)s ();
void f();
private:\n''' % {"p":self.name}
if len(self.uses) > 0:
for u in self.uses:
text += ' C%(p)s o%(p)s;\n' % {"p":u}
text += '''};
// --------------------------------------
#endif
'''
write_text (os.path.join (self.path, "includes", self.name, 'Lib%s.hxx' % self.name), text)
#---------------------------------------------------------
def set_sources (self):
#print '> create %s/src/Lib%s.cxx' % (self.path, self.name)
text = '''// --------------------------------------
#include <iostream>
#include <%(p)s/Lib%(p)s.hxx>
C%(p)s::C%(p)s ()
{
std::cout << "Constructor C%(p)s" << std::endl;
}
C%(p)s::~C%(p)s ()
{
std::cout << "Destructor C%(p)s" << std::endl;
}
void C%(p)s::f ()
{
std::cout << "C%(p)s.f" << std::endl;\n''' % {"p":self.name}
if len(self.uses) > 0:
for k in self.uses:
text += ' o%s.f();\n' % (k)
text += '''}
// --------------------------------------
'''
write_text (os.path.join (self.path, 'src', 'Lib%s.cxx' % self.name), text)
#---------------------------------------------------------
def set_test (self):
#print '> create %s/src/test%s.cxx' % (self.path, self.name)
text = '''// --------------------------------------
#include <iostream>
#include <%(p)s/Lib%(p)s.hxx>
int main ()
{
std::cout << "Test the program for package %(f)s" << std::endl;
C%(p)s o;
o.f ();
}
// --------------------------------------
''' % {"p":self.name, "f":self.full_name}
write_text (os.path.join (self.path, 'src', 'test%s.cxx' % self.name), text)
#---------------------------------------------------------
def set_requirements (self):
pass
#---------------------------------------------------------
def show_uses (self, t=''):
if t == '':
print 'show uses> Package %s' % (self.name)
for u in set(self.uses):
print '%s use:%s' % (t, u)
p = CMTPackages[u]
p.show_uses (t + ' ')
def show_graph (self):
text = ''
for u in set(self.uses):
text += '%s -> %s\n' % (self.name, u)
return text
#---------------------------------------------------------
def show_all_uses (self):
print '------------------ show all uses'
for k in set(CMTPackages.keys()):
p = CMTPackages[k]
print 'Package:%s' % (k)
p.show_uses ()
print '------------------'
#---------------------------------------------------------
def get_uses (self):
def recurse (package, all):
# get complete (recursive) list of used packages
for u in set(package.uses):
#print '(p:%s u:%s uses:%s all:%s)' % (package.name, u, package.uses, all)
if not (u in all):
all.add (u)
p = CMTPackages[u]
all |= recurse (p, all)
return (all)
all = set()
if len(self.uses) > 0:
all = recurse (self, set())
#print 'in get_uses for %s all=%s' % (self.name, all)
return (all)
#---------------------------------------------------------
def set (self):
#print '----------- Set package %s' % (self.name)
if len(CMTPackages) > 1:
# get all packages but self
keys = list(CMTPackages.keys())
del keys[keys.index(self.name)]
nuses = len(keys)/2
if nuses > 0:
# select a subset of those
# we remove recursive uses
# the list of uses specifies only the first level of uses. Thefore, we need a search function to check
# if the current package is already in the chain of uses.
# eg: A use B and B use A
# if [A use B] was defined first, we need to remove A from the list of uses of B
uses = set(random.sample (keys, random.choice (range(len(keys)/2))))
#print 'pack> Original all used %s for package %s' % (uses, self.name)
toberemoved = set()
for u in uses:
p = CMTPackages[u]
if self.name in p.get_uses():
toberemoved.add(u)
#print 'pack> to be removed used %s' % (toberemoved)
uses -= toberemoved
# now we have to remove packages that belong to a project which is NOT used by the current project.
for u in uses:
pack = CMTPackages[u]
proj = CMTProjects[pack.project]
if not ((self.project in proj.get_uses ()) or (self.project == pack.project)):
toberemoved.add(u)
#print 'pack> to be removed used %s' % (toberemoved)
uses -= toberemoved
#print 'pack> Final all used %s' % (uses)
self.uses = uses
#print 'pack> package %s uses = %s' % (self.name, self.uses)
self.set_headers ()
self.set_sources ()
self.set_test ()
#self.set_requirements ()
#---------------------------------------------------------
def set_config_file (self):
#print '> create %s/hscript.yml' % (self.path)
longuses = [ 'Settings_%s' % self.project ]
longuses.extend ([CMTPackages[u].full_name for u in self.uses])
#print 'longuses=%s' % longuses
deps = '''
deps: {
public: %s,
},
''' % [u for u in longuses]
libdeps = ''
if len (self.uses) > 0:
libdeps = '''
use: %s,
''' % ['Lib%s' % u for u in self.uses]
alluses = [ self.name ]
alluses.extend (self.uses)
appdeps = '''
use: %s,
''' % ['Lib%s' % u for u in alluses]
text = '''
## -*- yaml -*-
package: {
name: "%s",
authors: ["my"],
%s
}
configure: {
tools: ["compiler_c", "compiler_cxx", "python"],
env: {
PYTHONPATH: "${INSTALL_AREA}/python:${PYTHONPATH}"
},
}
build: {
test%s: {
features: "cxx cxxprogram hwaf_install_headers hwaf_export_lib",
includes: "includes",
export_includes: "includes",
cwd: "includes",
source: "src/test%s.cxx",
%s
},
Lib%s: {
features: "cxx cxxshlib hwaf_install_headers hwaf_export_lib",
includes: "includes",
export_includes: "includes",
cwd: "includes",
source: "src/Lib%s.cxx",
%s
},
}
''' % (self.full_name, deps, self.name, self.name, appdeps, self.name, self.name, libdeps)
write_text (os.path.join (self.path, 'hscript.yml'), text)
class Settings:
#---------------------------------------------------------
def __init__ (self, project):
self.name = 'Settings_%s' % project
#print 'creating the package %s' % (self.name)
self.project = project
self.path = os.path.join (project, 'src', self.name)
#---------------------------------------------------------
def set_structure (self):
global Script
#print '> mkdir %(path)s/src' % {'path':self.path}
create_dirs (self.path)
print 'Script=%s' % Script
shutil.copy (os.path.join (Script, 'install_headers.py'), self.path)
#---------------------------------------------------------
def set_config_file (self):
#print '> create %s/hscript.yml' % (self.path)
text = '''
## -*- yaml -*-
package: {
name: "%s",
authors: ["my"],
}
configure: {
tools: ["compiler_c", "compiler_cxx", "python"],
env: {
PYTHONPATH: "${INSTALL_AREA}/python:${PYTHONPATH}"
},
}
build: {
hwaf-call: [
"install_headers.py",
],
}
''' % (self.name)
write_text (os.path.join (self.path, 'hscript.yml'), text)
class CMTProject:
#---------------------------------------------------------
def __init__ (self, name, first_package, packages = 1):
global Letters
self.name = name
#self.base = base
self.packages = []
self.uses = {}
self.parents = []
self.done = False
self.settings = Settings (name)
for package in range(first_package, first_package + packages):
prefix = ''
if random.choice (xrange (2)) == 1:
prefix = os.path.normpath (os.path.join ('Pre', 'Fix'))
prefix = prefix.replace ('\\', '/')
package_name = name + '_' + encode(package)
CMTPackages[package_name] = CMTPackage (name, prefix, package_name)
self.packages.append(package_name)
#---------------------------------------------------------
def get_uses (self):
def recurse (project, all):
# get complete (recursive) list of used projects
for u in set(project.uses):
#print '(p:%s u:%s uses:%s all:%s)' % (project.name, u, project.uses, all)
if not (u in all):
all.add (u)
p = CMTProjects[u]
all |= recurse (p, all)
return (all)
all = set()
if len(self.uses) > 0:
all = recurse (self, set())
#print 'in get_uses for %s all=%s' % (self.name, all)
return (all)
#---------------------------------------------------------
def set (self):
if len(CMTProjects) > 1:
# get all projects but self
keys = list(CMTProjects.keys())
del keys[keys.index(self.name)]
nuses = len(keys)/2
if nuses > 0:
# select a subset of those
# we remove recursive uses
# the list of uses specifies only the first level of uses. Thefore, we need a search function to check
# if the current package is already in the chain of uses.
# eg: A use B and B use A
# if [A use B] was defined first, we need to remove A from the list of uses of B
uses = set(random.sample (keys, random.choice (range(len(keys)))))
#print 'Original all used %s for project %s' % (uses, self.name)
#for u in uses:
# p = CMTProjects[u]
toberemoved = set()
for u in uses:
p = CMTProjects[u]
if self.name in p.get_uses():
toberemoved.add(u)
#print 'to be removed used %s' % (toberemoved)
uses -= toberemoved
#print 'Final all used %s' % (uses)
self.uses = uses
#print 'project %s uses = %s' % (self.name, self.uses)
#---------------------------------------------------------
def cleanup (self):
#print '> rmdir (%s)' % self.name
if os.path.exists(self.name):
shutil.rmtree(self.name)
#---------------------------------------------------------
def set_structure (self):
#print '> mkdir %s' % (self.name)
os.mkdir (self.name)
#print '> mkdir %s/__build__' % (self.name)
os.mkdir (os.path.join (self.name, "__build__"))
self.settings.set_structure ()
for package in self.packages:
p = CMTPackages[package]
p.set_structure ()
#---------------------------------------------------------
def set_packages (self):
for package in self.packages:
p = CMTPackages[package]
p.set ()
#---------------------------------------------------------
def get_used_projects (self):
# We start by building the uses of this project through the use graph of the packages.
# but now, the use graph of the projects has been constructed a-priori.
uses = []
for package in self.packages:
p = CMTPackages[package]
for u in p.uses:
pu = CMTPackages[u]
if not(pu.project in uses) and (pu.project != self.name):
uses.append (pu.project)
#print 'project %s use projects %s' % (self.name, uses)
return (uses)
#---------------------------------------------------------
def set_config_file (self):
config_dir = self.name
self.settings.set_config_file ()
for package in self.packages:
p = CMTPackages[package]
p.set_config_file ()
#---------------------------------------------------------
def broadcast (self):
b = []
if self.done:
return b;
self.done = True
used = self.get_used_projects ()
if len(used) > 0:
for k in used:
use = CMTProjects[k]
bu = use.broadcast ()
if len(bu) > 0:
b.extend (bu)
b.extend (self.name)
return (b)
"""
CMTGenerator
Construct a CMT project with a hierarchy of projects each containing a hierarchy of packages each containing
- sources of a library (with one C++ class)
- sources of a test program (which instantiates one object of the class)
- a hscript file
Projects may use other projects.
Packages may use other packages.
For each used package, the package's class includes the used classes, and instantiates one object for each the used classe
"""
class CMTGenerator:
#---------------------------------------------------------
def __init__ (self, projects = 1, max_packages = 1, uses = []):
global Letters
self.projects = projects
self.max_packages = max_packages
self.uses = uses
self.used = {}
first = 0
for project in range(projects):
name = encode(project)
packages = random.choice (xrange (self.max_packages)) + 1
#print 'creating the project %s' % (name)
CMTProjects[name] = CMTProject (name, first, packages)
first += packages
#---------------------------------------------------------
def cleanup_projects (self):
for project in CMTProjects:
p = CMTProjects[project]
p.cleanup()
#---------------------------------------------------------
def set_structure (self):
for project in CMTProjects:
p = CMTProjects[project]
p.set_structure ()
#---------------------------------------------------------
def set (self):
for project in CMTProjects:
p = CMTProjects[project]
p.set()
for project in CMTProjects:
p = CMTProjects[project]
p.set_packages()
#---------------------------------------------------------
def set_config_files (self):
for project in CMTProjects:
p = CMTProjects[project]
p.set_config_file ()
#---------------------------------------------------------
def get_used (self, p):
used = set ()
if p in self.used:
used = set (self.used[p])
return used
#---------------------------------------------------------
def get_all_used (self, p):
def recurse (p, all = []):
u = self.get_used (p)
if not (p - self.base) in range (self.packages): return []
t = [p]
for sub in u:
if not sub in t + all:
t += recurse (sub, t + all)
return t
all = recurse (p, [])
return all
#---------------------------------------------------------
def generate (self):
self.cleanup_projects ()
self.set_structure ()
self.set ()
self.set_config_files ()
#-------------------------
# Main class holding command interface
#
class CMTInterface:
#---------------------------------------------------------
def generate_project (self, projects, max_packages, uses = []):
here = os.getcwd ()
generator = CMTGenerator (projects, max_packages, uses)
generator.generate ()
print "Fin de la generation"
print "Production du graph des dependances generator.dot"
text = """digraph G {
node [shape=box]
"""
for k in set(CMTProjects.keys()):
p = CMTProjects[k]
text += "%s;\n" % (p.name)
for u in p.get_used_projects ():
text += "%s -> %s;\n" % (p.name, u)
text += """node [shape=ellipse];
"""
for k in set(CMTPackages.keys()):
p = CMTPackages[k]
text += "%s;\n" % (p.name)
text += p.show_graph ()
text += "}\n"
write_text ("generator.dot", text)
print ''' >>>>> Show the use graph for projects: '''
for k in set(CMTProjects.keys()):
p = CMTProjects[k]
uses = p.get_used_projects ()
if len(uses) > 0:
text = "%s use " % (p.name)
for u in p.get_used_projects ():
child = CMTProjects[u]
child.parents.append (k)
text += "%s " % (u)
print text
#---------------------------------------------------------
def broadcast (self):
global CMTProjects
for k in set(CMTProjects.keys()):
p = CMTProjects[k]
p.done = False
b = []
for k in set(CMTProjects.keys()):
p = CMTProjects[k]
bu = p.broadcast ()
if len(bu) > 0:
b.extend (bu)
return (b)
Interface = CMTInterface ()
#----------------------------------------------------------------------------------------------------------------------
# Interface to waflib
#----------------------------------------------------------------------------------------------------------------------
import __main__
if __name__ == "__main__":
project = 'A'
projects = 1
packages = 5
uses = []
Script = os.path.dirname (os.path.abspath(sys.modules['__main__'].__file__))
print 'script=%s' % Script
if len(sys.argv) > 1:
for arg in sys.argv:
if re.match ('projects=(\d+)', arg):
m = re.match ('projects=(\d+)', arg)
projects = int(m.group(1))
elif re.match ('packages=(\d+)', arg):
m = re.match ('packages=(\d+)', arg)
packages = int(m.group(1))
elif re.match ('uses=(.+)', arg):
m = re.match ('uses=(.+)', arg)
uses = re.split ('\W+', m.group(1))
else:
print """
generator.py
projects=<n>
packages=<n>
uses=<list>
"""
print 'projects=%d packages=%d uses=%s' % ( projects, packages, uses)
if projects == 0:
print "Syntax: generator.py projects=[1] packages=[5]"
exit (1)
print ''' Creation d'un espace de test '''
test = 'test'
if os.path.exists (test):
print ">>> rmdir %s" % test
shutil.rmtree (test)
print ">>> mkdir %s" % test
os.mkdir (test)
print ">>> chdir %s" % test
os.chdir (test)
base = os.getcwd ()
print ''' Creating projects into %s''' % base
Interface.generate_project (projects, packages)
#exit (1)
b = Interface.broadcast ()
for name in b:
p = CMTProjects[name]
used = p.get_used_projects ()
contexts = ''
if len(used) > 0:
contexts = "-p="
first = True
for u in used:
if first:
first = False
else:
contexts += ':'
contexts += '%s/%s/install-area' % (base, u)
print 'do %s with contexts=%s' % (name, contexts)
print "%s >>> hwaf init %s" % (os.getcwd (), name)
os.system ("hwaf init %s" % name)
print "%s >>> cd %s" % (os.getcwd (), name)
os.chdir (name)
print "%s >>> hwaf setup %s" % (os.getcwd (), contexts)
os.system ("hwaf setup %s" % contexts)
print ">>> hwaf configure"
os.system ("hwaf configure")
print ">>> hwaf"
os.system ("hwaf")
print ">>> hwaf show pkg-tree"
os.system ("hwaf show pkg-tree")
for root, dirs, files in os.walk('.'):
for file in files:
if file == 'hscript.yml':
p = os.path.basename (root)
if p != "Settings_%s" % name:
print root, dirs, files, p
print ">>> hwaf run test%s" % p
os.system ("hwaf run test%s" % p)
print ">>> cd .."
os.chdir ("..")