hgext/infinitepush/store.py
author Joerg Sonnenberger <joerg@bec.de>
Tue, 01 Dec 2020 21:54:46 +0100
changeset 46113 59fa3890d40a
parent 45211 bfc6e75c0114
child 48875 6000f5b25c9b
permissions -rw-r--r--
node: import symbols explicitly There is no point in lazy importing mercurial.node, it is used all over the place anyway. So consistently import the used symbols directly. Fix one file using symbols indirectly via mercurial.revlog. Differential Revision: https://phab.mercurial-scm.org/D9480
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     1
# This software may be used and distributed according to the terms of the
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     2
# GNU General Public License version 2 or any later version.
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     3
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     4
# based on bundleheads extension by Gregory Szorc <gps@mozilla.com>
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     5
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     6
from __future__ import absolute_import
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     7
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     8
import abc
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     9
import os
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    10
import subprocess
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    11
46113
59fa3890d40a node: import symbols explicitly
Joerg Sonnenberger <joerg@bec.de>
parents: 45211
diff changeset
    12
from mercurial.node import hex
43085
eef9a2d67051 py3: manually import pycompat.open into files that need it
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43077
diff changeset
    13
from mercurial.pycompat import open
46113
59fa3890d40a node: import symbols explicitly
Joerg Sonnenberger <joerg@bec.de>
parents: 45211
diff changeset
    14
from mercurial import pycompat
44062
2d49482d0dd4 hgext: replace references to hashlib.sha1 with hashutil.sha1
Augie Fackler <augie@google.com>
parents: 43474
diff changeset
    15
from mercurial.utils import (
2d49482d0dd4 hgext: replace references to hashlib.sha1 with hashutil.sha1
Augie Fackler <augie@google.com>
parents: 43474
diff changeset
    16
    hashutil,
2d49482d0dd4 hgext: replace references to hashlib.sha1 with hashutil.sha1
Augie Fackler <augie@google.com>
parents: 43474
diff changeset
    17
    procutil,
2d49482d0dd4 hgext: replace references to hashlib.sha1 with hashutil.sha1
Augie Fackler <augie@google.com>
parents: 43474
diff changeset
    18
)
39826
c31ce080eb75 py3: convert arguments, cwd and env to native strings when spawning subprocess
Matt Harbison <matt_harbison@yahoo.com>
parents: 38166
diff changeset
    19
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
    20
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    21
class BundleWriteException(Exception):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    22
    pass
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    23
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
    24
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    25
class BundleReadException(Exception):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    26
    pass
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    27
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
    28
43474
70d42e2ad9b4 pytype: don't warn us about ignored-on-py3 metaclasses
Augie Fackler <augie@google.com>
parents: 43085
diff changeset
    29
class abstractbundlestore(object):  # pytype: disable=ignored-metaclass
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    30
    """Defines the interface for bundle stores.
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    31
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    32
    A bundle store is an entity that stores raw bundle data. It is a simple
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    33
    key-value store. However, the keys are chosen by the store. The keys can
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    34
    be any Python object understood by the corresponding bundle index (see
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    35
    ``abstractbundleindex`` below).
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    36
    """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
    37
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    38
    __metaclass__ = abc.ABCMeta
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    39
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    40
    @abc.abstractmethod
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    41
    def write(self, data):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    42
        """Write bundle data to the store.
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    43
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    44
        This function receives the raw data to be written as a str.
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    45
        Throws BundleWriteException
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    46
        The key of the written data MUST be returned.
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    47
        """
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    48
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    49
    @abc.abstractmethod
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    50
    def read(self, key):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    51
        """Obtain bundle data for a key.
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    52
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    53
        Returns None if the bundle isn't known.
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    54
        Throws BundleReadException
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    55
        The returned object should be a file object supporting read()
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    56
        and close().
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    57
        """
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    58
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
    59
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    60
class filebundlestore(object):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    61
    """bundle store in filesystem
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    62
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    63
    meant for storing bundles somewhere on disk and on network filesystems
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    64
    """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
    65
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    66
    def __init__(self, ui, repo):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    67
        self.ui = ui
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    68
        self.repo = repo
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    69
        self.storepath = ui.configpath(b'scratchbranch', b'storepath')
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    70
        if not self.storepath:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
    71
            self.storepath = self.repo.vfs.join(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    72
                b"scratchbranches", b"filebundlestore"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
    73
            )
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    74
        if not os.path.exists(self.storepath):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    75
            os.makedirs(self.storepath)
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    76
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    77
    def _dirpath(self, hashvalue):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    78
        """First two bytes of the hash are the name of the upper
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    79
        level directory, next two bytes are the name of the
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    80
        next level directory"""
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    81
        return os.path.join(self.storepath, hashvalue[0:2], hashvalue[2:4])
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    82
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    83
    def _filepath(self, filename):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    84
        return os.path.join(self._dirpath(filename), filename)
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    85
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    86
    def write(self, data):
