Skip to content

Commit 48bf4f5

Browse files
author
perrette
committed
bugfixes and test goes through for Global Install
- pre-existing local isntall now deleted when global install is performed in overwrite mode
1 parent fbe435c commit 48bf4f5

File tree

4 files changed

+93
-32
lines changed

4 files changed

+93
-32
lines changed

papers/__main__.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from papers.config import bcolors, Config, search_config, CONFIG_FILE, CONFIG_FILE_LOCAL, DATA_DIR, CONFIG_FILE_LEGACY
1919
from papers.duplicate import list_duplicates, list_uniques, edit_entries
2020
from papers.bib import Biblio, FUZZY_RATIO, DEFAULT_SIMILARITY, entry_filecheck, backupfile as backupfile_func, isvalidkey
21-
from papers.utils import move
21+
from papers.utils import move, checksum
2222
from papers import __version__
2323

2424

@@ -45,6 +45,8 @@ def get_biblio(config):
4545

4646

4747
def _backup_bib(biblio, config, message=None):
48+
if not config.git:
49+
raise PapersExit('cannot backup without --git enabled')
4850
# backupdir = Path(config.file).parent
4951
backupdir = Path(config.gitdir)
5052
backupdir.mkdir(exist_ok=True)
@@ -55,6 +57,7 @@ def _backup_bib(biblio, config, message=None):
5557

5658
## Here we could create a copy of biblio since it is modified in place
5759
## For now, we exit the program after saving, so don't bother
60+
logger.debug(f'BACKUP: cp {config.bibtex} {config.backupfile}')
5861
shutil.copy(config.bibtex, config.backupfile)
5962
config.gitcmd(f"add {config.backupfile.name}")
6063

@@ -196,6 +199,8 @@ def savebib(biblio, config):
196199
biblio.save(config.bibtex)
197200
if config.file and config.git:
198201
_backup_bib(biblio, config)
202+
else:
203+
logger.debug(f'do not backup bib: {config.file}, {config.git}')
199204
# if config.git:
200205
# config.gitcommit()
201206

@@ -239,12 +244,18 @@ def installcmd(parser, o, config):
239244
o.edit = ans.lower() == 'e'
240245

241246
if not o.edit:
247+
if config.local and os.path.exists(config.file):
248+
# if we don't do that the local install will take precedence over global install
249+
logger.warning(f"remove pre-existing local configuration file: {config.file}")
250+
os.remove(config.file)
242251
config = Config()
243252

244253
if o.local is None:
245254
if config.local is not None:
255+
logger.debug(f'keep config to pre-existing: {config.local}')
246256
o.local = config.local
247257
else:
258+
logger.debug(f'default to global config')
248259
# default ?
249260
o.local = False
250261

@@ -269,6 +280,7 @@ def installcmd(parser, o, config):
269280
workdir = Path(DATA_DIR)
270281
bibtex_files = [str(f) for f in sorted(Path('.').glob("*.bib"))] + [str(f) for f in sorted(workdir.glob("*.bib"))]
271282
checkdirs = [os.path.join(DATA_DIR, "files")] + checkdirs
283+
config.gitdir = config.data = DATA_DIR
272284

273285
if o.absolute_paths is None:
274286
o.absolute_paths = True
@@ -324,7 +336,6 @@ def installcmd(parser, o, config):
324336
config.bibtex = o.bibtex
325337
config.filesdir = o.filesdir
326338
config.file = papersconfig
327-
config.gitdir = config.data = os.path.dirname(config.file)
328339
config.local = o.local
329340
config.absolute_paths = o.absolute_paths
330341

@@ -395,11 +406,6 @@ def installcmd(parser, o, config):
395406
config.gitcmd('lfs track "files/"')
396407
config.gitcmd('add .gitattributes')
397408

398-
with open(Path(config.gitdir)/'.gitignore', 'a+') as f:
399-
lines = f.readlines()
400-
if 'futures.txt' not in (l.strip() for l in lines):
401-
f.write('futures.txt\n')
402-
config.gitcmd('add .gitignore')
403409
config.gitcmd(f'add {os.path.abspath(config.file)}')
404410
message = f'papers ' +' '.join(sys.argv[1:])
405411
config.gitcmd(f'commit -m "new install: config file"', check=False)
@@ -432,7 +438,7 @@ def uninstallcmd(parser, o, config):
432438
config.file = check_legacy_config(config.file)
433439
uninstallcmd(parser, o, config)
434440

