From 82a5bcc9876a1c7d6ac4999029956212580fd7af Mon Sep 17 00:00:00 2001 From: Angus Trau Date: Mon, 16 Aug 2021 22:16:20 +1000 Subject: [PATCH 1/2] Update flake --- flake.lock | 27 +++++---------------------- flake.nix | 12 +++++------- nix/linux.nix | 2 +- 3 files changed, 11 insertions(+), 30 deletions(-) diff --git a/flake.lock b/flake.lock index f2ea7624..bffbb6f3 100644 --- a/flake.lock +++ b/flake.lock @@ -40,16 +40,16 @@ }, "nixpkgs": { "locked": { - "lastModified": 1622516815, - "narHash": "sha256-ZjBd81a6J3TwtlBr3rHsZspYUwT9OdhDk+a/SgSEf7I=", + "lastModified": 1628696641, + "narHash": "sha256-4Qb0Rpx8kjnPxNJiw15CmmV9hfiyvzlUzGcOn8bXKXQ=", "owner": "nixos", "repo": "nixpkgs", - "rev": "7e9b0dff974c89e070da1ad85713ff3c20b0ca97", + "rev": "10bb036486138341e4c760c4c98ddadef56fff7b", "type": "github" }, "original": { "owner": "nixos", - "ref": "21.05", + "ref": "nixpkgs-unstable", "repo": "nixpkgs", "type": "github" } @@ -58,24 +58,7 @@ "inputs": { "flake-utils": "flake-utils", "mindpile": "mindpile", - "nixpkgs": "nixpkgs", - "unstable": "unstable" - } - }, - "unstable": { - "locked": { - "lastModified": 1627148163, - "narHash": "sha256-3FXDUlHtdRW9ajQ4NqcfZHx6IYnxQluhB+06pxE4m1k=", - "owner": "nixos", - "repo": "nixpkgs", - "rev": "9aec01027f7ea2bca07bb51d5ed83e78088871c1", - "type": "github" - }, - "original": { - "owner": "nixos", - "ref": "nixpkgs-unstable", - "repo": "nixpkgs", - "type": "github" + "nixpkgs": "nixpkgs" } } }, diff --git a/flake.nix b/flake.nix index 232c842c..7e5e4c5c 100644 --- a/flake.nix +++ b/flake.nix @@ -1,24 +1,22 @@ { - description = "A simulator for soccer robots programmed with ev3dev."; + description = "A simulator for soccer robots programmed with ev3dev"; - inputs.nixpkgs.url = "github:nixos/nixpkgs/21.05"; # On unstable to use some new python packages. Eventually will be merged into Nix 21.11 - inputs.unstable.url = "github:nixos/nixpkgs/nixpkgs-unstable"; + inputs.nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; inputs.flake-utils.url = "github:numtide/flake-utils"; inputs.mindpile.url = "github:MelbourneHighSchoolRobotics/mindpile"; inputs.mindpile.inputs.nixpkgs.follows = "nixpkgs"; inputs.mindpile.inputs.flake-utils.follows = "flake-utils"; - outputs = { nixpkgs, unstable, flake-utils, mindpile, ... }: + outputs = { nixpkgs, flake-utils, mindpile, ... }: flake-utils.lib.eachSystem [ "x86_64-linux" ] (system: let pkgs = nixpkgs.legacyPackages.${system}; - unstablePkgs = unstable.legacyPackages.${system}; + mp = mindpile.legacyPackages.${system}; in rec { packages = { linux = pkgs.callPackage ./nix/linux.nix { - inherit (unstablePkgs.python39Packages) ev3dev2 opensimplex pymunk pygame pygame-gui; - mindpile = mindpile.legacyPackages.${system}.mindpile; + inherit (mp) mindpile; }; windows = pkgs.callPackage ./nix/windows-installer.nix { }; diff --git a/nix/linux.nix b/nix/linux.nix index 2253e251..032e702a 100644 --- a/nix/linux.nix +++ b/nix/linux.nix @@ -1,4 +1,4 @@ -{ pkgs, pymunk, ev3dev2, opensimplex, pygame, pygame-gui, mindpile, ... }: +{ pkgs, mindpile, ... }: pkgs.python39Packages.buildPythonPackage (let version = pkgs.callPackage ./version.nix { }; in { From 22f514d819c534a4c593004813df79630e5533c3 Mon Sep 17 00:00:00 2001 From: Angus Trau Date: Mon, 16 Aug 2021 22:17:13 +1000 Subject: [PATCH 2/2] Add platform folders to search paths --- ev3sim/entry.py | 20 +++++++++++++++----- ev3sim/file_helper.py | 35 ++++++++++++++++++++++++++++++++++- ev3sim/search_locations.py | 18 ++---------------- 3 files changed, 51 insertions(+), 22 deletions(-) diff --git a/ev3sim/entry.py b/ev3sim/entry.py index 36cf55a8..a1d183cd 100644 --- a/ev3sim/entry.py +++ b/ev3sim/entry.py @@ -7,7 +7,7 @@ from ev3sim import __version__ from ev3sim.file_helper import WorkspaceError, find_abs, find_abs_directory, make_relative -from ev3sim.search_locations import bot_locations, config_locations, preset_locations +from ev3sim.search_locations import bot_locations, config_locations, preset_locations, workspace_locations from ev3sim.simulation.loader import StateHandler from ev3sim.updates import handle_updates from ev3sim.utils import canUpdate @@ -199,7 +199,7 @@ def main(passed_args=None): args.__dict__.update(passed_args) # Useful for a few things. - ev3sim_folder = dirname(abspath(__file__)) + ev3sim_folder = find_abs_directory(config_locations()) # Step 1: Handling helper commands @@ -219,8 +219,18 @@ def main(passed_args=None): # Step 2: Safely load configs and set up error reporting try: - # This should always exist. - conf_file = find_abs("user_config.yaml", allowed_areas=config_locations()) + try: + conf_file = find_abs("user_config.yaml", allowed_areas=config_locations()) + except ValueError: + # Config file can't be found + # Can happen when this is a fresh install + import shutil + + conf_dir = find_abs_directory(config_locations()) + default_conf_file = find_abs("default_config.yaml", allowed_areas=["package/presets/"]) + conf_file = join(conf_dir, "user_config.yaml") + shutil.copyfile(default_conf_file, conf_file) + with open(conf_file, "r") as f: conf = yaml.safe_load(f) @@ -232,7 +242,7 @@ def main(passed_args=None): handler.setConfig( **{ "app": { - "workspace_folder": find_abs_directory("package/workspace", create=True), + "workspace_folder": find_abs_directory(workspace_locations(), create=True), } } ) diff --git a/ev3sim/file_helper.py b/ev3sim/file_helper.py index ee0c0b45..d0625e4f 100644 --- a/ev3sim/file_helper.py +++ b/ev3sim/file_helper.py @@ -1,4 +1,6 @@ import os +import platform +from pathlib import Path ROOT = os.path.abspath(os.path.dirname(__file__)) @@ -7,6 +9,22 @@ class WorkspaceError(Exception): pass +def find_platform_location(dir_type): + """ + Attempt to find and create operating system data folders + """ + if platform.system() == "Linux": + home_dir = os.path.expanduser("~") + if dir_type == "config": + xdg_dir = os.environ.get("XDG_CONFIG_HOME") or os.path.join(home_dir, ".config") + elif dir_type == "data": + xdg_dir = os.environ.get("XDG_DATA_HOME") or os.path.join(home_dir, ".local/share") + ev3sim_dir = os.path.join(xdg_dir, "ev3sim") + Path(ev3sim_dir).mkdir(parents=True, exist_ok=True) + return ev3sim_dir + return None + + def find_abs(filepath, allowed_areas=None): """ Attempt to find a reference file, from this list of appropriate places specified. @@ -51,6 +69,17 @@ def find_abs(filepath, allowed_areas=None): if not WORKSPACE: continue path = os.path.join(WORKSPACE, area[10:], filepath) + elif area.startswith("platform"): + if area.startswith("platform/config"): + pl = find_platform_location("config") + if pl is None: + continue + path = os.path.join(pl, area[16:], filepath) + elif area.startswith("platform/data"): + pl = find_platform_location("data") + if pl is None: + continue + path = os.path.join(pl, area[14:], filepath) elif area == "local": path = filepath elif area.startswith("local"): @@ -91,12 +120,16 @@ def fix(): def find_abs_directory(dirpath, create=True): try: - return find_abs("", allowed_areas=[dirpath]) + if not isinstance(dirpath, list): + dirpath = [dirpath] + return find_abs("", allowed_areas=dirpath) except ValueError as e: if not create: raise e else: # Remove one part of the directory, then try again. + if isinstance(dirpath, list): + dirpath = dirpath[0] rest, single = os.path.split(dirpath.rstrip("/")) if rest == dirpath: raise ValueError(f"Find abs dir failed with input {dirpath}") diff --git a/ev3sim/search_locations.py b/ev3sim/search_locations.py index 59a3f382..eeaaad94 100644 --- a/ev3sim/search_locations.py +++ b/ev3sim/search_locations.py @@ -1,23 +1,9 @@ preset_locations = lambda: ["workspace/presets/", "workspace", "package/presets/"] +config_locations = lambda: ["workspace", "platform/config", "package"] device_locations = lambda: ["workspace/devices/", "package/devices/"] theme_locations = lambda: ["workspace/assets/", "workspace", "package/assets"] asset_locations = lambda: ["workspace/assets/", "workspace", "package/assets/"] - - -def config_locations(): - import os - import platform - from pathlib import Path - - if platform.system() == "Linux": - home_dir = os.path.expanduser("~") - xdg_config_dir = os.environ.get("XDG_CONFIG_HOME") or os.path.join(home_dir, ".config") - config_dir = os.path.join(xdg_config_dir, "ev3sim") - # Ensure config dir exists - Path(config_dir).mkdir(parents=True, exist_ok=True) - return ["workspace", "local|" + config_dir] - - return ["workspace", "package"] +workspace_locations = lambda: ["platform/data/workspace/", "package/workspace/"] def code_locations(bot_path):