Skip to content

Commit 7c4a6ae

Browse files
dmachmcepl
authored andcommitted
Migrate store_* functions to use get_store instead of the original code
That allows us to use both OBS SCM store and Git SCM store
1 parent 33d4530 commit 7c4a6ae

File tree

2 files changed

+87
-109
lines changed

2 files changed

+87
-109
lines changed

osc/obs_scm/package.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ def __write_storelist(self, name, data):
659659
except:
660660
pass
661661
else:
662-
store_write_string(self.absdir, name, '%s\n' % '\n'.join(data))
662+
store_write_string(self.absdir, name, "\n".join(data))
663663

664664
def write_conflictlist(self):
665665
self.__write_storelist('_in_conflict', self.in_conflict)

osc/obs_scm/store.py

Lines changed: 86 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,8 @@ def check_store_version(dir):
383383
with open(versionfile) as f:
384384
v = f.read().strip()
385385
except:
386-
if is_project_dir(dir):
386+
# we need to initialize store without `check` to avoid recursive calling of check_store_version()
387+
if Store(dir, check=False).is_project:
387388
v = '1.0'
388389
else:
389390
v = ''
@@ -451,203 +452,180 @@ def check_store_version(dir):
451452

452453

453454
def is_project_dir(d):
454-
global store
455+
from ..store import get_store
455456

456-
return os.path.exists(os.path.join(d, store, '_project')) and not \
457-
os.path.exists(os.path.join(d, store, '_package'))
457+
try:
458+
store = get_store(d)
459+
return store.is_project
460+
except oscerr.NoWorkingCopy:
461+
return False
458462

459463

460464
def is_package_dir(d):
461-
global store
465+
from ..store import get_store
462466

463-
return os.path.exists(os.path.join(d, store, '_project')) and \
464-
os.path.exists(os.path.join(d, store, '_package'))
467+
try:
468+
store = get_store(d)
469+
return store.is_package
470+
except oscerr.NoWorkingCopy:
471+
return False
465472

466473

467474
def read_filemeta(dir):
468-
from ..util.xml import xml_parse
475+
from ..store import get_store
469476

470-
global store
477+
store = get_store(dir)
471478

472-
msg = f'\'{dir}\' is not a valid working copy.'
473-
filesmeta = os.path.join(dir, store, '_files')
474-
if not is_package_dir(dir):
479+
store.assert_is_package()
480+
if store.exists("_scm"):
481+
msg = "Package '{store.path}' is managed via SCM"
482+
raise oscerr.NoWorkingCopy(msg)
483+
if not store.exists("_files"):
484+
msg = "Package '{store.path}' doesn't contain _files metadata"
475485
raise oscerr.NoWorkingCopy(msg)
476-
if os.path.isfile(os.path.join(dir, store, '_scm')):
477-
raise oscerr.NoWorkingCopy("Is managed via scm")
478-
if not os.path.isfile(filesmeta):
479-
raise oscerr.NoWorkingCopy(f'{msg} ({filesmeta} does not exist)')
480486

481-
try:
482-
r = xml_parse(filesmeta)
483-
except SyntaxError as e:
484-
raise oscerr.NoWorkingCopy(f'{msg}\nWhen parsing .osc/_files, the following error was encountered:\n{e}')
485-
return r
487+
return store.read_xml_node("_files", "directory")
486488

487489

488490
def store_readlist(dir, name):
489-
global store
491+
from ..store import get_store
490492

491-
r = []
492-
if os.path.exists(os.path.join(dir, store, name)):
493-
with open(os.path.join(dir, store, name)) as f:
494-
r = [line.rstrip('\n') for line in f]
495-
return r
493+
store = get_store(dir)
494+
return store.read_list(name)
496495

497496

498497
def read_tobeadded(dir):
499-
return store_readlist(dir, '_to_be_added')
498+
from ..store import get_store
499+
500+
store = get_store(dir)
501+
return store.to_be_added
500502

501503

502504
def read_tobedeleted(dir):
503-
return store_readlist(dir, '_to_be_deleted')
505+
from ..store import get_store
504506

507+
store = get_store(dir)
508+
return store.to_be_deleted
505509

506-
def read_sizelimit(dir):
507-
global store
508-
509-
r = None
510-
fname = os.path.join(dir, store, '_size_limit')
511510

512-
if os.path.exists(fname):
513-
with open(fname) as f:
514-
r = f.readline().strip()
511+
def read_sizelimit(dir):
512+
from ..store import get_store
515513

516-
if r is None or not r.isdigit():
517-
return None
518-
return int(r)
514+
store = get_store(dir)
515+
return store.size_limit
519516

520517

521518
def read_inconflict(dir):
522-
return store_readlist(dir, '_in_conflict')
519+
from ..store import get_store
520+
521+
store = get_store(dir)
522+
return store.in_conflict
523523

524524

525525
def store_read_project(dir):
526-
global store
526+
from ..store import get_store
527527

