Let users customize the path to the scouting database
I want to write a script that runs the scouting webserver. For that to
work elegantly, I need to be able to customize the database path.
Otherwise it gets created in the runfiles tree.
Signed-off-by: Philipp Schrader <philipp.schrader@gmail.com>
Change-Id: I6f06f9b82ca2d3476c76d795ad57b06f7b360ba8
diff --git a/scouting/webserver/main.go b/scouting/webserver/main.go
index 668104b..3c699fd 100644
--- a/scouting/webserver/main.go
+++ b/scouting/webserver/main.go
@@ -3,9 +3,11 @@
import (
"flag"
"fmt"
+ "io/ioutil"
"log"
"os"
"os/signal"
+ "path/filepath"
"syscall"
"github.com/frc971/971-Robot-Code/scouting/db"
@@ -14,12 +16,33 @@
"github.com/frc971/971-Robot-Code/scouting/webserver/static"
)
+func getDefaultDatabasePath() string {
+ // If using `bazel run`, let's create the database in the root of the
+ // workspace.
+ workspaceDir := os.Getenv("BUILD_WORKSPACE_DIRECTORY")
+ if workspaceDir != "" {
+ return filepath.Join(workspaceDir, "scouting.db")
+ }
+ // If we're inside a `bazel test`, then we will create the database in
+ // a temporary directory.
+ testTempDir := os.Getenv("TEST_TMPDIR")
+ if testTempDir != "" {
+ tempDir, err := ioutil.TempDir(testTempDir, "db")
+ if err != nil {
+ log.Fatal("Failed to create temporary directory in TEST_TMPDIR: ", err)
+ }
+ return filepath.Join(tempDir, "scouting.db")
+ }
+ return filepath.Join(".", "scouting.db")
+}
+
func main() {
portPtr := flag.Int("port", 8080, "The port number to bind to.")
dirPtr := flag.String("directory", ".", "The directory to serve at /.")
+ dbPathPtr := flag.String("database", getDefaultDatabasePath(), "The path to the database.")
flag.Parse()
- database, err := db.NewDatabase()
+ database, err := db.NewDatabase(*dbPathPtr)
if err != nil {
log.Fatal("Failed to connect to database: ", err)
}
diff --git a/scouting/webserver/requests/debug/cli/cli_test.py b/scouting/webserver/requests/debug/cli/cli_test.py
index 925cb99..682e179 100644
--- a/scouting/webserver/requests/debug/cli/cli_test.py
+++ b/scouting/webserver/requests/debug/cli/cli_test.py
@@ -31,12 +31,7 @@
class TestDebugCli(unittest.TestCase):
def setUp(self):
- # Since the webserver creates a database in the current directory,
- # let's run the test in TEST_TMPDIR where we can do whatever we want.
- self.webserver_working_dir = Path(os.environ["TEST_TMPDIR"]) / "webserver"
- os.mkdir(self.webserver_working_dir)
- webserver_path = os.path.abspath("scouting/webserver/webserver_/webserver")
- self.webserver = subprocess.Popen([webserver_path], cwd=self.webserver_working_dir)
+ self.webserver = subprocess.Popen(["scouting/webserver/webserver_/webserver"])
# Wait for the server to respond to requests.
while True:
@@ -52,7 +47,6 @@
def tearDown(self):
self.webserver.terminate()
self.webserver.wait()
- shutil.rmtree(self.webserver_working_dir)
def test_submit_data_scouting(self):
json_path = write_json({