def scp(user, host, remotepath, localpath, method=None): """Copy a file on the remote server. :param user: user login to authenticate as :type user: str :param host: the server to connect to :type host: str :param remotepath: path on the remote server :type remotepath: str :param localpath: path on the local server :type localpath: str :param method: 'get' or 'put' :type method: str :return: (output content, error content, exit status) :rtype: (str, str, int) """ if method not in ('get', 'put'): return ('', 'Method %s not implemented' % method, 1) client = SSHClient() client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) if not socket.has_ipv6: # If python is configured without IPv6 then paramiko connect() # can fail with: getsockaddrarg: bad family host = socket.gethostbyname(host) client.connect(host, username=user) sftp = client.open_sftp() remotepath = unixpath(remotepath) localpath = unixpath(localpath) err = '' status = 0 out = localpath if method == 'get' else remotepath try: if method == 'get': sftp.get(remotepath, localpath) else: # method: put sftp.put(localpath, remotepath) except IOError as e: err = e status = 1 finally: sftp.close() client.close() return (out, err, status)
def encode(cls, path): """Encode a test name. Use the dirname to compute the testcase display name. This uses mappings set in the configuration file. This file is loaded if necessary. """ cls._load_config() path, basename = os.path.split(unixpath(path)) return '%s.%s' % (cls.encoding['/'.join(path.split('/'))], basename)
def register_path_subst(self, path, subst=''): """Register a path to be substituted in actual and expected output. Note that that substitution are only applied when using helper functions such as analyze_diff :param path: a path :type path: str :param subst: substitution string :type subst: str """ self.subst.append((os.path.abspath(path).replace('\\', '\\\\'), subst)) self.subst.append((unixpath(path).replace('\\', '\\\\'), subst)) self.subst.append((path.replace('\\', '\\\\'), subst))
def scp_put(user, host, localpath, remotepath=None): """Copy a local file to the host as remotepath. :param user: user login to authenticate as :type user: str :param host: the server to connect to :type host: str :param localpath: path on the local server to copy to the host :type localpath: str :param remotepath: path on the remote serve (if None use the remotepath basename) :type remotepath: str | None :return: (output content, error content, exit status) :rtype: (str, str, int) """ if remotepath is None: remotepath = os.path.basename(localpath) p = Run([ 'scp', '-B', unixpath(localpath), '%s@%s:%s' % (user, host, unixpath(remotepath)) ]) return (remotepath, p.err, p.status)
def __init__(self, url, dest, rev=None, use_externals=False, force=False): """See SVNBase.__init__.""" self.ext_args = \ ['--config-option=config:miscellany:use-commit-times=yes'] if not use_externals: self.ext_args.append('--ignore-externals') self.rev_args = [] if rev is not None: self.rev_args = ['-r', rev] SVNBase.__init__(self, url, unixpath(dest), rev, use_externals, force)
def unixpath_to(pgmname): """Return the absolute path to the executable file expected in the current directory for a main subprogram PGMNAME, unixified.""" return unixpath(os.path.abspath(exename_for(pgmname)))
def __init__(self, url, dest, branch='master', rev=None, force_checkout=True): """Initialize a Git working environment. :param url: the remote git url :type url: str :param dest: the local git repository path :type dest: str :param branch: the branch :type branch: str :param rev: the revision used :type rev: str | None :param force_checkout: do a checkout of the given `rev` or `branch` even if the repository already exists, it overwrite existing files :type force_checkout: bool :raise: Git_Error """ self.url = unixpath(url) self.dest = unixpath(dest) self.branch = branch self.rev = rev self.remote = None self.git = which('git', default=None) if not self.git: raise Git_Error('git not found') try: # If the dest directory does not exist or is empty, do a git clone if not os.path.exists(self.dest) or not os.listdir(self.dest): self.clone() return remotes = self.remote_info() except Git_Error: if force_checkout: self.init() remotes = self.remote_info() else: self.__error("%s not empty and force_checkout is not True" % self.dest, traceback=sys.exc_traceback) configured_remote = [r[0] for r in remotes if r[1] == self.url] if configured_remote: self.remote = configured_remote[0] elif not configured_remote: error_msg = "Remote for %s not found. " % self.url if not remotes: error_msg += "No configured remotes" else: error_msg += "Configured remotes are:\n" error_msg += '\n'.join(set((r[1] for r in remotes))) if force_checkout: vcslogger.debug(error_msg) self.init() else: self.__error(error_msg) if force_checkout: try: if rev is not None: self.checkout(rev, force=True) else: self.checkout("%s/%s" % (self.remote, branch), force=True) except Git_Error: # ??? the ref to checkout is maybe not already fetched # force an update in that case self.update(rev)