Indexer.hx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import sys.io.*;
  2. using StringTools;
  3. class Indexer
  4. {
  5. public static function index(s3path:String)
  6. {
  7. var spaceRegex = ~/[ \t]+/g,
  8. s3pathRegex = ~/s3:\/\/([^\/]+)/;
  9. if (!s3path.endsWith('/')) {
  10. s3path = '$s3path/';
  11. }
  12. if (!s3pathRegex.match(s3path)) {
  13. throw 'Invalid s3 path $s3path';
  14. }
  15. var basePath = 'http://' + s3pathRegex.matched(1) + '.s3-website-us-east-1.amazonaws.com' + s3pathRegex.matchedRight();
  16. var proc = new Process('aws',['s3','ls',s3path,'--region','us-east-1']);
  17. var records = [],
  18. dirs = [];
  19. try
  20. {
  21. var i = proc.stdout;
  22. while(true)
  23. {
  24. var ln = spaceRegex.split(i.readLine());
  25. // trace(ln);
  26. inline function getPath(path:String)
  27. {
  28. return basePath + path;
  29. }
  30. switch(ln[1])
  31. {
  32. case 'PRE':
  33. dirs.push(getPath(ln[2]));
  34. case _:
  35. var size = ln[2];
  36. var path = ln[3];
  37. path = getPath(path);
  38. records.push({ date: ln[0] + ' ' + ln[1], size: ln[2], path: path, fname:haxe.io.Path.withoutDirectory(path) });
  39. }
  40. }
  41. }
  42. catch(e:haxe.io.Eof) {}
  43. var maxSizes = { date:25, size:15, fname:0 };
  44. for (r in records)
  45. {
  46. if (r.date.length > maxSizes.date)
  47. maxSizes.date = r.date.length;
  48. if (r.size.length > maxSizes.size)
  49. maxSizes.size = r.size.length;
  50. if (r.fname.length > maxSizes.fname)
  51. maxSizes.fname = r.fname.length;
  52. }
  53. records.sort(function(v1,v2) return Reflect.compare(v2.date,v1.date));
  54. var index = sys.io.File.write('index.html');
  55. index.writeString(
  56. '
  57. <html>
  58. <head>
  59. <title>Haxe git builds</title>
  60. </head>
  61. <body>
  62. <!-- Google Tag Manager -->
  63. <noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-M4JZKD"
  64. height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
  65. <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({"gtm.start":
  66. new Date().getTime(),event:"gtm.js"});var f=d.getElementsByTagName(s)[0],
  67. j=d.createElement(s),dl=l!="dataLayer"?"&l="+l:"";j.async=true;j.src=
  68. "//www.googletagmanager.com/gtm.js?id="+i+dl;f.parentNode.insertBefore(j,f);
  69. })(window,document,"script","dataLayer","GTM-M4JZKD");</script>
  70. <!-- End Google Tag Manager -->
  71. <div id="listing">
  72. <pre>
  73. ');
  74. inline function data(date:String,size:String,key:String,path:String)
  75. {
  76. index.writeString(date.rpad(' ',maxSizes.date));
  77. index.writeString(size.rpad(' ',maxSizes.size));
  78. if (path != null && path != '')
  79. index.writeString('<a href="$path">$key</a>\n');
  80. else
  81. index.writeString('$key\n');
  82. }
  83. data('Last Modified', 'Size', 'Path','');
  84. for (i in 0...(maxSizes.date + maxSizes.size + maxSizes.fname + 5))
  85. index.writeString('-');
  86. index.writeString('\n\n');
  87. for (dir in dirs)
  88. data('','DIR',haxe.io.Path.withoutDirectory(dir.substr(0,dir.length-1)),dir);
  89. for (r in records)
  90. if (r.fname != 'index.html')
  91. data(r.date,r.size,r.fname,r.path);
  92. index.writeString(
  93. '
  94. </pre>
  95. </div>
  96. </body>
  97. </html>
  98. ');
  99. index.close();
  100. }
  101. }