mirror of
				https://github.com/krateng/maloja.git
				synced 2023-08-10 21:12:55 +03:00 
			
		
		
		
	Reworked initial determination of user folders, GH-63
This commit is contained in:
		| Can't render this file because it has a wrong number of fields in line 4. | 
| Can't render this file because it has a wrong number of fields in line 5. | 
| Can't render this file because it has a wrong number of fields in line 4. | 
| Can't render this file because it has a wrong number of fields in line 5. | 
| Can't render this file because it has a wrong number of fields in line 5. | 
| Can't render this file because it has a wrong number of fields in line 5. | 
| Can't render this file because it has a wrong number of fields in line 4. | 
| Can't render this file because it has a wrong number of fields in line 5. | 
| Can't render this file because it has a wrong number of fields in line 4. | 
| @@ -2,6 +2,15 @@ | ||||
| # Instead, simply write an entry with the same name in your own settings.ini file | ||||
| # Category headers in [brackets] are only for organization and not necessary | ||||
| 
 | ||||
| [Directories] | ||||
| # DATA_DIR determines the base directory. It should be specified as environment | ||||
| # variable because this file itself is loaded from it. | ||||
| # Configuration is always in this base directory. Other data defaults to be here | ||||
| # too, but can be customized with the options below. | ||||
| DATA_DIR_STATE = None   # This is /var/lib/maloja per XDG | ||||
| DATA_DIR_LOGS = None    # this is /var/log/maloja per XDG | ||||
| DATA_DIR_CACHE = None    # this is /var/cache/maloja per XDG | ||||
| 
 | ||||
| [HTTP] | ||||
| 
 | ||||
