diff --git a/saspy/sasiohttp.py b/saspy/sasiohttp.py index 00a31216..9a39d862 100644 --- a/saspy/sasiohttp.py +++ b/saspy/sasiohttp.py @@ -1278,13 +1278,28 @@ def upload(self, localfile: str, remotefile: str, overwrite: bool = True, permis """ valid = self._sb.file_info(remotefile, quiet = True) + # check for non-exist, dir or existing file if valid is None: - remf = remotefile + remf = remotefile + exist = False else: if valid == {}: remf = remotefile + self._sb.hostsep + localfile.rpartition(os.sep)[2] + valid = self._sb.file_info(remf, quiet = True) + if valid is None: + exist = False + else: + if valid == {}: + return {'Success' : False, + 'LOG' : "File "+str(remf)+" is an existing directory. Upload was stopped."} + else: + exist = True + if overwrite == False: + return {'Success' : False, + 'LOG' : "File "+str(remf)+" exists and overwrite was set to False. Upload was stopped."} else: - remf = remotefile + remf = remotefile + exist = True if overwrite == False: return {'Success' : False, 'LOG' : "File "+str(remotefile)+" exists and overwrite was set to False. Upload was stopped."} @@ -1302,6 +1317,21 @@ def upload(self, localfile: str, remotefile: str, overwrite: bool = True, permis ll = self.submit(code, 'text') logf = ll['LOG'] + # See if that worked cuz the next call will abend SAS if not; if lockdown/WatchDog ... + conn = self.sascfg.HTTPConn; conn.connect() + headers={"Accept":"application/vnd.sas.compute.fileref+json;application/json", + "Authorization":"Bearer "+self.sascfg._token} + conn.request('HEAD', self._uri_files+"/_sp_updn", headers=headers) + req = conn.getresponse() + status = req.status + resp = req.read() + conn.close() + + if status > 299: + fd.close() + return {'Success' : False, + 'LOG' : logf} + # GET Etag conn = self.sascfg.HTTPConn; conn.connect() headers={"Accept":"application/vnd.sas.compute.fileref+json;application/json", @@ -1345,8 +1375,15 @@ def upload(self, localfile: str, remotefile: str, overwrite: bool = True, permis status = req.status resp = req.read() conn.close() + fd.close() - code = "filename _sp_updn;" + ll = self.submit("filename _sp_updn;", 'text') + logf += ll['LOG'] + + if status > 299: + return {'Success' : False, + 'LOG' : "Failure in upload. Status="+str(status)+"\nResponse="+str(resp.decode())+ + "\n\n"+logf} else: logf = '' code = """ @@ -1358,17 +1395,26 @@ def upload(self, localfile: str, remotefile: str, overwrite: bool = True, permis run; filename _sp_updn; """ + ll = self.submit(code, 'text') + logf += ll['LOG'] + fd.close() - ll = self.submit(code, 'text') - logf += ll['LOG'] - fd.close() + valid2 = self._sb.file_info(remf, quiet = True) - if status > 299: - return {'Success' : False, - 'LOG' : "Failure in upload. Status="+str(status)+"\nResponse="+str(resp.decode())} + if valid2 is not None: + if exist: + success = False + for key in valid.keys(): + if valid[key] != valid2[key]: + success = True + break + else: + success = True else: - return {'Success' : True, - 'LOG' : logf} + success = False + + return {'Success' : success, + 'LOG' : logf} def download(self, localfile: str, remotefile: str, overwrite: bool = True, **kwargs): """ diff --git a/saspy/sasioiom.py b/saspy/sasioiom.py index 1a7b0295..a9fc00bf 100644 --- a/saspy/sasioiom.py +++ b/saspy/sasioiom.py @@ -1272,13 +1272,28 @@ def upload(self, localfile: str, remotefile: str, overwrite: bool = True, permis """ valid = self._sb.file_info(remotefile, quiet = True) + # check for non-exist, dir or existing file if valid is None: - remf = remotefile + remf = remotefile + exist = False else: if valid == {}: remf = remotefile + self._sb.hostsep + localfile.rpartition(os.sep)[2] + valid = self._sb.file_info(remf, quiet = True) + if valid is None: + exist = False + else: + if valid == {}: + return {'Success' : False, + 'LOG' : "File "+str(remf)+" is an existing directory. Upload was stopped."} + else: + exist = True + if overwrite == False: + return {'Success' : False, + 'LOG' : "File "+str(remf)+" exists and overwrite was set to False. Upload was stopped."} else: - remf = remotefile + remf = remotefile + exist = True if overwrite == False: return {'Success' : False, 'LOG' : "File "+str(remotefile)+" exists and overwrite was set to False. Upload was stopped."} @@ -1329,8 +1344,23 @@ def upload(self, localfile: str, remotefile: str, overwrite: bool = True, permis ll2 = self.submit(code, 'text') fd.close() - return {'Success' : True, - 'LOG' : log1+ll2['LOG']} + logf = log1+ll2['LOG'] + valid2 = self._sb.file_info(remf, quiet = True) + + if valid2 is not None: + if exist: + success = False + for key in valid.keys(): + if valid[key] != valid2[key]: + success = True + break + else: + success = True + else: + success = False + + return {'Success' : success, + 'LOG' : logf} def download(self, localfile: str, remotefile: str, overwrite: bool = True, **kwargs): """ diff --git a/saspy/sasiostdio.py b/saspy/sasiostdio.py index 749d5e38..4c6f2e0f 100755 --- a/saspy/sasiostdio.py +++ b/saspy/sasiostdio.py @@ -1352,13 +1352,28 @@ def upload(self, localfile: str, remotefile: str, overwrite: bool = True, permis """ valid = self._sb.file_info(remotefile, quiet = True) + # check for non-exist, dir or existing file if valid is None: - remf = remotefile + remf = remotefile + exist = False else: if valid == {}: remf = remotefile + self._sb.hostsep + localfile.rpartition(os.sep)[2] + valid = self._sb.file_info(remf, quiet = True) + if valid is None: + exist = False + else: + if valid == {}: + return {'Success' : False, + 'LOG' : "File "+str(remf)+" is an existing directory. Upload was stopped."} + else: + exist = True + if overwrite == False: + return {'Success' : False, + 'LOG' : "File "+str(remf)+" exists and overwrite was set to False. Upload was stopped."} else: - remf = remotefile + remf = remotefile + exist = True if overwrite == False: return {'Success' : False, 'LOG' : "File "+str(remotefile)+" exists and overwrite was set to False. Upload was stopped."} @@ -1370,7 +1385,7 @@ def upload(self, localfile: str, remotefile: str, overwrite: bool = True, permis port = self.sascfg.rtunnel host = 'localhost' else: - return self._upload_client(localfile, remotefile, overwrite, permission, **kwargs) + return self._upload_client(localfile, remf, overwrite, permission, valid=valid, **kwargs) try: fd = open(localfile, 'rb') @@ -1393,7 +1408,7 @@ def upload(self, localfile: str, remotefile: str, overwrite: bool = True, permis filename sock;\n""" self._asubmit(code, "text") - + sleep(1) sock = socks.socket() sock.connect((host, port)) @@ -1438,31 +1453,36 @@ def upload(self, localfile: str, remotefile: str, overwrite: bool = True, permis 'LOG' : "Download was interrupted. Returning the SAS log:\n\n"+str(e)+"\n\n"+ll['LOG']} ll = self.submit("", 'text') - return {'Success' : True, + + valid2 = self._sb.file_info(remf, quiet = True) + + if valid2 is not None: + if exist: + success = False + for key in valid.keys(): + if valid[key] != valid2[key]: + success = True + break + else: + success = True + else: + success = False + + return {'Success' : success, 'LOG' : ll['LOG']} - def _upload_client(self, localfile: str, remotefile: str, overwrite: bool = True, permission: str = '', **kwargs): + def _upload_client(self, localfile: str, remf: str, overwrite: bool = True, permission: str = '', **kwargs): """ This method uploads a local file to the SAS servers file system. localfile - path to the local file to upload - remotefile - path to remote file to create or overwrite + remf - path to remote file to create or overwrite overwrite - overwrite the output file if it exists? permission - permissions to set on the new file. See SAS Filename Statement Doc for syntax """ - valid = self._sb.file_info(remotefile, quiet = True) + valid = kwargs.pop('valid', None) + exist = False if valid is None else True - if valid is None: - remf = remotefile - else: - if valid == {}: - remf = remotefile + self._sb.hostsep + localfile.rpartition(os.sep)[2] - else: - remf = remotefile - if overwrite == False: - return {'Success' : False, - 'LOG' : "File "+str(remotefile)+" exists and overwrite was set to False. Upload was stopped."} - - port = kwargs.get('port', 0) + port = kwargs.get('port', 0) if port==0 and self.sascfg.tunnel: # we are using a tunnel; default to that port @@ -1558,7 +1578,22 @@ def _upload_client(self, localfile: str, remotefile: str, overwrite: bool = True 'LOG' : "Download was interrupted. Returning the SAS log:\n\n"+str(e)+"\n\n"+ll['LOG']} ll = self.submit("", 'text') - return {'Success' : True, + + valid2 = self._sb.file_info(remf, quiet = True) + + if valid2 is not None: + if exist: + success = False + for key in valid.keys(): + if valid[key] != valid2[key]: + success = True + break + else: + success = True + else: + success = False + + return {'Success' : success, 'LOG' : ll['LOG']} def download(self, localfile: str, remotefile: str, overwrite: bool = True, **kwargs):