Esempio n. 1
0
    def lock(self):
        self._homedir = get_user_dir(
        ) + utils.path_sep + u"." + self._name.lower()
        if not utils.path_exists(self._homedir):
            utils.path_makedirs(self._homedir)
        self._lockfilename = self._homedir + utils.path_sep + "monitor.lock"
        try:
            if is_linux():
                import fcntl
                self._lockfile = utils.file_open(self._lockfilename, "w")
                fcntl.lockf(self._lockfile, fcntl.LOCK_EX | fcntl.LOCK_NB)
            else:
                if utils.path_exists(self._lockfilename):
                    utils.path_remove(self._lockfilename)
                self._lockfile = utils.file_open(self._lockfilename, "w")
                self._lockfile.write("\x00")
        except:
            try:
                self._lockfile.close()
            except:
                None
            if self._mode == "systray":
                print("An Instance is already running.")
            else:
                self.add_show_file()
            return False

        #self.remove_show_file()
        return True
Esempio n. 2
0
def download_url_file(urlsrc,
                      fdest,
                      proxy_info=None,
                      response_transfer_progress=None):
    sredurl = None
    sp = _split_utl(urlsrc)
    #Richiesta al server
    sock = _connect_socket(sp["host"], sp["port"], proxy_info)
    try:
        req = Request("GET", sp["path"], {
            'Host': sp["host"] + ':' + str(sp["port"], ),
            'Connection': 'close'
        })
        sock.sendall(req.to_message())

        #Legge risposta
        if utils.path_exists(fdest):
            utils.path_remove(fdest)
        ftmp = fdest + "TMP"
        if utils.path_exists(ftmp):
            utils.path_remove(ftmp)
        resp = Response(sock, ftmp, response_transfer_progress)
        if resp.get_code() == '301':
            sredurl = resp.get_headers()["Location"]
        elif resp.get_code() != '200':
            raise Exception("Download error " + str(resp.get_code()) + ".")
    finally:
        sock.shutdown(1)
        sock.close()
    if sredurl is not None:
        download_url_file(sredurl, fdest, proxy_info,
                          response_transfer_progress)
    else:
        if utils.path_exists(ftmp):
            utils.path_move(ftmp, fdest)
Esempio n. 3
0
 def term_guilnc(self):
     self._propguilnc_semaphore.acquire()
     try:
         if utils.path_exists("guilnc.run"):
             utils.path_remove("guilnc.run")
         if self._propguilnc is not None:
             for l in self._propguilnc:
                 self._propguilnc[l].close()
             self._propguilnc = None
     finally:
         self._propguilnc_semaphore.release()
Esempio n. 4
0
    def _write(self, path, prop):
        if "encoding" in prop:
            enc = prop["encoding"]
        else:
            enc = sys.getfilesystemencoding()
        if "endline" in prop:
            endl = prop["endline"]
        else:
            endl = utils.line_sep
        bm = None

        #CREA FILE TEMPORANEO
        pathtmp = None
        sprnpath = utils.path_dirname(path)
        while True:
            r = "".join([random.choice("0123456789") for x in xrange(6)])
            pathtmp = sprnpath + utils.path_sep + "temporary" + r + ".dwstext"
            if not utils.path_exists(pathtmp):
                utils.file_open(
                    pathtmp,
                    'wb').close()  #Crea il file per imposta i permessi
                self._agent_main.get_osmodule().fix_file_permissions(
                    "CREATE_FILE", pathtmp)
                if (enc is not None):
                    flgop = "w"
                    if "bom" in prop and prop["bom"] == 'true':
                        bm = self._get_bom_byname(enc)
                        if bm is not None:
                            #Scrive BOM
                            text_file = utils.file_open(pathtmp, 'wb')
                            text_file.write(bm["Data"])
                            text_file.close()
                            flgop = "a"
                    text_file = utils.file_open(pathtmp, flgop, enc)
                else:
                    text_file = utils.file_open(pathtmp, 'w')
                break
        try:
            s = prop["text"]
            s = endl.join(s.split("\n"))
            text_file.write(s)
        finally:
            text_file.close()
        if utils.path_exists(path):
            if utils.path_isdir(path):
                utils.path_remove(self._tmpname)
                raise Exception("PATH is directory.")
            else:
                self._agent_main.get_osmodule().fix_file_permissions(
                    "COPY_FILE", pathtmp, path)
                utils.path_remove(path)
        shutil.move(pathtmp, path)