| WEB_PORT = 42010 | ||||
| @@ -2,41 +2,144 @@ import os | ||||
| from doreah.settings import get_settings | ||||
| from doreah.settings import config as settingsconfig | ||||
|  | ||||
| pthj = os.path.join | ||||
|  | ||||
|  | ||||
| # check environment variables for data directory | ||||
| # otherwise, go with defaults | ||||
| setting_datadir = get_settings("DATA_DIRECTORY",files=[],environ_prefix="MALOJA_") | ||||
| if setting_datadir is not None and os.path.exists(setting_datadir): | ||||
| 	DATA_DIR = setting_datadir | ||||
| else: | ||||
| 	try: | ||||
| try: | ||||
| 	HOME_DIR = os.environ["XDG_DATA_HOME"].split(":")[0] | ||||
| 	assert os.path.exists(HOME_DIR) | ||||
| 	except: | ||||
| except: | ||||
| 	HOME_DIR = os.path.join(os.environ["HOME"],".local/share/") | ||||
|  | ||||
| 	OLD_DATA_DIR = os.path.join(HOME_DIR,"maloja") | ||||
| 	NEW_DATA_DIR = "/etc/maloja" | ||||
|  | ||||
| 	if os.path.exists(OLD_DATA_DIR): | ||||
| 		DATA_DIR = OLD_DATA_DIR | ||||
| 	else: | ||||
| # if DATA_DIRECTORY is specified, this is the directory to use for EVERYTHING, no matter what | ||||
| # but with asynnetrical structure, cache and logs in subfolders | ||||
| # otherwise, each directory is treated seperately | ||||
| # in that case, individual settings for each are respected | ||||
| # DIRECRORY_CONFIG, DIRECRORY_STATE, DIRECTORY_LOGS and DIRECTORY_CACHE | ||||
| # config can only be determined by environment variable, the others can be loaded | ||||
| # from the config files | ||||
| # explicit settings will always be respected. if there are none: | ||||
| # first check if there is any indication of one of the possibilities being populated already | ||||
| # if not, use the first we have permissions for | ||||
| # after we decide which to use, fix it in settings to avoid future heuristics | ||||
|  | ||||
|  | ||||
| usrfol = pthj(HOME_DIR,"maloja") | ||||
| etccfg = '/etc/maloja' | ||||
| varlib = '/var/lib/maloja' | ||||
| varcac = '/var/cache/maloja' | ||||
| varlog = '/var/log/maloja' | ||||
|  | ||||
| dir_settings = { | ||||
| 	"config":None, | ||||
| 	"state":None, | ||||
| 	"logs":None, | ||||
| 	"cache":None, | ||||
| #	"clients":None, | ||||
| #	"rules":None, | ||||
| #	"settings":None, | ||||
| #	"auth":None, | ||||
| #	"backups":None, | ||||
| #	"images":None, | ||||
| #	"scrobbles":None, | ||||
| #	"logs":None, | ||||
| #	"cache":None | ||||
| } | ||||
|  | ||||
| dir_options = { | ||||
| 	"config":[ | ||||
| 		"/etc/maloja", | ||||
| 		pthj(HOME_DIR,"maloja") | ||||
| 	], | ||||
| 	"state":[ | ||||
| 		"/var/lib/maloja", | ||||
| 		"/etc/maloja", | ||||
| 		pthj(HOME_DIR,"maloja") | ||||
| 	], | ||||
| 	"logs":[ | ||||
| 		"/var/log/maloja", | ||||
| 		"/etc/maloja/logs", | ||||
| 		pthj(HOME_DIR,"maloja","logs") | ||||
| 	], | ||||
| 	"cache":[ | ||||
| 		"/var/cache/maloja", | ||||
| 		"/etc/maloja/cache", | ||||
| 		pthj(HOME_DIR,"maloja","cache") | ||||
| 	] | ||||
| } | ||||
|  | ||||
| sentinels = { | ||||
| 	"config":"settings", | ||||
| 	"state":"scrobbles", | ||||
| 	"logs":None, | ||||
| 	"cache":None, | ||||
| } | ||||
|  | ||||
| # check environ variables | ||||
| stng_data = get_settings("DATA_DIRECTORY",files=[],environ_prefix="MALOJA_") | ||||
| if stng_data is not None: | ||||
| 	dir_settings['config'] = stng_data | ||||
| 	dir_settings['state'] = stng_data | ||||
| 	dir_settings['cache'] = pthj(stng_data,'cache') | ||||
| 	dir_settings['logs'] = pthj(stng_data,'logs') | ||||
| else: | ||||
| 	dir_settings['config'], dir_settings['state'], dir_settings['cache'], dir_settings['logs'] = get_settings("DIRECTORY_CONFIG","DIRECTORY_STATE","DIRECTORY_LOGS","DIRECTORY_CACHE",files=[],environ_prefix="MALOJA_") | ||||
| 	# as soon as we know the config directory, we can load from settings file | ||||
| 	if dir_settings['config'] is not None: | ||||
| 		settingsfiles = [pthj(dir_settings['config'],'settings','default.ini'),pthj(dir_settings['config'],'settings','settings.ini')] | ||||
| 		dir_settings['config'], dir_settings['state'], dir_settings['cache'], dir_settings['logs'] = get_settings("DIRECTORY_CONFIG","DIRECTORY_STATE","DIRECTORY_LOGS","DIRECTORY_CACHE",files=settingsfiles,environ_prefix="MALOJA_") | ||||
|  | ||||
|  | ||||
| # now to the stuff no setting has explicitly defined | ||||
| for dirtype in dir_settings: | ||||
| 	if dir_settings[dirtype] is None: | ||||
| 		for option in dir_options[dirtype]: | ||||
| 			if os.path.exists(option): | ||||
| 				# check if this is really the directory used for this category (/etc/maloja could be used for state or just config) | ||||
| 				if sentinels[dirtype] is None or os.path.exists(pthj(option,sentinels[dirtype])): | ||||
| 					dir_settings[dirtype] = option | ||||
| 					break | ||||
|  | ||||
| # if no directory seems to exist, use the first writable one | ||||
| for dirtype in dir_settings: | ||||
| 	if dir_settings[dirtype] is None: | ||||
| 		for option in dir_options[dirtype]: | ||||
| 			try: | ||||
| 			#check if we have permissions | ||||
| 			os.makedirs(NEW_DATA_DIR,exist_ok=True) | ||||
| 			os.mknod(os.path.join(NEW_DATA_DIR,".test")) | ||||
| 			os.remove(os.path.join(NEW_DATA_DIR,".test")) | ||||
| 			DATA_DIR = NEW_DATA_DIR | ||||
| 				os.makedirs(option,exist_ok=True) | ||||
| 				os.mknod(pthj(option,".test")) | ||||
| 				os.remove(pthj(option,".test")) | ||||
| 				dir_settings[dirtype] = option | ||||
| 				break | ||||
| 			except: | ||||
| 			DATA_DIR = OLD_DATA_DIR | ||||
|  | ||||
| os.makedirs(DATA_DIR,exist_ok=True) | ||||
| 				pass | ||||
|  | ||||
|  | ||||
| print("") | ||||
| print("") | ||||
| print(dir_settings) | ||||
| print("") | ||||
| print("") | ||||
|  | ||||
| def datadir(*args): | ||||
| 	return os.path.join(DATA_DIR,*args) | ||||
| if any((dir_settings[s] is None) for s in dir_settings): | ||||
| 	print("NOT ALL DIRECTORIES SET!") | ||||
|  | ||||
|  | ||||
| data_directories = { | ||||
| 	"auth":pthj(dir_settings['state'],"auth"), | ||||
| 	"backups":pthj(dir_settings['state'],"backups"), | ||||
| 	"images":pthj(dir_settings['state'],"images"), | ||||
| 	"scrobbles":pthj(dir_settings['state'],"scrobbles"), | ||||
| 	"rules":pthj(dir_settings['config'],"rules"), | ||||
| 	"clients":pthj(dir_settings['config'],"clients"), | ||||
| 	"settings":pthj(dir_settings['config'],"settings"), | ||||
| 	"logs":pthj(dir_settings['logs']), | ||||
| 	"cache":pthj(dir_settings['cache']), | ||||
| } | ||||
|  | ||||
|  | ||||
| data_dir = { | ||||
| 	k:lambda *x: pthj(data_directories[k],*x)  for k in data_directories | ||||
| } | ||||
|  | ||||
|  | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Krateng
					Krateng