435-
def check_install(parser, o, config):
441+
def check_install(parser, o, config, bibtex_must_exist=True):
436442
"""
437443
Given an option and config, checks to see if the install is done correctly on this filesystem.
438444
"""
@@ -450,7 +456,8 @@ def check_install(parser, o, config):
450456
parser.print_help()
451457
print(f"--bibtex must be specified, or {install_doc}")
452458
raise PapersExit()
453-
elif not os.path.exists(config.bibtex):
459+
460+
if bibtex_must_exist and not os.path.exists(config.bibtex):
454461
print(f'papers: error: no bibtex file found, do `touch {config.bibtex}` or {install_doc}')
455462
raise PapersExit()
456463
logger.info(f'bibtex: {config.bibtex!r}')

papers/config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
CACHE_HOME = os.environ.get('XDG_CACHE_HOME', os.path.join(HOME, '.cache'))
1818
DATA_HOME = os.environ.get('XDG_DATA_HOME', os.path.join(HOME, '.local','share'))
1919

20-
CONFIG_FILE_LEGACY = os.path.join(CONFIG_HOME, 'papersconfig.json')
21-
CONFIG_FILE = os.path.join(DATA_HOME, 'config.json')
2220
CONFIG_FILE_LOCAL = '.papers/config.json'
2321
DATA_DIR = os.path.join(DATA_HOME, 'papers')
22+
CONFIG_FILE = os.path.join(DATA_DIR, 'config.json')
23+
CONFIG_FILE_LEGACY = os.path.join(CONFIG_HOME, 'papersconfig.json')
2424
CACHE_DIR = os.path.join(CACHE_HOME, 'papers')
2525

2626

@@ -83,6 +83,7 @@ def root(self):
8383
return Path(os.path.sep)
8484

8585
def gitcmd(self, cmd, check=True, **kw):
86+
logger.debug(f"git add {self.backupfile.name}")
8687
return (sp.check_call if check else sp.call)(f"git {cmd}", shell=True, cwd=self.gitdir, **kw)
8788

8889

tests/common.py

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from papers import logger, logging
1616
from papers.utils import set_directory
1717
from papers.__main__ import main
18-
from papers.config import CONFIG_FILE, CONFIG_FILE_LOCAL, Config
18+
from papers.config import CONFIG_FILE, CONFIG_FILE_LOCAL, Config, DATA_DIR
1919
from papers.bib import Biblio
2020

2121
debug_level = logging.DEBUG
@@ -129,6 +129,39 @@ class BibTest(unittest.TestCase):
129129
"""base class for bib tests: create a new bibliography
130130
"""
131131

