Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/svn/utils.py
Show All 27 Lines | if strdate: | ||||
'seconds': int(dt.timestamp()), | 'seconds': int(dt.timestamp()), | ||||
'microseconds': dt.microsecond, | 'microseconds': dt.microsecond, | ||||
} | } | ||||
else: # epoch | else: # epoch | ||||
ts = {'seconds': 0, 'microseconds': 0} | ts = {'seconds': 0, 'microseconds': 0} | ||||
return ts | return ts | ||||
def init_svn_repo_from_archive_dump(archive_path, prefix=None, suffix=None, | def init_svn_repo_from_dump(dump_path, prefix=None, suffix=None, | ||||
root_dir='/tmp'): | root_dir='/tmp', gzip=False): | ||||
"""Given a path to an archive containing an svn dump. | """Given a path to a svn dump. | ||||
Initialize an svn repository with the content of said dump. | Initialize an svn repository with the content of said dump. | ||||
Returns: | Returns: | ||||
A tuple: | A tuple: | ||||
- temporary folder (str): containing the mounted repository | - temporary folder (str): containing the mounted repository | ||||
- repo_path (str): path to the mounted repository inside the | - repo_path (str): path to the mounted repository inside the | ||||
temporary folder | temporary folder | ||||
Raises: | Raises: | ||||
ValueError in case of failure to run the command to uncompress | ValueError in case of failure to run the command to uncompress | ||||
and load the dump. | and load the dump. | ||||
""" | """ | ||||
project_name = os.path.basename(os.path.dirname(archive_path)) | project_name = os.path.basename(os.path.dirname(dump_path)) | ||||
temp_dir = tempfile.mkdtemp(prefix=prefix, suffix=suffix, dir=root_dir) | temp_dir = tempfile.mkdtemp(prefix=prefix, suffix=suffix, dir=root_dir) | ||||
try: | try: | ||||
repo_path = os.path.join(temp_dir, project_name) | repo_path = os.path.join(temp_dir, project_name) | ||||
# create the repository that will be loaded with the dump | # create the repository that will be loaded with the dump | ||||
cmd = ['svnadmin', 'create', repo_path] | cmd = ['svnadmin', 'create', repo_path] | ||||
r = call(cmd) | r = call(cmd) | ||||
if r != 0: | if r != 0: | ||||
raise ValueError( | raise ValueError( | ||||
'Failed to initialize empty svn repo for %s' % | 'Failed to initialize empty svn repo for %s' % | ||||
project_name) | project_name) | ||||
with Popen(['gzip', '-dc', archive_path], stdout=PIPE) as dump: | read_dump_cmd = ['cat', dump_path] | ||||
if gzip: | |||||
read_dump_cmd = ['gzip', '-dc', dump_path] | |||||
with Popen(read_dump_cmd, stdout=PIPE) as dump: | |||||
cmd = ['svnadmin', 'load', '-q', repo_path] | cmd = ['svnadmin', 'load', '-q', repo_path] | ||||
r = call(cmd, stdin=dump.stdout) | r = call(cmd, stdin=dump.stdout) | ||||
if r != 0: | if r != 0: | ||||
raise ValueError( | raise ValueError( | ||||
'Failed to mount the svn dump for project %s' % | 'Failed to mount the svn dump for project %s' % | ||||
project_name) | project_name) | ||||
return temp_dir, repo_path | return temp_dir, repo_path | ||||
except Exception as e: | except Exception as e: | ||||
shutil.rmtree(temp_dir) | shutil.rmtree(temp_dir) | ||||
raise e | raise e | ||||
def init_svn_repo_from_archive_dump(archive_path, prefix=None, suffix=None, | |||||
root_dir='/tmp'): | |||||
"""Given a path to an archive containing an svn dump. | |||||
Initialize an svn repository with the content of said dump. | |||||
Returns: | |||||
A tuple: | |||||
- temporary folder (str): containing the mounted repository | |||||
- repo_path (str): path to the mounted repository inside the | |||||
temporary folder | |||||
Raises: | |||||
ValueError in case of failure to run the command to uncompress | |||||
and load the dump. | |||||
""" | |||||
return init_svn_repo_from_dump(archive_path, prefix=prefix, suffix=suffix, | |||||
root_dir=root_dir, gzip=True) |