528-
try:
529-
with open(os.path.join(dir, store, '_project')) as f:
530-
p = f.readline().strip()
531-
except OSError:
532-
msg = f'Error: \'{os.path.abspath(dir)}\' is not an osc project dir or working copy'
533-
if os.path.exists(os.path.join(dir, '.svn')):
534-
msg += '\nTry svn instead of osc.'
535-
raise oscerr.NoWorkingCopy(msg)
536-
return p
528+
store = get_store(dir)
529+
return store.project
537530

538531

539532
def store_read_package(dir):
540-
global store
533+
from ..store import get_store
541534

542-
try:
543-
with open(os.path.join(dir, store, '_package')) as f:
544-
p = f.readline().strip()
545-
except OSError:
546-
msg = f'Error: \'{os.path.abspath(dir)}\' is not an osc package working copy'
547-
if os.path.exists(os.path.join(dir, '.svn')):
548-
msg += '\nTry svn instead of osc.'
549-
raise oscerr.NoWorkingCopy(msg)
550-
return p
535+
store = get_store(dir)
536+
return store.package
551537

552538

553539
def store_read_scmurl(dir):
554540
import warnings
541+
from ..store import get_store
542+
555543
warnings.warn(
556544
"osc.core.store_read_scmurl() is deprecated. "
557545
"You should be using high-level classes such as Store, Project or Package instead.",
558546
DeprecationWarning
559547
)
560-
return Store(dir).scmurl
548+
store = get_store(dir)
549+
return store.scmurl
561550

562551

563552
def store_read_apiurl(dir, defaulturl=True):
564553
import warnings
554+
from ..store import get_store
555+
565556
warnings.warn(
566557
"osc.core.store_read_apiurl() is deprecated. "
567558
"You should be using high-level classes such as Store, Project or Package instead.",
568559
DeprecationWarning
569560
)
570-
return Store(dir).apiurl
561+
store = get_store(dir)
562+
return store.apiurl
571563

572564

573565
def store_read_last_buildroot(dir):
574-
global store
566+
from ..store import get_store
575567

576-
fname = os.path.join(dir, store, '_last_buildroot')
577-
if os.path.exists(fname):
578-
lines = open(fname).read().splitlines()
579-
if len(lines) == 3:
580-
return lines
568+
store = get_store(dir)
569+
return store.last_buildroot
581570

582-
return
583571

572+
def store_write_string(dir, file, string, subdir=None):
573+
from ..store import get_store
584574

585-
def store_write_string(dir, file, string, subdir=''):
586-
from ..core import decode_it
587-
588-
global store
589-
590-
if subdir and not os.path.isdir(os.path.join(dir, store, subdir)):
591-
os.mkdir(os.path.join(dir, store, subdir))
592-
fname = os.path.join(dir, store, subdir, file)
593-
try:
594-
f = open(fname + '.new', 'w')
595-
if not isinstance(string, str):
596-
string = decode_it(string)
597-
f.write(string)
598-
f.close()
599-
os.rename(fname + '.new', fname)
600-
except:
601-
if os.path.exists(fname + '.new'):
602-
os.unlink(fname + '.new')
603-
raise
575+
store = get_store(dir)
576+
store.write_string(file, string, subdir)
604577

605578

606579
def store_write_project(dir, project):
607-
store_write_string(dir, '_project', project + '\n')
580+
from ..store import get_store
581+
582+
store = get_store(dir)
583+
store.project = project
608584

609585

610586
def store_write_apiurl(dir, apiurl):
611587
import warnings
588+
from ..store import get_store
589+
612590
warnings.warn(
613591
"osc.core.store_write_apiurl() is deprecated. "
614592
"You should be using high-level classes such as Store, Project or Package instead.",
615593
DeprecationWarning
616594
)
617-
Store(dir).apiurl = apiurl
595+
store = get_store(dir)
596+
store.apiurl = apiurl
618597

619598

620599
def store_write_last_buildroot(dir, repo, arch, vm_type):
621-
store_write_string(dir, '_last_buildroot', repo + '\n' + arch + '\n' + vm_type + '\n')
600+
from ..store import get_store
601+
602+
store = get_store(dir)
603+
store.last_buildroot = repo, arch, vm_type
622604

623605

624606
def store_unlink_file(dir, file):
625-
global store
607+
from ..store import get_store
626608

627-
try:
628-
os.unlink(os.path.join(dir, store, file))
629-
except:
630-
pass
609+
store = get_store(dir)
610+
store.unlink(file)
631611

632612

633613
def store_read_file(dir, file):
634-
global store
614+
from ..store import get_store
635615

636-
try:
637-
with open(os.path.join(dir, store, file)) as f:
638-
return f.read()
639-
except:
640-
return None
616+
store = get_store(dir)
617+
return store.read_file(file)
641618

642619

643620
def store_write_initial_packages(dir, project, subelements):
644-
global store
621+
from ..store import get_store
622+
623+
store = get_store(dir)
645624

646-
fname = os.path.join(dir, store, '_packages')
647625
root = ET.Element('project', name=project)
648-
for elem in subelements:
649-
root.append(elem)
650-
ET.ElementTree(root).write(fname)
626+
root.extend(subelements)
627+
628+
store.write_xml_node("_packages", "project", root)
651629

652630

653631
def delete_storedir(store_dir):

0 commit comments

Comments
 (0)