バックアップをとろう(その6)

前回の続きです。Partitionクラスのmount, umountを実装します。

class Partition():
    def mount(self):
        """ マウントする """
        # devnodeが解らなければ、マウントしようがない
        if not self._attr['devnode']:
            # fixme 例外を送出した方が良いかも
            return

        # 既にマウントされているnilfs2パーティション
        # では、nilfs2のスナップショット機能を利用する
        if self._attr['fstype'] == 'nilfs2' and self._attr['is_mt']:
            self._attr['use_nilfs2_snapshot'] = True

            # 直近のチェックポイントを変数check_pointに格納します
            lscp_result = Popen(('/usr/bin/lscp', '-r', '-n1', \
                    self._attr['devnode']),stdout=PIPE).communicate()[0]
            check_point = lscp_result.split()[7]

            # nilfs2のsnapshotを作成します
            exec_command('cp2ss', [self._attr['devnode'], check_point])

            # snapshotをマウントする為のオプションを設定します
            self._attr['extraparam'].extend(("-o", "ro,cp="+check_point))

        # 一時的なディレクトリの作成が必要なのは、
        # 1.マウントされていなくてマウントポイントがない場合
        # 2.nilfs2スナップショットを利用する場合
        if not (self._attr['is_mt'] or self._attr['mtpt']) \
                or self._attr['use_nilfs2_snapshot']:
            self._attr['mtpt'] = mkdtemp()
            self._attr['is_createdir'] = True

        # 必要ならばマウントコマンドを実行する
        if not self._attr['is_mt'] or self._attr['use_nilfs2_snapshot']:
            # 渡すリストが長いので、整形
            ext = self._attr['extraparam'] + [\
                    self._attr['devnode'], \
                    self._attr['mtpt']]
            if exec_command('mount', ext) == 0:
                # mountコマンドが成功した場合、フラグを建てておく
                self._attr['is_mt'] = True


    def umount(self):
        """ アンマウントする """
        # アンマウントしなければいけないのは、
        # 1.オートマウントでないのにマウントされたパーティション
        # 2.nilfs2のスナップショットを利用したパーティション
        if (not self._attr['is_automount'] and self._attr['is_mt']) \
                or self._attr['use_nilfs2_snapshot']:

            # umountが成功した場合は、is_mtフラグを下げる
            if  exec_command('umount', [self._attr['mtpt']]) == 0:
                self._attr['is_mt'] = False

                # ディレクトリを作成した場合は、消しておく
                if self._attr['is_createdir']:
                    removedirs(self._attr['mtpt'])
                    self._attr['mtpt'] = None
                    self._attr['is_createdir'] = False

                    # nilfs2スナップショット利用の場合の後始末
                    if self._attr['use_nilfs2_snapshot']:
                        self._load_local_settings(self._attr['devnode'])
                        self._attr['use_nilfs2_snapshot'] = False

なんだか条件分岐が複雑なので、これを簡単にする方法を考えます