hgext/infinitepush/store.py
author Augie Fackler <augie@google.com>
Sun, 06 Oct 2019 09:48:39 -0400
changeset 43077 687b865b95ad
parent 43076 2372284d9457
child 43085 eef9a2d67051
permissions -rw-r--r--
formatting: byteify all mercurial/ and hgext/ string literals Done with python3.7 contrib/byteify-strings.py -i $(hg files 'set:mercurial/**.py - mercurial/thirdparty/** + hgext/**.py - hgext/fsmonitor/pywatchman/** - mercurial/__init__.py') black -l 80 -t py33 -S $(hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**" - hgext/fsmonitor/pywatchman/**') # skip-blame mass-reformatting only Differential Revision: https://phab.mercurial-scm.org/D6972
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 hashlib
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    10
import os
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    11
import subprocess
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    12
import tempfile
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    13
39826
c31ce080eb75 py3: convert arguments, cwd and env to native strings when spawning subprocess
Matt Harbison <matt_harbison@yahoo.com>
parents: 38166
diff changeset
    14
from mercurial import (
40252
090e5f3900b7 py3: fix infinitepush extension tests
Mark Thomas <mbthomas@fb.com>
parents: 39826
diff changeset
    15
    node,
39826
c31ce080eb75 py3: convert arguments, cwd and env to native strings when spawning subprocess
Matt Harbison <matt_harbison@yahoo.com>
parents: 38166
diff changeset
    16
    pycompat,
c31ce080eb75 py3: convert arguments, cwd and env to native strings when spawning subprocess
Matt Harbison <matt_harbison@yahoo.com>
parents: 38166
diff changeset
    17
)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
    18
from mercurial.utils import procutil
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
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    20
NamedTemporaryFile = tempfile.NamedTemporaryFile
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    21
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
    22
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    23
class BundleWriteException(Exception):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    24
    pass
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    25
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
    26
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    27
class BundleReadException(Exception):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    28
    pass
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    29
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
    30
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    31
class abstractbundlestore(object):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    32
    """Defines the interface for bundle stores.
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    33
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    34
    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
    35
    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
    36
    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
    37
    ``abstractbundleindex`` below).
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    38
    """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
    39
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    40
    __metaclass__ = abc.ABCMeta
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    41
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    42
    @abc.abstractmethod
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    43
    def write(self, data):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    44
        """Write bundle data to the store.
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    45
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    46
        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
    47
        Throws BundleWriteException
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    48
        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
    49
        """
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    50
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    51
    @abc.abstractmethod
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    52
    def read(self, key):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    53
        """Obtain bundle data for a key.
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    54
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    55
        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
    56
        Throws BundleReadException
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    57
        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
    58
        and close().
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    59
        """
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    60
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
    61
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    62
class filebundlestore(object):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    63
    """bundle store in filesystem
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    64
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    65
    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
    66
    """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
    67
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    68
    def __init__(self, ui, repo):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    69
        self.ui = ui
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    70
        self.repo = repo
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    71
        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
    72
        if not self.storepath:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
    73
            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
    74
                b"scratchbranches", b"filebundlestore"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
    75
            )
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    76
        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
    77
            os.makedirs(self.storepath)
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    78
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    79
    def _dirpath(self, hashvalue):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    80
        """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
    81
        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
    82
        next level directory"""
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    83
        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
    84
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    85
    def _filepath(self, filename):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    86
        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
    87
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    88
    def write(self, data):
40252
090e5f3900b7 py3: fix infinitepush extension tests
Mark Thomas <mbthomas@fb.com>
parents: 39826
diff changeset
    89
        filename = node.hex(hashlib.sha1(data).digest())
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    90
        dirpath = self._dirpath(filename)
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    91
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    92
        if not os.path.exists(dirpath):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    93
            os.makedirs(dirpath)
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    94
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    95
        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
    96
            f.write(data)
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
        return filename
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    99
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   100
    def read(self, key):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   101
        try:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   102
            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
   103
                return f.read()
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   104
        except IOError:
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   105
            return None
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   106
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   107
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   108
class externalbundlestore(abstractbundlestore):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   109
    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
   110
        """
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   111
        `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
   112
            storage and prints key to stdout
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   113
        `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
   114
                     {filename} replacement field can be used.
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   115
        `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
   116
            (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
   117
        `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
   118
                     {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
   119
        """
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   120
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   121
        self.put_args = put_args
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   122
        self.get_args = get_args
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   123
        self.put_binary = put_binary
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   124
        self.get_binary = get_binary
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
    def _call_binary(self, args):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   127
        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
   128
            pycompat.rapply(procutil.tonativestr, args),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   129
            stdout=subprocess.PIPE,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   130
            stderr=subprocess.PIPE,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   131
            close_fds=True,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   132
        )
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   133
        stdout, stderr = p.communicate()
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   134
        returncode = p.returncode
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   135
        return returncode, stdout, stderr
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   136
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   137
    def write(self, data):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   138
        # 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
   139
        # closing it
38166
cc9aa88792fe py3: wrap tempfile.NamedTemporaryFile() to return bytes fp.name
Yuya Nishihara <yuya@tcha.org>
parents: 37796
diff changeset
   140
        # 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
   141
        # with pycompat.namedtempfile()
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   142
        with NamedTemporaryFile() as temp:
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   143
            temp.write(data)
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   144
            temp.flush()
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   145
            temp.seek(0)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   146
            formatted_args = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   147
                arg.format(filename=temp.name) for arg in self.put_args
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   148
            ]
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   149
            returncode, stdout, stderr = self._call_binary(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   150
                [self.put_binary] + formatted_args
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   151
            )
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   152
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   153
            if returncode != 0:
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   154
                raise BundleWriteException(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   155
                    b'Failed to upload to external store: %s' % stderr
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   156
                )
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   157
            stdout_lines = stdout.splitlines()
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   158
            if len(stdout_lines) == 1:
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   159
                return stdout_lines[0]
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   160
            else:
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   161
                raise BundleWriteException(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   162
                    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
   163
                )
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   164
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   165
    def read(self, handle):
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   166
        # 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
   167
        # closing it
38166
cc9aa88792fe py3: wrap tempfile.NamedTemporaryFile() to return bytes fp.name
Yuya Nishihara <yuya@tcha.org>
parents: 37796
diff changeset
   168
        # 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
   169
        # with pycompat.namedtempfile()
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   170
        with NamedTemporaryFile() as temp:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   171
            formatted_args = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   172
                arg.format(filename=temp.name, handle=handle)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   173
                for arg in self.get_args
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   174
            ]
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   175
            returncode, stdout, stderr = self._call_binary(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   176
                [self.get_binary] + formatted_args
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   177
            )
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   178
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   179
            if returncode != 0:
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   180
                raise BundleReadException(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   181
                    b'Failed to download from external store: %s' % stderr
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40252
diff changeset
   182
                )
37187
03ff17a4bf53 infinitepush: move the extension to core from fb-hgext
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   183
            return temp.read()