46113
59fa3890d40a node: import symbols explicitly
Joerg Sonnenberger <joerg@bec.de>
parents: 45211
diff changeset
    87
        filename = hex(hashutil.sha1(data).digest())
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    88
        dirpath = self._dirpath(filename)
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    89
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    90
        if not os.path.exists(dirpath):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    91
            os.makedirs(dirpath)
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    92
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    93
        with open(self._filepath(filename), b'wb') as f:
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    94
            f.write(data)
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    95
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    96
        return filename
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    97
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    98
    def read(self, key):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    99
        try:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   100
            with open(self._filepath(key), b'rb') as f:
37796
968ac00c4017 inifinitepush: fix filebundlestore to close file
Yuya Nishihara <yuya@tcha.org>
parents: 37792
diff changeset
   101
                return f.read()
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   102
        except IOError:
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   103
            return None
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   104
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   105
45187
e285655c37c5 infinitepush: fix `{get,put}_args` formatting on Python 3
Connor Sheehan <sheehan@mozilla.com>
parents: 45186
diff changeset
   106
def format_placeholders_args(args, filename=None, handle=None):
e285655c37c5 infinitepush: fix `{get,put}_args` formatting on Python 3
Connor Sheehan <sheehan@mozilla.com>
parents: 45186
diff changeset
   107
    """Formats `args` with Infinitepush replacements.
e285655c37c5 infinitepush: fix `{get,put}_args` formatting on Python 3
Connor Sheehan <sheehan@mozilla.com>
parents: 45186
diff changeset
   108
e285655c37c5 infinitepush: fix `{get,put}_args` formatting on Python 3
Connor Sheehan <sheehan@mozilla.com>
parents: 45186
diff changeset
   109
    Hack to get `str.format()`-ed strings working in a BC way with
e285655c37c5 infinitepush: fix `{get,put}_args` formatting on Python 3
Connor Sheehan <sheehan@mozilla.com>
parents: 45186
diff changeset
   110
    bytes.
e285655c37c5 infinitepush: fix `{get,put}_args` formatting on Python 3
Connor Sheehan <sheehan@mozilla.com>
parents: 45186
diff changeset
   111
    """
e285655c37c5 infinitepush: fix `{get,put}_args` formatting on Python 3
Connor Sheehan <sheehan@mozilla.com>
parents: 45186
diff changeset
   112
    formatted_args = []
e285655c37c5 infinitepush: fix `{get,put}_args` formatting on Python 3
Connor Sheehan <sheehan@mozilla.com>
parents: 45186
diff changeset
   113
    for arg in args:
e285655c37c5 infinitepush: fix `{get,put}_args` formatting on Python 3
Connor Sheehan <sheehan@mozilla.com>
parents: 45186
diff changeset
   114
        if filename and arg == b'{filename}':
e285655c37c5 infinitepush: fix `{get,put}_args` formatting on Python 3
Connor Sheehan <sheehan@mozilla.com>
parents: 45186
diff changeset
   115
            formatted_args.append(filename)
e285655c37c5 infinitepush: fix `{get,put}_args` formatting on Python 3
Connor Sheehan <sheehan@mozilla.com>
parents: 45186
diff changeset
   116
        elif handle and arg == b'{handle}':
e285655c37c5 infinitepush: fix `{get,put}_args` formatting on Python 3
Connor Sheehan <sheehan@mozilla.com>
parents: 45186
diff changeset
   117
            formatted_args.append(handle)
e285655c37c5 infinitepush: fix `{get,put}_args` formatting on Python 3
Connor Sheehan <sheehan@mozilla.com>
parents: 45186
diff changeset
   118
        else:
e285655c37c5 infinitepush: fix `{get,put}_args` formatting on Python 3
Connor Sheehan <sheehan@mozilla.com>
parents: 45186
diff changeset
   119
            formatted_args.append(arg)
e285655c37c5 infinitepush: fix `{get,put}_args` formatting on Python 3
Connor Sheehan <sheehan@mozilla.com>
parents: 45186
diff changeset
   120
    return formatted_args
e285655c37c5 infinitepush: fix `{get,put}_args` formatting on Python 3
Connor Sheehan <sheehan@mozilla.com>
parents: 45186
diff changeset
   121
