Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/svn/svn.py
# Copyright (C) 2015-2018 The Software Heritage developers | # Copyright (C) 2015-2018 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
"""SVN client in charge of iterating over svn logs and yield commit | """SVN client in charge of iterating over svn logs and yield commit | ||||
representations including the hash tree/content computations per svn | representations including the hash tree/content computations per svn | ||||
commit. | commit. | ||||
""" | """ | ||||
import logging | |||||
import os | import os | ||||
import tempfile | import tempfile | ||||
import shutil | import shutil | ||||
from subvertpy.ra import RemoteAccess, Auth, get_username_provider | from subvertpy.ra import RemoteAccess, Auth, get_username_provider | ||||
from subvertpy import client, properties | from subvertpy import client, properties | ||||
from swh.model.from_disk import Directory | from swh.model.from_disk import Directory | ||||
from . import ra, converters | from . import ra, converters | ||||
# When log message contains empty data | # When log message contains empty data | ||||
DEFAULT_AUTHOR_MESSAGE = '' | DEFAULT_AUTHOR_MESSAGE = '' | ||||
class SWHSvnRepo: | class SvnRepo: | ||||
"""SWH's svn repository representation. | """Svn repository representation. | ||||
Args: | Args: | ||||
remote_url (str): | remote_url (str): | ||||
origin_id (int): Associated origin identifier | origin_id (int): Associated origin identifier | ||||
storage (Storage): Storage to use to execute storage statements | |||||
local_dirname (str): Path to write intermediary svn action results | local_dirname (str): Path to write intermediary svn action results | ||||
""" | """ | ||||
def __init__(self, remote_url, origin_id, storage, local_dirname): | def __init__(self, remote_url, origin_id, local_dirname): | ||||
self.remote_url = remote_url.rstrip('/') | self.remote_url = remote_url.rstrip('/') | ||||
self.storage = storage | |||||
self.origin_id = origin_id | self.origin_id = origin_id | ||||
auth = Auth([get_username_provider()]) | auth = Auth([get_username_provider()]) | ||||
# one connection for log iteration | # one connection for log iteration | ||||
self.conn_log = RemoteAccess(self.remote_url, auth=auth) | self.conn_log = RemoteAccess(self.remote_url, auth=auth) | ||||
# another for replay | # another for replay | ||||
self.conn = RemoteAccess(self.remote_url, auth=auth) | self.conn = RemoteAccess(self.remote_url, auth=auth) | ||||
# one client for update operation | # one client for update operation | ||||
self.client = client.Client(auth=auth) | self.client = client.Client(auth=auth) | ||||
self.local_dirname = local_dirname | self.local_dirname = local_dirname | ||||
local_name = os.path.basename(self.remote_url) | local_name = os.path.basename(self.remote_url) | ||||
self.local_url = os.path.join(self.local_dirname, local_name).encode( | self.local_url = os.path.join(self.local_dirname, local_name).encode( | ||||
'utf-8') | 'utf-8') | ||||
self.uuid = self.conn.get_uuid().encode('utf-8') | self.uuid = self.conn.get_uuid().encode('utf-8') | ||||
self.swhreplay = ra.SWHReplay(conn=self.conn, rootpath=self.local_url) | self.swhreplay = ra.Replay(conn=self.conn, rootpath=self.local_url) | ||||
def __str__(self): | def __str__(self): | ||||
return str({ | return str({ | ||||
'swh-origin': self.origin_id, | 'swh-origin': self.origin_id, | ||||
'remote_url': self.remote_url, | 'remote_url': self.remote_url, | ||||
'local_url': self.local_url, | 'local_url': self.local_url, | ||||
'uuid': self.uuid, | 'uuid': self.uuid, | ||||
}) | }) | ||||
▲ Show 20 Lines • Show All 172 Lines • ▼ Show 20 Lines | def swh_hash_data_at_revision(self, revision): | ||||
""" | """ | ||||
# Update the disk at revision | # Update the disk at revision | ||||
self.export(revision) | self.export(revision) | ||||
# Compute the current hashes on disk | # Compute the current hashes on disk | ||||
directory = Directory.from_disk(path=os.fsencode(self.local_url), | directory = Directory.from_disk(path=os.fsencode(self.local_url), | ||||
save_path=True) | save_path=True) | ||||
# Update the replay collaborator with the right state | # Update the replay collaborator with the right state | ||||
self.swhreplay = ra.SWHReplay( | self.swhreplay = ra.Replay( | ||||
conn=self.conn, | conn=self.conn, | ||||
rootpath=self.local_url, | rootpath=self.local_url, | ||||
directory=directory) | directory=directory) | ||||
# Retrieve the commit information for revision | # Retrieve the commit information for revision | ||||
commit = list(self.logs(revision, revision))[0] | commit = list(self.logs(revision, revision))[0] | ||||
yield revision, revision + 1, commit, {}, directory | yield revision, revision + 1, commit, {}, directory | ||||
def clean_fs(self, local_dirname=None): | def clean_fs(self, local_dirname=None): | ||||
"""Clean up the local working copy. | """Clean up the local working copy. | ||||
Args: | Args: | ||||
local_dirname (str): Path to remove recursively if | local_dirname (str): Path to remove recursively if | ||||
provided. Otherwise, remove the temporary upper root tree | provided. Otherwise, remove the temporary upper root tree | ||||
used for svn repository loading. | used for svn repository loading. | ||||
""" | """ | ||||
if local_dirname: | dirname = local_dirname if local_dirname else self.local_dirname | ||||
shutil.rmtree(local_dirname) | if os.path.exists(dirname): | ||||
else: | logging.debug('cleanup %s' % dirname) | ||||
shutil.rmtree(self.local_dirname) | shutil.rmtree(dirname) |