def test_move_dir_caseinsensitive(self): # Renames a folder to the same name # but a different case. self.src_dir = tempfile.mkdtemp() dst_dir = os.path.join(os.path.dirname(self.src_dir), os.path.basename(self.src_dir).upper()) self.assertNotEqual(self.src_dir, dst_dir) try: shutil.move(self.src_dir, dst_dir) self.assertTrue(os.path.isdir(dst_dir)) finally: if os.path.exists(dst_dir): os.rmdir(dst_dir)
def _check_move_file(self, src, dst, real_dst): f = open(src, "rb") try: contents = f.read() finally: f.close() shutil.move(src, dst) f = open(real_dst, "rb") try: self.assertEqual(contents, f.read()) finally: f.close() self.assertFalse(os.path.exists(src))
def test_move_dir_caseinsensitive(self): # Renames a folder to the same name # but a different case. self.src_dir = tempfile.mkdtemp() dst_dir = os.path.join( os.path.dirname(self.src_dir), os.path.basename(self.src_dir).upper()) self.assertNotEqual(self.src_dir, dst_dir) try: shutil.move(self.src_dir, dst_dir) self.assertTrue(os.path.isdir(dst_dir)) finally: if os.path.exists(dst_dir): os.rmdir(dst_dir)
def _check_move_dir(self, src, dst, real_dst): contents = sorted(os.listdir(src)) shutil.move(src, dst) self.assertEqual(contents, sorted(os.listdir(real_dst))) self.assertFalse(os.path.exists(src))
def move_file(self, src, dst): logger.info("moving %r to %r", src, dst) if self.dry_run: return return move(src, dst)
def move_file(self, src, dst, level=1): """Move a file respectin dry-run flag.""" if self.dry_run: return # XXX log ? return move(src, dst)
def _write_cfg(self): if os.path.exists(_FILENAME): if os.path.exists('%s.old' % _FILENAME): message = ("ERROR: %(name)s.old backup exists, please check " "that current %(name)s is correct and remove " "%(name)s.old" % { 'name': _FILENAME }) logger.error(message) return shutil.move(_FILENAME, '%s.old' % _FILENAME) with open(_FILENAME, 'w', encoding='utf-8') as fp: fp.write('[metadata]\n') # TODO use metadata module instead of hard-coding field-specific # behavior here # simple string entries for name in ('name', 'version', 'summary', 'download_url'): fp.write('%s = %s\n' % (name, self.data.get(name, 'UNKNOWN'))) # optional string entries if 'keywords' in self.data and self.data['keywords']: # XXX shoud use comma to separate, not space fp.write('keywords = %s\n' % ' '.join(self.data['keywords'])) for name in ('home_page', 'author', 'author_email', 'maintainer', 'maintainer_email', 'description-file'): if name in self.data and self.data[name]: fp.write('%s = %s\n' % (name, self.data[name])) if 'description' in self.data: fp.write( 'description = %s\n' % '\n |'.join(self.data['description'].split('\n'))) # multiple use string entries for name in ('platform', 'supported-platform', 'classifier', 'requires-dist', 'provides-dist', 'obsoletes-dist', 'requires-external'): if not (name in self.data and self.data[name]): continue fp.write('%s = ' % name) fp.write(''.join(' %s\n' % val for val in self.data[name]).lstrip()) fp.write('\n[files]\n') for name in ('packages', 'modules', 'scripts', 'extra_files'): if not (name in self.data and self.data[name]): continue fp.write('%s = %s\n' % (name, '\n '.join(self.data[name]).strip())) if self.data.get('package_data'): fp.write('package_data =\n') for pkg, spec in sorted(self.data['package_data'].items()): # put one spec per line, indented under the package name indent = ' ' * (len(pkg) + 7) spec = ('\n' + indent).join(spec) fp.write(' %s = %s\n' % (pkg, spec)) fp.write('\n') if self.data.get('resources'): fp.write('resources =\n') for src, dest in self.data['resources']: fp.write(' %s = %s\n' % (src, dest)) fp.write('\n') os.chmod(_FILENAME, 0o644) logger.info('Wrote "%s".' % _FILENAME)
def install_from_infos(install_path=None, install=[], remove=[], conflicts=[], paths=None): """Install and remove the given distributions. The function signature is made to be compatible with the one of get_infos. The aim of this script is to povide a way to install/remove what's asked, and to rollback if needed. So, it's not possible to be in an inconsistant state, it could be either installed, either uninstalled, not half-installed. The process follow those steps: 1. Move all distributions that will be removed in a temporary location 2. Install all the distributions that will be installed in a temp. loc. 3. If the installation fails, rollback (eg. move back) those distributions, or remove what have been installed. 4. Else, move the distributions to the right locations, and remove for real the distributions thats need to be removed. :param install_path: the installation path where we want to install the distributions. :param install: list of distributions that will be installed; install_path must be provided if this list is not empty. :param remove: list of distributions that will be removed. :param conflicts: list of conflicting distributions, eg. that will be in conflict once the install and remove distribution will be processed. :param paths: list of paths (defaults to sys.path) to look for info """ # first of all, if we have conflicts, stop here. if conflicts: raise InstallationConflict(conflicts) if install and not install_path: raise ValueError("Distributions are to be installed but `install_path`" " is not provided.") # before removing the files, we will start by moving them away # then, if any error occurs, we could replace them in the good place. temp_files = {} # contains lists of {dist: (old, new)} paths temp_dir = None if remove: temp_dir = tempfile.mkdtemp() for dist in remove: files = dist.list_installed_files() paths = [path for path, md5, size in files] temp_files[dist] = _move_files(paths, temp_dir) try: if install: install_dists(install, install_path, paths) except: # if an error occurs, put back the files in the right place. for files in temp_files.values(): for old, new in files: shutil.move(new, old) if temp_dir: shutil.rmtree(temp_dir) # now re-raising raise # we can remove them for good for files in temp_files.values(): for old, new in files: os.remove(new) if temp_dir: shutil.rmtree(temp_dir)
def _write_cfg(self): if os.path.exists(_FILENAME): if os.path.exists('%s.old' % _FILENAME): message = ("ERROR: %(name)s.old backup exists, please check " "that current %(name)s is correct and remove " "%(name)s.old" % {'name': _FILENAME}) logger.error(message) return shutil.move(_FILENAME, '%s.old' % _FILENAME) fp = codecs.open(_FILENAME, 'w', encoding='utf-8') try: fp.write(u'[metadata]\n') # TODO use metadata module instead of hard-coding field-specific # behavior here # simple string entries for name in ('name', 'version', 'summary', 'download_url'): fp.write(u'%s = %s\n' % (name, self.data.get(name, 'UNKNOWN'))) # optional string entries if 'keywords' in self.data and self.data['keywords']: # XXX shoud use comma to separate, not space fp.write(u'keywords = %s\n' % ' '.join(self.data['keywords'])) for name in ('home_page', 'author', 'author_email', 'maintainer', 'maintainer_email', 'description-file'): if name in self.data and self.data[name]: fp.write(u'%s = %s\n' % (name.decode('utf-8'), self.data[name].decode('utf-8'))) if 'description' in self.data: fp.write( u'description = %s\n' % u'\n |'.join(self.data['description'].split('\n'))) # multiple use string entries for name in ('platform', 'supported-platform', 'classifier', 'requires-dist', 'provides-dist', 'obsoletes-dist', 'requires-external'): if not(name in self.data and self.data[name]): continue fp.write(u'%s = ' % name) fp.write(u''.join(' %s\n' % val for val in self.data[name]).lstrip()) fp.write(u'\n[files]\n') for name in ('packages', 'modules', 'scripts', 'extra_files'): if not(name in self.data and self.data[name]): continue fp.write(u'%s = %s\n' % (name, u'\n '.join(self.data[name]).strip())) if self.data.get('package_data'): fp.write(u'package_data =\n') for pkg, spec in sorted(self.data['package_data'].items()): # put one spec per line, indented under the package name indent = u' ' * (len(pkg) + 7) spec = (u'\n' + indent).join(spec) fp.write(u' %s = %s\n' % (pkg, spec)) fp.write(u'\n') if self.data.get('resources'): fp.write(u'resources =\n') for src, dest in self.data['resources']: fp.write(u' %s = %s\n' % (src, dest)) fp.write(u'\n') finally: fp.close() os.chmod(_FILENAME, 0644) logger.info('Wrote "%s".' % _FILENAME)
def move_file(self, src, dst, level=1): """Move a file respecting the dry-run flag.""" if self.dry_run: return # XXX same thing return move(src, dst)