Esempio n. 5
0
 def create(self, fname, fieldsdef, fixperm=None):
     self._semaphore.acquire()
     try:
         if self._binit:
             raise Exception("Already initialized.")
         self._path = sharedmem_manager.getPath(fname)
         if utils.path_exists(self._path):
             if fixperm is not None:
                 fixperm(self._path)
             self.open(fname)
             #Verifica se la struttura è identica
             bok = True
             for f in fieldsdef:
                 if f["name"] in self._fields:
                     if f["size"] != self._fields[f["name"]]["size"]:
                         bok = False
                         break
                 else:
                     bok = False
                     break
             if not bok:
                 self.close()
                 #Prova a rimuovere il file
                 try:
                     utils.path_remove(self._path)
                 except:
                     raise Exception("Shared file is locked.")
             else:
                 self._binit = True
                 return
         #CREAZIONE DEL FILE
         self._fields = {}
         szdata = 0
         for f in fieldsdef:
             self._fields[f["name"]] = {"pos": szdata, "size": f["size"]}
             szdata += f["size"]
         shead = json.dumps(self._fields)
         self._len_def = len(shead)
         self._size = 4 + self._len_def + szdata
         with utils.file_open(self._path, "wb") as f:
             f.write(" " * self._size)
         if fixperm is not None:
             fixperm(self._path)
         self._file = utils.file_open(self._path, "r+b")
         self._mmap = mmap.mmap(self._file.fileno(), 0)
         self._mmap.seek(0)
         self._mmap.write(struct.pack('!i', self._len_def))
         self._mmap.write(shead)
         self._binit = True
     finally:
         self._semaphore.release()
Esempio n. 6
0
def init_path():
    if not utils.path_exists(SHAREDMEM_PATH):
        utils.path_makedir(SHAREDMEM_PATH)
    else:
        #Elimina tutti i file
        lst = utils.path_list(SHAREDMEM_PATH)
        for fname in lst:
            try:
                if fname[0:7] == "stream_":
                    if utils.path_exists(SHAREDMEM_PATH + utils.path_sep +
                                         fname):
                        utils.path_remove(SHAREDMEM_PATH + utils.path_sep +
                                          fname)
            except:
                None
Esempio n. 7
0
 def destroy(self):
     if not self.bdestroy:
         self.bdestroy = True
         if self.bcreate:
             if self.ftype == "F":
                 if utils.path_exists(self.fpath):
                     utils.path_remove(self.fpath)
             elif self.ftype == "M":
                 if not utils.is_windows():
                     iret = _sharememmap["libbase"].sharedMemoryUnlink(
                         self.fname)
                     if iret != 0:
                         raise Exception("sharedMemoryUnlink fail")
                 else:
                     MapFile._memlistname.remove(self.fname)
Esempio n. 8
0
 def _cpmv(self, tp, fs, fd, replace):
     bok = True
     if utils.path_isdir(fs):
         if not utils.path_exists(fd):
             utils.path_makedirs(fd)
             if tp=="copy":
                 self._agent_main.get_osmodule().fix_file_permissions("COPY_DIRECTORY",fd, fs)
             elif tp=="move":
                 self._agent_main.get_osmodule().fix_file_permissions("MOVE_DIRECTORY",fd, fs)
         lst=None
         try:
             lst=utils.path_list(fs)
             for fname in lst:
                 b = self._cpmv(tp, fs + utils.path_sep + fname, fd + utils.path_sep + fname, replace)
                 if bok is True:
                     bok = b
         except Exception:
             bok=False
         if tp=="move":
             try:
                 utils.path_remove(fs)
             except Exception:
                 bok=False
     else:
         b=True
         if utils.path_exists(fd):
             if replace is True:
                 try:
                     utils.path_remove(fd)
                 except Exception:
                     bok = False
                     b = False
             else:
                 b = False
         if b is True:
             try:
                 if tp=="copy":
                     utils.path_copy(fs, fd)
                     self._agent_main.get_osmodule().fix_file_permissions("COPY_FILE",fd, fs)
                 elif tp=="move":
                     utils.path_move(fs, fd)
                     self._agent_main.get_osmodule().fix_file_permissions("MOVE_FILE",fd)
             except Exception:
                 bok=False
     return bok
Esempio n. 9
0
 def _destroy_file(self):
     if self._side == 1:
         if self._terminate_retry >= 0:
             if utils.path_exists(self._path):
                 apptm = long(time.time() * 1000)
                 elp = apptm - self._terminate_time
                 if elp > 2000:
                     try:
                         self._terminate_retry += 1
                         utils.path_remove(self._path)
                     except Exception as e:
                         if self._terminate_retry >= 5:
                             raise e
                         return False
                     return True
                 else:
                     if elp < 0:
                         self._terminate_time = long(time.time() * 1000)
                     return False
     return True
Esempio n. 10
0
    def req_remove(self, cinfo ,params):
        path = self.check_and_replace_path(cinfo, agent.get_prop(params,'path',None), self.OPERATION_EDIT)
        files = agent.get_prop(params,'files',None)

        arfiles = json.loads(files)
        arret=[]
        for i in range(len(arfiles)):
            fp = path + arfiles[i]
            b = True
            try:
                utils.path_remove(fp)
            except Exception:
                b=False            
            tp = None
            if b is True:
                tp="K"
            else:
                tp="E"
            arret.append({'Name': tp + ":" + arfiles[i]})
        return json.dumps({'items' : arret})
Esempio n. 11
0
 def remove_show_file(self):
     showfilename = self._homedir + utils.path_sep + "monitor.show"
     try:
         utils.path_remove(showfilename)
     except:
         None
Esempio n. 12
0
 def unlock(self):
     self._lockfile.close()
     try:
         utils.path_remove(self._lockfilename)
     except:
         None