@@ -356,6 +356,104 @@ func tryUpdateGoModAndGoSum(modMode ModMode, depMode DependencyInstallerMode) {
356
356
}
357
357
}
358
358
359
+ func moveToTemporaryGopath (srcdir string , importpath string ) {
360
+ // a temporary directory where everything is moved while the correct
361
+ // directory structure is created.
362
+ scratch , err := ioutil .TempDir (srcdir , "scratch" )
363
+ if err != nil {
364
+ log .Fatalf ("Failed to create temporary directory %s in directory %s: %s\n " ,
365
+ scratch , srcdir , err .Error ())
366
+ }
367
+ log .Printf ("Temporary directory is %s.\n " , scratch )
368
+
369
+ // move all files in `srcdir` to `scratch`
370
+ dir , err := os .Open (srcdir )
371
+ if err != nil {
372
+ log .Fatalf ("Failed to open source directory %s for reading: %s\n " , srcdir , err .Error ())
373
+ }
374
+ files , err := dir .Readdirnames (- 1 )
375
+ if err != nil {
376
+ log .Fatalf ("Failed to read source directory %s: %s\n " , srcdir , err .Error ())
377
+ }
378
+ for _ , file := range files {
379
+ if file != filepath .Base (scratch ) {
380
+ log .Printf ("Moving %s/%s to %s/%s.\n " , srcdir , file , scratch , file )
381
+ err := os .Rename (filepath .Join (srcdir , file ), filepath .Join (scratch , file ))
382
+ if err != nil {
383
+ log .Fatalf ("Failed to move file %s to the temporary directory: %s\n " , file , err .Error ())
384
+ }
385
+ }
386
+ }
387
+
388
+ // create a new folder which we will add to GOPATH below
389
+ // Note we evaluate all symlinks here for consistency: otherwise os.Chdir below
390
+ // will follow links but other references to the path may not, which can lead to
391
+ // disagreements between GOPATH and the working directory.
392
+ realSrc , err := filepath .EvalSymlinks (srcdir )
393
+ if err != nil {
394
+ log .Fatalf ("Failed to evaluate symlinks in %s: %s\n " , srcdir , err .Error ())
395
+ }
396
+
397
+ root := filepath .Join (realSrc , "root" )
398
+
399
+ // move source files to where Go expects them to be
400
+ newdir := filepath .Join (root , "src" , importpath )
401
+ err = os .MkdirAll (filepath .Dir (newdir ), 0755 )
402
+ if err != nil {
403
+ log .Fatalf ("Failed to create directory %s: %s\n " , newdir , err .Error ())
404
+ }
405
+ log .Printf ("Moving %s to %s.\n " , scratch , newdir )
406
+ err = os .Rename (scratch , newdir )
407
+ if err != nil {
408
+ log .Fatalf ("Failed to rename %s to %s: %s\n " , scratch , newdir , err .Error ())
409
+ }
410
+
411
+ // schedule restoring the contents of newdir to their original location after this function completes:
412
+ defer restoreRepoLayout (newdir , files , filepath .Base (scratch ), srcdir )
413
+
414
+ err = os .Chdir (newdir )
415
+ if err != nil {
416
+ log .Fatalf ("Failed to chdir into %s: %s\n " , newdir , err .Error ())
417
+ }
418
+
419
+ // set up SEMMLE_PATH_TRANSFORMER to ensure paths in the source archive and the snapshot
420
+ // match the original source location, not the location we moved it to
421
+ pt , err := ioutil .TempFile ("" , "path-transformer" )
422
+ if err != nil {
423
+ log .Fatalf ("Unable to create path transformer file: %s." , err .Error ())
424
+ }
425
+ defer os .Remove (pt .Name ())
426
+ _ , err = pt .WriteString ("#" + realSrc + "\n " + newdir + "//\n " )
427
+ if err != nil {
428
+ log .Fatalf ("Unable to write path transformer file: %s." , err .Error ())
429
+ }
430
+ err = pt .Close ()
431
+ if err != nil {
432
+ log .Fatalf ("Unable to close path transformer file: %s." , err .Error ())
433
+ }
434
+ err = os .Setenv ("SEMMLE_PATH_TRANSFORMER" , pt .Name ())
435
+ if err != nil {
436
+ log .Fatalf ("Unable to set SEMMLE_PATH_TRANSFORMER environment variable: %s.\n " , err .Error ())
437
+ }
438
+
439
+ // set/extend GOPATH
440
+ oldGopath := os .Getenv ("GOPATH" )
441
+ var newGopath string
442
+ if oldGopath != "" {
443
+ newGopath = strings .Join (
444
+ []string {root , oldGopath },
445
+ string (os .PathListSeparator ),
446
+ )
447
+ } else {
448
+ newGopath = root
449
+ }
450
+ err = os .Setenv ("GOPATH" , newGopath )
451
+ if err != nil {
452
+ log .Fatalf ("Unable to set GOPATH to %s: %s\n " , newGopath , err .Error ())
453
+ }
454
+ log .Printf ("GOPATH set to %s.\n " , newGopath )
455
+ }
456
+
359
457
func main () {
360
458
if len (os .Args ) > 1 {
361
459
usage ()
@@ -406,101 +504,7 @@ func main() {
406
504
inLGTM := os .Getenv ("LGTM_SRC" ) != "" || os .Getenv ("LGTM_INDEX_NEED_GOPATH" ) != ""
407
505
408
506
if inLGTM && needGopath {
409
- // a temporary directory where everything is moved while the correct
410
- // directory structure is created.
411
- scratch , err := ioutil .TempDir (srcdir , "scratch" )
412
- if err != nil {
413
- log .Fatalf ("Failed to create temporary directory %s in directory %s: %s\n " ,
414
- scratch , srcdir , err .Error ())
415
- }
416
- log .Printf ("Temporary directory is %s.\n " , scratch )
417
-
418
- // move all files in `srcdir` to `scratch`
419
- dir , err := os .Open (srcdir )
420
- if err != nil {
421
- log .Fatalf ("Failed to open source directory %s for reading: %s\n " , srcdir , err .Error ())
422
- }
423
- files , err := dir .Readdirnames (- 1 )
424
- if err != nil {
425
- log .Fatalf ("Failed to read source directory %s: %s\n " , srcdir , err .Error ())
426
- }
427
- for _ , file := range files {
428
- if file != filepath .Base (scratch ) {
429
- log .Printf ("Moving %s/%s to %s/%s.\n " , srcdir , file , scratch , file )
430
- err := os .Rename (filepath .Join (srcdir , file ), filepath .Join (scratch , file ))
431
- if err != nil {
432
- log .Fatalf ("Failed to move file %s to the temporary directory: %s\n " , file , err .Error ())
433
- }
434
- }
435
- }
436
-
437
- // create a new folder which we will add to GOPATH below
438
- // Note we evaluate all symlinks here for consistency: otherwise os.Chdir below
439
- // will follow links but other references to the path may not, which can lead to
440
- // disagreements between GOPATH and the working directory.
441
- realSrc , err := filepath .EvalSymlinks (srcdir )
442
- if err != nil {
443
- log .Fatalf ("Failed to evaluate symlinks in %s: %s\n " , srcdir , err .Error ())
444
- }
445
-
446
- root := filepath .Join (realSrc , "root" )
447
-
448
- // move source files to where Go expects them to be
449
- newdir := filepath .Join (root , "src" , importpath )
450
- err = os .MkdirAll (filepath .Dir (newdir ), 0755 )
451
- if err != nil {
452
- log .Fatalf ("Failed to create directory %s: %s\n " , newdir , err .Error ())
453
- }
454
- log .Printf ("Moving %s to %s.\n " , scratch , newdir )
455
- err = os .Rename (scratch , newdir )
456
- if err != nil {
457
- log .Fatalf ("Failed to rename %s to %s: %s\n " , scratch , newdir , err .Error ())
458
- }
459
-
460
- // schedule restoring the contents of newdir to their original location after this function completes:
461
- defer restoreRepoLayout (newdir , files , filepath .Base (scratch ), srcdir )
462
-
463
- err = os .Chdir (newdir )
464
- if err != nil {
465
- log .Fatalf ("Failed to chdir into %s: %s\n " , newdir , err .Error ())
466
- }
467
-
468
- // set up SEMMLE_PATH_TRANSFORMER to ensure paths in the source archive and the snapshot
469
- // match the original source location, not the location we moved it to
470
- pt , err := ioutil .TempFile ("" , "path-transformer" )
471
- if err != nil {
472
- log .Fatalf ("Unable to create path transformer file: %s." , err .Error ())
473
- }
474
- defer os .Remove (pt .Name ())
475
- _ , err = pt .WriteString ("#" + realSrc + "\n " + newdir + "//\n " )
476
- if err != nil {
477
- log .Fatalf ("Unable to write path transformer file: %s." , err .Error ())
478
- }
479
- err = pt .Close ()
480
- if err != nil {
481
- log .Fatalf ("Unable to close path transformer file: %s." , err .Error ())
482
- }
483
- err = os .Setenv ("SEMMLE_PATH_TRANSFORMER" , pt .Name ())
484
- if err != nil {
485
- log .Fatalf ("Unable to set SEMMLE_PATH_TRANSFORMER environment variable: %s.\n " , err .Error ())
486
- }
487
-
488
- // set/extend GOPATH
489
- oldGopath := os .Getenv ("GOPATH" )
490
- var newGopath string
491
- if oldGopath != "" {
492
- newGopath = strings .Join (
493
- []string {root , oldGopath },
494
- string (os .PathListSeparator ),
495
- )
496
- } else {
497
- newGopath = root
498
- }
499
- err = os .Setenv ("GOPATH" , newGopath )
500
- if err != nil {
501
- log .Fatalf ("Unable to set GOPATH to %s: %s\n " , newGopath , err .Error ())
502
- }
503
- log .Printf ("GOPATH set to %s.\n " , newGopath )
507
+ moveToTemporaryGopath (srcdir , importpath )
504
508
}
505
509
506
510
// check whether an explicit dependency installation command was provided
0 commit comments