77# -- Licence GPLv2
88
99import sys
10+ import os .path
1011import json
11- import re
1212import platform
1313from enum import Enum
1414from collections import OrderedDict
@@ -141,16 +141,8 @@ def __init__(
141141 project_dir_arg is None
142142 ), "project_dir_arg specified for scope None"
143143
144- # -- Determine apio home dir. Some tools get confused if the home
145- # -- dir contains spaces so we prohibit it.
144+ # -- Determine apio home dir.
146145 self .home_dir : Path = ApioContext ._get_home_dir ()
147- if re .search ("\\ s" , str (self .home_dir )):
148- secho (
149- "Error: The apio home dir path should not contain spaces." ,
150- fg = "red" ,
151- )
152- secho (f"Home dir: '{ str (self .home_dir )} '" , fg = "yellow" )
153- sys .exit (1 )
154146
155147 # -- Profile information, from ~/.apio/profile.json
156148 self .profile = Profile (self .home_dir )
@@ -408,12 +400,10 @@ def _resolve_package_envs(
408400 given packages dictionary. For example, %p is replaced with
409401 the package's absolute path."""
410402
411- for _ , package_config in packages .items ():
403+ for package_name , package_config in packages .items ():
412404
413405 # -- Get the package root dir.
414- package_path = (
415- packages_dir / package_config ["release" ]["folder_name" ]
416- )
406+ package_path = packages_dir / package_name
417407
418408 # -- Get the json 'env' section. We require it, even if empty,
419409 # -- for clarity reasons.
@@ -446,32 +436,10 @@ def get_package_info(self, package_name: str) -> str:
446436
447437 return package_info
448438
449- def get_package_folder_name (self , package_name : str ) -> str :
450- """Returns name of the package folder, within the packages dir.
451- Exits with an error message if not found."""
452-
453- package_info = self .get_package_info (package_name )
454-
455- release = package_info .get ("release" , {})
456- folder_name = release .get ("folder_name" , None )
457- if not folder_name :
458- # -- This is a programming error, not a user error
459- secho (
460- f"Error: package '{ package_name } ' definition has an "
461- "empty or missing 'folder_name' field." ,
462- fg = "red" ,
463- )
464- sys .exit (1 )
465-
466- return folder_name
467-
468439 def get_package_dir (self , package_name : str ) -> Path :
469440 """Returns the root path of a package with given name."""
470441
471- package_folder = self .get_package_folder_name (package_name )
472- package_dir = self .packages_dir / package_folder
473-
474- return package_dir
442+ return self .packages_dir / package_name
475443
476444 @staticmethod
477445 def _determine_platform_id (platforms : Dict [str , Dict ]) -> str :
@@ -589,6 +557,51 @@ def is_windows(self) -> bool:
589557 """Returns True iff platform_id indicates windows."""
590558 return "windows" in self .platform_id
591559
560+ @staticmethod
561+ def _check_home_dir (home_dir : Path ):
562+ """Check the path that was specified in APIO_HOME_DIR. Exit with an
563+ error message if it doesn't comply with apio's requirements.
564+ """
565+
566+ # Sanity check. If this fails, it's a programming error.
567+ assert isinstance (
568+ home_dir , Path
569+ ), f"Error: home_dir is no a Path: { type (home_dir )} , { home_dir } "
570+
571+ # -- The path should be abosolute, see discussion here:
572+ # -- https://github.com/FPGAwars/apio/issues/522
573+ if not home_dir .is_absolute ():
574+ secho (
575+ "Error: apio home dir should be an absolute path "
576+ f"[{ str (home_dir )} ]." ,
577+ fg = "red" ,
578+ )
579+ secho (
580+ "You can use the system env var APIO_HOME_DIR to set "
581+ "a different apio home dir." ,
582+ fg = "yellow" ,
583+ )
584+ sys .exit (1 )
585+
586+ # -- We have problem with spaces and non ascii character above value
587+ # -- 127, so we allow only ascii characters in the range [33, 127].
588+ # -- See here https://github.com/FPGAwars/apio/issues/515
589+ for ch in str (home_dir ):
590+ if ord (ch ) < 33 or ord (ch ) > 127 :
591+ secho (
592+ f"Error: Unsupported character [{ ch } ] in apio home dir: "
593+ f"[{ str (home_dir )} ]." ,
594+ fg = "red" ,
595+ )
596+ secho (
597+ "Only the ASCII characters in the range 33 to 127 are "
598+ "allowed. You can use the\n "
599+ "system env var 'APIO_HOME_DIR' to set a different apio"
600+ "home dir." ,
601+ fg = "yellow" ,
602+ )
603+ sys .exit (1 )
604+
592605 @staticmethod
593606 def _get_home_dir () -> Path :
594607 """Get the absolute apio home dir. This is the apio folder where the
@@ -609,12 +622,17 @@ def _get_home_dir() -> Path:
609622 # -- Get the home dir. It is what the APIO_HOME_DIR env variable
610623 # -- says, or the default folder if None
611624 if apio_home_dir_env :
625+ # -- Expand user home '~' marker, if exists.
626+ apio_home_dir_env = os .path .expanduser (apio_home_dir_env )
627+ # -- Expand varas such as $HOME or %HOME% on windows.
628+ apio_home_dir_env = os .path .expandvars (apio_home_dir_env )
629+ # -- Convert string to path.
612630 home_dir = Path (apio_home_dir_env )
613631 else :
614632 home_dir = Path .home () / ".apio"
615633
616- # -- Make it absolute
617- home_dir = home_dir . absolute ( )
634+ # -- Verify that the home dir meets apio's requirments.
635+ ApioContext . _check_home_dir ( home_dir )
618636
619637 # -- Create the folder if it does not exist
620638 try :
0 commit comments