Skip to content

Commit d77bdc3

Browse files
committed
issue 3: set APT_CONFIG and (a new) cache dir explicitely for better decoupling
from the local systems apt-configuration. This changes the default cache directory to ~/.cache/apt-repos which should be more conform to XDG
1 parent 30926e0 commit d77bdc3

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

apt_repos/__init__.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,32 @@
3737
import logging
3838
import re
3939
import json
40+
from os.path import expanduser
41+
42+
logger = logging.getLogger(__name__)
43+
44+
__defaultBaseDirs = [ expanduser('~') + '/.config/apt-repos', expanduser('~') + '/.apt-repos', '/etc/apt-repos' ]
45+
__defaultCacheDir = expanduser('~') + '/.cache/apt-repos'
46+
__aptConf = __defaultCacheDir + "/apt.conf"
47+
if not os.path.isdir(__defaultCacheDir):
48+
os.makedirs(__defaultCacheDir, exist_ok=True)
49+
with open(__aptConf, "w") as fh:
50+
print('Dir "{}";'.format(__defaultCacheDir), file=fh)
51+
os.environ["APT_CONFIG"] = __aptConf
52+
__baseDirs = __defaultBaseDirs
53+
__cacheDir = __defaultCacheDir
4054

4155
import apt_pkg
4256
import apt.progress
4357
import functools
4458

45-
from os.path import expanduser
4659
from enum import Enum
4760

4861
from apt_repos.RepoSuite import RepoSuite
4962
from apt_repos.PackageField import PackageField
5063
from apt_repos.QueryResult import QueryResult
5164
from apt_repos.Repository import Repository
5265

53-
logger = logging.getLogger(__name__)
54-
55-
__baseDirs = [ expanduser('~') + '/.config/apt-repos', expanduser('~') + '/.apt-repos', '/etc/apt-repos' ]
56-
__cacheDir = __baseDirs[0] + '/.apt-repos_cache'
57-
5866

5967
import contextlib
6068
@contextlib.contextmanager
@@ -124,14 +132,22 @@ def setAptReposBaseDir(dir):
124132
files and for the apt-repos cache that will be created in this directory
125133
named <dir>/.apt-repos_cache.
126134
'''
135+
global __defaultBaseDirs
136+
global __defaultCacheDir
127137
global __baseDirs
128138
global __cacheDir
129139
if(os.path.isdir(dir)):
130-
logger.info("Using basedir '{}'".format(os.path.relpath(dir)))
131-
__baseDirs = [ os.path.realpath(dir) ]
132-
__cacheDir = __baseDirs[0] + '/.apt-repos_cache'
140+
realDir = os.path.relpath(dir)
141+
logger.info("Using basedir '{}'".format(realDir))
142+
__baseDirs = [ realDir ]
143+
if realDir in __defaultBaseDirs:
144+
__cacheDir = __defaultCacheDir
145+
else:
146+
__cacheDir = realDir + '/.apt-repos_cache'
133147
else:
134148
raise Exception("base-directory doesn't exist: " + dir)
149+
if not os.path.isdir(__cacheDir):
150+
os.makedirs(__cacheDir, exist_ok=True)
135151

136152

137153
def __filenameWithoutPrefix(item):

0 commit comments

Comments
 (0)