132+
@classmethod
133+
def setUpClass(cls):
134+
""" Move aside any pre-existing global config
135+
"""
136+
if os.path.exists(CONFIG_FILE):
137+
cls._backup = tempfile.mktemp(prefix='papers.bib.backup')
138+
shutil.move(CONFIG_FILE, cls._backup)
139+
else:
140+
cls._backup = None
141+
142+
if os.path.exists(DATA_DIR):
143+
cls._backup_dir = tempfile.TemporaryDirectory()
144+
shutil.move(DATA_DIR, cls._backup_dir.name)
145+
else:
146+
cls._backup_dir = None
147+
148+
@classmethod
149+
def tearDownClass(cls):
150+
""" Restore any pre-existing global config
151+
"""
152+
if os.path.exists(CONFIG_FILE):
153+
os.remove(CONFIG_FILE)
154+
if cls._backup:
155+
shutil.move(cls._backup, CONFIG_FILE)
156+
157+
if os.path.exists(DATA_DIR):
158+
shutil.rmtree(DATA_DIR)
159+
160+
if cls._backup_dir is not None:
161+
shutil.move(os.path.join(cls._backup_dir.name, os.path.basename(DATA_DIR)), DATA_DIR)
162+
cls._backup_dir.cleanup()
163+
164+
132165
def assertMultiLineEqual(self, first, second, msg=None):
133166
"""Assert that two multi-line strings are equal.
134167
@@ -181,14 +214,12 @@ class BaseTest(BibTest):
181214
anotherbib_content = bibtex
182215
initial_content = None
183216

217+
184218
def setUp(self):
185-
if os.path.exists(CONFIG_FILE):
186-
self.backup = tempfile.mktemp(prefix='papers.bib.backup')
187-
shutil.move(CONFIG_FILE, self.backup)
188-
else:
189-
self.backup = None
219+
190220

191221
self.temp_dir = tempfile.TemporaryDirectory()
222+
print(self, 'setUp', self.temp_dir.name)
192223

193224
if self.anotherbib_content is not None:
194225
open(self._path(self.anotherbib), 'w').write(self.anotherbib_content)
@@ -197,11 +228,9 @@ def setUp(self):
197228
open(self._path(self.mybib), 'w').write(self.initial_content)
198229

199230
def tearDown(self):
200-
if os.path.exists(CONFIG_FILE):
201-
os.remove(CONFIG_FILE)
202-
if self.backup:
203-
shutil.move(self.backup, CONFIG_FILE)
231+
print(self, 'cleanup', self.temp_dir.name)
204232
self.temp_dir.cleanup()
233+
assert not os.path.exists(self.temp_dir.name)
205234

206235

207236
def _path(self, p):
@@ -222,38 +251,38 @@ class LocalInstallTest(BaseTest):
222251
def setUp(self):
223252
super().setUp()
224253
self.papers(f'install --force --local --bibtex {self.mybib} --files {self.filesdir}')
225-
self.config = Config.load(self._path(CONFIG_FILE_LOCAL))
254+
255+
@property
256+
def config(self):
257+
return Config.load(self._path(CONFIG_FILE_LOCAL))
226258

227259
class GlobalInstallTest(BaseTest):
228260
def setUp(self):
229261
super().setUp()
230262
self.papers(f'install --force --bibtex {self.mybib} --files {self.filesdir}')
231-
self.config = Config.load(CONFIG_FILE)
263+
264+
@property
265+
def config(self):
266+
return Config.load(CONFIG_FILE)
232267

233268

234269
class LocalGitInstallTest(LocalInstallTest):
235270
def setUp(self):
236271
super().setUp()
237272
self.papers(f'install --force --local --git --bibtex {self.mybib} --files {self.filesdir}')
238-
self.config = Config.load(self._path(CONFIG_FILE_LOCAL))
239273

240-
241-
class GlobalGitInstallTest(LocalInstallTest):
274+
class GlobalGitInstallTest(GlobalInstallTest):
242275
def setUp(self):
243276
super().setUp()
244277
self.papers(f'install --force --git --bibtex {self.mybib} --files {self.filesdir}')
245-
self.config = Config.load(self._path(CONFIG_FILE))
246-
247278

248279
class LocalGitLFSInstallTest(LocalInstallTest):
249280
def setUp(self):
250281
super().setUp()
251282
self.papers(f'install --force --local --git --git-lfs --bibtex {self.mybib} --files {self.filesdir}')
252-
self.config = Config.load(self._path(CONFIG_FILE_LOCAL))
253283

254284

255-
class GlobalGitLFSInstallTest(LocalInstallTest):
285+
class GlobalGitLFSInstallTest(GlobalInstallTest):
256286
def setUp(self):
257287
super().setUp()
258288
self.papers(f'install --force --git --git-lfs --bibtex {self.mybib} --files {self.filesdir}')
259-
self.config = Config.load(self._path(CONFIG_FILE))

tests/test_undo.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,41 @@
1919
year = {2000}
2020
}"""
2121

22-
class TestTimeTravelGitLocal(LocalGitInstallTest):
22+
class TimeTravelBase:
2323

2424
def get_commit(self):
2525
return sp.check_output(f"git rev-parse HEAD", shell=True, cwd=self.config.gitdir).strip().decode()
2626

2727
def test_undo(self):
2828
## Make sure git undo / redo travels as expected
2929

30+
print(self.config.status(verbose=True))
31+
self.assertTrue(self.config.git)
32+
3033
commits = []
3134
commits.append(self.get_commit())
3235

3336
self.papers(f'add {self.anotherbib}')
37+
self.assertTrue(self.config.git)
3438
commits.append(self.get_commit())
39+
print("bib add paper:", self._path(self.mybib))
40+
print(open(self._path(self.mybib)).read())
41+
print("backup after add paper:", self.config.backupfile_clean)
42+
print(open(self.config.backupfile_clean).read())
3543

3644
self.papers(f'list --add-tag change')
45+
self.assertTrue(self.config.git)
3746
commits.append(self.get_commit())
47+
print("bib add-tag:", self._path(self.mybib))
48+
print(open(self._path(self.mybib)).read())
49+
print("backup after add-tag:", self.config.backupfile_clean)
50+
print(open(self.config.backupfile_clean).read())
3851

3952
# make sure we have 3 distinct commits
53+
self.config.gitcmd('log')
54+
print(commits)
55+
print(self.config.gitdir)
56+
sp.check_call(f'ls {self.config.gitdir}', shell=True)
4057
self.assertEqual(len(set(commits)), 3)
4158

4259
self.papers(f'undo')
@@ -72,6 +89,13 @@ def test_undo(self):
7289
self.assertEqual(current, commits[-1])
7390

7491

92+
class TestTimeTravelGitLocal(LocalGitInstallTest, TimeTravelBase):
93+
pass
94+
95+
class TestTimeTravelGitGlobal(GlobalGitInstallTest, TimeTravelBase):
96+
pass
97+
98+
7599

76100
class TestUndoGitLocal(LocalGitLFSInstallTest):
77101

0 commit comments

Comments
 (0)