<commit_before># Copyright 2013 <NAME> <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from datetime import datetime

import dbm.gnu
import json
import os
import stat
import tempfile
import logging

from gletscher.aws.client import GlacierClient
from gletscher.aws.streaming import StreamingUploader
from gletscher.catalog import Catalog
from gletscher.chunker import FileChunker
from gletscher.config import BackupConfiguration
from gletscher.crypto import Crypter
from gletscher.chunk_streamer import ChunkStreamer
from gletscher.index import Index
from gletscher.kv_pack import kv_pack
from gletscher.scanner import FileScanner

logger = logging.getLogger(__name__)

def register(subparsers):
    backup_parser = subparsers.add_parser(
        "backup", help="start backing-up some directories")
    backup_parser.add_argument(
        "--catalog", help="catalog name to use", required=False, default="default")
    backup_parser.add_argument(
        "--exclude", help="files or directories to exclude", action="append", default=[])
    backup_parser.add_argument(
        "files", metavar="file", nargs="+",
        help="a set of files and directories to be backed-up")
    backup_parser.set_defaults(fn=command)


def command(args):
    config = BackupConfiguration.LoadFromFile(args.config)

    crypter = Crypter(config.secret_key())
    chunker = FileChunker(32 * 1024 * 1024)

    main_index = Index(dbm.gnu.open(config.index_file_location(), "cf", 0o600))
    global_catalog = Catalog(dbm.gnu.open(config.global_catalog_location(), "cf", 0o600))

    tmp_catalog_name = tempfile.mktemp(prefix='gletscher-tmp', dir=config.tmp_dir_location())
    tmp_catalog = Catalog(dbm.gnu.open(tmp_catalog_name, "nf", 0o600))

    glacier_client = GlacierClient.FromConfig(config)
    streaming_uploader = StreamingUploader(glacier_client)
    data_streamer = ChunkStreamer(main_index, streaming_uploader, crypter, config.uuid())

    scanner = FileScanner(
        args.files, skip_files=args.exclude + [config.config_dir_location()])
    for full_path, file_stat in scanner:
        if stat.S_ISREG(file_stat.st_mode):
            base_entry = global_catalog.find(full_path)
            if base_entry and not base_entry.has_changed(file_stat):
                # make sure we still have all the pieces in the index
                all_digests_in_index = True
                for digest in base_entry.digests():
                    if not main_index.contains(digest):
                        all_digests_in_index = False
                        break

                if all_digests_in_index:
                    # we've got this file covered
                    tmp_catalog.transfer(full_path, base_entry)<commit_msg>slight refactoring for digest availability check<commit_after># Copyright 2013 <NAME> <<EMAIL>>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from datetime import datetime

import dbm.gnu
import json
import os
import stat
import tempfile
import logging

from gletscher.aws.client import GlacierClient
from gletscher.aws.streaming import StreamingUploader
from gletscher.catalog import Catalog
from gletscher.chunker import FileChunker
from gletscher.config import BackupConfiguration
from gletscher.crypto import Crypter
from gletscher.chunk_streamer import ChunkStreamer
from gletscher.index import Index
from gletscher.kv_pack import kv_pack
from gletscher.scanner import FileScanner

logger = logging.getLogger(__name__)

def register(subparsers):
    backup_parser = subparsers.add_parser(
        "backup", help="start backing-up some directories")
    backup_parser.add_argument(
        "--catalog", help="catalog name to use", required=False, default="default")
    backup_parser.add_argument(
        "--exclude", help="files or directories to exclude", action="append", default=[])
    backup_parser.add_argument(
        "files", metavar="file", nargs="+",
        help="a set of files and directories to be backed-up")
    backup_parser.set_defaults(fn=command)


def command(args):
    config = BackupConfiguration.LoadFromFile(args.config)

    crypter = Crypter(config.secret_key())
    chunker = FileChunker(32 * 1024 * 1024)

    main_index = Index(dbm.gnu.open(config.index_file_location(), "cf", 0o600))
    global_catalog = Catalog(dbm.gnu.open(config.global_catalog_location(), "cf", 0o600))

    tmp_catalog_name = tempfile.mktemp(prefix='gletscher-tmp', dir=config.tmp_dir_location())
    tmp_catalog = Catalog(dbm.gnu.open(tmp_catalog_name, "nf", 0o600))

    glacier_client = GlacierClient.FromConfig(config)
    streaming_uploader = StreamingUploader(glacier_client)
    data_streamer = ChunkStreamer(main_index, streaming_uploader, crypter, config.uuid())

    scanner = FileScanner(
        args.files, skip_files=args.exclude + [config.config_dir_location()])
    for full_path, file_stat in scanner:
        if stat.S_ISREG(file_stat.st_mode):
            base_entry = global_catalog.find(full_path)
            if base_entry and not base_entry.has_changed(file_stat):
                # make sure we still have all the pieces in the index
                if all(main_index.contains(digest) for digest in base_entry.digests()):
                    # we've got this file covered
                    tmp_catalog.transfer(full_path, base_entry)
{'fraction_non_alphanumeric': 0.08516047028916428, 'fraction_numerical': 0.009532888465204958, 'mean_word_length': 3.883630721489527, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2617630', 'n_tokens_mistral': 1894, 'n_tokens_neox': 1800, 'n_words': 528}