e285655c37c5 infinitepush: fix `{get,put}_args` formatting on Python 3
Connor Sheehan <sheehan@mozilla.com>
parents: 45186
diff changeset
   122
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   123
class externalbundlestore(abstractbundlestore):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   124
    def __init__(self, put_binary, put_args, get_binary, get_args):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   125
        """
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   126
        `put_binary` - path to binary file which uploads bundle to external
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   127
            storage and prints key to stdout
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   128
        `put_args` - format string with additional args to `put_binary`
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   129
                     {filename} replacement field can be used.
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   130
        `get_binary` - path to binary file which accepts filename and key
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   131
            (in that order), downloads bundle from store and saves it to file
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   132
        `get_args` - format string with additional args to `get_binary`.
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   133
                     {filename} and {handle} replacement field can be used.
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   134
        """
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   135
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   136
        self.put_args = put_args
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   137
        self.get_args = get_args
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   138
        self.put_binary = put_binary
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   139
        self.get_binary = get_binary
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   140
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   141
    def _call_binary(self, args):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   142
        p = subprocess.Popen(
39826
c31ce080eb75 py3: convert arguments, cwd and env to native strings when spawning subprocess
Matt Harbison <matt_harbison@yahoo.com>
parents: 38166
diff changeset
   143
            pycompat.rapply(procutil.tonativestr, args),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   144
            stdout=subprocess.PIPE,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   145
            stderr=subprocess.PIPE,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   146
            close_fds=True,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   147
        )
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   148
        stdout, stderr = p.communicate()
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   149
        returncode = p.returncode
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   150
        return returncode, stdout, stderr
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   151
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   152
    def write(self, data):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   153
        # Won't work on windows because you can't open file second time without
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   154
        # closing it
38166
cc9aa88792fe py3: wrap tempfile.NamedTemporaryFile() to return bytes fp.name
Yuya Nishihara <yuya@tcha.org>
parents: 37796
diff changeset
   155
        # TODO: rewrite without str.format() and replace NamedTemporaryFile()
cc9aa88792fe py3: wrap tempfile.NamedTemporaryFile() to return bytes fp.name
Yuya Nishihara <yuya@tcha.org>
parents: 37796
diff changeset
   156
        # with pycompat.namedtempfile()
45186
a52bf967e90a infinitepush: replace `NamedTemporaryFile` with `pycompat.namedtempfile`
Connor Sheehan <sheehan@mozilla.com>
parents: 44062
diff changeset
   157
        with pycompat.namedtempfile() as temp:
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   158
            temp.write(data)
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   159
            temp.flush()
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   160
            temp.seek(0)
45187
e285655c37c5 infinitepush: fix `{get,put}_args` formatting on Python 3
Connor Sheehan <sheehan@mozilla.com>
parents: 45186
diff changeset
   161
            formatted_args = format_placeholders_args(
e285655c37c5 infinitepush: fix `{get,put}_args` formatting on Python 3
Connor Sheehan <sheehan@mozilla.com>
parents: 45186
diff changeset
   162
                self.put_args, filename=temp.name
e285655c37c5 infinitepush: fix `{get,put}_args` formatting on Python 3
Connor Sheehan <sheehan@mozilla.com>
parents: 45186
diff changeset
   163
            )
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   164
            returncode, stdout, stderr = self._call_binary(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   165
                [self.put_binary] + formatted_args
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   166
            )
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   167
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   168
            if returncode != 0:
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   169
                raise BundleWriteException(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   170
                    b'Failed to upload to external store: %s' % stderr
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   171
                )
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   172
            stdout_lines = stdout.splitlines()
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   173
            if len(stdout_lines) == 1:
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   174
                return stdout_lines[0]
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   175
            else:
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   176
                raise BundleWriteException(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   177
                    b'Bad output from %s: %s' % (self.put_binary, stdout)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   178
                )
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   179
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   180
    def read(self, handle):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   181
        # Won't work on windows because you can't open file second time without
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   182
        # closing it
45186
a52bf967e90a infinitepush: replace `NamedTemporaryFile` with `pycompat.namedtempfile`
Connor Sheehan <sheehan@mozilla.com>
parents: 44062
diff changeset
   183
        with pycompat.namedtempfile() as temp:
45187
e285655c37c5 infinitepush: fix `{get,put}_args` formatting on Python 3
Connor Sheehan <sheehan@mozilla.com>
parents: 45186
diff changeset
   184
            formatted_args = format_placeholders_args(
e285655c37c5 infinitepush: fix `{get,put}_args` formatting on Python 3
Connor Sheehan <sheehan@mozilla.com>
parents: 45186
diff changeset
   185
                self.get_args, filename=temp.name, handle=handle
e285655c37c5 infinitepush: fix `{get,put}_args` formatting on Python 3
Connor Sheehan <sheehan@mozilla.com>
parents: 45186
diff changeset
   186
            )
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   187
            returncode, stdout, stderr = self._call_binary(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   188
                [self.get_binary] + formatted_args
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   189
            )
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   190
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   191
            if returncode != 0:
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   192
                raise BundleReadException(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   193
                    b'Failed to download from external store: %s' % stderr
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   194
                )
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   195
            return temp.read()