def DuplicatePath(path): """Create a copy of the item at the end of the given path. The item will be created with a name in the form of Dirname_Copy for directories and FileName_Copy.extension for files. @param path: path to duplicate @return: Tuple of (success?, filename OR Error Message) """ head, tail = os.path.split(path) if os.path.isdir(path): name = util.GetUniqueName(head, tail + "_Copy") copy = shutil.copytree else: tmp = [part for part in tail.split('.') if len(part)] if tail.startswith('.'): tmp[0] = "." + tmp[0] if '.' not in tail[1:]: tmp[0] = tmp[0] + "_Copy" else: tmp.insert(-1, "_Copy.") tmp = ''.join(tmp) else: tmp = '.'.join(tmp[:-1]) + "_Copy." + tmp[-1] if len(tmp) > 1: name = util.GetUniqueName(head, tmp) copy = shutil.copy2 try: copy(path, name) except Exception, msg: return (False, str(msg))
def GetUpdateFiles(self, dl_to=wx.GetHomeDir()): """Gets the requested version of the program from the website if possible. It will download the current files for the host system to location (dl_to). On success it returns True, otherwise it returns false. @keyword dl_to: where to download the file to """ # Check version to see if update is needed # Dont allow update if files are current verpat = re.compile('[0-9]+\.[0-9]+\.[0-9]+') current = self.GetCurrentVersionStr() if not re.match(verpat, current): return False if CalcVersionValue(ed_glob.VERSION) < CalcVersionValue(current): dl_path = self.GetCurrFileURL() dl_file = dl_path.split('/')[-1] dl_to = util.GetUniqueName(dl_to, dl_file) blk_sz = 4096 read = 0 try: # Download the file in chunks so it can be aborted if need be # inbetween reads. webfile = self.__GetUrlHandle(dl_path) fsize = int(webfile.info()['Content-Length']) locfile = open(dl_to, 'wb') while read < fsize and not self._abort: locfile.write(webfile.read(blk_sz)) read += blk_sz self.UpdaterHook(int(read / blk_sz), blk_sz, fsize) locfile.close() webfile.close() finally: self._abort = False if os.path.exists(dl_to) and \ os.stat(dl_to)[stat.ST_SIZE] == fsize: return True else: return False else: return False
def MakeArchive(path): """Create a Zip archive of the item at the end of the given path @param path: full path to item to archive @return: Tuple of (success?, file name OR Error Message) @rtype: (bool, str) @todo: support for zipping multiple paths """ dname, fname = os.path.split(path) ok = True name = '' if dname and fname: name = util.GetUniqueName(dname, fname + ".zip") files = list() cwd = os.getcwd() head = dname try: try: os.chdir(dname) if os.path.isdir(path): for dpath, dname, fnames in os.walk(path): files.extend([ os.path.join(dpath, fname).\ replace(head, '', 1).\ lstrip(os.path.sep) for fname in fnames]) zfile = zipfile.ZipFile(name, 'w', compression=zipfile.ZIP_DEFLATED) for fname in files: zfile.write(fname.encode(sys.getfilesystemencoding())) except Exception, msg: ok = False name = str(msg) finally: zfile.close() os.chdir(cwd) return (ok, name)